summaryrefslogtreecommitdiffstats
path: root/mail-server/postfix.nix
blob: fb4dbc12336ee53041e5e2d7dc7686e504b9013f (plain)
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
#  nixos-mailserver: a simple mail server
#  Copyright (C) 2016-2017  Robin Raymond
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program. If not, see <http://www.gnu.org/licenses/>

{ lib, mail_dir, domain, valiases, cert, key }:

let
  # valiases_postfix :: [ String ]
  valiases_postfix = map
    (from:
      let to = valiases.${from};
      in "${from}@${domain} ${to}@${domain}")
    (builtins.attrNames valiases);

  # valiases_file :: Path
  valiases_file = builtins.toFile "valias" (lib.concatStringsSep "\n" valiases_postfix);

  # vhosts_file :: Path
  vhosts_file = builtins.toFile "vhosts" domain;

  # vaccounts_file :: Path
  # see
  # https://blog.grimneko.de/2011/12/24/a-bunch-of-tips-for-improving-your-postfix-setup/
  # for details on how this file looks. By using the same file as valiases,
  # every alias is owned (uniquely) by its user.
  vaccounts_file = valiases_file;

in
{
  enable = true;
  networksStyle = "host";
  mapFiles."valias" = valiases_file;
  mapFiles."vaccounts" = vaccounts_file; 
  sslCert = cert;
  sslKey = key;
  enableSubmission = true;

  extraConfig = 
  ''

    # Extra Config

    smtpd_banner = $myhostname ESMTP NO UCE
    smtpd_tls_auth_only = yes
    disable_vrfy_command = yes
    message_size_limit = 20971520

    # virtual mail system
    virtual_uid_maps = static:5000
    virtual_gid_maps = static:5000
    virtual_mailbox_base = ${mail_dir}
    virtual_mailbox_domains = ${vhosts_file}
    virtual_alias_maps = hash:/var/lib/postfix/conf/valias
    virtual_transport = lmtp:unix:private/dovecot-lmtp

    # sasl with dovecot
    smtpd_sasl_type = dovecot
    smtpd_sasl_path = private/auth
    smtpd_sasl_auth_enable = yes
    smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
  '';

  submissionOptions =
  { 
      smtpd_tls_security_level = "encrypt";
      smtpd_sasl_auth_enable = "yes";
      smtpd_sasl_type = "dovecot";
      smtpd_sasl_path = "private/auth";
      smtpd_sasl_security_options = "noanonymous";
      smtpd_sasl_local_domain = "$myhostname";
      smtpd_client_restrictions = "permit_sasl_authenticated,reject";
      smtpd_sender_login_maps = "hash:/etc/postfix/vaccounts";
      smtpd_sender_restrictions = "reject_sender_login_mismatch";
      smtpd_recipient_restrictions = "reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject";
  };
}