From cbd22db8c0cdf768a00f9f30e101b26036d55dc6 Mon Sep 17 00:00:00 2001 From: oddlama Date: Mon, 27 May 2024 01:28:07 +0200 Subject: [PATCH] feat: introduce shared global variable aggregator with recursive redistribution --- flake.nix | 19 +++++++++++++++++++ modules/default.nix | 1 + modules/globals.nix | 46 +++++++++++++++++++++++++++++++++++++++++++++ nix/hosts.nix | 2 +- 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 modules/globals.nix diff --git a/flake.nix b/flake.nix index 32570a3..c4db527 100644 --- a/flake.nix +++ b/flake.nix @@ -141,6 +141,25 @@ # Add a shorthand to easily target toplevel derivations "@" = mapAttrs (_: v: v.config.system.build.toplevel) self.nodes; + globals = let + globalsSystem = nixpkgs.lib.evalModules { + prefix = ["globals"]; + modules = [ + ./modules/globals.nix + ({lib, ...}: { + globals = lib.mkMerge ( + lib.concatLists (lib.flip lib.mapAttrsToList self.nodes ( + name: cfg: + builtins.addErrorContext "while aggregating globals from nixosConfigurations.${name} into flake-level globals:" + cfg.config._globalsDefs + )) + ); + }) + ]; + }; + in + globalsSystem.config.globals; + # For each true NixOS system, we want to expose an installer package that # can be used to do the initial setup on the node from a live environment. # We use the minimal sibling configuration to reduce the amount of stuff diff --git a/modules/default.nix b/modules/default.nix index d5e4270..6a39544 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -9,6 +9,7 @@ ./backups.nix ./deterministic-ids.nix ./distributed-config.nix + ./globals.nix ./kanidm.nix ./meta.nix ./netbird-client.nix diff --git a/modules/globals.nix b/modules/globals.nix new file mode 100644 index 0000000..a9da0ee --- /dev/null +++ b/modules/globals.nix @@ -0,0 +1,46 @@ +{ + lib, + options, + ... +}: let + inherit + (lib) + mkOption + types + ; +in { + options = { + globals = mkOption { + default = {}; + type = types.submodule { + options = { + services = mkOption { + type = types.attrsOf (types.submodule { + options = { + domain = mkOption { + type = types.str; + description = ""; + }; + }; + }); + + #telegrafChecks = mkOption { + # type = types.attrsOf (types.submodule { + # options = { + # domain = mkOption {}; + # }; + # }); + #}; + }; + }; + }; + }; + + _globalsDefs = mkOption { + type = types.unspecified; + default = options.globals.definitions; + readOnly = true; + internal = true; + }; + }; +} diff --git a/nix/hosts.nix b/nix/hosts.nix index 27709a2..231db53 100644 --- a/nix/hosts.nix +++ b/nix/hosts.nix @@ -19,7 +19,7 @@ inputs: let specialArgs = { # Use the correct instance lib that has our overlays inherit (pkgs) lib; - inherit (self) nodes; + inherit (self) nodes globals; inherit inputs minimal; }; modules = [