1{ config, lib, pkgs, ...}:
2
3with lib;
4
5let
6 cfg = config.services.znc;
7
8 defaultUser = "znc"; # Default user to own process.
9
10 # Default user and pass:
11 # un=znc
12 # pw=nixospass
13
14 defaultUserName = "znc";
15 defaultPassBlock = "
16 <Pass password>
17 Method = sha256
18 Hash = e2ce303c7ea75c571d80d8540a8699b46535be6a085be3414947d638e48d9e93
19 Salt = l5Xryew4g*!oa(ECfX2o
20 </Pass>
21 ";
22
23 modules = pkgs.buildEnv {
24 name = "znc-modules";
25 paths = cfg.modulePackages;
26 };
27
28 # Keep znc.conf in nix store, then symlink or copy into `dataDir`, depending on `mutable`.
29 notNull = a: ! isNull a;
30 mkZncConf = confOpts: ''
31 Version = 1.6.3
32 ${concatMapStrings (n: "LoadModule = ${n}\n") confOpts.modules}
33
34 <Listener l>
35 Port = ${toString confOpts.port}
36 IPv4 = true
37 IPv6 = true
38 SSL = ${boolToString confOpts.useSSL}
39 </Listener>
40
41 <User ${confOpts.userName}>
42 ${confOpts.passBlock}
43 Admin = true
44 Nick = ${confOpts.nick}
45 AltNick = ${confOpts.nick}_
46 Ident = ${confOpts.nick}
47 RealName = ${confOpts.nick}
48 ${concatMapStrings (n: "LoadModule = ${n}\n") confOpts.userModules}
49
50 ${ lib.concatStringsSep "\n" (lib.mapAttrsToList (name: net: ''
51 <Network ${name}>
52 ${concatMapStrings (m: "LoadModule = ${m}\n") net.modules}
53 Server = ${net.server} ${lib.optionalString net.useSSL "+"}${toString net.port} ${net.password}
54 ${concatMapStrings (c: "<Chan #${c}>\n</Chan>\n") net.channels}
55 ${lib.optionalString net.hasBitlbeeControlChannel ''
56 <Chan &bitlbee>
57 </Chan>
58 ''}
59 ${net.extraConf}
60 </Network>
61 '') confOpts.networks) }
62 </User>
63 ${confOpts.extraZncConf}
64 '';
65
66 zncConfFile = pkgs.writeTextFile {
67 name = "znc.conf";
68 text = if cfg.zncConf != ""
69 then cfg.zncConf
70 else mkZncConf cfg.confOptions;
71 };
72
73 networkOpts = { ... }: {
74 options = {
75 server = mkOption {
76 type = types.str;
77 example = "chat.freenode.net";
78 description = ''
79 IRC server address.
80 '';
81 };
82
83 port = mkOption {
84 type = types.int;
85 default = 6697;
86 example = 6697;
87 description = ''
88 IRC server port.
89 '';
90 };
91
92 userName = mkOption {
93 default = "";
94 example = "johntron";
95 type = types.string;
96 description = ''
97 A nick identity specific to the IRC server.
98 '';
99 };
100
101 password = mkOption {
102 type = types.str;
103 default = "";
104 description = ''
105 IRC server password, such as for a Slack gateway.
106 '';
107 };
108
109 useSSL = mkOption {
110 type = types.bool;
111 default = true;
112 description = ''
113 Whether to use SSL to connect to the IRC server.
114 '';
115 };
116
117 modulePackages = mkOption {
118 type = types.listOf types.package;
119 default = [];
120 example = [ "pkgs.zncModules.push" "pkgs.zncModules.fish" ];
121 description = ''
122 External ZNC modules to build.
123 '';
124 };
125
126 modules = mkOption {
127 type = types.listOf types.str;
128 default = [ "simple_away" ];
129 example = literalExample "[ simple_away sasl ]";
130 description = ''
131 ZNC modules to load.
132 '';
133 };
134
135 channels = mkOption {
136 type = types.listOf types.str;
137 default = [];
138 example = [ "nixos" ];
139 description = ''
140 IRC channels to join.
141 '';
142 };
143
144 hasBitlbeeControlChannel = mkOption {
145 type = types.bool;
146 default = false;
147 description = ''
148 Whether to add the special Bitlbee operations channel.
149 '';
150 };
151
152 extraConf = mkOption {
153 default = "";
154 type = types.lines;
155 example = ''
156 Encoding = ^UTF-8
157 FloodBurst = 4
158 FloodRate = 1.00
159 IRCConnectEnabled = true
160 Ident = johntron
161 JoinDelay = 0
162 Nick = johntron
163 '';
164 description = ''
165 Extra config for the network.
166 '';
167 };
168 };
169 };
170
171in
172
173{
174
175 ###### Interface
176
177 options = {
178 services.znc = {
179 enable = mkOption {
180 default = false;
181 type = types.bool;
182 description = ''
183 Enable a ZNC service for a user.
184 '';
185 };
186
187 user = mkOption {
188 default = "znc";
189 example = "john";
190 type = types.string;
191 description = ''
192 The name of an existing user account to use to own the ZNC server process.
193 If not specified, a default user will be created to own the process.
194 '';
195 };
196
197 group = mkOption {
198 default = "";
199 example = "users";
200 type = types.string;
201 description = ''
202 Group to own the ZNCserver process.
203 '';
204 };
205
206 dataDir = mkOption {
207 default = "/var/lib/znc/";
208 example = "/home/john/.znc/";
209 type = types.path;
210 description = ''
211 The data directory. Used for configuration files and modules.
212 '';
213 };
214
215 openFirewall = mkOption {
216 type = types.bool;
217 default = false;
218 description = ''
219 Whether to open ports in the firewall for ZNC.
220 '';
221 };
222
223 zncConf = mkOption {
224 default = "";
225 example = "See: http://wiki.znc.in/Configuration";
226 type = types.lines;
227 description = ''
228 Config file as generated with `znc --makeconf` to use for the whole ZNC configuration.
229 If specified, `confOptions` will be ignored, and this value, as-is, will be used.
230 If left empty, a conf file with default values will be used.
231 '';
232 };
233
234 confOptions = {
235 modules = mkOption {
236 type = types.listOf types.str;
237 default = [ "webadmin" "adminlog" ];
238 example = [ "partyline" "webadmin" "adminlog" "log" ];
239 description = ''
240 A list of modules to include in the `znc.conf` file.
241 '';
242 };
243
244 userModules = mkOption {
245 type = types.listOf types.str;
246 default = [ "chansaver" "controlpanel" ];
247 example = [ "chansaver" "controlpanel" "fish" "push" ];
248 description = ''
249 A list of user modules to include in the `znc.conf` file.
250 '';
251 };
252
253 userName = mkOption {
254 default = defaultUserName;
255 example = "johntron";
256 type = types.string;
257 description = ''
258 The user name used to log in to the ZNC web admin interface.
259 '';
260 };
261
262 networks = mkOption {
263 default = { };
264 type = with types; attrsOf (submodule networkOpts);
265 description = ''
266 IRC networks to connect the user to.
267 '';
268 example = {
269 "freenode" = {
270 server = "chat.freenode.net";
271 port = 6697;
272 useSSL = true;
273 modules = [ "simple_away" ];
274 };
275 };
276 };
277
278 nick = mkOption {
279 default = "znc-user";
280 example = "john";
281 type = types.string;
282 description = ''
283 The IRC nick.
284 '';
285 };
286
287 passBlock = mkOption {
288 example = defaultPassBlock;
289 type = types.string;
290 description = ''
291 Generate with `nix-shell -p znc --command "znc --makepass"`.
292 This is the password used to log in to the ZNC web admin interface.
293 '';
294 };
295
296 port = mkOption {
297 default = 5000;
298 example = 5000;
299 type = types.int;
300 description = ''
301 Specifies the port on which to listen.
302 '';
303 };
304
305 useSSL = mkOption {
306 default = true;
307 type = types.bool;
308 description = ''
309 Indicates whether the ZNC server should use SSL when listening on the specified port. A self-signed certificate will be generated.
310 '';
311 };
312
313 extraZncConf = mkOption {
314 default = "";
315 type = types.lines;
316 description = ''
317 Extra config to `znc.conf` file.
318 '';
319 };
320 };
321
322 modulePackages = mkOption {
323 type = types.listOf types.package;
324 default = [ ];
325 example = literalExample "[ pkgs.zncModules.fish pkgs.zncModules.push ]";
326 description = ''
327 A list of global znc module packages to add to znc.
328 '';
329 };
330
331 mutable = mkOption {
332 default = true;
333 type = types.bool;
334 description = ''
335 Indicates whether to allow the contents of the `dataDir` directory to be changed
336 by the user at run-time.
337 If true, modifications to the ZNC configuration after its initial creation are not
338 overwritten by a NixOS system rebuild.
339 If false, the ZNC configuration is rebuilt by every system rebuild.
340 If the user wants to manage the ZNC service using the web admin interface, this value
341 should be set to true.
342 '';
343 };
344
345 extraFlags = mkOption {
346 default = [ ];
347 example = [ "--debug" ];
348 type = types.listOf types.str;
349 description = ''
350 Extra flags to use when executing znc command.
351 '';
352 };
353 };
354 };
355
356
357 ###### Implementation
358
359 config = mkIf cfg.enable {
360
361 networking.firewall = mkIf cfg.openFirewall {
362 allowedTCPPorts = [ cfg.confOptions.port ];
363 };
364
365 systemd.services.znc = {
366 description = "ZNC Server";
367 wantedBy = [ "multi-user.target" ];
368 after = [ "network.service" ];
369 serviceConfig = {
370 User = cfg.user;
371 Group = cfg.group;
372 Restart = "always";
373 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
374 ExecStop = "${pkgs.coreutils}/bin/kill -INT $MAINPID";
375 };
376 preStart = ''
377 ${pkgs.coreutils}/bin/mkdir -p ${cfg.dataDir}/configs
378
379 # If mutable, regenerate conf file every time.
380 ${optionalString (!cfg.mutable) ''
381 ${pkgs.coreutils}/bin/echo "znc is set to be system-managed. Now deleting old znc.conf file to be regenerated."
382 ${pkgs.coreutils}/bin/rm -f ${cfg.dataDir}/configs/znc.conf
383 ''}
384
385 # Ensure essential files exist.
386 if [[ ! -f ${cfg.dataDir}/configs/znc.conf ]]; then
387 ${pkgs.coreutils}/bin/echo "No znc.conf file found in ${cfg.dataDir}. Creating one now."
388 ${pkgs.coreutils}/bin/cp --no-clobber ${zncConfFile} ${cfg.dataDir}/configs/znc.conf
389 ${pkgs.coreutils}/bin/chmod u+rw ${cfg.dataDir}/configs/znc.conf
390 ${pkgs.coreutils}/bin/chown ${cfg.user} ${cfg.dataDir}/configs/znc.conf
391 fi
392
393 if [[ ! -f ${cfg.dataDir}/znc.pem ]]; then
394 ${pkgs.coreutils}/bin/echo "No znc.pem file found in ${cfg.dataDir}. Creating one now."
395 ${pkgs.znc}/bin/znc --makepem --datadir ${cfg.dataDir}
396 fi
397
398 # Symlink modules
399 rm ${cfg.dataDir}/modules || true
400 ln -fs ${modules}/lib/znc ${cfg.dataDir}/modules
401 '';
402 script = "${pkgs.znc}/bin/znc --foreground --datadir ${cfg.dataDir} ${toString cfg.extraFlags}";
403 };
404
405 users.extraUsers = optional (cfg.user == defaultUser)
406 { name = defaultUser;
407 description = "ZNC server daemon owner";
408 group = defaultUser;
409 uid = config.ids.uids.znc;
410 home = cfg.dataDir;
411 createHome = true;
412 };
413
414 users.extraGroups = optional (cfg.user == defaultUser)
415 { name = defaultUser;
416 gid = config.ids.gids.znc;
417 members = [ defaultUser ];
418 };
419
420 };
421}