From 4313e043bc763877c85234fe4f6fbf0acf3165ec Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 17 Sep 2026 17:00:35 -0400 Subject: [PATCH] fixing nats --- modules/host.nix | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/modules/host.nix b/modules/host.nix index 6f8a1ab..9e70f74 100644 --- a/modules/host.nix +++ b/modules/host.nix @@ -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 + } + ''; }; # =====================================================================