@@ -6,15 +6,32 @@ let
66
77 cfg = config.services.fail2ban;
88
9− fail2banConf = pkgs.writeText "fail2ban.conf" cfg.daemonConfig;
9+ fail2banConf = pkgs.writeText "fail2ban.local" cfg.daemonConfig;
1010
11− jailConf = pkgs.writeText "jail.conf"
12− (concatStringsSep "\n" (attrValues (flip mapAttrs cfg.jails (name: def:
11+ jailConf = pkgs.writeText "jail.local" ''
12+ [INCLUDES]
13+
14+ before = paths-nixos.conf
15+
16+ ${concatStringsSep "\n" (attrValues (flip mapAttrs cfg.jails (name: def:
1317 optionalString (def != "")
1418 ''
1519 [${name}]
1620 ${def}
17− ''))));
21+ '')))}
22+ '';
23+
24+ pathsConf = pkgs.writeText "paths-nixos.conf" ''
25+ # NixOS
26+
27+ [INCLUDES]
28+
29+ before = paths-common.conf
30+
31+ after = paths-overrides.local
32+
33+ [DEFAULT]
34+ '';
1835
1936in
2037
@@ -31,21 +48,26 @@ in
3148 description = "Whether to enable the fail2ban service.";
3249 };
3350
51+ package = mkOption {
52+ default = pkgs.fail2ban;
53+ type = types.package;
54+ example = "pkgs.fail2ban_0_11";
55+ description = "The fail2ban package to use for running the fail2ban service.";
56+ };
57+
3458 daemonConfig = mkOption {
35− default =
36− ''
37− [Definition]
38− loglevel = INFO
39− logtarget = SYSLOG
40− socket = /run/fail2ban/fail2ban.sock
41− pidfile = /run/fail2ban/fail2ban.pid
42− '';
59+ default = ''
60+ [Definition]
61+ logtarget = SYSLOG
62+ socket = /run/fail2ban/fail2ban.sock
63+ pidfile = /run/fail2ban/fail2ban.pid
64+ dbfile = /var/lib/fail2ban/fail2ban.sqlite3
65+ '';
4366 type = types.lines;
44− description =
45− ''
46− The contents of Fail2ban's main configuration file. It's
47− generally not necessary to change it.
48− '';
67+ description = ''
68+ The contents of Fail2ban's main configuration file. It's
69+ generally not necessary to change it.
70+ '';
4971 };
5072
5173 jails = mkOption {
@@ -65,17 +87,16 @@ in
6587 }
6688 '';
6789 type = types.attrsOf types.lines;
68− description =
69− ''
70− The configuration of each Fail2ban “jail”. A jail
71− consists of an action (such as blocking a port using
72− <command>iptables</command>) that is triggered when a
73− filter applied to a log file triggers more than a certain
74− number of times in a certain time period. Actions are
75− defined in <filename>/etc/fail2ban/action.d</filename>,
76− while filters are defined in
77− <filename>/etc/fail2ban/filter.d</filename>.
78− '';
90+ description = ''
91+ The configuration of each Fail2ban “jail”. A jail
92+ consists of an action (such as blocking a port using
93+ <command>iptables</command>) that is triggered when a
94+ filter applied to a log file triggers more than a certain
95+ number of times in a certain time period. Actions are
96+ defined in <filename>/etc/fail2ban/action.d</filename>,
97+ while filters are defined in
98+ <filename>/etc/fail2ban/filter.d</filename>.
99+ '';
79100 };
80101
81102 };
@@ -87,66 +108,65 @@ in
87108
88109 config = mkIf cfg.enable {
89110
90− environment.systemPackages = [ pkgs.fail2ban ];
91−
92− environment.etc."fail2ban/fail2ban.conf".source = fail2banConf;
93− environment.etc."fail2ban/jail.conf".source = jailConf;
94− environment.etc."fail2ban/action.d".source = "${pkgs.fail2ban}/etc/fail2ban/action.d/*.conf";
95− environment.etc."fail2ban/filter.d".source = "${pkgs.fail2ban}/etc/fail2ban/filter.d/*.conf";
96−
97− systemd.services.fail2ban =
98− { description = "Fail2ban Intrusion Prevention System";
111+ environment.systemPackages = [ cfg.package ];
112+
113+ environment.etc = {
114+ "fail2ban/fail2ban.local".source = fail2banConf;
115+ "fail2ban/jail.local".source = jailConf;
116+ "fail2ban/fail2ban.conf".source = "${cfg.package}/etc/fail2ban/fail2ban.conf";
117+ "fail2ban/jail.conf".source = "${cfg.package}/etc/fail2ban/jail.conf";
118+ "fail2ban/paths-common.conf".source = "${cfg.package}/etc/fail2ban/paths-common.conf";
119+ "fail2ban/paths-nixos.conf".source = pathsConf;
120+ "fail2ban/action.d".source = "${cfg.package}/etc/fail2ban/action.d/*.conf";
121+ "fail2ban/filter.d".source = "${cfg.package}/etc/fail2ban/filter.d/*.conf";
122+ };
99123
100− wantedBy = [ "multi-user.target" ];
101− after = [ "network.target" ];
102− partOf = optional config.networking.firewall.enable "firewall.service";
124+ systemd.services.fail2ban = {
125+ description = "Fail2ban Intrusion Prevention System";
103126
104− restartTriggers = [ fail2banConf jailConf ];
105− path = [ pkgs.fail2ban pkgs.iptables pkgs.iproute ];
127+ wantedBy = [ "multi-user.target" ];
128+ after = [ "network.target" ];
129+ partOf = optional config.networking.firewall.enable "firewall.service";
106130
107− preStart =
108− ''
109− mkdir -p /var/lib/fail2ban
110− '';
131+ restartTriggers = [ fail2banConf jailConf pathsConf ];
132+ reloadIfChanged = true;
111133
112− unitConfig.Documentation = "man:fail2ban(1)";
134+ path = [ cfg.package pkgs.iptables pkgs.iproute ];
113135
114− serviceConfig =
115− { Type = "forking";
116− ExecStart = "${pkgs.fail2ban}/bin/fail2ban-client -x start";
117− ExecStop = "${pkgs.fail2ban}/bin/fail2ban-client stop";
118− ExecReload = "${pkgs.fail2ban}/bin/fail2ban-client reload";
119− PIDFile = "/run/fail2ban/fail2ban.pid";
120− Restart = "always";
136+ preStart = ''
137+ mkdir -p /var/lib/fail2ban
138+ '';
121139
122− ReadOnlyDirectories = "/";
123− ReadWriteDirectories = "/run/fail2ban /var/tmp /var/lib";
124− PrivateTmp = "true";
125− RuntimeDirectory = "fail2ban";
126− CapabilityBoundingSet = "CAP_DAC_READ_SEARCH CAP_NET_ADMIN CAP_NET_RAW";
127− };
140+ unitConfig.Documentation = "man:fail2ban(1)";
141+
142+ serviceConfig = {
143+ Type = "forking";
144+ ExecStart = "${cfg.package}/bin/fail2ban-server -xf start";
145+ ExecStop = "${cfg.package}/bin/fail2ban-server stop";
146+ ExecReload = "${cfg.package}/bin/fail2ban-server reload";
147+ PIDFile = "/run/fail2ban/fail2ban.pid";
148+ Restart = "always";
149+
150+ ReadOnlyDirectories = "/";
151+ ReadWriteDirectories = "/run/fail2ban /var/tmp /var/lib";
152+ PrivateTmp = "true";
153+ RuntimeDirectory = "fail2ban";
154+ CapabilityBoundingSet = "CAP_DAC_READ_SEARCH CAP_NET_ADMIN CAP_NET_RAW";
128155 };
156+ };
129157
130158 # Add some reasonable default jails. The special "DEFAULT" jail
131159 # sets default values for all other jails.
132− services.fail2ban.jails.DEFAULT =
133− ''
134− ignoreip = 127.0.0.1/8
135− bantime = 600
136− findtime = 600
137− maxretry = 3
138− backend = systemd
139− enabled = true
140− '';
141−
160+ services.fail2ban.jails.DEFAULT = ''
161+ # Miscellaneous options
162+ ignoreip = 127.0.0.1/8 ${optionalString config.networking.enableIPv6 "::1"}
163+ maxretry = 3
164+ backend = systemd
165+ '';
142166 # Block SSH if there are too many failing connection attempts.
143− services.fail2ban.jails.ssh-iptables =
144− ''
145− filter = sshd
146− action = iptables-multiport[name=SSH, port="${concatMapStringsSep "," (p: toString p) config.services.openssh.ports}", protocol=tcp]
147− maxretry = 5
148− '';
149−
167+ services.fail2ban.jails.sshd = mkDefault ''
168+ enabled = true
169+ port = ${concatMapStringsSep "," (p: toString p) config.services.openssh.ports}
170+ '';
150171 };
151−
152172}