krebs-hosts-*: generate from krebs.hosts

This commit is contained in:
tv 2019-01-31 14:39:02 +01:00
parent 54517574dd
commit 478b45cf97
2 changed files with 59 additions and 24 deletions

View File

@ -1,6 +1,5 @@
with import <stockholm/lib>;
{ config, ... }: let
# TODO dedup functions with ./retiolum-hosts.nix
check = hostname: any (domain: hasSuffix ".${domain}" hostname) domains;
domains = attrNames (filterAttrs (_: eq "hosts") config.krebs.dns.providers);
in {
@ -30,6 +29,62 @@ in {
map (addr: { ${addr} = aliases; }) net.addrs)
(attrValues host.nets))
(attrValues config.krebs.hosts)));
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:
mapAttrs (_: net: filter (x: x.name != null) [
{ name = net.ip4.addr or null; value = net.aliases; }
{ name = net.ip6.addr or null; value = net.aliases; }
]) 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);
# allAddrAliases : [addrAliases]
allAddrAliases =
flatten
(map
(host: attrValues (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
{
krebs-hosts = writeHosts "krebs-hosts" allAddrAliases;
}
//
genAttrs' (attrNames netAliases) (netname: rec {
name = "krebs-hosts-${netname}";
value = writeHosts name netAliases.${netname};
});
};
}

View File

@ -1,28 +1,8 @@
with import <stockholm/lib>;
{ config, ... }: let
# TODO dedup functions with ./hosts.nix
check = hostname: any (domain: hasSuffix ".${domain}" hostname) domains;
domains = attrNames (filterAttrs (_: eq "hosts") config.krebs.dns.providers);
in {
{ config, pkgs, ... }: {
nixpkgs.config.packageOverrides = super: {
retiolum-hosts =
super.writeText "retiolum-hosts" ''
${
concatStringsSep
"\n"
(flatten
(map
(host: let
net = host.nets.retiolum;
aliases = longs;
longs = filter check net.aliases;
in
optionals
(aliases != [])
(map (addr: "${addr} ${toString aliases}") net.addrs))
(filter (host: hasAttr "retiolum" host.nets)
(attrValues config.krebs.hosts))))
}
'';
trace "pkgs.retiolum-hosts is deprecated, use pkgs.krebs-hosts-retiolum instead"
pkgs.krebs-hosts-retiolum;
};
}