From 79b23d1f1751d70ad6130fd6fb1c226a3241ca31 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 20 Sep 2023 14:37:21 +0200 Subject: [PATCH] feat: simplified colmena config --- flake.nix | 20 ++++----- modules/hardware/physical.nix | 2 +- nix/colmena.nix | 34 --------------- nix/generate-node.nix | 19 --------- nix/hosts.nix | 78 +++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 64 deletions(-) delete mode 100644 nix/colmena.nix delete mode 100644 nix/generate-node.nix create mode 100644 nix/hosts.nix diff --git a/flake.nix b/flake.nix index 5f711e2..d84b99f 100644 --- a/flake.nix +++ b/flake.nix @@ -95,21 +95,21 @@ }; inherit stateVersion; - - hosts = builtins.fromTOML (builtins.readFile ./hosts.toml); - - colmena = import ./nix/colmena.nix inputs; - # all bare metal nodes - colmenaNodes = ((colmena.lib.makeHive self.colmena).introspect (x: x)).nodes; - # todo add microvmNodes - - nodes = self.colmenaNodes; + inherit + (import ./nix/hosts.nix inputs) + colmena + hosts + microvmConfigurations + nixosConfigurations + ; + nodes = self.nixosConfigurations // self.microvmConfigurations; + top = lib.mapAttrs (_: x: x.config.system.build.toplevel) self.nodes; inherit (lib.foldl' lib.recursiveUpdate {} (lib.mapAttrsToList (import ./nix/generate-installer-package.nix inputs) - self.colmenaNodes)) + self.nixosConfigurations)) packages ; } diff --git a/modules/hardware/physical.nix b/modules/hardware/physical.nix index f8349d6..5eb7123 100644 --- a/modules/hardware/physical.nix +++ b/modules/hardware/physical.nix @@ -8,6 +8,6 @@ services = { fwupd.enable = true; smartd.enable = true; - thermald.enable = builtins.elem config.nixpkgs.system ["x86_64-linux"]; + thermald.enable = builtins.elem config.nixpkgs.hostPlatform.system ["x86_64-linux"]; }; } diff --git a/nix/colmena.nix b/nix/colmena.nix deleted file mode 100644 index 15653f2..0000000 --- a/nix/colmena.nix +++ /dev/null @@ -1,34 +0,0 @@ -{ - self, - nixpkgs, - ... -} @ inputs: let - inherit - (nixpkgs.lib) - filterAttrs - mapAttrs - flip - ; - - nixosNodes = filterAttrs (_: x: x.type == "nixos") self.hosts; - nodes = flip mapAttrs nixosNodes (name: hostCfg: - import ./generate-node.nix inputs { - inherit name; - inherit (hostCfg) system; - modules = [ - ../hosts/${name} - {node.secretsDir = ../hosts/${name}/secrets;} - ]; - }); -in - { - meta = { - description = "Patrick's colmena configuration(Eigenhändig geklaut von oddlama)"; - # Just a required dummy for colmena, overwritten on a per-node basis by nodeNixpkgs below. - nixpkgs = self.pkgs.x86_64-linux; - # This is so colmena uses the correct nixpkgs and specialarges for each host - nodeNixpkgs = mapAttrs (_: node: node.pkgs) nodes; - nodeSpecialArgs = mapAttrs (_: node: node.specialArgs) nodes; - }; - } - // mapAttrs (_: node: {inherit (node) imports;}) nodes diff --git a/nix/generate-node.nix b/nix/generate-node.nix deleted file mode 100644 index 12fe8d9..0000000 --- a/nix/generate-node.nix +++ /dev/null @@ -1,19 +0,0 @@ -{self, ...} @ inputs: { - name, - # Additional modules to import - modules ? [], - system, - ... -}: { - inherit system; - pkgs = self.pkgs.${system}; - specialArgs = { - inherit (self.pkgs.${system}) lib; - inherit (self) nodes stateVersion; - inherit - inputs - ; - }; - imports = - modules ++ [{node.name = name;}]; -} diff --git a/nix/hosts.nix b/nix/hosts.nix new file mode 100644 index 0000000..495b7c2 --- /dev/null +++ b/nix/hosts.nix @@ -0,0 +1,78 @@ +inputs: let + inherit (inputs) self; + inherit + (inputs.nixpkgs.lib) + concatMapAttrs + filterAttrs + flip + mapAttrs + mapAttrs' + nameValuePair + nixosSystem + ; + + mapNixosConfigs = f: mapAttrs (_: f) self.nixosConfigurations; + + # Creates a new nixosSystem with the correct specialArgs, pkgs and name definition + mkHost = name: system: let + pkgs = self.pkgs.${system}; + in + nixosSystem { + specialArgs = { + # Use the correct instance lib that has our overlays + inherit (pkgs) lib; + inherit (self) nodes stateVersion; + inherit inputs; + }; + 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. + nixpkgs.hostPlatform = system; + 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 + nixosConfigurations = flip mapAttrs nixosHosts (name: hostCfg: mkHost name hostCfg.system); + + # We now wrap nixosConfigurations so that colmena understands it + colmena = + { + meta = { + # Just a required dummy for colmena, overwritten on a per-node basis by nodeNixpkgs below. + nixpkgs = self.pkgs.x86_64-linux; + nodeNixpkgs = mapNixosConfigs (v: v.pkgs); + nodeSpecialArgs = mapNixosConfigs (v: v._module.specialArgs); + }; + } + // mapNixosConfigs (v: {imports = v._module.args.modules;}); + + # True NixOS nodes can define additional microvms (guest nodes) that are built + # together with the true host. We collect all defined microvm nodes + # from each node here to allow accessing any node via the unified attribute `nodes`. + microvmConfigurations = flip concatMapAttrs self.nixosConfigurations (_: node: + mapAttrs' + (vm: def: nameValuePair def.nodeName node.config.microvm.vms.${vm}.config) + (node.config.meta.microvms.vms or {})); +in { + inherit + colmena + hosts + microvmConfigurations + nixosConfigurations + ; +}