11{ config , pkgs , ... } :
22
33{
4- # Open ports in the firewall.
5- # networking.firewall.allowedTCPPorts = [ ... ];
6- # networking.firewall.allowedUDPPorts = [ ... ];
7- # Or disable the firewall altogether.
8- # networking.firewall.enable = false;
9-
10- # https://nixos.wiki/wiki/Firewall
11- # https://scvalex.net/posts/54/
12- # sudo nft --stateless list table filter
13- # sudo sudo iptables-save
14- networking . firewall = {
15- enable = false ;
16- allowedTCPPorts = [
17- 22 # ssh
18- 5001 # iperf2
19- ] ;
20- # allowedTCPPorts = [ 22 5001 ];
21- # #allowedUDPPortRanges = [
22- # # { from = 4000; to = 4007; }
23- # # { from = 8000; to = 8010; }
24- # #];
25- # NixOS automagically creates stateful connection tracking, which we don't want
26- # for performance reasons
27- # extraCommands = ''
28- # iptables --delete nixos-fw -m conntrack --ctstate RELATED,ESTABLISHED -j nixos-fw-accept || true
29- # '';
4+ # Disable the default iptables firewall since we're using nftables
5+ networking . firewall . enable = false ;
6+
7+ # Enable nftables with connection tracking for maximum security
8+ networking . nftables = {
9+ enable = true ;
10+ ruleset = ''
11+ table inet filter {
12+ chain input {
13+ type filter hook input priority 0; policy drop;
14+
15+ # Enable connection tracking
16+ ct state established,related accept
17+ ct state invalid drop
18+
19+ # Allow loopback
20+ iif lo accept
21+ oif lo accept
22+
23+ # Allow SSH from anywhere
24+ tcp dport 22 accept
25+
26+ # Allow DNS queries
27+ udp dport 53 accept
28+ tcp dport 53 accept
29+
30+ # Allow DHCP
31+ udp dport 67 accept
32+ udp dport 547 accept
33+
34+ # Allow ICMP (ping, etc.)
35+ icmp type echo-request accept
36+ icmpv6 type echo-request accept
37+
38+ # Allow RA (Router Advertisement)
39+ icmpv6 type nd-router-advert accept
40+ }
41+
42+ chain forward {
43+ type filter hook forward priority 0; policy drop;
44+
45+ # Allow traffic from internal network to external
46+ # Use meta iifname to avoid interface existence check at load time
47+ meta iifname "br0" oifname "enp1s0" accept
48+
49+ # Allow return traffic from external to internal
50+ meta iifname "enp1s0" oifname "br0" ct state established,related accept
51+ }
52+
53+ chain output {
54+ type filter hook output priority 0; policy accept;
55+ }
56+ }
57+
58+ table ip nat {
59+ chain prerouting {
60+ type nat hook prerouting priority dstnat;
61+ }
62+
63+ chain postrouting {
64+ type nat hook postrouting priority srcnat;
65+ # IPv4 masquerading
66+ meta oifname "enp1s0" masquerade
67+ }
68+ }
69+
70+ table ip6 nat {
71+ chain prerouting {
72+ type nat hook prerouting priority dstnat;
73+ }
74+
75+ chain postrouting {
76+ type nat hook postrouting priority srcnat;
77+ # IPv6 masquerading
78+ meta oifname "enp1s0" masquerade
79+ }
80+ }
81+ '' ;
3082 } ;
31- # networking.firewall.interfaces."eth0".allowedTCPPorts = [ 80 443 ];
3283}
0 commit comments