nixos-config/nixos/lib/clanlib.nix
2024-06-06 12:10:59 +02:00

27 lines
675 B
Nix

{ 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;
readFactFromAllMachines = fact:
let
machines = allMachineNames;
facts = lib.genAttrs machines (readFact fact);
filteredFacts = lib.filterAttrs (_machine: fact: fact != null) facts;
in
filteredFacts;
in
{ inherit allMachineNames getFactPath readFact readFactFromAllMachines; }