Firewall
NixOS includes an integrated firewall based on iptables/nftables, which can be configured declaratively through the NixOS system configuration. By default, the firewall is enabled and restricts incoming network connections, allowing users to explicitly define which ports and services should be accessible.
Enable
The firewall is enabled by default on NixOS. To explicitly ensure it is enabled, add the following option to your system configuration:
networking.firewall.enable = true;
With the firewall enabled, all local ports and services will be unreachable from external connections unless explicitly allowed.
To use the newer nftables backend instead of iptables, set the option networking.nftables.enable
to true.
Configuration
To allow specific TCP/UDP ports or port ranges on all interfaces, use following syntax:
networking.firewall = {
enable = true;
allowedTCPPorts = [ 80 443 ];
allowedUDPPortRanges = [
{ from = 4000; to = 4007; }
{ from = 8000; to = 8010; }
];
};
Refer to networking.firewall
for more firewall module options.
services.jellyfin.openFirewall = true;
which will open the required TCP ports.Interface specific rules
It is possible to define firewall rules for specific network interfaces. This can be useful for allowing different ports or services on different network connections. Add the following to your system configuration:
networking.firewall.interfaces."eth0".allowedTCPPorts = [ 80 443 ];
In this case, ports 80
and 443
will be allowed for the interface eth0
.
Tips and tricks
Temporary firewall rules
If using iptables, for temporary changes to the firewall rules, you can install the nixos-firewall-tool
package, which is a thin wrapper around iptables
.