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
Signed by: palo
GPG key ID: 76BF5F1928B9618B
4 changed files with 51 additions and 28 deletions

View file

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

View file

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

View file

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

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