clan-core/clanModules/static-hosts/default.nix

55 lines
1.8 KiB
Nix
Raw Normal View History

2024-05-19 10:41:14 +00:00
{ lib, config, ... }:
{
options.clan.static-hosts = {
excludeHosts = lib.mkOption {
type = lib.types.listOf lib.types.str;
default =
2024-06-17 10:42:28 +00:00
if config.clan.static-hosts.topLevelDomain != "" then [ ] else [ config.clan.core.machineName ];
2024-05-19 10:41:14 +00:00
description = "Hosts that should be excluded";
};
topLevelDomain = lib.mkOption {
type = lib.types.str;
default = "";
description = "Top level domain to reach hosts";
};
2024-05-19 10:41:14 +00:00
};
config.networking.hosts =
let
2024-06-17 10:42:28 +00:00
clanDir = config.clan.core.clanDir;
2024-05-19 10:41:14 +00:00
machineDir = clanDir + "/machines/";
zerotierIpMachinePath = machines: machineDir + machines + "/facts/zerotier-ip";
machinesFileSet = builtins.readDir machineDir;
machines = lib.mapAttrsToList (name: _: name) machinesFileSet;
networkIpsUnchecked = builtins.map (
machine:
let
fullPath = zerotierIpMachinePath machine;
in
if builtins.pathExists fullPath then machine else null
) machines;
networkIps = lib.filter (machine: machine != null) networkIpsUnchecked;
machinesWithIp = lib.filterAttrs (name: _: (lib.elem name networkIps)) machinesFileSet;
2024-05-19 10:41:14 +00:00
filteredMachines = lib.filterAttrs (
name: _: !(lib.elem name config.clan.static-hosts.excludeHosts)
) machinesWithIp;
2024-05-19 10:41:14 +00:00
in
lib.filterAttrs (_: value: value != null) (
lib.mapAttrs' (
machine: _:
let
path = zerotierIpMachinePath machine;
in
if builtins.pathExists path then
lib.nameValuePair (builtins.readFile path) (
if (config.clan.static-hosts.topLevelDomain == "") then
[ machine ]
else
[ "${machine}.${config.clan.static-hosts.topLevelDomain}" ]
)
else
{ }
) filteredMachines
);
2024-05-19 10:41:14 +00:00
}