43 lines
1 KiB
Nix
43 lines
1 KiB
Nix
{ config, lib, meta, name, ... }:
|
|
|
|
let
|
|
inherit (lib)
|
|
mapAttrs'
|
|
mkEnableOption
|
|
mkIf
|
|
optionalAttrs;
|
|
|
|
net = meta.network.${name};
|
|
|
|
mkAddress = { address, prefixLength, ... }: "${address}/${builtins.toString prefixLength}";
|
|
mkRoute = gateway: { routeConfig = { Gateway = gateway; GatewayOnLink = true; }; };
|
|
|
|
mkInterface = interface: net: {
|
|
name = "10-${interface}";
|
|
value = {
|
|
name = interface;
|
|
address = builtins.map mkAddress (net.ipv4 ++ net.ipv6);
|
|
routes = builtins.map mkRoute net.gateways;
|
|
|
|
networkConfig = optionalAttrs (net ? DHCP) { inherit (net) DHCP; };
|
|
};
|
|
};
|
|
|
|
cfg = config.dgn-network;
|
|
in
|
|
|
|
{
|
|
options.dgn-network.enable = mkEnableOption "automatic network configuration based on metadata" // { default = true; };
|
|
|
|
config = mkIf cfg.enable {
|
|
networking = {
|
|
inherit (net) hostId;
|
|
|
|
hostName = name;
|
|
domain = "${meta.nodes.${name}.zone}.infra.dgnum.eu";
|
|
useNetworkd = true;
|
|
};
|
|
|
|
systemd.network.networks = mapAttrs' mkInterface net.interfaces;
|
|
};
|
|
}
|