From f2282398349aa74c8dc444508ec95aa4873a6cd7 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Wed, 19 Jun 2024 14:57:53 +0200 Subject: [PATCH 1/2] add lib/facts.nix --- lib/facts.nix | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/facts.nix diff --git a/lib/facts.nix b/lib/facts.nix new file mode 100644 index 00000000..6f19a6c1 --- /dev/null +++ b/lib/facts.nix @@ -0,0 +1,64 @@ +{ lib, machineDir, ... }: +let + + allMachineNames = lib.mapAttrsToList (name: _: name) (builtins.readDir machineDir); + + getFactPath = fact: machine: + "${machineDir}/${machine}/facts/${fact}"; + + readFact = fact: machine: + let + path = getFactPath fact machine; + in + if builtins.pathExists path then + builtins.readFile path + else + null; + + # Example: + # + # readFactFromAllMachines zerotier-ip + # => { + # machineA = "1.2.3.4"; + # machineB = "5.6.7.8"; + # }; + readFactFromAllMachines = fact: + let + machines = allMachineNames; + facts = lib.genAttrs machines (readFact fact); + filteredFacts = lib.filterAttrs (_machine: fact: fact != null) facts; + in + filteredFacts; + + # all given facts are are set and factvalues are never null. + # + # Example: + # + # readFactsFromAllMachines [ "zerotier-ip" "syncthing.pub" ] + # => { + # machineA = + # { + # "zerotier-ip" = "1.2.3.4"; + # "synching.pub" = "1234"; + # }; + # machineB = + # { + # "zerotier-ip" = "5.6.7.8"; + # "synching.pub" = "23456719"; + # }; + # }; + readFactsFromAllMachines = facts: + let + # machine -> fact -> factvalue + machinesFactsAttrs = lib.genAttrs allMachineNames (machine: lib.genAttrs facts (fact: readFact fact machine)); + # remove all machines which don't have all facts set + filteredMachineFactAttrs = + lib.filterAttrs (_machine: values: builtins.all (fact: values.${fact} != null) facts) + machinesFactsAttrs; + in + filteredMachineFactAttrs; + + + +in +{ inherit allMachineNames getFactPath readFact readFactFromAllMachines readFactsFromAllMachines; } From 1a969d884eedf5cffc57bb0563e049fa19bf67ff Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 2 Jul 2024 11:26:50 +0200 Subject: [PATCH 2/2] lib: add fact loaders to clan lib --- lib/default.nix | 3 ++- lib/facts.nix | 42 +++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 086fcce8..91a23d6d 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -6,8 +6,9 @@ }: { evalClanModules = import ./eval-clan-modules { inherit clan-core nixpkgs lib; }; + buildClan = import ./build-clan { inherit clan-core lib nixpkgs; }; + facts = import ./facts.nix { inherit lib; }; inventory = import ./inventory { inherit lib clan-core; }; jsonschema = import ./jsonschema { inherit lib; }; modules = import ./description.nix { inherit clan-core lib; }; - buildClan = import ./build-clan { inherit clan-core lib nixpkgs; }; } diff --git a/lib/facts.nix b/lib/facts.nix index 6f19a6c1..5ba36b5d 100644 --- a/lib/facts.nix +++ b/lib/facts.nix @@ -1,19 +1,17 @@ -{ lib, machineDir, ... }: +{ lib, ... }: +machineDir: let allMachineNames = lib.mapAttrsToList (name: _: name) (builtins.readDir machineDir); - getFactPath = fact: machine: - "${machineDir}/${machine}/facts/${fact}"; + getFactPath = fact: machine: "${machineDir}/${machine}/facts/${fact}"; - readFact = fact: machine: + readFact = + fact: machine: let path = getFactPath fact machine; in - if builtins.pathExists path then - builtins.readFile path - else - null; + if builtins.pathExists path then builtins.readFile path else null; # Example: # @@ -22,7 +20,8 @@ let # machineA = "1.2.3.4"; # machineB = "5.6.7.8"; # }; - readFactFromAllMachines = fact: + readFactFromAllMachines = + fact: let machines = allMachineNames; facts = lib.genAttrs machines (readFact fact); @@ -47,18 +46,27 @@ let # "synching.pub" = "23456719"; # }; # }; - readFactsFromAllMachines = facts: + readFactsFromAllMachines = + facts: let # machine -> fact -> factvalue - machinesFactsAttrs = lib.genAttrs allMachineNames (machine: lib.genAttrs facts (fact: readFact fact machine)); + machinesFactsAttrs = lib.genAttrs allMachineNames ( + machine: lib.genAttrs facts (fact: readFact fact machine) + ); # remove all machines which don't have all facts set - filteredMachineFactAttrs = - lib.filterAttrs (_machine: values: builtins.all (fact: values.${fact} != null) facts) - machinesFactsAttrs; + filteredMachineFactAttrs = lib.filterAttrs ( + _machine: values: builtins.all (fact: values.${fact} != null) facts + ) machinesFactsAttrs; in filteredMachineFactAttrs; - - in -{ inherit allMachineNames getFactPath readFact readFactFromAllMachines readFactsFromAllMachines; } +{ + inherit + allMachineNames + getFactPath + readFact + readFactFromAllMachines + readFactsFromAllMachines + ; +}