~jb55/citadel

ref: d49a08a54e2552e0a471f6bb678e81f368ac0a9c citadel/nix-config/services/mailz/default.nix -rw-r--r-- 8.7 KiB
d49a08a5William Casarin nix: fix machine forked configs 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.mailz;
  mailbox = name: ''
    mailbox ${name} {
      auto = subscribe
    }
  '';

  # Convert:
  #
  #   {
  #     a = { aliases = [ "x", "y" ]; };
  #     b = { aliases = [ "x" ]; };
  #   }
  #
  # To:
  #
  #   {
  #     x = [ "a" "b" ];
  #     y = [ "a" ];
  #   }
  aliases = foldAttrs (user: users: [user] ++ users) [ ]
    (flatten (flip mapAttrsToList cfg.users
      (user: options: flip map options.aliases
        (alias: { ${alias} = user; }))));

  files = {
    credentials = pkgs.writeText "credentials"
      (concatStringsSep "\n"
        (flip mapAttrsToList cfg.users
          (user: options: "${user}@${cfg.domain} ${options.password}")));

    users = pkgs.writeText "users"
      (concatStringsSep "\n"
        (flip mapAttrsToList cfg.users
          (user: options: "${user}:${options.password}:::::")));

    recipients = pkgs.writeText "recipients"
      (concatStringsSep "\n"
        (map (user: "${user}@${cfg.domain}")
          (attrNames cfg.users ++ flatten ((flip mapAttrsToList) cfg.users
            (user: options: options.aliases)))));

    aliases = pkgs.writeText "aliases"
      (concatStringsSep "\n"
        (flip mapAttrsToList aliases
          (alias: users: "${alias} ${concatStringsSep "," users}")));

    spamassassinSieve = pkgs.writeText "spamassassin.sieve" ''
      require "fileinto";
      if header :contains "X-Spam-Flag" "YES" {
        fileinto "Spam";
      }
    '';

    # From <https://github.com/OpenSMTPD/OpenSMTPD-extras/blob/master/extras/wip/filters/filter-regex/filter-regex.conf>
    regex = pkgs.writeText "filter-regex.conf" ''
      helo ! ^\[
      helo ^\.
      helo \.$
      helo ^[^\.]*$
    '';
  };

in

{
  options = {
    services.mailz = {
      domain = mkOption {
        default = cfg.networking.hostName;
        type = types.str;
        description = "Domain for this mail server.";
      };

      enable = mkEnableOption "enable mailz: self-hosted email";

      user = mkOption {
        default = "vmail";
        type = types.str;
      };

      sieves = mkOption {
        default = "";
        type = types.str;
      };

      group = mkOption {
        default = "vmail";
        type = types.str;
      };

      uid = mkOption {
        default = 2000;
        type = types.int;
      };

      gid = mkOption {
        default = 2000;
        type = types.int;
      };

      dkimDirectory = mkOption {
        default = "/var/lib/dkim";
        type = types.str;
        description = "Where to store DKIM keys.";
      };

      dkimBits = mkOption {
        type = types.int;
        default = 2048;
        description = "Size of the generated DKIM key.";
      };

      users = mkOption {
        default = { };
        type = types.loaOf types.optionSet;
        description = ''
          Attribute set of users.
        '';

        options = {
          password = mkOption {
            type = types.str;
            description = ''
              The user password, generated with
              <literal>smtpctl encrypt</literal>.
            '';
          };

          aliases = mkOption {
            type = types.listOf types.str;
            default = [ ];
            example = [ "postmaster" ];
            description = "A list of aliases for this user.";
          };
        };

        example = {
          "foo" = {
            password = "encrypted";
            aliases = [ "postmaster" ];
          };
          "bar" = {
            password = "encrypted";
          };
        };
      };
    };
  };

  config = mkIf (cfg.enable && cfg.users != { }) {
    nixpkgs.config.packageOverrides = pkgs: {
      opensmtpd = pkgs.callPackage ./opensmtpd.nix { };
      opensmtpd-extras = pkgs.opensmtpd-extras.override {
        # Needed to have PRNG working in chroot (for dkim-signer)
        openssl = pkgs.libressl;
      };
    };

    system.activationScripts.mailz = ''
      # Make sure SpamAssassin database is present
      if ! [ -d /etc/spamassassin ]; then
        cp -r ${pkgs.spamassassin}/share/spamassassin /etc
      fi

      # Make sure a DKIM private key exist
      if ! [ -d ${cfg.dkimDirectory}/${cfg.domain} ]; then
        mkdir -p ${cfg.dkimDirectory}/${cfg.domain}
        chmod 700 ${cfg.dkimDirectory}/${cfg.domain}
        ${pkgs.opendkim}/bin/opendkim-genkey --bits ${toString cfg.dkimBits} --domain ${cfg.domain} --directory ${cfg.dkimDirectory}/${cfg.domain}
      fi
    '';

    services.spamassassin.enable = true;

    services.opensmtpd = {
      enable = true;
      serverConfiguration = ''
        filter filter-pause pause
        filter filter-regex regex "${files.regex}"
        filter filter-spamassassin spamassassin "-s accept"
        filter filter-dkim-signer dkim-signer "-d ${cfg.domain}" "-p${cfg.dkimDirectory}/${cfg.domain}/default.private"
        filter in chain filter-regex filter-spamassassin
        filter out chain filter-dkim-signer

        pki ${cfg.domain} certificate "/var/lib/acme/${cfg.domain}/fullchain.pem"
        pki ${cfg.domain} key "/var/lib/acme/${cfg.domain}/key.pem"

        table credentials file:${files.credentials}
        table recipients file:${files.recipients}
        table aliases file:${files.aliases}

        listen on 0.0.0.0 port 25 hostname ${cfg.domain} filter in tls pki ${cfg.domain}
        listen on 0.0.0.0 port 12566 hostname ${cfg.domain} filter out tls-require pki ${cfg.domain} auth <credentials>

        accept from any for domain "${cfg.domain}" recipient <recipients> alias <aliases> deliver to lmtp localhost:24
        accept from local for any relay
      '';
      procPackages = [ pkgs.opensmtpd-extras ];
    };

    services.dovecot2 = {
      enable = true;
      enablePop3 = false;
      enableLmtp = true;
      mailLocation = "maildir:/var/spool/mail/%n";
      mailUser = cfg.user;
      mailGroup = cfg.group;
      modules = [ pkgs.dovecot_pigeonhole ];
      sslServerCert = "/var/lib/acme/${cfg.domain}/fullchain.pem";
      sslServerKey = "/var/lib/acme/${cfg.domain}/key.pem";
      enablePAM = false;
      sieveScripts = {
        before = files.spamassassinSieve;
        before2 = pkgs.writeText "sieves" cfg.sieves;
      };
      extraConfig = ''
        postmaster_address = postmaster@${cfg.domain}
        mail_attribute_dict = file:/var/spool/mail/%n/dovecot-attributes

        service lmtp {
          inet_listener lmtp {
            address = 127.0.0.1 ::1
            port = 24
          }
        }

        service imap-login {
          inet_listener imaps {
            port = 12788
            ssl = yes
          }
        }

        userdb {
          driver = passwd-file
          args = username_format=%n ${files.users}
          default_fields = uid=${cfg.user} gid=${cfg.user} home=/var/spool/mail/%n
        }

        passdb {
          driver = passwd-file
          args = username_format=%n ${files.users}
        }

        namespace inbox {
          inbox = yes

          mailbox Sent {
              auto = subscribe
              special_use = \Sent
          }

          mailbox Drafts {
              auto = subscribe
              special_use = \Drafts
          }

          mailbox Spam {
              auto = subscribe
              special_use = \Junk
          }

          mailbox Trash {
              auto = subscribe
              special_use = \Trash
          }

          mailbox Archives {
              auto = subscribe
              special_use = \Archive
          }

          ${mailbox "Alerts"}
          ${mailbox "RSS"}
          ${mailbox "GitHub"}
          ${mailbox "Lists"}
          ${mailbox "YouTube"}
          ${mailbox "Lists.ats"}
          ${mailbox "Arxiv"}
          ${mailbox "Reddit"}
          ${mailbox "Lists.lobsters"}
          ${mailbox "Lists.icn"}
          ${mailbox "HackerNews"}
          ${mailbox "Lists.craigslist"}
          ${mailbox "Lists.bitcoin"}
          ${mailbox "Lists.elm"}
          ${mailbox "Lists.emacs"}
          ${mailbox "Lists.guix"}
          ${mailbox "Lists.haskell"}
          ${mailbox "Lists.lkml"}
          ${mailbox "Lists.nix"}
          ${mailbox "Lists.nixpkgs"}
          ${mailbox "Lists.shen"}
          ${mailbox "Lists.spacemacs"}
          ${mailbox "Monstercat"}
          ${mailbox "Updates"}

        }

        protocol lmtp {
          mail_plugins = $mail_plugins sieve notify push_notification
        }

        protocol imap {
          imap_metadata = yes
        }
      '';
    };

    # users.extraUsers = optional (cfg.user == "vmail") {
    #   name = "vmail";
    #   uid = cfg.uid;
    #   group = cfg.group;
    # };

    # users.extraGroups = optional (cfg.group == "vmail") {
    #   name = "vmail";
    #   gid = cfg.gid;
    # };

    networking.firewall.allowedTCPPorts = [ 25 ];
  };
}