1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.mlmmj;
8 stateDir = "/var/lib/mlmmj";
9 spoolDir = "/var/spool/mlmmj";
10 listDir = domain: list: "${spoolDir}/${domain}/${list}";
11 listCtl = domain: list: "${listDir domain list}/control";
12 transport = domain: list: "${domain}--${list}@local.list.mlmmj mlmmj:${domain}/${list}";
13 virtual = domain: list: "${list}@${domain} ${domain}--${list}@local.list.mlmmj";
14 alias = domain: list: "${list}: \"|${pkgs.mlmmj}/bin/mlmmj-receive -L ${listDir domain list}/\"";
15 subjectPrefix = list: "[${list}]";
16 listAddress = domain: list: "${list}@${domain}";
17 customHeaders = domain: list: [ "List-Id: ${list}" "Reply-To: ${list}@${domain}" ];
18 footer = domain: list: "To unsubscribe send a mail to ${list}+unsubscribe@${domain}";
19 createList = d: l: ''
20 ${pkgs.coreutils}/bin/mkdir -p ${listCtl d l}
21 echo ${listAddress d l} > ${listCtl d l}/listadress
22 echo "${lib.concatStringsSep "\n" (customHeaders d l)}" > ${listCtl d l}/customheaders
23 echo ${footer d l} > ${listCtl d l}/footer
24 echo ${subjectPrefix l} > ${listCtl d l}/prefix
25 '';
26in
27
28{
29
30 ###### interface
31
32 options = {
33
34 services.mlmmj = {
35
36 enable = mkOption {
37 type = types.bool;
38 default = false;
39 description = "Enable mlmmj";
40 };
41
42 user = mkOption {
43 type = types.str;
44 default = "mlmmj";
45 description = "mailinglist local user";
46 };
47
48 group = mkOption {
49 type = types.str;
50 default = "mlmmj";
51 description = "mailinglist local group";
52 };
53
54 listDomain = mkOption {
55 type = types.str;
56 default = "localhost";
57 description = "Set the mailing list domain";
58 };
59
60 mailLists = mkOption {
61 type = types.listOf types.str;
62 default = [];
63 description = "The collection of hosted maillists";
64 };
65
66 };
67
68 };
69
70 ###### implementation
71
72 config = mkIf cfg.enable {
73
74 users.extraUsers = singleton {
75 name = cfg.user;
76 description = "mlmmj user";
77 home = stateDir;
78 createHome = true;
79 uid = config.ids.uids.mlmmj;
80 group = cfg.group;
81 useDefaultShell = true;
82 };
83
84 users.extraGroups = singleton {
85 name = cfg.group;
86 gid = config.ids.gids.mlmmj;
87 };
88
89 services.postfix = {
90 enable = true;
91 recipientDelimiter= "+";
92 extraMasterConf = ''
93 mlmmj unix - n n - - pipe flags=ORhu user=mlmmj argv=${pkgs.mlmmj}/bin/mlmmj-receive -F -L ${spoolDir}/$nexthop
94 '';
95
96 extraAliases = concatMapStrings (alias cfg.listDomain) cfg.mailLists;
97
98 extraConfig = ''
99 transport_maps = hash:${stateDir}/transports
100 virtual_alias_maps = hash:${stateDir}/virtuals
101 propagate_unmatched_extensions = virtual
102 '';
103 };
104
105 environment.systemPackages = [ pkgs.mlmmj ];
106
107 system.activationScripts.mlmmj = ''
108 ${pkgs.coreutils}/bin/mkdir -p ${stateDir} ${spoolDir}/${cfg.listDomain}
109 ${pkgs.coreutils}/bin/chown -R ${cfg.user}:${cfg.group} ${spoolDir}
110 ${lib.concatMapStrings (createList cfg.listDomain) cfg.mailLists}
111 echo ${lib.concatMapStrings (virtual cfg.listDomain) cfg.mailLists} > ${stateDir}/virtuals
112 echo ${lib.concatMapStrings (transport cfg.listDomain) cfg.mailLists} > ${stateDir}/transports
113 ${pkgs.postfix}/bin/postmap ${stateDir}/virtuals
114 ${pkgs.postfix}/bin/postmap ${stateDir}/transports
115 '';
116
117 systemd.services."mlmmj-maintd" = {
118 description = "mlmmj maintenance daemon";
119 wantedBy = [ "multi-user.target" ];
120
121 serviceConfig = {
122 User = cfg.user;
123 Group = cfg.group;
124 ExecStart = "${pkgs.mlmmj}/bin/mlmmj-maintd -F -d ${spoolDir}/${cfg.listDomain}";
125 };
126 };
127
128 };
129
130}