diff --git a/flake.nix b/flake.nix index 9040815..3ad880b 100644 --- a/flake.nix +++ b/flake.nix @@ -169,6 +169,7 @@ inherit private_assets; assets = ./nixos/assets; factsGenerator = clan-fact-generators.lib { inherit pkgs; }; + clanLib = import ./nixos/lib/clanlib.nix { inherit (pkgs) lib; machineDir = ./machines; } ; }; }; diff --git a/nixos/components/network/syncthing.nix b/nixos/components/network/syncthing.nix index 5f87385..110fef2 100644 --- a/nixos/components/network/syncthing.nix +++ b/nixos/components/network/syncthing.nix @@ -1,10 +1,7 @@ -{ config, lib, pkgs, factsGenerator, ... }: +{ config, lib, pkgs, factsGenerator, clanLib, ... }: let - machineDir = "${config.clanCore.clanDir}/machines"; - syncthingPub = machine: - lib.removeSuffix "\n" - (builtins.readFile "${machineDir}/${machine}/facts/syncthing.pub"); - zerotierIp = machine: (builtins.readFile "${machineDir}/${machine}/facts/zerotier-ip"); + syncthingPub = clanLib.readFact "syncthing.pub"; + zerotierIp = clanLib.readFact "zerotier-ip"; in with lib; { @@ -21,12 +18,6 @@ with lib; { cert = config.clanCore.facts.services.syncthing.secret."syncthing.cert".path; settings.devices = let - #machineDir = "${config.clanCore.clanDir}/machines"; - #syncthingPub = machine: - # lib.removeSuffix "\n" - # (builtins.readFile "${machineDir}/${machine}/facts/syncthing.pub"); - #zerotierIp = machine: (builtins.readFile "${machineDir}/${machine}/facts/zerotier-ip"); - zeroDevice = machine: { "${machine}" = { name = machine; diff --git a/nixos/components/network/zerotier.nix b/nixos/components/network/zerotier.nix index b37c327..6537bf9 100644 --- a/nixos/components/network/zerotier.nix +++ b/nixos/components/network/zerotier.nix @@ -1,25 +1,23 @@ -{ lib, config, ... }: +{ lib, config, clanLib, ... }: let - machineDir = "${config.clanCore.clanDir}/machines/"; - publicKey = machine: (builtins.readFile "${machineDir}/${machine}/facts/ssh.id_ed25519.pub"); - machinesFileSet = builtins.readDir machineDir; - machines = lib.mapAttrsToList (name: _: name) machinesFileSet; + machines = clanLib.allMachineNames; + publicKey = clanLib.readFact "ssh.id_ed25519.pub"; tld = config.clan.static-hosts.topLevelDomain; - knownHosts = lib.mapAttrs - (name: _: + + knownHosts = lib.genAttrs machines + (machine: { hostNames = [ - "[${name}]:2222" - "[${name}.${tld}]:2222" - "[${name}.private]:2222" - "${name}" - "${name}.${tld}" - "${name}.private" + "[${machine}]:2222" + "[${machine}.${tld}]:2222" + "[${machine}.private]:2222" + "${machine}" + "${machine}.${tld}" + "${machine}.private" ]; - publicKey = publicKey name; + publicKey = publicKey machine; } - ) - machinesFileSet; + ); in { services.openssh.knownHosts = knownHosts; diff --git a/nixos/lib/clanlib.nix b/nixos/lib/clanlib.nix new file mode 100644 index 0000000..4c365d5 --- /dev/null +++ b/nixos/lib/clanlib.nix @@ -0,0 +1,33 @@ +{lib, machineDir, ... }: +let + + allMachineNames = + let + #machineDir = "${config.clanCore.clanDir}/machines/"; + #machineDir = ../../machines; + machines = lib.mapAttrsToList (name: _: name) (builtins.readDir machineDir); + in + machines; + + 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; }