os/lib/mkFlake/default.nix
Pacman99 deb47dbfae move otherArguments to right side of merge
for overlay list ordering, otherArguments should have priority
2021-05-02 11:15:44 -07:00

103 lines
3 KiB
Nix

{ lib, deploy }:
let
inherit (builtins) mapAttrs attrNames attrValues head isFunction;
in
_: { self, inputs, ... } @ args:
let
config = lib.mkFlake.evalArgs {
inherit args;
};
cfg = config.config;
otherArguments = removeAttrs args (attrNames config.options);
defaultModules = with lib.modules; [
(hmDefaults {
inherit (cfg.home) suites;
modules = cfg.home.modules ++ cfg.home.externalModules;
})
(globalDefaults {
inherit self inputs;
})
];
stripChannel = channel: removeAttrs channel [
# arguments in our channels api that shouldn't be passed to fup
"overlays"
];
getDefaultChannel = channels: channels.${cfg.nixos.hostDefaults.channelName};
# evalArgs sets channelName and system to null by default
# but for proper default handling in fup, null args have to be removed
stripHost = args: removeAttrs (lib.filterAttrs (_: arg: arg != null) args) [
# arguments in our hosts/hostDefaults api that shouldn't be passed to fup
"externalModules"
];
hosts = lib.mapAttrs (_: stripHost) cfg.nixos.hosts;
hostDefaults = stripHost cfg.nixos.hostDefaults;
in
lib.systemFlake (lib.mergeAny
{
inherit self inputs hosts;
inherit (cfg) channelsConfig supportedSystems;
channels = mapAttrs
(name: channel:
stripChannel (channel // {
# pass channels if "overlay" has three arguments
overlaysBuilder = channels: lib.unifyOverlays channels channel.overlays;
})
)
cfg.channels;
sharedOverlays = [
(final: prev: {
__dontExport = true;
devlib = lib;
})
];
hostDefaults = lib.mergeAny hostDefaults {
specialArgs.suites = cfg.nixos.suites;
modules = cfg.nixos.hostDefaults.externalModules ++ defaultModules;
builder = args: args.specialArgs.channel.input.lib.nixosSystem (lib.mergeAny args {
# So modules and functions can create their own version of the build
modules = [{ lib.builderArgs = args; }];
});
};
nixosModules = lib.exporter.modulesFromList cfg.nixos.hostDefaults.modules;
homeModules = lib.exporter.modulesFromList cfg.home.modules;
homeConfigurations = lib.mkHomeConfigurations self.nixosConfigurations;
deploy.nodes = lib.mkDeployNodes deploy self.nixosConfigurations;
overlays = lib.exporter.overlaysFromChannelsExporter {
# since we can't detect overlays owned by self
# we have to filter out ones exported by the inputs
# optimally we would want a solution for NixOS/nix#4740
inherit inputs;
inherit (self) pkgs;
};
packagesBuilder = lib.builder.packagesFromOverlaysBuilderConstructor self.overlays;
checksBuilder = channels:
lib.pkgs-lib.tests.mkChecks {
pkgs = getDefaultChannel channels;
inherit (self.deploy) nodes;
hosts = self.nixosConfigurations;
homes = self.homeConfigurations;
};
devShellBuilder = channels:
lib.pkgs-lib.shell {
pkgs = getDefaultChannel channels;
};
}
otherArguments # for overlays list order
)