-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move modules in another directory and split snippets into seperate files
- Loading branch information
Showing
15 changed files
with
314 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
flakes: { | ||
imports = [ | ||
flakes.self.nixosModules.zoned | ||
./snippets/nnf-common.nix | ||
./snippets/nnf-default-stopRuleset.nix | ||
./snippets/nnf-conntrack.nix | ||
./snippets/nnf-drop.nix | ||
./snippets/nnf-loopback.nix | ||
./snippets/nnf-dhcpv6.nix | ||
./snippets/nnf-icmp.nix | ||
./snippets/nnf-ssh.nix | ||
./snippets/nnf-nixos-firewall.nix | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-common; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-common = { | ||
enable = mkEnableOption (mdDoc "the nnf-common firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
assertions = [ | ||
{ | ||
assertion = cfg.enable -> config.networking.nftables.firewall.enable; | ||
message = "You enabled the `nnf-common` firewall snippet, but you did not enable the firewall itself."; | ||
} | ||
]; | ||
|
||
networking.nftables.firewall.snippets = mkDefault { | ||
nnf-conntrack.enable = true; | ||
nnf-default-stopRuleset.enable = true; | ||
nnf-drop.enable = true; | ||
nnf-loopback.enable = true; | ||
nnf-dhcpv6.enable = true; | ||
nnf-icmp.enable = true; | ||
nnf-ssh.enable = true; | ||
nnf-nixos-firewall.enable = true; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-conntrack; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-conntrack = { | ||
enable = mkEnableOption (mdDoc "the nnf-conntrack firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.chains = let | ||
conntrackRule = { | ||
after = mkForce ["veryEarly"]; | ||
before = ["early"]; | ||
rules = [ | ||
"ct state {established, related} accept" | ||
"ct state invalid drop" | ||
]; | ||
}; | ||
in { | ||
input.conntrack = conntrackRule; | ||
forward.conntrack = conntrackRule; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-default-stopRuleset; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-default-stopRuleset = { | ||
enable = mkEnableOption (mdDoc "the nnf-default-stopRuleset snippet"); | ||
allowedTCPPorts = mkOption { | ||
type = types.listOf types.port; | ||
default = config.services.openssh.ports; | ||
defaultText = literalExpression "config.services.openssh.ports"; | ||
description = mdDoc '' | ||
List of allowd TCP ports while the firewall is disabled. | ||
''; | ||
}; | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.stopRuleset = let | ||
ports = cfg.allowedTCPPorts; | ||
toPortList = ports: assert length ports > 0; "{ ${concatStringsSep ", " (map toString ports)} }"; | ||
in | ||
mkDefault '' | ||
# Check out https://wiki.nftables.org/ for better documentation. | ||
# Table for both IPv4 and IPv6. | ||
table inet filter { | ||
# Block all incomming connections traffic except SSH and "ping". | ||
chain input { | ||
type filter hook input priority 0; policy drop | ||
# accept any localhost traffic | ||
iifname lo accept | ||
# accept traffic originated from us | ||
ct state {established, related} accept | ||
# ICMP | ||
# routers may also want: mld-listener-query, nd-router-solicit | ||
ip6 nexthdr icmpv6 icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, parameter-problem, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert } accept | ||
ip protocol icmp icmp type { destination-unreachable, router-advertisement, time-exceeded, parameter-problem } accept | ||
# allow "ping" | ||
ip6 nexthdr icmpv6 icmpv6 type echo-request accept | ||
ip protocol icmp icmp type echo-request accept | ||
# accept SSH connections (required for a server) | ||
${optionalString (ports > 0) "tcp dport ${toPortList ports} accept"} | ||
# count and drop any other traffic | ||
counter drop | ||
} | ||
chain forward { | ||
type filter hook forward priority 0; policy drop | ||
counter drop | ||
} | ||
} | ||
''; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-dhcpv6; | ||
localZoneName = config.networking.nftables.firewall.localZoneName; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-dhcpv6 = { | ||
enable = mkEnableOption (mdDoc "the nnf-dhcpv6 firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.firewall.rules.dhcpv6 = { | ||
after = ["ct" "ssh"]; | ||
from = "all"; | ||
to = [localZoneName]; | ||
extraLines = [ | ||
"ip6 saddr fe80::/10 ip6 daddr fe80::/10 udp dport 546 accept" | ||
]; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-drop; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-drop = { | ||
enable = mkEnableOption (mdDoc "the nnf-drop firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.chains = let | ||
dropRule = { | ||
after = mkForce ["veryLate"]; | ||
before = mkForce ["end"]; | ||
rules = singleton "counter drop"; | ||
}; | ||
in { | ||
input.drop = dropRule; | ||
forward.drop = dropRule; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-icmp; | ||
localZoneName = config.networking.nftables.firewall.localZoneName; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-icmp = { | ||
enable = mkEnableOption (mdDoc "the nnf-icmp firewall snippet"); | ||
ipv6Types = mkOption { | ||
type = types.listOf types.str; | ||
default = ["echo-request" "nd-router-advert" "nd-neighbor-solicit" "nd-neighbor-advert"]; | ||
description = mdDoc '' | ||
List of allowed ICMPv6 types. | ||
''; | ||
}; | ||
ipv4Types = mkOption { | ||
type = types.listOf types.str; | ||
default = ["echo-request" "router-advertisement"]; | ||
description = mdDoc '' | ||
List of allowed ICMP types. | ||
''; | ||
}; | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.firewall.rules.icmp = { | ||
after = ["ct" "ssh"]; | ||
from = "all"; | ||
to = [localZoneName]; | ||
extraLines = [ | ||
"ip6 nexthdr icmpv6 icmpv6 type { ${concatStringsSep ", " cfg.ipv6Types} } accept" | ||
"ip protocol icmp icmp type { ${concatStringsSep ", " cfg.ipv4Types} } accept" | ||
]; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-loopback; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-loopback = { | ||
enable = mkEnableOption (mdDoc "the nnf-loopback firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.chains.input.loopback = { | ||
after = mkForce ["veryEarly"]; | ||
before = ["conntrack" "early"]; | ||
rules = singleton "iifname { lo } accept"; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-nixos-firewall; | ||
localZoneName = config.networking.nftables.firewall.localZoneName; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-nixos-firewall = { | ||
enable = mkEnableOption (mdDoc "the nnf-nixos-firewall firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.firewall.rules.nixos-firewall = { | ||
from = mkDefault "all"; | ||
to = [localZoneName]; | ||
allowedTCPPorts = config.networking.firewall.allowedTCPPorts; | ||
allowedUDPPorts = config.networking.firewall.allowedUDPPorts; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
lib, | ||
config, | ||
... | ||
}: let | ||
cfg = config.networking.nftables.firewall.snippets.nnf-ssh; | ||
localZoneName = config.networking.nftables.firewall.localZoneName; | ||
in | ||
with lib; { | ||
options.networking.nftables.firewall.snippets = { | ||
nnf-ssh = { | ||
enable = mkEnableOption (mdDoc "the nnf-ssh firewall snippet"); | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
networking.nftables.firewall.rules.ssh = { | ||
early = true; | ||
after = ["ct"]; | ||
from = "all"; | ||
to = [localZoneName]; | ||
allowedTCPPorts = config.services.openssh.ports; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.