2023-08-27 07:48:57 +02:00
|
|
|
# flakelight -- Framework for simplifying flake setup
|
2023-08-25 06:14:55 +02:00
|
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2024-01-14 10:29:16 +01:00
|
|
|
{ config, lib, inputs, flakelight, moduleArgs, ... }:
|
2023-08-25 06:14:55 +02:00
|
|
|
let
|
2024-01-14 21:59:39 +01:00
|
|
|
inherit (builtins) mapAttrs;
|
|
|
|
inherit (lib) foldl mapAttrsToList mkIf mkOption recursiveUpdate;
|
2024-01-14 10:29:16 +01:00
|
|
|
inherit (lib.types) attrs lazyAttrsOf;
|
2024-01-14 21:59:39 +01:00
|
|
|
inherit (flakelight) selectAttr;
|
2024-01-15 04:05:39 +01:00
|
|
|
inherit (flakelight.types) optCallWith;
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2024-01-14 10:29:16 +01:00
|
|
|
# Avoid checking if toplevel is a derivation as it causes the nixos modules
|
|
|
|
# to be evaluated.
|
|
|
|
isNixos = x: x ? config.system.build.toplevel;
|
|
|
|
|
2024-01-14 21:59:39 +01:00
|
|
|
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 [ ];
|
|
|
|
});
|
2024-01-14 10:29:16 +01:00
|
|
|
|
2024-01-14 21:59:39 +01:00
|
|
|
configs = mapAttrs
|
2024-01-15 04:05:39 +01:00
|
|
|
(hostname: cfg: if isNixos cfg then cfg else mkNixos hostname cfg)
|
2024-01-14 10:29:16 +01:00
|
|
|
config.nixosConfigurations;
|
2023-08-25 06:14:55 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.nixosConfigurations = mkOption {
|
2024-01-15 04:17:50 +01:00
|
|
|
type = optCallWith moduleArgs (lazyAttrsOf (optCallWith moduleArgs attrs));
|
2023-08-25 06:14:55 +02:00
|
|
|
default = { };
|
|
|
|
};
|
|
|
|
|
2024-01-15 09:07:59 +01:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
nixDirAliases.nixosConfigurations = [ "nixos" ];
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
}
|