nftables Rule Writing (Quickstart, Pitfalls, Constraints)
Use this skill when generating, reviewing, or debugging nftables rulesets, especially when an LLM may produce rules that are syntactically valid but semantically invalid for a specific chain type, hook, or family.
When to Use
Trigger on requests like:
- "Write an
nftables.conf"
- "Review this
.nft file"
- "Why does
nft -f / nft -c fail?"
- "Which actions are valid on which hooks?"
- "How do I write IPv4 + IPv6 rules in
table inet?"
- "Create a lint checklist for generated nftables rules"
What This Skill Covers
- Quickstart ruleset patterns (safe defaults and common base chains)
- Reusable nftables patterns for common host/router scenarios
- Ops workflows (validate/apply/list/monitor/reload)
- Persistence discovery (including systemd units and drop-ins that may load custom
.nft files)
- Hook / chain-type / family constraints
- Statement-specific constraints (
dnat, snat, masquerade, redirect, reject, tproxy, queue)
inet family IPv4/IPv6 pitfalls
- Debugging workflow with
nft -c and --debug
- Source map to official docs and userspace code (
nftables project)
Workflow (Authoring + Review)
- Identify the ruleset shape first:
family (ip, ip6, inet, bridge, netdev)
- table purpose (
filter, nat, etc.)
- chain
type, hook, priority, policy
- whether
device is required (netdev, inet + ingress)
- If starting from scratch, use
references/quickstart.md and references/nftables-patterns.md to pick a known-good template.
- Check structural legality in
references/constraints-matrix.md:
- chain definition validity
- statement vs hook compatibility
- Check common LLM mistakes in
references/common-pitfalls.md.
- For fast triage, use
references/hook-action-cheatsheet.md.
- If
nft is available, run:
nft -c -f <file>
nft -c -d parser,eval,netlink -f <file> (when error cause is unclear)
- For deployment/runtime concerns, use
references/nftables-ops-cheatsheet.md and references/nftables-troubleshooting.md.
- If needed, use
references/source-map.md to trace the constraint back to official docs or src/evaluate.c.
Quickstart First (Important)
Do not start by writing isolated rules. Start from a valid chain declaration and then add rules.
For common starting points:
- Minimal host firewall:
references/quickstart.md
- Reusable practical patterns:
references/nftables-patterns.md
- NAT placement reminders:
references/hook-action-cheatsheet.md
inet dual-stack gotchas: references/common-pitfalls.md
Review / Lint Output Format (Recommended)
When reviewing LLM-generated rules, output findings in this order:
Issue (specific chain/rule)
Why it is invalid or risky (hook/family/statement constraint)
Fix (replacement snippet)
Validation (nft -c / version-sensitive caveat)
Core Principles
- Legality is layered:
- parser (
scanner.l / parser_bison.y)
- userspace semantic checks (
src/evaluate.c)
- kernel
nf_tables support (final authority)
- Many failures are not syntax errors. Common real causes:
- wrong chain type/hook
- invalid statement in that chain/hook
- missing
device on netdev or inet ingress base chains
inet NAT address missing explicit ip/ip6
- conflicting protocol contexts (
ip + ip6 in one rule)
- If the target environment is unknown, say so explicitly and require
nft -c -f on the target host.
Reference Navigation (Load Only What You Need)
references/quickstart.md
- Minimal and common ruleset templates to start from
references/hook-action-cheatsheet.md
- Fast hook/action legality reminders
references/nftables-patterns.md
- Reusable host/router patterns (sets, NAT, port forwarding, egress)
references/constraints-matrix.md
- Detailed constraints with doc/source rationale
references/common-pitfalls.md
- LLM-heavy mistakes and corrected examples
references/nftables-ops-cheatsheet.md
- Operational commands, persistence, systemd unit/drop-in scanning
references/debug-workflow.md
nft -c, debug flags, and triage workflow
references/nftables-troubleshooting.md
- Runtime troubleshooting and multi-source persistence debugging
references/reference-index.md
- Topic map for this skill (what to open for what task)
references/source-map.md
- Official docs/source file map and search keywords
examples/nftables/
- Ready-to-edit example rulesets (web server, router NAT, dual-stack host)
Authoring Guardrails for Agents
- Prefer explicit protocol context (
tcp, udp, icmp, icmpv6) over ambiguous shorthand when generating code.
- In
table inet, prefer separate IPv4 and IPv6 rules unless there is a strong reason to merge.
- Do not invent hook support. If unsure, check the matrix and verify with
nft -c.
- Treat wiki examples as useful examples, not the sole source of truth.
1---2name: nftables-rule-writing3description: Write, review, and lint nftables rulesets for coding-agent generated configs. Use for quickstarts, rule authoring, audits, and troubleshooting, especially chain type/hook constraints, statement validity (nat/reject/tproxy/redirect), inet IPv4/IPv6 pitfalls, and source-informed validation with nft -c.4---56# nftables Rule Writing (Quickstart, Pitfalls, Constraints)78Use this skill when generating, reviewing, or debugging `nftables` rulesets, especially when an LLM may produce rules that are syntactically valid but semantically invalid for a specific chain type, hook, or family.910## When to Use1112Trigger on requests like:1314- "Write an `nftables.conf`"15- "Review this `.nft` file"16- "Why does `nft -f` / `nft -c` fail?"17- "Which actions are valid on which hooks?"18- "How do I write IPv4 + IPv6 rules in `table inet`?"19- "Create a lint checklist for generated nftables rules"2021## What This Skill Covers2223- Quickstart ruleset patterns (safe defaults and common base chains)24- Reusable nftables patterns for common host/router scenarios25- Ops workflows (validate/apply/list/monitor/reload)26- Persistence discovery (including systemd units and drop-ins that may load custom `.nft` files)27- Hook / chain-type / family constraints28- Statement-specific constraints (`dnat`, `snat`, `masquerade`, `redirect`, `reject`, `tproxy`, `queue`)29- `inet` family IPv4/IPv6 pitfalls30- Debugging workflow with `nft -c` and `--debug`31- Source map to official docs and userspace code (`nftables` project)3233## Workflow (Authoring + Review)34351. Identify the ruleset shape first:36 - `family` (`ip`, `ip6`, `inet`, `bridge`, `netdev`)37 - table purpose (`filter`, `nat`, etc.)38 - chain `type`, `hook`, `priority`, `policy`39 - whether `device` is required (`netdev`, `inet` + `ingress`)402. If starting from scratch, use `references/quickstart.md` and `references/nftables-patterns.md` to pick a known-good template.413. Check structural legality in `references/constraints-matrix.md`:42 - chain definition validity43 - statement vs hook compatibility444. Check common LLM mistakes in `references/common-pitfalls.md`.455. For fast triage, use `references/hook-action-cheatsheet.md`.466. If `nft` is available, run:47 - `nft -c -f <file>`48 - `nft -c -d parser,eval,netlink -f <file>` (when error cause is unclear)497. For deployment/runtime concerns, use `references/nftables-ops-cheatsheet.md` and `references/nftables-troubleshooting.md`.508. If needed, use `references/source-map.md` to trace the constraint back to official docs or `src/evaluate.c`.5152## Quickstart First (Important)5354Do not start by writing isolated rules. Start from a valid chain declaration and then add rules.5556For common starting points:5758- Minimal host firewall: `references/quickstart.md`59- Reusable practical patterns: `references/nftables-patterns.md`60- NAT placement reminders: `references/hook-action-cheatsheet.md`61- `inet` dual-stack gotchas: `references/common-pitfalls.md`6263## Review / Lint Output Format (Recommended)6465When reviewing LLM-generated rules, output findings in this order:66671. `Issue` (specific chain/rule)682. `Why it is invalid or risky` (hook/family/statement constraint)693. `Fix` (replacement snippet)704. `Validation` (`nft -c` / version-sensitive caveat)7172## Core Principles7374- Legality is layered:75 - parser (`scanner.l` / `parser_bison.y`)76 - userspace semantic checks (`src/evaluate.c`)77 - kernel `nf_tables` support (final authority)78- Many failures are not syntax errors. Common real causes:79 - wrong chain type/hook80 - invalid statement in that chain/hook81 - missing `device` on `netdev` or `inet` ingress base chains82 - `inet` NAT address missing explicit `ip`/`ip6`83 - conflicting protocol contexts (`ip` + `ip6` in one rule)84- If the target environment is unknown, say so explicitly and require `nft -c -f` on the target host.8586## Reference Navigation (Load Only What You Need)8788- `references/quickstart.md`89 - Minimal and common ruleset templates to start from90- `references/hook-action-cheatsheet.md`91 - Fast hook/action legality reminders92- `references/nftables-patterns.md`93 - Reusable host/router patterns (sets, NAT, port forwarding, egress)94- `references/constraints-matrix.md`95 - Detailed constraints with doc/source rationale96- `references/common-pitfalls.md`97 - LLM-heavy mistakes and corrected examples98- `references/nftables-ops-cheatsheet.md`99 - Operational commands, persistence, systemd unit/drop-in scanning100- `references/debug-workflow.md`101 - `nft -c`, debug flags, and triage workflow102- `references/nftables-troubleshooting.md`103 - Runtime troubleshooting and multi-source persistence debugging104- `references/reference-index.md`105 - Topic map for this skill (what to open for what task)106- `references/source-map.md`107 - Official docs/source file map and search keywords108- `examples/nftables/`109 - Ready-to-edit example rulesets (web server, router NAT, dual-stack host)110111## Authoring Guardrails for Agents112113- Prefer explicit protocol context (`tcp`, `udp`, `icmp`, `icmpv6`) over ambiguous shorthand when generating code.114- In `table inet`, prefer separate IPv4 and IPv6 rules unless there is a strong reason to merge.115- Do not invent hook support. If unsure, check the matrix and verify with `nft -c`.116- Treat wiki examples as useful examples, not the sole source of truth.