network/nix/nixos-module/server/network.nix

119 lines
2.9 KiB
Nix
Raw Normal View History

2021-04-10 14:52:13 +02:00
# Server network configuration
2022-03-22 18:13:17 +01:00
{ config, lib, ... }:
let
2021-04-10 14:52:13 +02:00
# LXC containers on this host
containers =
lib.filterAttrs (_: { role, model, ... }:
role == "container" &&
model == "lxc"
) config.site.hosts;
2021-04-10 14:52:13 +02:00
# Every bridged veth network required by all containers
bridgeNets =
lib.lists.unique (
builtins.concatMap ({ interfaces, ... }:
builtins.attrNames (
lib.filterAttrs (_: { type, ... }: type == "veth") interfaces
)) (builtins.attrValues containers)
);
2021-04-10 14:52:13 +02:00
# Every network (both veth+phys) required by all containers
ctNets =
lib.lists.unique (
2021-05-31 00:06:56 +02:00
builtins.concatMap ({ physicalInterfaces, ... }:
builtins.attrNames physicalInterfaces
) (builtins.attrValues containers)
);
in
{
networking.firewall = {
enable = true;
2021-04-10 14:52:13 +02:00
allowedTCPPorts = [
# SSH
22
];
};
systemd.network = {
enable = true;
netdevs = {
bond0.netdevConfig = {
Kind = "bond";
Name = "bond0";
};
2021-04-10 14:52:13 +02:00
# LACP
bond0.bondConfig.Mode = "802.3ad";
} // (
builtins.foldl' (result: net: result // {
2021-04-10 14:52:13 +02:00
# Bridges are named just like the corresponding net.
"${net}" = {
netdevConfig = {
Kind = "bridge";
Name = "${net}";
};
extraConfig = ''
[Bridge]
ForwardDelaySec=2
STP=true
'';
};
}) {} bridgeNets
) // (
builtins.foldl' (result: net: result // {
2021-04-10 14:52:13 +02:00
# External VLAN interfaces (to be attached to net bridges) are
# named with an "ext-" prefix.
"ext-${net}" = {
netdevConfig = {
Kind = "vlan";
Name = "ext-${net}";
};
vlanConfig.Id = config.site.net.${net}.vlan;
};
}) {} ctNets
);
networks = {
en = {
2021-04-10 14:52:13 +02:00
# physical ethernet ports
matchConfig.Name = "en*";
2021-06-14 22:00:06 +02:00
networkConfig = {
Bond = "bond0";
LLDP = true;
EmitLLDP = true;
};
};
bond0 = {
DHCP = "no";
matchConfig.Name = "bond0";
networkConfig = {
VLAN = map (net: "ext-${net}") ctNets;
LinkLocalAddressing = "no";
2021-06-14 22:00:06 +02:00
LLDP = true;
EmitLLDP = true;
};
};
} // (builtins.foldl' (result: net: result // {
"${net}" = {
matchConfig.Name = net;
networkConfig = {
2021-04-10 14:52:13 +02:00
# Disable all automatic addressing on bridges. It will delay
# networkd going into operational state.
DHCP = lib.mkDefault "no";
LinkLocalAddressing = lib.mkDefault "no";
2021-06-14 22:00:06 +02:00
LLDP = true;
EmitLLDP = true;
};
};
}) {} bridgeNets) // builtins.foldl' (result: net: result // {
"ext-${net}" = {
matchConfig.Name = "ext-${net}";
2021-04-10 14:52:13 +02:00
# Attach eth*/bond0/VLAN to bridge
networkConfig.Bridge = net;
};
}) {} ctNets;
};
}