1 { lib, pkgs, config, myconfig, ... }:
3 config.services.backup.profiles.mail.excludeFile = ''
6 config.secrets.keys = [
8 dest = "postfix/mysql_alias_maps";
9 user = config.services.postfix.user;
10 group = config.services.postfix.group;
13 # We need to specify that option to trigger ssl connection
15 user = ${myconfig.env.mail.postfix.mysql.user}
16 password = ${myconfig.env.mail.postfix.mysql.password}
17 hosts = unix:${myconfig.env.mail.postfix.mysql.socket}
18 dbname = ${myconfig.env.mail.postfix.mysql.database}
19 query = SELECT DISTINCT destination
20 FROM forwardings_merge
22 ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s'))
27 FROM forwardings_blacklisted
30 SELECT 'devnull@immae.eu'
31 FROM forwardings_blacklisted
36 dest = "postfix/mysql_mailbox_maps";
37 user = config.services.postfix.user;
38 group = config.services.postfix.group;
41 # We need to specify that option to trigger ssl connection
43 user = ${myconfig.env.mail.postfix.mysql.user}
44 password = ${myconfig.env.mail.postfix.mysql.password}
45 hosts = unix:${myconfig.env.mail.postfix.mysql.socket}
46 dbname = ${myconfig.env.mail.postfix.mysql.database}
47 result_format = /%d/%u
48 query = SELECT DISTINCT '%s'
52 (domain = '%d' AND user = '%u' AND regex = 0)
55 AND '%d' REGEXP CONCAT('^',domain,'$')
56 AND '%u' REGEXP CONCAT('^',user,'$')
63 dest = "postfix/mysql_sender_login_maps";
64 user = config.services.postfix.user;
65 group = config.services.postfix.group;
68 # We need to specify that option to trigger ssl connection
70 user = ${myconfig.env.mail.postfix.mysql.user}
71 password = ${myconfig.env.mail.postfix.mysql.password}
72 hosts = unix:${myconfig.env.mail.postfix.mysql.socket}
73 dbname = ${myconfig.env.mail.postfix.mysql.database}
74 query = SELECT DISTINCT destination
75 FROM forwardings_merge
77 ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s'))
79 UNION SELECT '%s' AS destination
84 config.networking.firewall.allowedTCPPorts = [ 25 465 587 ];
86 config.nixpkgs.overlays = [ (self: super: {
87 postfix = super.postfix.override { withMySQL = true; };
89 config.users.users."${config.services.postfix.user}".extraGroups = [ "keys" ];
90 config.services.filesWatcher.postfix = {
93 config.secrets.fullPaths."postfix/mysql_alias_maps"
94 config.secrets.fullPaths."postfix/mysql_mailbox_maps"
95 config.secrets.fullPaths."postfix/mysql_sender_login_maps"
98 config.services.postfix = {
101 name = n: i: "relay_${n}_${toString i}";
102 pair = n: i: m: lib.attrsets.nameValuePair (name n i) (
104 then pkgs.writeText (name n i) m.content
107 pairs = n: v: lib.imap1 (i: m: pair n i m) v.recipient_maps;
108 in lib.attrsets.filterAttrs (k: v: v != null) (
109 lib.attrsets.listToAttrs (lib.flatten (
110 lib.attrsets.mapAttrsToList pairs myconfig.env.mail.postfix.backup_domains
113 relay_restrictions = lib.attrsets.filterAttrs (k: v: v != null) (
114 lib.attrsets.mapAttrs' (n: v:
115 lib.attrsets.nameValuePair "recipient_access_${n}" (
116 if lib.attrsets.hasAttr "relay_restrictions" v
117 then pkgs.writeText "recipient_access_${n}" v.relay_restrictions
120 ) myconfig.env.mail.postfix.backup_domains
123 recipient_maps // relay_restrictions;
125 ### postfix module overrides
126 readme_directory = "${pkgs.postfix}/share/postfix/doc";
127 smtp_tls_CAfile = lib.mkForce "";
128 smtp_tls_cert_file = lib.mkForce "";
129 smtp_tls_key_file = lib.mkForce "";
131 message_size_limit = "1073741824"; # Don't put 0 here, it's not equivalent to "unlimited"
132 alias_database = "\$alias_maps";
134 ### Virtual mailboxes config
135 virtual_alias_maps = "mysql:${config.secrets.fullPaths."postfix/mysql_alias_maps"}";
136 virtual_mailbox_domains = myconfig.env.mail.postfix.additional_mailbox_domains
137 ++ lib.remove "localhost.immae.eu" (lib.remove null (lib.flatten (map
140 then "${e.domain}${lib.optionalString (e.domain != "") "."}${zone.name}"
143 (zone.withEmail or [])
145 myconfig.env.dns.masterZones
147 virtual_mailbox_maps = "mysql:${config.secrets.fullPaths."postfix/mysql_mailbox_maps"}";
148 dovecot_destination_recipient_limit = "1";
149 virtual_transport = "dovecot";
152 relay_domains = lib.flatten (lib.attrsets.mapAttrsToList (n: v: v.domains or []) myconfig.env.mail.postfix.backup_domains);
153 relay_recipient_maps = lib.flatten (lib.attrsets.mapAttrsToList (n: v:
154 lib.imap1 (i: m: "${m.type}:/etc/postfix/relay_${n}_${toString i}") v.recipient_maps
155 ) myconfig.env.mail.postfix.backup_domains);
156 smtpd_relay_restrictions = [
158 "permit_sasl_authenticated"
159 "defer_unauth_destination"
160 ] ++ lib.flatten (lib.attrsets.mapAttrsToList (n: v:
161 if lib.attrsets.hasAttr "relay_restrictions" v
162 then [ "check_recipient_access hash:/etc/postfix/recipient_access_${n}" ]
164 ) myconfig.env.mail.postfix.backup_domains);
166 ### Additional smtpd configuration
167 smtpd_tls_received_header = "yes";
168 smtpd_tls_loglevel = "1";
170 ### Email sending configuration
171 smtp_tls_security_level = "may";
172 smtp_tls_loglevel = "1";
174 ### Force ip bind for smtp
175 smtp_bind_address = myconfig.env.servers.eldiron.ips.main.ip4;
176 smtp_bind_address6 = builtins.head myconfig.env.servers.eldiron.ips.main.ip6;
178 # #Unneeded if postfix can only send e-mail from "self" domains
179 # #smtp_sasl_auth_enable = "yes";
180 # #smtp_sasl_password_maps = "hash:/etc/postfix/relay_creds";
181 # #smtp_sasl_security_options = "noanonymous";
182 # #smtp_sender_dependent_authentication = "yes";
183 # #sender_dependent_relayhost_maps = "hash:/etc/postfix/sender_relay";
185 ### opendkim, opendmarc, openarc milters
186 non_smtpd_milters = [
187 "unix:${config.myServices.mail.milters.sockets.opendkim}"
188 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
189 "unix:${config.myServices.mail.milters.sockets.openarc}"
192 "unix:${config.myServices.mail.milters.sockets.opendkim}"
193 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
194 "unix:${config.myServices.mail.milters.sockets.openarc}"
199 enableSubmission = true;
200 submissionOptions = {
201 smtpd_tls_security_level = "encrypt";
202 smtpd_sasl_auth_enable = "yes";
203 smtpd_tls_auth_only = "yes";
204 smtpd_sasl_tls_security_options = "noanonymous";
205 smtpd_sasl_type = "dovecot";
206 smtpd_sasl_path = "private/auth";
207 smtpd_reject_unlisted_recipient = "no";
208 smtpd_client_restrictions = "permit_sasl_authenticated,reject";
209 # Refuse to send e-mails with a From that is not handled
210 smtpd_sender_restrictions =
211 "reject_sender_login_mismatch,reject_unlisted_sender,permit_sasl_authenticated,reject";
212 smtpd_sender_login_maps = "mysql:${config.secrets.fullPaths."postfix/mysql_sender_login_maps"}";
213 smtpd_recipient_restrictions = "permit_sasl_authenticated,reject";
214 milter_macro_daemon_name = "ORIGINATING";
215 smtpd_milters = "unix:${config.myServices.mail.milters.sockets.opendkim}";
217 # FIXME: Mail adressed to localhost.immae.eu will still have mx-1 as
218 # prioritized MX, which provokes "mail for localhost.immae.eu loops
219 # back to myself" errors. This transport entry forces to push
220 # e-mails to its right destination.
222 localhost.immae.eu smtp:[immae.eu]:25
224 destination = ["localhost"];
225 # This needs to reverse DNS
226 hostname = "eldiron.immae.eu";
228 sslCert = "/var/lib/acme/mail/fullchain.pem";
229 sslKey = "/var/lib/acme/mail/key.pem";
230 recipientDelimiter = "+";
236 args = ["-o" "smtpd_tls_wrappermode=yes" ] ++ (let
237 mkKeyVal = opt: val: [ "-o" (opt + "=" + val) ];
238 in lib.concatLists (lib.mapAttrsToList mkKeyVal config.services.postfix.submissionOptions)
247 # rspamd could be used as a milter, but then it cannot apply
248 # its checks "per user" (milter is not yet dispatched to
249 # users), so we wrap dovecot-lda inside rspamc per recipient
251 dovecot_exe = "${pkgs.dovecot}/libexec/dovecot/dovecot-lda -f \${sender} -a \${original_recipient} -d \${user}@\${nexthop}";
253 "flags=DRhu" "user=vhost:vhost"
254 "argv=${pkgs.rspamd}/bin/rspamc -h ${config.myServices.mail.rspamd.sockets.worker-controller} -c bayes -d \${user}@\${nexthop} --mime --exec {${dovecot_exe}}"
259 config.security.acme.certs."mail" = {
261 systemctl restart postfix.service
264 "smtp.immae.eu" = null;