nixos: fix ip46tables invocation in nat

Changed files
+18 -13
nixos
modules
services
+3 -12
nixos/modules/services/networking/firewall.nix
···
kernelHasRPFilter = ((kernel.config.isEnabled or (x: false)) "IP_NF_MATCH_RPFILTER") || (kernel.features.netfilterRPFilter or false);
-
helpers =
-
''
-
# Helper command to manipulate both the IPv4 and IPv6 tables.
-
ip46tables() {
-
iptables -w "$@"
-
${optionalString config.networking.enableIPv6 ''
-
ip6tables -w "$@"
-
''}
-
}
-
'';
writeShScript = name: text: let dir = pkgs.writeScriptBin name ''
#! ${pkgs.runtimeShell} -e
···
apply = canonicalizePortList;
example = [ 22 80 ];
description =
-
''
List of TCP ports on which incoming connections are
accepted.
'';
···
default = [ ];
example = [ { from = 8999; to = 9003; } ];
description =
-
''
A range of TCP ports on which incoming connections are
accepted.
'';
···
kernelHasRPFilter = ((kernel.config.isEnabled or (x: false)) "IP_NF_MATCH_RPFILTER") || (kernel.features.netfilterRPFilter or false);
+
helpers = import ./helpers.nix { inherit config lib; };
writeShScript = name: text: let dir = pkgs.writeScriptBin name ''
#! ${pkgs.runtimeShell} -e
···
apply = canonicalizePortList;
example = [ 22 80 ];
description =
+
''
List of TCP ports on which incoming connections are
accepted.
'';
···
default = [ ];
example = [ { from = 8999; to = 9003; } ];
description =
+
''
A range of TCP ports on which incoming connections are
accepted.
'';
+11
nixos/modules/services/networking/helpers.nix
···
···
+
{ config, lib, ... }: ''
+
# Helper command to manipulate both the IPv4 and IPv6 tables.
+
ip46tables() {
+
iptables -w "$@"
+
${
+
lib.optionalString config.networking.enableIPv6 ''
+
ip6tables -w "$@"
+
''
+
}
+
}
+
''
+4 -1
nixos/modules/services/networking/nat.nix
···
with lib;
let
-
cfg = config.networking.nat;
dest = if cfg.externalIP == null then "-j MASQUERADE" else "-j SNAT --to-source ${cfg.externalIP}";
flushNat = ''
ip46tables -w -t nat -D PREROUTING -j nixos-nat-pre 2>/dev/null|| true
ip46tables -w -t nat -F nixos-nat-pre 2>/dev/null || true
ip46tables -w -t nat -X nixos-nat-pre 2>/dev/null || true
···
'';
setupNat = ''
# Create subchain where we store rules
ip46tables -w -t nat -N nixos-nat-pre
ip46tables -w -t nat -N nixos-nat-post
···
with lib;
let
cfg = config.networking.nat;
dest = if cfg.externalIP == null then "-j MASQUERADE" else "-j SNAT --to-source ${cfg.externalIP}";
+
helpers = import ./helpers.nix { inherit config lib; };
+
flushNat = ''
+
${helpers}
ip46tables -w -t nat -D PREROUTING -j nixos-nat-pre 2>/dev/null|| true
ip46tables -w -t nat -F nixos-nat-pre 2>/dev/null || true
ip46tables -w -t nat -X nixos-nat-pre 2>/dev/null || true
···
'';
setupNat = ''
+
${helpers}
# Create subchain where we store rules
ip46tables -w -t nat -N nixos-nat-pre
ip46tables -w -t nat -N nixos-nat-post