add clanlib.nix to make stuff more readable
This commit is contained in:
parent
c8917d9584
commit
6b4496a926
4 changed files with 51 additions and 28 deletions
|
@ -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; } ;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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
33
nixos/lib/clanlib.nix
Normal 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; }
|
Loading…
Reference in a new issue