2024-05-19 10:41:14 +00:00
|
|
|
{ lib, config, ... }:
|
|
|
|
{
|
|
|
|
options.clan.static-hosts = {
|
|
|
|
excludeHosts = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2024-06-02 18:57:15 +00:00
|
|
|
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";
|
|
|
|
};
|
2024-05-29 17:32:47 +00:00
|
|
|
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";
|
2024-06-05 14:53:41 +00:00
|
|
|
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)
|
2024-06-05 14:53:41 +00:00
|
|
|
) machinesWithIp;
|
2024-05-19 10:41:14 +00:00
|
|
|
in
|
2024-05-21 15:23:49 +00:00
|
|
|
lib.filterAttrs (_: value: value != null) (
|
|
|
|
lib.mapAttrs' (
|
|
|
|
machine: _:
|
|
|
|
let
|
|
|
|
path = zerotierIpMachinePath machine;
|
|
|
|
in
|
2024-05-29 17:32:47 +00:00
|
|
|
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
|
2024-06-05 14:53:41 +00:00
|
|
|
{ }
|
2024-05-21 15:23:49 +00:00
|
|
|
) filteredMachines
|
|
|
|
);
|
2024-05-19 10:41:14 +00:00
|
|
|
}
|