Skip to content

NixOS nftables firewall

Describe your network with zones, then use declarative rules to control traffic between them. These NixOS modules turn that configuration into an nftables ruleset.

  • Zones identify networks or hosts by interface, address, or an nftables expression.
  • Rules describe traffic from one zone to another, including allowed ports.
  • Snippets provide reusable policies for common needs such as SSH and connection tracking.

Once you have imported the module, a configuration like this allows HTTP and HTTPS from uplink to the local machine, represented by the built-in fw zone:

configuration.nix
{
  networking.nftables.firewall = {
    enable = true;
    snippets.nnf-common.enable = true;

    zones.uplink.interfaces = [ "eth0" ];

    rules.web = {
      from = [ "uplink" ];
      to = [ "fw" ];
      allowedTCPPorts = [ 80 443 ];
    };
  };
}

Replace eth0 with your network interface. The nnf-common snippet also enables baseline policies including SSH, loopback traffic, and connection tracking. See Snippets for the full set of defaults.