Test list
 help / color / mirror / code / Atom feed
248a88dce6588a4eef181b1a2240698dd35f7434 blob 8358 bytes (raw)

  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
 
# Email server configuration
# Copyright (C) 2023-2024  Nguyễn Gia Phong
#
# This file is part of loang configuration.
#
# Loang configuration is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Loang configuration is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with loang configuration.  If not, see <https://www.gnu.org/licenses/>.

{ config, lib, pkgs, ... }:
let
  alps = config.services.alps;
  domain = config.networking.domain;
  hostname = "tem.${domain}";
  publicHost = "loa.${domain}";
  public-inbox = config.services.public-inbox;
in {
  networking.firewall.allowedTCPPorts = [
    25 # SMTP-MTA
    alps.smtps.port
    alps.imaps.port
    public-inbox.imap.port
  ];

  services = {
    alps = {
      enable = true;
      imaps.host = hostname;
      theme = "alps";
    };

    maddy = {
      config = ''
        auth_map email_localpart

        auth.shadow local_authdb {
          use_helper no
        }

        log syslog /var/log/maddy/maddy.log

        storage.imapsql local_mailboxes {
          driver sqlite3
          dsn imapsql.db
        }

        table.chain local_rewrites {
          optional_step regexp "(.+)\+(.+)@(.+)" "$1@$3"
          optional_step static {
            entry postmaster cnx@$(primary_domain)
          }
          optional_step file /etc/maddy/aliases
        }

        msgpipeline local_routing {
          destination ${publicHost} {
            deliver_to smtp tcp://localhost:2525
          }
          destination postmaster $(local_domains) {
            modify {
              replace_rcpt &local_rewrites
            }
            deliver_to &local_mailboxes
          }
          default_destination {
            reject 550 5.1.1 "User doesn't exist"
          }
        }

        smtp tcp://0.0.0.0:25 {
          limits {
            all rate 20 1s
            all concurrency 10
          }
          dmarc yes
          check {
            require_mx_record
            dkim
            spf
          }
          source $(local_domains) {
            reject 501 5.1.8 "Use Submission for outgoing SMTP"
          }
          default_source {
            destination postmaster $(local_domains) {
              deliver_to &local_routing
            }
            default_destination {
              reject 550 5.1.1 "User doesn't exist"
            }
          }
        }

        submission tls://0.0.0.0:${toString alps.smtps.port} {
          limits {
            all rate 50 1s
          }
          auth &local_authdb
          source $(local_domains) {
            check {
              authorize_sender {
                prepare_email &local_rewrites
                user_to_email identity
              }
            }
            destination postmaster $(local_domains) {
              deliver_to &local_routing
            }
            default_destination {
              modify {
                dkim $(primary_domain) $(local_domains) default
              }
              deliver_to &remote_queue
            }
          }
          default_source {
            reject 501 5.1.8 "Non-local sender domain"
          }
        }

        target.remote outbound_delivery {
          limits {
            destination rate 20 1s
            destination concurrency 10
          }
          mx_auth {
            dane
            mtasts {
              cache fs
              fs_dir mtasts_cache/
            }
            local_policy {
              min_tls_level encrypted
              min_mx_level none
            }
          }
        }

        target.queue remote_queue {
          target &outbound_delivery
          autogenerated_msg_domain $(primary_domain)
          bounce {
            destination postmaster $(local_domains) {
              deliver_to &local_routing
            }
            default_destination {
              reject 550 5.0.0 "Refusing to send DSNs to non-local addresses"
            }
          }
        }

        imap tls://0.0.0.0:${toString alps.imaps.port} {
          auth &local_authdb
          storage &local_mailboxes
        }
      '';
      enable = true;
      hostname = hostname;
      primaryDomain = domain;
      localDomains = [ domain publicHost ];
      tls = {
        loader = "file";
        certificates = let
          certDir = config.security.acme.certs.${hostname}.directory;
        in [ {
          certPath = "${certDir}/cert.pem";
          keyPath = "${certDir}/key.pem";
        } ];
      };
    };

    postfix = {
      enable = true; # bridge between maddy and public-inbox
      enableSmtp = false; # override default port 25
      extraMasterConf = ''
        2525          inet  n        -       -       -        -        smtpd
      '';
    };

    public-inbox = {
      enable = true;
      http = {
        enable = true;
        port = 1430;
      };
      imap = let
        certDir = config.security.acme.certs.${publicHost}.directory;
      in {
        cert = "${certDir}/cert.pem";
        enable = true;
        key = "${certDir}/key.pem";
        port = 143;
      };
      inboxes = builtins.mapAttrs (name: value: value // {
        address = [ "${name}@${publicHost}" ];
        url = "https://${publicHost}/${name}";
        newsgroup = "inbox.${name}";
      }) {
        chung = {
          description = "News, requests and patches for loang.net";
          coderepo = [ "nixos-conf" "phylactery" "site" ];
        };
        test = {
          description = "Test list";
          coderepo = [ "nixos-conf" ];
        };
      };
      mda.enable = true;
      postfix.enable = true;
      settings = {
        coderepo = builtins.listToAttrs (map (name: {
          name = name;
          value = {
            cgitUrl = "https://trong.loang.net/${name}";
            dir = name;
          };
        }) [ "nixos-conf" "phylactery" "site" ]);
        publicinbox = {
          imapserver = [ publicHost ];
          wwwlisting = "match=domain";
        };
      };
    };

    nginx.virtualHosts = {
      "mta-sts.${domain}" = {
        enableACME = true;
        forceSSL = true;
        locations."/".root = pkgs.writeTextDir ".well-known/mta-sts.txt" ''
          version: STSv1
          mode: enforce
          max_age: 604800
          mx: ${hostname}
        '';
      };

      ${domain}.locations."^~ /.well-known/openpgpkey" = {
        root = with pkgs; stdenvNoCC.mkDerivation {
          pname = "wkd";
          version = domain;
          src = ./wkd;
          nativeBuildInputs = [ gnupg ];
          installPhase = let
            printWKDHash = "${gnupg}/libexec/gpg-wks-client --print-wkd-hash";
          in ''
            hu=$out/.well-known/openpgpkey/hu
            mkdir -p $hu
            for key in *.asc
            do
              mb="''${key%.asc}@${domain}"
              hash=$(echo "$mb" | ${printWKDHash})
              gpg --dearmor < "$key" > $hu/''${hash%" $mb"}
            done
            touch $out/.well-known/openpgpkey/policy
          '';
        };
        extraConfig = ''
          add_header Access-Control-Allow-Origin *;
        '';
      };

      "${publicHost}" = {
        enableACME = true;
        forceSSL = true;
        locations."/".proxyPass = let
          port = public-inbox.http.port;
        in "PI-LINK-d2edaa1f37c4eab0915ede5a4da098b3534aac0c${toString port}";
      };

      ${hostname} = {
        enableACME = true;
        forceSSL = true;
        locations."/".proxyPass = "http://${alps.bindIP}:${toString alps.port}";
      };
    };
  };

  systemd.services = {
    alps.unitConfig.Requires = "maddy.service";
    public-inbox-imapd.serviceConfig.BindReadOnlyPaths = [
      config.security.acme.certs.${publicHost}.directory
    ];
  };

  users.users = {
    maddy.extraGroups = [
      config.security.acme.certs.${hostname}.group
      "shadow"
    ];
    public-inbox.extraGroups = [
      config.security.acme.certs.${publicHost}.group
    ];
  };
}
debug log:

solving 248a88dce658 ...
found 248a88dce658 in https://loa.loang.net/test/20240418010455.1748303-2-cnx@loang.net/
found ae145566f2fb in https://trong.loang.net/nixos-conf
preparing index
index prepared:
100644 ae145566f2fbec097b122690a861766a434239a3	mail.nix

applying [1/1] https://loa.loang.net/test/20240418010455.1748303-2-cnx@loang.net/
diff --git a/mail.nix b/mail.nix
index ae145566f2fb..248a88dce658 100644

Checking patch mail.nix...
Applied patch mail.nix cleanly.

index at:
100644 248a88dce6588a4eef181b1a2240698dd35f7434	mail.nix

Code repositories for project(s) associated with this public inbox

	https://trong.loang.net/nixos-conf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).