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;
|
||||
assets = ./nixos/assets;
|
||||
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
|
||||
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;
|
||||
|
|
|
@ -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
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