fixing nats

This commit is contained in:
ari committed 2026-09-17 17:00:35 -04:00
1 parent 708a02a6d5
commit 4313e043bc
1 file changed
+25 -9
+25 -9
View File
@@ -746,15 +746,31 @@ in
networkConfig.Bridge = net.bridge;
};
networking.nat = {
enable = true;
internalInterfaces = [ net.bridge ];
inherit (net) externalInterface;
forwardPorts = map (s: {
sourcePort = s.hostPort;
destination = "${net.guestAddress}:${toString s.guestPort}";
inherit (s) proto;
}) cfg.publish;
# ip_forward is required for the guest to reach the internet and for
# inbound DNAT to work. networking.nat used to set this implicitly, but
# its nftables translation of forwardPorts is unreliable when
# networking.nftables.enable = true, so we manage NAT directly.
boot.kernel.sysctl."net.ipv4.ip_forward" = true;
# Masquerade outbound guest traffic and DNAT published ports inbound.
# We use an explicit nftables table instead of networking.nat so that the
# rules are certain to appear in the prerouting/postrouting hooks.
networking.nftables.tables.hermenix-nat = lib.mkIf (net.externalInterface != null) {
family = "ip";
content = ''
chain prerouting {
type nat hook prerouting priority dstnat;
''
+ lib.concatMapStrings (s: ''
iifname "${net.externalInterface}" ${s.proto} dport ${toString s.hostPort} dnat to ${net.guestAddress}:${toString s.guestPort}
'') cfg.publish
+ ''
}
chain postrouting {
type nat hook postrouting priority srcnat;
iifname "${net.bridge}" oifname "${net.externalInterface}" masquerade
}
'';
};
# =====================================================================