2023-06-10 10:50:53 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib; let
|
2019-01-21 10:04:37 +00:00
|
|
|
check = hostname: any (domain: hasSuffix ".${domain}" hostname) domains;
|
2023-06-10 10:50:53 +00:00
|
|
|
domains = attrNames (filterAttrs (_: slib.eq "hosts") config.krebs.dns.providers);
|
|
|
|
# we need this import because we have infinite recursion otherwise
|
|
|
|
slib = import ../../lib/pure.nix { inherit lib; };
|
2019-01-21 10:04:37 +00:00
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
krebs.hosts = mkOption {
|
|
|
|
default = {};
|
2023-06-10 10:50:53 +00:00
|
|
|
type = types.attrsOf slib.types.host;
|
2019-01-21 10:04:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-10 10:50:53 +00:00
|
|
|
config = lib.mkIf config.krebs.enable {
|
2019-01-21 10:51:15 +00:00
|
|
|
networking.hosts =
|
|
|
|
filterAttrs
|
|
|
|
(_name: value: value != [])
|
|
|
|
(zipAttrsWith
|
|
|
|
(_: concatLists)
|
|
|
|
(concatMap
|
|
|
|
(host:
|
|
|
|
concatMap
|
|
|
|
(net: let
|
2019-01-21 10:04:37 +00:00
|
|
|
aliases = longs ++ shorts;
|
|
|
|
longs = filter check net.aliases;
|
|
|
|
shorts = let s = ".${config.krebs.dns.search-domain}"; in
|
2020-08-16 09:28:27 +00:00
|
|
|
optionals
|
|
|
|
(config.krebs.dns.search-domain != null)
|
|
|
|
(map (removeSuffix s)
|
|
|
|
(filter (hasSuffix s)
|
|
|
|
longs));
|
2019-01-21 10:04:37 +00:00
|
|
|
in
|
2019-01-21 10:51:15 +00:00
|
|
|
map (addr: { ${addr} = aliases; }) net.addrs)
|
|
|
|
(attrValues host.nets))
|
|
|
|
(attrValues config.krebs.hosts)));
|
2019-01-31 13:39:02 +00:00
|
|
|
|
|
|
|
nixpkgs.config.packageOverrides = super: let
|
|
|
|
# nameValuePair name value : { "name" : name, "value" : value }
|
|
|
|
|
|
|
|
# addr : str
|
|
|
|
# aliase : str
|
|
|
|
# hostname : str
|
|
|
|
# netname : str
|
|
|
|
|
|
|
|
# addrAliases : nameValuePair addr [alias]
|
|
|
|
|
|
|
|
# hostNetAliases : host -> { ${netname} : [addrAliases] }
|
|
|
|
hostNetAliases = host:
|
2019-02-02 08:22:06 +00:00
|
|
|
mapAttrs (_: net: filter (x: x.name != null && x.value != []) [
|
2019-01-31 13:39:02 +00:00
|
|
|
{ name = net.ip4.addr or null; value = net.aliases; }
|
2020-08-22 08:07:03 +00:00
|
|
|
{ name = net.ip4.addr or null; value = (map (alias: "4.${alias}") net.aliases); }
|
2019-01-31 13:39:02 +00:00
|
|
|
{ name = net.ip6.addr or null; value = net.aliases; }
|
2020-08-22 08:07:03 +00:00
|
|
|
{ name = net.ip6.addr or null; value = (map (alias: "6.${alias}") net.aliases); }
|
2019-01-31 13:39:02 +00:00
|
|
|
]) host.nets;
|
|
|
|
|
|
|
|
# netAliases : { ${netname} : [addrAliases] }
|
|
|
|
netAliases =
|
|
|
|
foldl'
|
|
|
|
(result: host:
|
|
|
|
foldl'
|
|
|
|
# λ netAliases -> [addrAliases] -> netAliases
|
|
|
|
(result: { name, value }: result // {
|
|
|
|
${name} = result.${name} or [] ++ value;
|
|
|
|
})
|
|
|
|
result
|
|
|
|
(mapAttrsToList nameValuePair (hostNetAliases host))
|
|
|
|
)
|
|
|
|
{}
|
|
|
|
(attrValues config.krebs.hosts);
|
|
|
|
|
|
|
|
# writeHosts : str -> [addrAliases] -> package
|
|
|
|
writeHosts = name: addrAliases: super.writeText name ''
|
|
|
|
${concatMapStringsSep
|
|
|
|
"\n"
|
|
|
|
({ name, value }: "${name} ${toString value}")
|
|
|
|
addrAliases}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
2019-01-31 14:02:05 +00:00
|
|
|
# hosts file for all krebs networks
|
|
|
|
krebs-hosts =
|
|
|
|
writeHosts "krebs-hosts" (concatLists [
|
|
|
|
netAliases.internet
|
|
|
|
netAliases.retiolum
|
|
|
|
netAliases.wiregrill
|
|
|
|
]);
|
|
|
|
|
|
|
|
# combined hosts file for all networks (even custom ones)
|
|
|
|
krebs-hosts_combined =
|
|
|
|
writeHosts "krebs-hosts_combined"
|
|
|
|
(concatLists (attrValues netAliases));
|
2019-01-31 13:39:02 +00:00
|
|
|
}
|
|
|
|
//
|
2023-06-10 10:50:53 +00:00
|
|
|
slib.genAttrs' (attrNames netAliases) (netname: rec {
|
2019-01-31 13:39:02 +00:00
|
|
|
name = "krebs-hosts-${netname}";
|
|
|
|
value = writeHosts name netAliases.${netname};
|
|
|
|
});
|
2019-01-21 10:04:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|