nix-config/hosts/nucnix/forwarding.nix

86 lines
2.1 KiB
Nix
Raw Normal View History

2024-12-22 00:10:37 +01:00
{ globals, lib, ... }:
let
inherit (lib)
concatStringsSep
net
toUpper
mkMerge
2024-12-22 20:21:56 +01:00
optionalString
2024-12-22 00:10:37 +01:00
;
forward =
{
service,
ports,
protocol,
2024-12-22 20:21:56 +01:00
fport ? null,
2024-12-22 00:10:37 +01:00
...
}:
{
networking.nftables = {
chains = {
prerouting.port-forward = {
after = [ "hook" ];
rules = [
2024-12-22 19:00:21 +01:00
"iifname { vlan-fritz, lan-home } ip daddr { ${net.cidr.host 1 globals.net.vlans.services.cidrv4}, ${net.cidr.host 2 "10.99.2.0/24"} } ${protocol} dport { ${concatStringsSep ", " (map toString ports)} } dnat ip to ${
2024-12-22 00:10:37 +01:00
net.cidr.host globals.services.${service}.ip globals.net.vlans.services.cidrv4
2024-12-22 20:21:56 +01:00
}${optionalString (fport != null) ":${toString fport}"}"
2024-12-22 19:00:21 +01:00
"iifname { vlan-fritz, lan-home } ip6 daddr ${net.cidr.host 1 globals.net.vlans.services.cidrv6} ${protocol} dport { ${concatStringsSep ", " (map toString ports)} } dnat ip6 to ${
2024-12-22 00:10:37 +01:00
net.cidr.host globals.services.${service}.ip globals.net.vlans.services.cidrv6
2024-12-22 20:21:56 +01:00
}${optionalString (fport != null) ":${toString fport}"}"
2024-12-22 00:10:37 +01:00
];
};
};
firewall = {
zones = {
${service}.ipv4Addresses = [
(lib.net.cidr.host globals.services.${service}.ip globals.net.vlans.services.cidrv4)
];
};
rules = {
"forward-${service}" = {
2024-12-22 19:00:21 +01:00
from = [
"fritz"
"home"
];
2024-12-22 00:10:37 +01:00
to = [ service ];
2024-12-27 17:50:44 +01:00
"allowed${toUpper protocol}Ports" = if fport != null then [ fport ] else ports;
2024-12-22 00:10:37 +01:00
};
};
};
};
};
in
mkMerge [
(forward {
service = "nginx";
ports = [
80
443
];
protocol = "tcp";
})
(forward {
service = "forgejo";
ports = [
9922
];
protocol = "tcp";
2024-12-22 20:21:56 +01:00
fport = 22;
2024-12-22 00:10:37 +01:00
})
(forward {
service = "murmur";
ports = [
9987
];
protocol = "udp";
})
(forward {
service = "netbird";
ports = [
3478
5349
];
protocol = "udp";
})
]