@@ -0,0 +1,596 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.sympa;
7+ dataDir = "/var/lib/sympa";
8+ user = "sympa";
9+ group = "sympa";
10+ pkg = pkgs.sympa;
11+ fqdns = attrNames cfg.domains;
12+ usingNginx = cfg.web.enable && cfg.web.server == "nginx";
13+ mysqlLocal = cfg.database.createLocally && cfg.database.type == "MySQL";
14+ pgsqlLocal = cfg.database.createLocally && cfg.database.type == "PostgreSQL";
15+
16+ sympaSubServices = [
17+ "sympa-archive.service"
18+ "sympa-bounce.service"
19+ "sympa-bulk.service"
20+ "sympa-task.service"
21+ ];
22+
23+ # common for all services including wwsympa
24+ commonServiceConfig = {
25+ StateDirectory = "sympa";
26+ ProtectHome = true;
27+ ProtectSystem = "full";
28+ ProtectKernelTunables = true;
29+ ProtectKernelModules = true;
30+ ProtectControlGroups = true;
31+ };
32+
33+ # wwsympa has its own service config
34+ sympaServiceConfig = srv: {
35+ Type = "simple";
36+ Restart = "always";
37+ ExecStart = "${pkg}/bin/${srv}.pl --foreground";
38+ PIDFile = "/run/sympa/${srv}.pid";
39+ User = user;
40+ Group = group;
41+
42+ # avoid duplicating log messageges in journal
43+ StandardError = "null";
44+ } // commonServiceConfig;
45+
46+ configVal = value:
47+ if isBool value then
48+ if value then "on" else "off"
49+ else toString value;
50+ configGenerator = c: concatStrings (flip mapAttrsToList c (key: val: "${key}\t${configVal val}\n"));
51+
52+ mainConfig = pkgs.writeText "sympa.conf" (configGenerator cfg.settings);
53+ robotConfig = fqdn: domain: pkgs.writeText "${fqdn}-robot.conf" (configGenerator domain.settings);
54+
55+ transport = pkgs.writeText "transport.sympa" (concatStringsSep "\n" (flip map fqdns (domain: ''
56+ ${domain} error:User unknown in recipient table
57+ sympa@${domain} sympa:sympa@${domain}
58+ listmaster@${domain} sympa:listmaster@${domain}
59+ bounce@${domain} sympabounce:sympa@${domain}
60+ abuse-feedback-report@${domain} sympabounce:sympa@${domain}
61+ '')));
62+
63+ virtual = pkgs.writeText "virtual.sympa" (concatStringsSep "\n" (flip map fqdns (domain: ''
64+ sympa-request@${domain} postmaster@localhost
65+ sympa-owner@${domain} postmaster@localhost
66+ '')));
67+
68+ listAliases = pkgs.writeText "list_aliases.tt2" ''
69+ #--- [% list.name %]@[% list.domain %]: list transport map created at [% date %]
70+ [% list.name %]@[% list.domain %] sympa:[% list.name %]@[% list.domain %]
71+ [% list.name %]-request@[% list.domain %] sympa:[% list.name %]-request@[% list.domain %]
72+ [% list.name %]-editor@[% list.domain %] sympa:[% list.name %]-editor@[% list.domain %]
73+ #[% list.name %]-subscribe@[% list.domain %] sympa:[% list.name %]-subscribe@[%list.domain %]
74+ [% list.name %]-unsubscribe@[% list.domain %] sympa:[% list.name %]-unsubscribe@[% list.domain %]
75+ [% list.name %][% return_path_suffix %]@[% list.domain %] sympabounce:[% list.name %]@[% list.domain %]
76+ '';
77+
78+ enabledFiles = filterAttrs (n: v: v.enable) cfg.settingsFile;
79+in
80+{
81+
82+ ###### interface
83+ options.services.sympa = with types; {
84+
85+ enable = mkEnableOption "Sympa mailing list manager";
86+
87+ lang = mkOption {
88+ type = str;
89+ default = "en_US";
90+ example = "cs";
91+ description = ''
92+ Default Sympa language.
93+ See <link xlink:href='https://github.com/sympa-community/sympa/tree/sympa-6.2/po/sympa' />
94+ for available options.
95+ '';
96+ };
97+
98+ listMasters = mkOption {
99+ type = listOf str;
100+ example = [ "postmaster@sympa.example.org" ];
101+ description = ''
102+ The list of the email addresses of the listmasters
103+ (users authorized to perform global server commands).
104+ '';
105+ };
106+
107+ mainDomain = mkOption {
108+ type = nullOr str;
109+ default = null;
110+ example = "lists.example.org";
111+ description = ''
112+ Main domain to be used in <filename>sympa.conf</filename>.
113+ If <literal>null</literal>, one of the <option>services.sympa.domains</option> is chosen for you.
114+ '';
115+ };
116+
117+ domains = mkOption {
118+ type = attrsOf (submodule ({ name, config, ... }: {
119+ options = {
120+ webHost = mkOption {
121+ type = nullOr str;
122+ default = null;
123+ example = "archive.example.org";
124+ description = ''
125+ Domain part of the web interface URL (no web interface for this domain if <literal>null</literal>).
126+ DNS record of type A (or AAAA or CNAME) has to exist with this value.
127+ '';
128+ };
129+ webLocation = mkOption {
130+ type = str;
131+ default = "/";
132+ example = "/sympa";
133+ description = "URL path part of the web interface.";
134+ };
135+ settings = mkOption {
136+ type = attrsOf (oneOf [ str int bool ]);
137+ default = {};
138+ example = {
139+ default_max_list_members = 3;
140+ };
141+ description = ''
142+ The <filename>robot.conf</filename> configuration file as key value set.
143+ See <link xlink:href='https://sympa-community.github.io/gpldoc/man/sympa.conf.5.html' />
144+ for list of configuration parameters.
145+ '';
146+ };
147+ };
148+
149+ config.settings = mkIf (cfg.web.enable && config.webHost != null) {
150+ wwsympa_url = mkDefault "https://${config.webHost}${strings.removeSuffix "/" config.webLocation}";
151+ };
152+ }));
153+
154+ description = ''
155+ Email domains handled by this instance. There have
156+ to be MX records for keys of this attribute set.
157+ '';
158+ example = literalExample ''
159+ {
160+ "lists.example.org" = {
161+ webHost = "lists.example.org";
162+ webLocation = "/";
163+ };
164+ "sympa.example.com" = {
165+ webHost = "example.com";
166+ webLocation = "/sympa";
167+ };
168+ }
169+ '';
170+ };
171+
172+ database = {
173+ type = mkOption {
174+ type = enum [ "SQLite" "PostgreSQL" "MySQL" ];
175+ default = "SQLite";
176+ example = "MySQL";
177+ description = "Database engine to use.";
178+ };
179+
180+ host = mkOption {
181+ type = nullOr str;
182+ default = null;
183+ description = ''
184+ Database host address.
185+
186+ For MySQL, use <literal>localhost</literal> to connect using Unix domain socket.
187+
188+ For PostgreSQL, use path to directory (e.g. <filename>/run/postgresql</filename>)
189+ to connect using Unix domain socket located in this directory.
190+
191+ Use <literal>null</literal> to fall back on Sympa default, or when using
192+ <option>services.sympa.database.createLocally</option>.
193+ '';
194+ };
195+
196+ port = mkOption {
197+ type = nullOr port;
198+ default = null;
199+ description = "Database port. Use <literal>null</literal> for default port.";
200+ };
201+
202+ name = mkOption {
203+ type = str;
204+ default = if cfg.database.type == "SQLite" then "${dataDir}/sympa.sqlite" else "sympa";
205+ defaultText = ''if database.type == "SQLite" then "${dataDir}/sympa.sqlite" else "sympa"'';
206+ description = ''
207+ Database name. When using SQLite this must be an absolute
208+ path to the database file.
209+ '';
210+ };
211+
212+ user = mkOption {
213+ type = nullOr str;
214+ default = user;
215+ description = "Database user. The system user name is used as a default.";
216+ };
217+
218+ passwordFile = mkOption {
219+ type = nullOr path;
220+ default = null;
221+ example = "/run/keys/sympa-dbpassword";
222+ description = ''
223+ A file containing the password for <option>services.sympa.database.user</option>.
224+ '';
225+ };
226+
227+ createLocally = mkOption {
228+ type = bool;
229+ default = true;
230+ description = "Whether to create a local database automatically.";
231+ };
232+ };
233+
234+ web = {
235+ enable = mkOption {
236+ type = bool;
237+ default = true;
238+ description = "Whether to enable Sympa web interface.";
239+ };
240+
241+ server = mkOption {
242+ type = enum [ "nginx" "none" ];
243+ default = "nginx";
244+ description = ''
245+ The webserver used for the Sympa web interface. Set it to `none` if you want to configure it yourself.
246+ Further nginx configuration can be done by adapting
247+ <option>services.nginx.virtualHosts.<replaceable>name</replaceable></option>.
248+ '';
249+ };
250+
251+ https = mkOption {
252+ type = bool;
253+ default = true;
254+ description = ''
255+ Whether to use HTTPS. When nginx integration is enabled, this option forces SSL and enables ACME.
256+ Please note that Sympa web interface always uses https links even when this option is disabled.
257+ '';
258+ };
259+
260+ fcgiProcs = mkOption {
261+ type = ints.positive;
262+ default = 2;
263+ description = "Number of FastCGI processes to fork.";
264+ };
265+ };
266+
267+ mta = {
268+ type = mkOption {
269+ type = enum [ "postfix" "none" ];
270+ default = "postfix";
271+ description = ''
272+ Mail transfer agent (MTA) integration. Use <literal>none</literal> if you want to configure it yourself.
273+
274+ The <literal>postfix</literal> integration sets up local Postfix instance that will pass incoming
275+ messages from configured domains to Sympa. You still need to configure at least outgoing message
276+ handling using e.g. <option>services.postfix.relayHost</option>.
277+ '';
278+ };
279+ };
280+
281+ settings = mkOption {
282+ type = attrsOf (oneOf [ str int bool ]);
283+ default = {};
284+ example = literalExample ''
285+ {
286+ default_home = "lists";
287+ viewlogs_page_size = 50;
288+ }
289+ '';
290+ description = ''
291+ The <filename>sympa.conf</filename> configuration file as key value set.
292+ See <link xlink:href='https://sympa-community.github.io/gpldoc/man/sympa.conf.5.html' />
293+ for list of configuration parameters.
294+ '';
295+ };
296+
297+ settingsFile = mkOption {
298+ type = attrsOf (submodule ({ name, config, ... }: {
299+ options = {
300+ enable = mkOption {
301+ type = bool;
302+ default = true;
303+ description = "Whether this file should be generated. This option allows specific files to be disabled.";
304+ };
305+ text = mkOption {
306+ default = null;
307+ type = nullOr lines;
308+ description = "Text of the file.";
309+ };
310+ source = mkOption {
311+ type = path;
312+ description = "Path of the source file.";
313+ };
314+ };
315+
316+ config.source = mkIf (config.text != null) (mkDefault (pkgs.writeText "sympa-${baseNameOf name}" config.text));
317+ }));
318+ default = {};
319+ example = literalExample ''
320+ {
321+ "list_data/lists.example.org/help" = {
322+ text = "subject This list provides help to users";
323+ };
324+ }
325+ '';
326+ description = "Set of files to be linked in <filename>${dataDir}</filename>.";
327+ };
328+ };
329+
330+ ###### implementation
331+
332+ config = mkIf cfg.enable {
333+
334+ services.sympa.settings = (mapAttrs (_: v: mkDefault v) {
335+ domain = if cfg.mainDomain != null then cfg.mainDomain else head fqdns;
336+ listmaster = concatStringsSep "," cfg.listMasters;
337+ lang = cfg.lang;
338+
339+ home = "${dataDir}/list_data";
340+ arc_path = "${dataDir}/arc";
341+ bounce_path = "${dataDir}/bounce";
342+
343+ sendmail = "${pkgs.system-sendmail}/bin/sendmail";
344+
345+ db_type = cfg.database.type;
346+ db_name = cfg.database.name;
347+ }
348+ // (optionalAttrs (cfg.database.host != null) {
349+ db_host = cfg.database.host;
350+ })
351+ // (optionalAttrs mysqlLocal {
352+ db_host = "localhost"; # use unix domain socket
353+ })
354+ // (optionalAttrs pgsqlLocal {
355+ db_host = "/run/postgresql"; # use unix domain socket
356+ })
357+ // (optionalAttrs (cfg.database.port != null) {
358+ db_port = cfg.database.port;
359+ })
360+ // (optionalAttrs (cfg.database.user != null) {
361+ db_user = cfg.database.user;
362+ })
363+ // (optionalAttrs (cfg.mta.type == "postfix") {
364+ sendmail_aliases = "${dataDir}/sympa_transport";
365+ aliases_program = "${pkgs.postfix}/bin/postmap";
366+ aliases_db_type = "hash";
367+ })
368+ // (optionalAttrs cfg.web.enable {
369+ static_content_path = "${dataDir}/static_content";
370+ css_path = "${dataDir}/static_content/css";
371+ pictures_path = "${dataDir}/static_content/pictures";
372+ mhonarc = "${pkgs.perlPackages.MHonArc}/bin/mhonarc";
373+ }));
374+
375+ services.sympa.settingsFile = {
376+ "virtual.sympa" = mkDefault { source = virtual; };
377+ "transport.sympa" = mkDefault { source = transport; };
378+ "etc/list_aliases.tt2" = mkDefault { source = listAliases; };
379+ }
380+ // (flip mapAttrs' cfg.domains (fqdn: domain:
381+ nameValuePair "etc/${fqdn}/robot.conf" (mkDefault { source = robotConfig fqdn domain; })));
382+
383+ environment = {
384+ systemPackages = [ pkg ];
385+ };
386+
387+ users.users.${user} = {
388+ description = "Sympa mailing list manager user";
389+ group = group;
390+ home = dataDir;
391+ createHome = false;
392+ isSystemUser = true;
393+ };
394+
395+ users.groups.${group} = {};
396+
397+ assertions = [
398+ { assertion = cfg.database.createLocally -> cfg.database.user == user;
399+ message = "services.sympa.database.user must be set to ${user} if services.sympa.database.createLocally is set to true";
400+ }
401+ { assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
402+ message = "a password cannot be specified if services.sympa.database.createLocally is set to true";
403+ }
404+ ];
405+
406+ systemd.tmpfiles.rules = [
407+ "d ${dataDir} 0711 ${user} ${group} - -"
408+ "d ${dataDir}/etc 0700 ${user} ${group} - -"
409+ "d ${dataDir}/spool 0700 ${user} ${group} - -"
410+ "d ${dataDir}/list_data 0700 ${user} ${group} - -"
411+ "d ${dataDir}/arc 0700 ${user} ${group} - -"
412+ "d ${dataDir}/bounce 0700 ${user} ${group} - -"
413+ "f ${dataDir}/sympa_transport 0600 ${user} ${group} - -"
414+
415+ # force-copy static_content so it's up to date with package
416+ # set permissions for wwsympa which needs write access (...)
417+ "R ${dataDir}/static_content - - - - -"
418+ "C ${dataDir}/static_content 0711 ${user} ${group} - ${pkg}/static_content"
419+ "e ${dataDir}/static_content/* 0711 ${user} ${group} - -"
420+
421+ "d /run/sympa 0755 ${user} ${group} - -"
422+ ]
423+ ++ (flip concatMap fqdns (fqdn: [
424+ "d ${dataDir}/etc/${fqdn} 0700 ${user} ${group} - -"
425+ "d ${dataDir}/list_data/${fqdn} 0700 ${user} ${group} - -"
426+ ]))
427+ #++ (flip mapAttrsToList enabledFiles (k: v:
428+ # "L+ ${dataDir}/${k} - - - - ${v.source}"
429+ #))
430+ ++ (concatLists (flip mapAttrsToList enabledFiles (k: v: [
431+ # sympa doesn't handle symlinks well (e.g. fails to create locks)
432+ # force-copy instead
433+ "R ${dataDir}/${k} - - - - -"
434+ "C ${dataDir}/${k} 0700 ${user} ${group} - ${v.source}"
435+ ])));
436+
437+ systemd.services.sympa = {
438+ description = "Sympa mailing list manager";
439+
440+ wantedBy = [ "multi-user.target" ];
441+ after = [ "network-online.target" ];
442+ wants = sympaSubServices;
443+ before = sympaSubServices;
444+ serviceConfig = sympaServiceConfig "sympa_msg";
445+
446+ preStart = ''
447+ umask 0077
448+
449+ cp -f ${mainConfig} ${dataDir}/etc/sympa.conf
450+ ${optionalString (cfg.database.passwordFile != null) ''
451+ chmod u+w ${dataDir}/etc/sympa.conf
452+ echo -n "db_passwd " >> ${dataDir}/etc/sympa.conf
453+ cat ${cfg.database.passwordFile} >> ${dataDir}/etc/sympa.conf
454+ ''}
455+
456+ ${optionalString (cfg.mta.type == "postfix") ''
457+ ${pkgs.postfix}/bin/postmap hash:${dataDir}/virtual.sympa
458+ ${pkgs.postfix}/bin/postmap hash:${dataDir}/transport.sympa
459+ ''}
460+ ${pkg}/bin/sympa_newaliases.pl
461+ ${pkg}/bin/sympa.pl --health_check
462+ '';
463+ };
464+ systemd.services.sympa-archive = {
465+ description = "Sympa mailing list manager (archiving)";
466+ bindsTo = [ "sympa.service" ];
467+ serviceConfig = sympaServiceConfig "archived";
468+ };
469+ systemd.services.sympa-bounce = {
470+ description = "Sympa mailing list manager (bounce processing)";
471+ bindsTo = [ "sympa.service" ];
472+ serviceConfig = sympaServiceConfig "bounced";
473+ };
474+ systemd.services.sympa-bulk = {
475+ description = "Sympa mailing list manager (message distribution)";
476+ bindsTo = [ "sympa.service" ];
477+ serviceConfig = sympaServiceConfig "bulk";
478+ };
479+ systemd.services.sympa-task = {
480+ description = "Sympa mailing list manager (task management)";
481+ bindsTo = [ "sympa.service" ];
482+ serviceConfig = sympaServiceConfig "task_manager";
483+ };
484+
485+ systemd.services.wwsympa = mkIf usingNginx {
486+ wantedBy = [ "multi-user.target" ];
487+ after = [ "sympa.service" ];
488+ serviceConfig = {
489+ Type = "forking";
490+ PIDFile = "/run/sympa/wwsympa.pid";
491+ Restart = "always";
492+ ExecStart = ''${pkgs.spawn_fcgi}/bin/spawn-fcgi \
493+ -u ${user} \
494+ -g ${group} \
495+ -U nginx \
496+ -M 0600 \
497+ -F ${toString cfg.web.fcgiProcs} \
498+ -P /run/sympa/wwsympa.pid \
499+ -s /run/sympa/wwsympa.socket \
500+ -- ${pkg}/bin/wwsympa.fcgi
501+ '';
502+
503+ } // commonServiceConfig;
504+ };
505+
506+ services.nginx.enable = mkIf usingNginx true;
507+ services.nginx.virtualHosts = mkIf usingNginx (let
508+ vHosts = unique (remove null (mapAttrsToList (_k: v: v.webHost) cfg.domains));
509+ hostLocations = host: map (v: v.webLocation) (filter (v: v.webHost == host) (attrValues cfg.domains));
510+ httpsOpts = optionalAttrs cfg.web.https { forceSSL = mkDefault true; enableACME = mkDefault true; };
511+ in
512+ genAttrs vHosts (host: {
513+ locations = genAttrs (hostLocations host) (loc: {
514+ extraConfig = ''
515+ include ${config.services.nginx.package}/conf/fastcgi_params;
516+
517+ fastcgi_pass unix:/run/sympa/wwsympa.socket;
518+ fastcgi_split_path_info ^(${loc})(.*)$;
519+
520+ fastcgi_param PATH_INFO $fastcgi_path_info;
521+ fastcgi_param SCRIPT_FILENAME ${pkg}/bin/wwsympa.fcgi;
522+ '';
523+ }) // {
524+ "/static-sympa/".alias = "${dataDir}/static_content/";
525+ };
526+ } // httpsOpts));
527+
528+ services.postfix = mkIf (cfg.mta.type == "postfix") {
529+ enable = true;
530+ recipientDelimiter = "+";
531+ config = {
532+ virtual_alias_maps = [ "hash:${dataDir}/virtual.sympa" ];
533+ virtual_mailbox_maps = [
534+ "hash:${dataDir}/transport.sympa"
535+ "hash:${dataDir}/sympa_transport"
536+ "hash:${dataDir}/virtual.sympa"
537+ ];
538+ virtual_mailbox_domains = [ "hash:${dataDir}/transport.sympa" ];
539+ transport_maps = [
540+ "hash:${dataDir}/transport.sympa"
541+ "hash:${dataDir}/sympa_transport"
542+ ];
543+ };
544+ masterConfig = {
545+ "sympa" = {
546+ type = "unix";
547+ privileged = true;
548+ chroot = false;
549+ command = "pipe";
550+ args = [
551+ "flags=hqRu"
552+ "user=${user}"
553+ "argv=${pkg}/bin/queue"
554+ "\${nexthop}"
555+ ];
556+ };
557+ "sympabounce" = {
558+ type = "unix";
559+ privileged = true;
560+ chroot = false;
561+ command = "pipe";
562+ args = [
563+ "flags=hqRu"
564+ "user=${user}"
565+ "argv=${pkg}/bin/bouncequeue"
566+ "\${nexthop}"
567+ ];
568+ };
569+ };
570+ };
571+
572+ services.mysql = optionalAttrs mysqlLocal {
573+ enable = true;
574+ package = mkDefault pkgs.mariadb;
575+ ensureDatabases = [ cfg.database.name ];
576+ ensureUsers = [
577+ { name = cfg.database.user;
578+ ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
579+ }
580+ ];
581+ };
582+
583+ services.postgresql = optionalAttrs pgsqlLocal {
584+ enable = true;
585+ ensureDatabases = [ cfg.database.name ];
586+ ensureUsers = [
587+ { name = cfg.database.user;
588+ ensurePermissions = { "DATABASE ${cfg.database.name}" = "ALL PRIVILEGES"; };
589+ }
590+ ];
591+ };
592+
593+ };
594+
595+ meta.maintainers = with maintainers; [ mmilata sorki ];
596+}