flake: fix patching process to assume less about nixpkgs internals

This commit is contained in:
2023-12-11 21:07:17 +00:00
parent 688b4edf13
commit bfe69a4708
2 changed files with 39 additions and 54 deletions

View File

@@ -83,8 +83,6 @@
inherit (builtins) attrNames elem listToAttrs map mapAttrs; inherit (builtins) attrNames elem listToAttrs map mapAttrs;
# redefine some nixpkgs `lib` functions to avoid the infinite recursion # redefine some nixpkgs `lib` functions to avoid the infinite recursion
# of if we tried to use patched `nixpkgs.lib` as part of the patching process. # of if we tried to use patched `nixpkgs.lib` as part of the patching process.
mapAttrs' = f: set:
listToAttrs (map (attr: f attr set.${attr}) (attrNames set));
optionalAttrs = cond: attrs: if cond then attrs else {}; optionalAttrs = cond: attrs: if cond then attrs else {};
# mapAttrs but without the `name` argument # mapAttrs but without the `name` argument
mapAttrValues = f: mapAttrs (_: f); mapAttrValues = f: mapAttrs (_: f);
@@ -113,16 +111,11 @@
nixpkgsCompiledBy = system: nixpkgs.legacyPackages."${system}"; nixpkgsCompiledBy = system: nixpkgs.legacyPackages."${system}";
evalHost = { name, local, target, light ? false }: nixpkgs.lib.nixosSystem { evalHost = { name, local, target, light ? false }: nixpkgs.lib.nixosSystem {
system = target; # system = target;
modules = [ modules = [
{ {
nixpkgs = (if (local != null) then { nixpkgs.buildPlatform.system = local;
buildPlatform = local; nixpkgs.hostPlatform.system = target;
} else {}) // {
# TODO: does the earlier `system` arg to nixosSystem make its way here?
hostPlatform.system = target;
};
# nixpkgs.buildPlatform = local; # set by instantiate.nix instead
# nixpkgs.config.replaceStdenv = { pkgs }: pkgs.ccacheStdenv; # nixpkgs.config.replaceStdenv = { pkgs }: pkgs.ccacheStdenv;
} }
(optionalAttrs light { (optionalAttrs light {
@@ -140,39 +133,18 @@
]; ];
}; };
in { in {
nixosConfigurations = nixosConfigurations = let
let hosts = {
hosts = { servo = { name = "servo"; local = "x86_64-linux"; target = "x86_64-linux"; };
servo = { name = "servo"; local = "x86_64-linux"; target = "x86_64-linux"; }; desko = { name = "desko"; local = "x86_64-linux"; target = "x86_64-linux"; };
desko = { name = "desko"; local = "x86_64-linux"; target = "x86_64-linux"; }; desko-light = { name = "desko"; local = "x86_64-linux"; target = "x86_64-linux"; light = true; };
desko-light = { name = "desko"; local = "x86_64-linux"; target = "x86_64-linux"; light = true; }; lappy = { name = "lappy"; local = "x86_64-linux"; target = "x86_64-linux"; };
lappy = { name = "lappy"; local = "x86_64-linux"; target = "x86_64-linux"; }; lappy-light = { name = "lappy"; local = "x86_64-linux"; target = "x86_64-linux"; light = true; };
lappy-light = { name = "lappy"; local = "x86_64-linux"; target = "x86_64-linux"; light = true; }; moby = { name = "moby"; local = "x86_64-linux"; target = "aarch64-linux"; };
moby = { name = "moby"; local = "x86_64-linux"; target = "aarch64-linux"; }; moby-light = { name = "moby"; local = "x86_64-linux"; target = "aarch64-linux"; light = true; };
moby-light = { name = "moby"; local = "x86_64-linux"; target = "aarch64-linux"; light = true; }; rescue = { name = "rescue"; local = "x86_64-linux"; target = "x86_64-linux"; };
rescue = { name = "rescue"; local = "x86_64-linux"; target = "x86_64-linux"; }; };
}; in mapAttrValues evalHost hosts;
# cross-compiled builds: instead of emulating the host, build using a cross-compiler.
# - these are faster to *build* than the emulated variants (useful when tweaking packages),
# - but fewer of their packages can be found in upstream caches.
cross = mapAttrValues evalHost hosts;
emulated = mapAttrValues
(args: evalHost (args // { local = null; }))
hosts;
prefixAttrs = prefix: attrs: mapAttrs'
(name: value: {
name = prefix + name;
inherit value;
})
attrs;
in
(prefixAttrs "cross-" cross) //
(prefixAttrs "emulated-" emulated) // {
# prefer native builds for these machines:
inherit (emulated) servo desko desko-light lappy lappy-light rescue;
# prefer cross-compiled builds for these machines:
inherit (cross) moby moby-light;
};
# unofficial output # unofficial output
# this produces a EFI-bootable .img file (GPT with a /boot partition and a system (/ or /nix) partition). # this produces a EFI-bootable .img file (GPT with a /boot partition and a system (/ or /nix) partition).

View File

@@ -15,21 +15,34 @@
}; };
patchedFlakeFor = system: import "${patchedPkgsFor system}/flake.nix"; patchedFlakeFor = system: import "${patchedPkgsFor system}/flake.nix";
patchedFlakeOutputsFor = system: patchedFlakeOutputsFor = system:
(patchedFlakeFor system).outputs { inherit self; }; (patchedFlakeFor system).outputs { self = self._forSystem system; };
extractBuildPlatform = nixosSystemArgs: extractBuildPlatform = nixosSystemArgs:
let builtins.foldl'
firstMod = builtins.head nixosSystemArgs.modules; (acc: mod: ((mod.nixpkgs or {}).buildPlatform or {}).system or acc)
in (nixosSystemArgs.system or null)
firstMod.nixpkgs.buildPlatform or nixosSystemArgs.system; (nixosSystemArgs.modules or []);
in in
{ {
lib.nixosSystem = args: (patchedFlakeOutputsFor (extractBuildPlatform args)).lib.nixosSystem args; # i attempt to mirror the non-patched nixpkgs flake outputs,
# however the act of patching is dependent on the build system (can't be done in pure nix),
# hence a 100% compatible interface has to be segmented by `system`:
_forSystem = system: {
inherit (patchedFlakeOutputsFor system) lib;
legacyPackages = builtins.mapAttrs
(system': _:
(patchedFlakeOutputsFor (if system != null then system else system'))
.legacyPackages."${system'}"
)
nixpkgs.legacyPackages;
};
legacyPackages = builtins.mapAttrs # although i can't expose all of the patched nixpkgs outputs without knowing the `system` to use for patching,
(system: _: # several outputs learn about the system implicitly, so i can expose those:
(patchedFlakeOutputsFor system).legacyPackages."${system}" lib.nixosSystem = args: (
) self._forSystem (extractBuildPlatform args)
nixpkgs.legacyPackages; ).lib.nixosSystem args;
legacyPackages = (self._forSystem null).legacyPackages;
}; };
} }