Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos-firewall-tool: add nftables support #324615

Merged
merged 3 commits into from
Oct 18, 2024

Conversation

nbdd0121
Copy link
Contributor

@nbdd0121 nbdd0121 commented Jul 4, 2024

Description of changes

Add nftables support in nixos-firewall-tool and install it by default when NixOS firewall is enabled.

Also add some tests for nixos-firewall-tool in nixosTests.firewall.

This is the continuation of work of #275126 by @duament. I removed the dependency on #275371 (although I think it's still a good refactor to have, but it doesn't have to block this PR).

All the temporary port openings are added to the temp-ports nft set, so that resetting to default would only require flushing this set. This avoids disrupting tables and sets that are maintained by other services.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 8.has: changelog 8.has: module (update) This PR changes an existing module in `nixos/` 8.has: maintainer-list (update) This PR changes `maintainers/maintainer-list.nix` labels Jul 4, 2024
@nbdd0121
Copy link
Contributor Author

nbdd0121 commented Jul 5, 2024

I wonder if the nixos-firewall-tool should just be a single script that handles both iptables and nftables. I was thinking of checking /etc/systemd/system/nftables.service for existence to tell which one is in use.

What do you think? @clerie

@clerie
Copy link
Member

clerie commented Jul 7, 2024

Hi @nbdd0121, thank you for the effort putting some work into this. Without looking at the details (as in doing a full review), I think that is something we should merge to get some traction into this topic.

Because nobody found a robust solution for deciding between nf- and iptables in the mean time, I think yielding two different packages based on system config is acceptable for now. If someone finds a proper solution we can still change that later.

@nbdd0121 nbdd0121 force-pushed the nixos-firewall-tool branch from 0212c16 to 07e2ab5 Compare August 13, 2024 10:35
Comment on lines 6 to 20
echo "nixos-firewall-tool"
echo ""
echo "Can temporarily manipulate the NixOS firewall"
echo ""
echo "Open TCP port:"
echo " nixos-firewall-tool open tcp 8888"
echo ""
echo "Show all firewall rules:"
echo " nixos-firewall-tool show"
echo ""
echo "Open UDP port:"
echo " nixos-firewall-tool open udp 51820"
echo ""
echo "Reset firewall configuration to system settings:"
echo " nixos-firewall-tool reset"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be combined into one echo with mutliline strings. Also echo already prints an empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied verbatim from pkgs/by-name/ni/nixos-firewall-tool/nixos-firewall-tool.sh, and I've changed approach to detect for iptables/nftables support instead of having two separate files.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Sep 27, 2024
@nbdd0121 nbdd0121 force-pushed the nixos-firewall-tool branch 3 times, most recently from ee9d14d to 900f9f6 Compare September 27, 2024 21:06
runtimeInputs = [
iptables
];
runtimeInputs = [ ({ inherit iptables nftables; }.${backend}) ];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the firewall module have cfg.package added to the environment package. Maybe the runtimeInputs can be removed completely and rely on iptables/nft being in PATH?

@ofborg ofborg bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Sep 27, 2024
@ofborg ofborg bot requested a review from clerie September 27, 2024 22:58
@ofborg ofborg bot added the 11.by: package-maintainer This PR was created by the maintainer of the package it changes label Sep 27, 2024
@nbdd0121 nbdd0121 force-pushed the nixos-firewall-tool branch from 900f9f6 to edb2f9a Compare October 6, 2024 13:10
nixos/modules/services/networking/firewall.nix Outdated Show resolved Hide resolved
nixos/modules/services/networking/firewall.nix Outdated Show resolved Hide resolved
pkgs/by-name/ni/nixos-firewall-tool/package.nix Outdated Show resolved Hide resolved
pkgs/by-name/ni/nixos-firewall-tool/package.nix Outdated Show resolved Hide resolved
@nbdd0121 nbdd0121 force-pushed the nixos-firewall-tool branch from edb2f9a to c8b6d9a Compare October 7, 2024 18:45
systemctl restart firewall.service
;;
nftables)
nft flush set inet nixos-fw "temp-ports"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This implementation differs from the one with iptables. With iptables, all rules are being reset to the definitions done with nix. With nftables only temporarily opened ports are flushed.

I appreciate the nftables implementation more, as it respects dynamic rules done by other tools like Docker, but maybe this should be highlighted, or the subcommands for nftables should be renamed, and a notice added that reset only works with iptables and for nftables the unit should be restarted manually.
Alternatively, reset could be implemented for nftables with the same functionality, and the current implementation gets moved into another separate subcommand.

Although I am also fine with the implementation as it is right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's intentional, since otherwise it's very disruptive. It's especially so when you uses nft sets, everything added there will also be lost.

Arguably the same should be done for iptables by setting up a separate chain, but I don't have bandwidth for it now, I just want to get nftables functionality to work on par with iptables so more people can switch over.

@SuperSandro2000 SuperSandro2000 merged commit f0bc4f6 into NixOS:master Oct 18, 2024
28 of 30 checks passed
@nbdd0121 nbdd0121 deleted the nixos-firewall-tool branch October 19, 2024 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog 8.has: documentation This PR adds or changes documentation 8.has: maintainer-list (update) This PR changes `maintainers/maintainer-list.nix` 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 10.rebuild-linux: 1-10 11.by: package-maintainer This PR was created by the maintainer of the package it changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants