2024-06-06 12:10:59 +02:00
|
|
|
{ lib, machineDir, ... }:
|
2024-06-06 11:56:18 +02:00
|
|
|
let
|
|
|
|
|
2024-06-06 12:10:59 +02:00
|
|
|
allMachineNames = lib.mapAttrsToList (name: _: name) (builtins.readDir machineDir);
|
2024-06-06 11:56:18 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-06-07 07:54:43 +02:00
|
|
|
# machine -> factvalue
|
2024-06-06 11:56:18 +02:00
|
|
|
readFactFromAllMachines = fact:
|
|
|
|
let
|
|
|
|
machines = allMachineNames;
|
|
|
|
facts = lib.genAttrs machines (readFact fact);
|
|
|
|
filteredFacts = lib.filterAttrs (_machine: fact: fact != null) facts;
|
|
|
|
in
|
|
|
|
filteredFacts;
|
|
|
|
|
2024-06-07 07:54:43 +02:00
|
|
|
# returns an Attrs of machines and it's facts which have all given facts set.
|
|
|
|
# machine -> fact -> value
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-06 11:56:18 +02:00
|
|
|
in
|
2024-06-07 07:54:43 +02:00
|
|
|
{ inherit allMachineNames getFactPath readFact readFactFromAllMachines readFactsFromAllMachines; }
|