2025-03-10 18:44:16 +03:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
2025-06-07 17:07:22 +03:00
|
|
|
inherit (builtins) concatLists filter;
|
2025-03-10 18:44:16 +03:00
|
|
|
inherit (lib)
|
2025-06-07 17:07:22 +03:00
|
|
|
getExe
|
2025-03-10 18:44:16 +03:00
|
|
|
mkDefault
|
|
|
|
mkEnableOption
|
|
|
|
mkForce
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
;
|
|
|
|
inherit (lib.types)
|
|
|
|
bool
|
|
|
|
listOf
|
|
|
|
nullOr
|
|
|
|
str
|
2025-06-07 17:07:22 +03:00
|
|
|
submodule
|
2025-03-10 18:44:16 +03:00
|
|
|
;
|
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";
|
|
|
|
};
|
|
|
|
};
|
2025-03-10 18:44:16 +03:00
|
|
|
in
|
|
|
|
{
|
2025-06-07 17:07:22 +03:00
|
|
|
options.ataraxia.networkd = {
|
2025-03-10 18:44:16 +03:00
|
|
|
enable = mkEnableOption "Enable systemd-networkd bridged network";
|
2025-06-07 17:07:22 +03:00
|
|
|
disableIPv6 = mkEnableOption "Enable IPv6";
|
2025-03-10 18:44:16 +03:00
|
|
|
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
|
2025-03-10 18:44:16 +03:00
|
|
|
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-03-10 18:44:16 +03:00
|
|
|
};
|
2025-06-07 17:07:22 +03:00
|
|
|
ipv6 = mkOption {
|
|
|
|
type = listOf ipAddressType;
|
|
|
|
default =
|
|
|
|
if !cfg.disableIPv6 then
|
|
|
|
[
|
|
|
|
{
|
|
|
|
address = "fc00::1/64";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[ ];
|
2025-03-10 18:44:16 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2025-03-10 18:44:16 +03:00
|
|
|
nftables.enable = true;
|
|
|
|
useDHCP = false;
|
2025-06-07 17:07:22 +03:00
|
|
|
useNetworkd = true;
|
2025-03-10 18:44:16 +03:00
|
|
|
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));
|
2025-03-10 18:44:16 +03:00
|
|
|
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;
|
2025-03-10 18:44:16 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2025-03-10 18:44:16 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|