2024-02-02 10:51:31 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
meta,
|
|
|
|
name,
|
|
|
|
...
|
|
|
|
}:
|
2023-07-20 12:32:31 +02:00
|
|
|
|
|
|
|
let
|
2024-02-02 10:51:31 +01:00
|
|
|
inherit (lib)
|
|
|
|
mapAttrs'
|
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
optionalAttrs
|
|
|
|
;
|
2023-07-20 12:32:31 +02:00
|
|
|
|
2024-02-20 17:47:34 +01:00
|
|
|
net' = meta.network.${name};
|
2023-07-20 12:32:31 +02:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
mkAddress = { address, prefixLength, ... }: "${address}/${builtins.toString prefixLength}";
|
2023-12-04 12:36:31 +01:00
|
|
|
mkRoute = gateway: {
|
|
|
|
routeConfig = {
|
|
|
|
Gateway = gateway;
|
|
|
|
GatewayOnLink = true;
|
|
|
|
};
|
|
|
|
};
|
2023-07-20 12:32:31 +02:00
|
|
|
|
|
|
|
mkInterface = interface: net: {
|
|
|
|
name = "10-${interface}";
|
|
|
|
value = {
|
|
|
|
name = interface;
|
|
|
|
address = builtins.map mkAddress (net.ipv4 ++ net.ipv6);
|
|
|
|
routes = builtins.map mkRoute net.gateways;
|
|
|
|
|
2023-12-04 12:36:31 +01:00
|
|
|
# Add default DNS servers
|
2024-02-20 17:57:24 +01:00
|
|
|
dns =
|
|
|
|
net'.dns or [
|
|
|
|
"1.1.1.1#cloudflare-dns.com"
|
|
|
|
"8.8.8.8#dns.google"
|
|
|
|
"1.0.0.1#cloudflare-dns.com"
|
|
|
|
"8.8.4.4#dns.google"
|
|
|
|
"2606:4700:4700::1111#cloudflare-dns.com"
|
|
|
|
"2001:4860:4860::8888#dns.google"
|
|
|
|
"2606:4700:4700::1001#cloudflare-dns.com"
|
|
|
|
"2001:4860:4860::8844#dns.google"
|
|
|
|
];
|
2023-12-04 12:36:31 +01:00
|
|
|
|
2023-07-20 12:32:31 +02:00
|
|
|
networkConfig = optionalAttrs (net ? DHCP) { inherit (net) DHCP; };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
cfg = config.dgn-network;
|
2024-02-02 10:51:31 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.dgn-network.enable = mkEnableOption "automatic network configuration based on metadata" // {
|
|
|
|
default = true;
|
|
|
|
};
|
2023-07-20 12:32:31 +02:00
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
networking = {
|
2024-02-20 17:47:34 +01:00
|
|
|
inherit (net') hostId;
|
2023-07-20 12:32:31 +02:00
|
|
|
|
|
|
|
hostName = name;
|
|
|
|
domain = "${meta.nodes.${name}.zone}.infra.dgnum.eu";
|
|
|
|
useNetworkd = true;
|
2023-11-13 14:21:52 +01:00
|
|
|
|
|
|
|
firewall.logRefusedConnections = false;
|
2023-07-20 12:32:31 +02:00
|
|
|
};
|
|
|
|
|
2024-02-20 17:47:34 +01:00
|
|
|
systemd.network.networks = mapAttrs' mkInterface net'.interfaces;
|
2023-07-20 12:32:31 +02:00
|
|
|
};
|
|
|
|
}
|