From dc0b7fc3bf7ee759d901564971d20c7b867d6c81 Mon Sep 17 00:00:00 2001 From: pinpox Date: Mon, 20 Oct 2025 14:17:36 +0200 Subject: [PATCH] Add library function to read public vars --- lib/default.nix | 2 ++ .../all-services-wrapper.nix | 4 +++ lib/vars.nix | 25 +++++++++++++++++++ modules/inventoryClass/inventory.nix | 2 +- 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 lib/vars.nix diff --git a/lib/default.nix b/lib/default.nix index d84860a1f..672b08515 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -30,6 +30,8 @@ lib.fix ( jsonschema = import ./jsonschema { inherit lib; }; docs = import ./docs.nix { inherit lib; }; + vars = import ./vars.nix { inherit lib; }; + # flakes flakes = clanLib.callLib ./flakes.nix { }; diff --git a/lib/inventory/distributed-service/all-services-wrapper.nix b/lib/inventory/distributed-service/all-services-wrapper.nix index d250ed517..7da8d68b6 100644 --- a/lib/inventory/distributed-service/all-services-wrapper.nix +++ b/lib/inventory/distributed-service/all-services-wrapper.nix @@ -37,6 +37,10 @@ in { name, ... }: { _module.args._ctx = [ name ]; + _module.args.clanLib = specialArgs.clanLib; + _module.args.exports = config.exports; + _module.args.directory = directory; + } ) ./service-module.nix diff --git a/lib/vars.nix b/lib/vars.nix new file mode 100644 index 000000000..539d215e0 --- /dev/null +++ b/lib/vars.nix @@ -0,0 +1,25 @@ +_: { + getPublicValue = + { + + backend ? "in_repo", + default ? throw "getPublicValue: Public value ${machine}/${generator}/${file} not found!", + shared ? false, + generator, + machine, + file, + flake, + }: + + if backend == "in_repo" then + let + path = + if shared then + "${flake}/vars/shared/${generator}/${file}/value" + else + "${flake}/vars/per-machine/${machine}/${generator}/${file}/value"; + in + if builtins.pathExists path then builtins.readFile path else default + else + throw "backend ${backend} does not implement getPublicValue"; +} diff --git a/modules/inventoryClass/inventory.nix b/modules/inventoryClass/inventory.nix index 833de5f1b..c26390b62 100644 --- a/modules/inventoryClass/inventory.nix +++ b/modules/inventoryClass/inventory.nix @@ -167,7 +167,7 @@ in ''; type = types.submoduleWith { specialArgs = { - inherit (config) machines; + inherit (config) machines clanLib; }; modules = [ {