nix-config/nix/hosts.nix

71 lines
2.4 KiB
Nix
Raw Normal View History

2023-09-20 14:37:21 +02:00
inputs: let
inherit (inputs) self;
inherit
(inputs.nixpkgs.lib)
concatMapAttrs
filterAttrs
flip
mapAttrs
mapAttrs'
nameValuePair
nixosSystem
;
# Creates a new nixosSystem with the correct specialArgs, pkgs and name definition
2023-09-26 22:25:58 +02:00
mkHost = {minimal}: name: hostCfg: let
pkgs = self.pkgs.${hostCfg.system};
2023-09-20 14:37:21 +02:00
in
nixosSystem {
specialArgs = {
# Use the correct instance lib that has our overlays
inherit (pkgs) lib;
inherit (self) nodes stateVersion;
2023-09-26 22:25:58 +02:00
inherit inputs minimal;
2023-09-20 14:37:21 +02:00
};
modules = [
{
# We cannot force the package set via nixpkgs.pkgs and
# inputs.nixpkgs.nixosModules.readOnlyPkgs, since some nixosModules
# like nixseparatedebuginfod depend on adding packages via nixpkgs.overlays.
# So we just mimic the options and overlays defined by the passed pkgs set.
2023-09-26 22:25:58 +02:00
nixpkgs.hostPlatform = hostCfg.system;
2023-09-20 14:37:21 +02:00
nixpkgs.overlays = pkgs.overlays;
nixpkgs.config = pkgs.config;
node.name = name;
node.secretsDir = ../hosts/${name}/secrets;
}
../hosts/${name}
];
};
# Load the list of hosts that this flake defines, which
# associates the minimum amount of metadata that is necessary
# to instanciate hosts correctly.
hosts = builtins.fromTOML (builtins.readFile ../hosts.toml);
# Get all hosts of type "nixos"
nixosHosts = filterAttrs (_: x: x.type == "nixos") hosts;
# Process each nixosHosts declaration and generatea nixosSystem definitions
2023-09-26 22:25:58 +02:00
nixosConfigurations = flip mapAttrs nixosHosts (mkHost {minimal = false;});
minimalConfigurations = flip mapAttrs nixosHosts (mkHost {minimal = true;});
2023-09-20 14:37:21 +02:00
# True NixOS nodes can define additional guest nodes that are built
# together with it. We collect all defined guests from each node here
# to allow accessing any node via the unified attribute `nodes`.
2023-12-18 02:11:24 +01:00
guestConfigurations = flip concatMapAttrs self.nixosConfigurations (_: node:
flip mapAttrs' (node.config.guests or {}) (
guestName: guestDef:
nameValuePair guestDef.nodeName (
if guestDef.backend == "microvm"
then node.config.microvm.vms.${guestName}.config
else node.config.containers.${guestName}.nixosConfiguration
)
));
2023-09-20 14:37:21 +02:00
in {
inherit
hosts
nixosConfigurations
2023-09-26 22:25:58 +02:00
minimalConfigurations
2023-12-18 02:11:24 +01:00
guestConfigurations
2023-09-20 14:37:21 +02:00
;
}