add clanlib.nix to make stuff more readable

This commit is contained in:
Ingolf Wagner 2024-06-06 11:56:18 +02:00
parent c8917d9584
commit 6b4496a926
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
4 changed files with 51 additions and 28 deletions

View file

@ -169,6 +169,7 @@
inherit private_assets; inherit private_assets;
assets = ./nixos/assets; assets = ./nixos/assets;
factsGenerator = clan-fact-generators.lib { inherit pkgs; }; factsGenerator = clan-fact-generators.lib { inherit pkgs; };
clanLib = import ./nixos/lib/clanlib.nix { inherit (pkgs) lib; machineDir = ./machines; } ;
}; };
}; };

View file

@ -1,10 +1,7 @@
{ config, lib, pkgs, factsGenerator, ... }: { config, lib, pkgs, factsGenerator, clanLib, ... }:
let let
machineDir = "${config.clanCore.clanDir}/machines"; syncthingPub = clanLib.readFact "syncthing.pub";
syncthingPub = machine: zerotierIp = clanLib.readFact "zerotier-ip";
lib.removeSuffix "\n"
(builtins.readFile "${machineDir}/${machine}/facts/syncthing.pub");
zerotierIp = machine: (builtins.readFile "${machineDir}/${machine}/facts/zerotier-ip");
in in
with lib; { with lib; {
@ -21,12 +18,6 @@ with lib; {
cert = config.clanCore.facts.services.syncthing.secret."syncthing.cert".path; cert = config.clanCore.facts.services.syncthing.secret."syncthing.cert".path;
settings.devices = settings.devices =
let 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: { zeroDevice = machine: {
"${machine}" = { "${machine}" = {
name = machine; name = machine;

View file

@ -1,25 +1,23 @@
{ lib, config, ... }: { lib, config, clanLib, ... }:
let let
machineDir = "${config.clanCore.clanDir}/machines/"; machines = clanLib.allMachineNames;
publicKey = machine: (builtins.readFile "${machineDir}/${machine}/facts/ssh.id_ed25519.pub"); publicKey = clanLib.readFact "ssh.id_ed25519.pub";
machinesFileSet = builtins.readDir machineDir;
machines = lib.mapAttrsToList (name: _: name) machinesFileSet;
tld = config.clan.static-hosts.topLevelDomain; tld = config.clan.static-hosts.topLevelDomain;
knownHosts = lib.mapAttrs
(name: _: knownHosts = lib.genAttrs machines
(machine:
{ {
hostNames = [ hostNames = [
"[${name}]:2222" "[${machine}]:2222"
"[${name}.${tld}]:2222" "[${machine}.${tld}]:2222"
"[${name}.private]:2222" "[${machine}.private]:2222"
"${name}" "${machine}"
"${name}.${tld}" "${machine}.${tld}"
"${name}.private" "${machine}.private"
]; ];
publicKey = publicKey name; publicKey = publicKey machine;
} }
) );
machinesFileSet;
in in
{ {
services.openssh.knownHosts = knownHosts; services.openssh.knownHosts = knownHosts;

33
nixos/lib/clanlib.nix Normal file
View file

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