From 777d803b765fa7d13997148c041100aecc464165 Mon Sep 17 00:00:00 2001 From: oddlama Date: Mon, 12 Jun 2023 00:17:55 +0200 Subject: [PATCH] feat: add module to allow extending configuration of other nodes --- modules/distributed-config.nix | 46 ++++++++++++++++++++++++++++++++++ nix/generate-node.nix | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 modules/distributed-config.nix diff --git a/modules/distributed-config.nix b/modules/distributed-config.nix new file mode 100644 index 0000000..647173d --- /dev/null +++ b/modules/distributed-config.nix @@ -0,0 +1,46 @@ +{ + config, + extraLib, + lib, + nodeName, + colmenaNodes, + ... +}: let + inherit + (lib) + attrNames + concatMap + filter + mdDoc + mkOption + types + unique + subtractLists + ; + + inherit + (extraLib) + mergeToplevelConfigs + ; +in { + options.nodes = mkOption { + type = types.attrsOf types.unspecified; + default = {}; + description = mdDoc "Allows extending the configuration of other machines."; + }; + + config = let + otherNodes = filter (n: n != nodeName) (attrNames colmenaNodes); + foreignConfigs = map (n: colmenaNodes.${n}.config.nodes.${nodeName} or {}) otherNodes; + toplevelAttrs = ["age" "networking" "systemd" "services"]; + in + { + assertions = + map (n: { + assertion = false; + message = "Cannot extend configuration using nodes.${n} because the given node is not a registered or not a first-class nixos node (microvm's can't be extended right now)."; + }) + (subtractLists (attrNames colmenaNodes) (attrNames config.nodes)); + } + // mergeToplevelConfigs toplevelAttrs foreignConfigs; +} diff --git a/nix/generate-node.nix b/nix/generate-node.nix index cea8d1a..5856345 100644 --- a/nix/generate-node.nix +++ b/nix/generate-node.nix @@ -18,7 +18,7 @@ in { pkgs = self.pkgs.${nodeMeta.system}; specialArgs = { inherit (nixpkgs) lib; - inherit (self) extraLib nodes stateVersion; + inherit (self) extraLib nodes stateVersion colmenaNodes; inherit inputs nodeName; # Only set the nodePath if it is an actual directory nodePath =