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 + ; +}