148 lines
3.3 KiB
Nix
Raw Normal View History

{
config,
lib,
pkgs,
...
}:
let
2025-06-07 17:07:22 +03:00
inherit (builtins) concatLists filter;
inherit (lib)
2025-06-07 17:07:22 +03:00
getExe
mkDefault
mkEnableOption
mkForce
mkIf
mkOption
;
inherit (lib.types)
bool
listOf
nullOr
str
2025-06-07 17:07:22 +03:00
submodule
;
2025-06-07 17:07:22 +03:00
cfg = config.ataraxia.networkd;
ipAddressType = submodule {
options = {
address = mkOption {
type = str;
};
gateway = mkOption {
type = nullOr str;
default = null;
};
dns = mkOption {
type = listOf str;
default = [ ];
};
gatewayOnLink = mkEnableOption "Enable GatewayOnLink";
};
};
in
{
2025-06-07 17:07:22 +03:00
options.ataraxia.networkd = {
enable = mkEnableOption "Enable systemd-networkd bridged network";
2025-06-07 17:07:22 +03:00
disableIPv6 = mkEnableOption "Enable IPv6";
domain = mkOption {
type = nullOr str;
default = null;
};
ifname = mkOption {
type = str;
};
mac = mkOption {
type = str;
};
2025-06-07 17:07:22 +03:00
# TODO: implement disabling bridge
bridge = {
enable = mkOption {
type = bool;
default = true;
};
name = mkOption {
type = str;
default = "br0";
};
};
2025-06-07 17:07:22 +03:00
ipv4 = mkOption {
type = listOf ipAddressType;
default = [ ];
};
2025-06-07 17:07:22 +03:00
ipv6 = mkOption {
type = listOf ipAddressType;
default =
if !cfg.disableIPv6 then
[
{
address = "fc00::1/64";
}
]
else
[ ];
};
};
config = mkIf cfg.enable {
services.resolved.enable = true;
networking = {
dhcpcd.enable = false;
2025-06-07 17:07:22 +03:00
domain = cfg.domain;
enableIPv6 = !cfg.disableIPv6;
nftables.enable = true;
useDHCP = false;
2025-06-07 17:07:22 +03:00
useNetworkd = true;
usePredictableInterfaceNames = mkForce true;
firewall = {
enable = true;
allowedTCPPorts = mkDefault [ ];
allowedUDPPorts = mkDefault [ ];
};
};
systemd.network = {
enable = true;
wait-online.ignoredInterfaces = [ "lo" ];
netdevs = {
"20-${cfg.bridge.name}" = {
netdevConfig = {
Kind = "bridge";
Name = cfg.bridge.name;
MACAddress = cfg.mac;
};
};
};
networks = {
"30-${cfg.ifname}" = {
matchConfig.Name = cfg.ifname;
linkConfig.RequiredForOnline = "enslaved";
networkConfig.Bridge = cfg.bridge.name;
networkConfig.DHCP = "no";
};
"40-${cfg.bridge.name}" = {
matchConfig.Name = cfg.bridge.name;
2025-06-07 17:07:22 +03:00
address = map (ip: ip.address) (cfg.ipv4 ++ cfg.ipv6);
dns = concatLists (map (ip: ip.dns) (cfg.ipv4 ++ cfg.ipv6));
networkConfig.LinkLocalAddressing = "no";
linkConfig.RequiredForOnline = "routable";
routes =
2025-06-07 17:07:22 +03:00
let
filteredRoutes = filter (ip: ip.gateway != null) (cfg.ipv4 ++ cfg.ipv6);
routes = map (x: {
Gateway = x.gateway;
GatewayOnLink = x.gatewayOnLink;
}) filteredRoutes;
in
routes;
};
};
};
system.activationScripts.udp-gro-forwarding = mkIf cfg.bridge.enable {
text = ''
2025-06-07 17:07:22 +03:00
${getExe pkgs.ethtool} -K ${cfg.bridge.name} rx-udp-gro-forwarding on rx-gro-list off
'';
};
};
}