1
1
forked from extern/flakelight
flakelight/builtinModules/nixosConfigurations.nix
Archit Gupta f2c32f5a42 Add optCallWith type for optional calling
optFunctionTo results in a function which needs to be called when using
the option value. This is needed when the argument is not known when
building types (such as with pkgs). When the args are known (for
example, moduleArgs), this leads to more complex code than just calling
the function and resulting in the target type. optCallWith does the
latter.
2024-01-14 19:22:46 -08:00

48 lines
1.6 KiB
Nix

# flakelight -- Framework for simplifying flake setup
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, lib, inputs, flakelight, moduleArgs, ... }:
let
inherit (builtins) mapAttrs;
inherit (lib) foldl mapAttrsToList mkIf mkOption recursiveUpdate;
inherit (lib.types) attrs lazyAttrsOf;
inherit (flakelight) selectAttr;
inherit (flakelight.types) optCallWith;
# Avoid checking if toplevel is a derivation as it causes the nixos modules
# to be evaluated.
isNixos = x: x ? config.system.build.toplevel;
mkNixos = hostname: cfg: inputs.nixpkgs.lib.nixosSystem (cfg // {
specialArgs = {
inherit inputs hostname;
inputs' = mapAttrs (_: selectAttr cfg.system) inputs;
} // cfg.specialArgs or { };
modules = [ config.propagationModule ] ++ cfg.modules or [ ];
});
configs = mapAttrs
(hostname: cfg: if isNixos cfg then cfg else mkNixos hostname cfg)
config.nixosConfigurations;
in
{
options.nixosConfigurations = mkOption {
type = lazyAttrsOf (optCallWith moduleArgs attrs);
default = { };
};
config.outputs = mkIf (config.nixosConfigurations != { }) {
nixosConfigurations = configs;
checks = foldl recursiveUpdate { } (mapAttrsToList
(n: v: {
# Wrapping the drv is needed as computing its name is expensive
# If not wrapped, it slows down `nix flake show` significantly
${v.config.nixpkgs.system}."nixos-${n}" = v.pkgs.runCommand
"check-nixos-${n}"
{ } "echo ${v.config.system.build.toplevel} > $out";
})
configs);
};
}