1 {pkgs, lib, config, nodes, ...}:
2 let inherit (builtins) attrNames toFile;
4 inherit (pkgs.lib) unlines unlinesAttrs;
5 inherit (config.services) x509 postfix dovecot2 postgrey openldap;
6 unwords = lib.concatStringsSep " ";
7 when = x: y: if x == null then "" else y;
9 submissionHeaderCleanupRules = pkgs.writeText "submission_header_cleanup_rules" ''
10 # Removes sensitive headers from mails handed in via the submission or smtps port.
11 # See https://thomas-leister.de/mailserver-debian-stretch/
12 # Uses "pcre" style regex.
18 /^X-Originating-IP:/ IGNORE
22 options.services.postfix.aliases = lib.mkOption {
23 type = with types; attrsOf (listOf str);
25 example = { "root@${config.networking.domain}" = [
26 "user1@${config.networking.domain}"
27 "user2@${config.networking.domain}"
29 "@example.coop" = ["user1@${config.networking.domain}"];
33 systemd.services.postfix.after =
34 if x509.scheme == "letsencrypt"
35 then [ "nginx.service" ] # XXX: not sure if this is enough
39 #hostname = config.networking.domain;
40 #domain = "localdomain";
41 networksStyle = "host";
42 #mapFiles."valias" = toFile "valias" (unlines (all_valiases_postfix ++ catchAllPostfix));
43 # See https://blog.grimneko.de/2011/12/24/a-bunch-of-tips-for-improving-your-postfix-setup/
44 # for details on how this file looks. By using the same file as valias,
45 # every alias is uniquely owned by its user.
46 # The user's own address is already in all_valiases_postfix.
47 #mapFiles."vaccounts" = toFile "vaccounts" (unlines all_valiases_postfix);
48 mapFiles."virtual_alias_maps" =
49 toFile "virtual_alias_maps"
51 (from: to: "${from} ${unwords to}")
53 mapFiles."ldap-virtual_alias_maps.cf" =
54 toFile "ldap-virtual_alias_maps.cf" ''
57 server_host = ldapi://
60 search_base = ou=posix,${openldap.domainSuffix}
63 query_filter = (&(mailAlias=%s)(mailEnabled=TRUE))
65 result_attribute = mail
67 mapFiles."ldap-forward.cf" =
68 toFile "ldap-forward.cf" ''
71 server_host = ldapi://
74 search_base = ou=posix,${openldap.domainSuffix}
77 query_filter = (&(mail=%s)(mailEnabled=TRUE))
79 result_attribute = mailForwardingAddress
83 #enableSubmission = true;
87 "localhost.localdomain"
88 config.networking.hostName
89 "${config.networking.hostName}.localdomain"
95 recipientDelimiter = "+";
97 # Appending .domain is the MUA's job
98 append_dot_mydomain = false;
99 # No console bell on new mail
102 #content_filter = "amavisfeed:[127.0.0.1]:10024";
103 #debug_peer_level = 4;
104 #debug_peer_list = ".$myhostname";
105 default_extra_recipient_limit = "5000";
106 # Uncomment the next line to generate "delayed mail" warnings
107 #delay_warning_time = "4h";
108 # Stops some techniques used to harvest email addresses
109 disable_vrfy_command = true;
110 duplicate_filter_limit = "5000";
111 enable_long_queue_ids = false;
112 # Pass unexisting $mydestination recipients to dovecot
113 fallback_transport = "lmtp:unix:private/dovecot-lmtp";
115 ''$home/.forward''${recipient_delimiter}''${extension}''
118 #header_checks = "regexp:/var/lib/postfix/conf/header_checks";
119 #inet_interfaces = "all";
120 line_length_limit = "2048";
122 # Let $fallback_transport check existence of recipients
123 local_recipient_maps = "";
124 #mail_spool_directory = "/var/spool/mail";
125 # NOTE: nixpkgs's default
126 #local_header_rewrite_clients = "";
127 #home_mailbox = "Maildir/";
128 #mailbox_command = ''
129 # ${pkgs.procmail}/bin/procmail -t -a "$SENDER" -a "$RECIPIENT" -a "$USER" -a "$EXTENSION" -a "$DOMAIN" -a "$ORIGINAL_RECIPIENT" "$HOME/.procmailrc"
131 mailbox_size_limit = "204800000";
133 masquerade_classes = [ "envelope_sender" "header_sender" "header_recipient" ];
134 masquerade_domains = "";
135 masquerade_exceptions = "root";
136 maximal_queue_lifetime = "5d";
137 message_size_limit = "20480000";
138 mime_header_checks = "";
139 milter_header_checks = "";
140 nested_header_checks = "";
141 non_smtpd_milters = "";
142 parent_domain_matches_subdomains = [
144 #"fast_flush_domains"
146 #"permit_mx_backup_networks"
147 #"qmqpd_authorized_clients"
150 permit_mx_backup_networks = "";
151 #policy-spf_time_limit = "3600s";
152 propagate_unmatched_extensions = [ "canonical" "virtual" "alias" ];
154 #receive_override_options = "no_address_mappings";
155 # no_unknown_recipient_checks
156 # Do not try to reject unknown recipients (SMTP server only).
157 # This is typically specified AFTER an external content filter.
158 # no_address_mappings
159 # Disable canonical address mapping, virtual alias map expansion,
160 # address masquerading, and automatic BCC (blind carbon-copy) recipients.
161 # This is typically specified BEFORE an external content filter (eg. amavis).
162 # no_header_body_checks
163 # Disable header/body_checks. This is typically specified AFTER
164 # an external content filter.
166 # Disable Milter (mail filter) applications.
167 # This is typically specified AFTER an external content filter.
168 # Parse the extension in email address, eg. contact+extension@
170 #relay_clientcerts = hash:/var/lib/postfix/conf/relay_clientcerts
171 # This is where to put backup MX domains
172 relay_domains = "$mydestination";
173 relay_recipient_maps = "";
174 smtp_body_checks = "";
175 #smtp_cname_overrides_servername = false;
176 smtp_connect_timeout = "60s";
177 #smtp_header_checks = "regexp:/var/lib/postfix/smtp_header_checks";
178 smtp_mime_header_checks = "";
179 smtp_nested_header_checks = "";
180 smtp_tls_exclude_ciphers = [ "ADH" "MD5" "CAMELLIA" "SEED" "3DES" "DES" "RC4" "eNULL" "aNULL" ];
181 #smtp_tls_fingerprint_digest = "sha1";
182 smtp_tls_loglevel = "1";
183 #smtp_tls_note_starttls_offer = true;
184 #smtp_tls_policy_maps = "hash:/var/lib/postfix/conf/tls_policy";
185 # Only allow TLSv* protocols
186 smtp_tls_protocols = [ "!SSLv2" "!SSLv3" ];
187 smtp_tls_scert_verifydepth = "5";
188 #smtp_tls_secure_cert_match = [ "nexthop" "dot-nexthop" ];
189 smtp_tls_security_level = "may";
190 smtp_tls_session_cache_database = "btree:$data_directory/smtp_tls_session_cache";
191 #smtp_tls_session_cache_timeout = "3600s";
192 #smtp_tls_verify_cert_match = "hostname";
193 # Useful to test restrictions
194 smtpd_authorized_xclient_hosts = "127.0.0.1";
195 smtpd_banner = "${config.networking.fqdn} ESMTP $mail_name (NixOS)";
196 smtpd_client_connection_count_limit = "50";
197 smtpd_client_connection_rate_limit = "0";
198 smtpd_client_event_limit_exceptions = "$mynetworks";
199 smtpd_client_message_rate_limit = "0";
200 smtpd_client_new_tls_session_rate_limit = "0";
201 smtpd_client_port_logging = false;
202 smtpd_client_recipient_rate_limit = "0";
203 smtpd_client_restrictions = [
204 #"check_client_access hash:/var/lib/postfix/conf/client_blacklist"
206 smtpd_data_restrictions = [
207 "reject_unauth_pipelining"
208 # Force the smtp client to wait OK before sending
211 # Disable opportunistic encryption
212 smtpd_discard_ehlo_keywords = "starttls";
213 #smtpd_end_of_data_restrictions = "";
215 smtpd_error_sleep_time = "5";
216 smtpd_helo_required = true;
217 smtpd_helo_restrictions = [
218 "reject_invalid_helo_hostname"
219 "reject_non_fqdn_helo_hostname"
220 #"reject_unknown_helo_hostname"
221 # May be useful to fight spam
226 smtpd_peername_lookup = true;
227 smtpd_recipient_limit = "5000";
228 smtpd_recipient_overshoot_limit = "5000";
229 smtpd_recipient_restrictions = [
230 "reject_non_fqdn_recipient"
231 #"reject_invalid_hostname"
232 "reject_unknown_recipient_domain"
233 #"reject_non_fqdn_sender"
234 "reject_unauth_pipelining"
235 #"check_policy_service inet:localhost:12340"
238 "permit_tls_clientcerts"
239 "permit_sasl_authenticated"
240 "reject_unverified_recipient"
241 # $fallback_transport is responsible of checking the existence of the recipient
242 # WARNING: verify(8) has a cache, dumpable if verify(8) is stopped, with:
243 # postmap -s btree:/var/lib/postfix/data/verify_cache
244 # Bypass SPF check and postgrey if the recipient is not for us or someone in backup_mx
245 "reject_unauth_destination"
247 #"check_policy_service unix:private/spfcheck"
248 # Greylisting using postgrey
249 "check_policy_service unix:${postgrey.socket.path}"
250 # Once postgrey passed, permit what is for us
251 "permit_auth_destination"
253 #"reject_unknown_sender_domain"
254 # Maybe better in smtpd_sender_restrictions
255 #"reject_rbl_client bl.spamcop.net"
256 #"reject_rbl_client list.dsbl.org"
257 #"reject_rbl_client zen.spamhaus.org"
258 #"reject_rbl_client dnsbl.sorbs.net"
260 smtpd_relay_restrictions = [
262 "permit_sasl_authenticated"
263 # NOTE: permit auth through dovecot's SASL
264 "reject_unauth_destination"
266 #smtpd_restriction_classes = "";
267 broken_sasl_auth_clients = false;
268 #smtpd_sasl_auth_enable = true;
269 #smtpd_sasl_path = "private/auth";
270 #smtpd_sasl_security_options = "noanonymous";
271 #smtpd_sasl_type = "dovecot";
272 smtpd_sender_restrictions = [
274 "permit_tls_clientcerts"
275 "permit_sasl_authenticated"
276 # NOTE: permit auth through dovecot's SASL
277 #"check_sender_access hash:/var/lib/postfix/conf/sender_access"
278 "reject_unauth_pipelining"
279 "reject_non_fqdn_sender"
280 #"reject_sender_login_mismatch"
281 #"reject_unknown_sender_domain"
284 smtpd_starttls_timeout = "300s";
285 #smtpd_tls_always_issue_session_ids = true;
286 # No SASL AUTH without TLS
287 smtpd_tls_auth_only = true;
288 #smtpd_tls_CApath = "/etc/postfix/x509/ca/";
289 smtpd_tls_ask_ccert = false;
290 #smtpd_tls_ccert_verifydepth = "5";
291 smtpd_tls_ciphers = "high";
292 smtpd_tls_eecdh_grade = "ultra";
293 # Disable weak ciphers as reported by https://ssl-tools.net
294 # https://serverfault.com/questions/744168/how-to-disable-rc4-on-postfix
295 smtpd_tls_exclude_ciphers = [ "RC4" "aNULL" ];
296 smtpd_tls_fingerprint_digest = "sha512";
297 # Log only a summary message on TLS handshake completion
298 smtpd_tls_loglevel = "1";
299 smtpd_tls_mandatory_ciphers = "high";
300 smtpd_tls_mandatory_protocols = "TLSv1";
302 smtpd_tls_protocols = [ "!SSLv2" "!SSLv3" ];
303 #smtpd_tls_received_header = false;
304 smtpd_tls_req_ccert = false;
305 # Postfix 2.3 and later
307 # Mandatory TLS encryption: announce STARTTLS support to SMTP clients, and require that clients use TLS
308 # encryption. According to [1720]RFC 2487 this MUST NOT be applied in case of a publicly-referenced
309 # SMTP server. Instead, this option should be used only on dedicated servers.
310 smtpd_tls_security_level = "may";
311 smtpd_tls_session_cache_database = "btree:$data_directory/smtpd_tls_session_cache";
312 #smtpd_tls_session_cache_timeout = "3600s";
313 # Stops mail from poorly written software
314 strict_rfc821_envelopes = true;
315 #sympa_destination_recipient_limit = "1";
316 #sympabounce_destination_recipient_limit = "1";
317 # postconf(5) discourages to change this
318 #tls_high_cipherlist = "AES256-SHA";
319 #tls_random_bytes = "32";
320 # Must not be in a chroot
321 #tls_random_exchange_name = "$data_directory/prng_exch";
322 #tls_random_prng_update_period = "3600s";
323 #tls_random_reseed_period = "3600s";
324 # Use a non blocking source of randomness
325 tls_random_source = "dev:/dev/urandom";
328 #"hash:/etc/postfix/transport-dovecot"
329 #"hash:/etc/postfix/$mydomain/transport"
330 #"hash:/etc/dovecot/transport"
331 #"regexp:/etc/sympa/transport"
333 # Rejects immediately what $fallback_transport rejects
334 unverified_recipient_reject_code = "550";
335 # Do not specify virtual alias domain names in mydestination
336 # or relay_domains configuration parameters
338 # With a virtual alias domain, the Postfix SMTP server
339 # accepts mail for known-user@virtual-alias.domain, and
340 # rejects mail for unknown-user@virtual-alias.domain as
342 virtual_alias_domains = [];
343 virtual_alias_maps = [
344 #"hash:/etc/postfix/virtual_alias_maps"
345 "ldap:/etc/postfix/ldap-forward.cf"
346 "ldap:/etc/postfix/ldap-virtual_alias_maps.cf"
347 #"hash:/etc/postfix/virtual_alias-dovecot"
348 #"hash:/var/lib/postfix/conf/valias"
349 #"regexp:/etc/sympa/virtual_alias"
351 #virtual_uid_maps = "static:5000";
352 #virtual_gid_maps = "static:5000";
353 #virtual_mailbox_base = dovecot2.mailDir;
354 virtual_mailbox_domains = toFile "virtual_mailbox_domains" (unlines (attrNames dovecot2.domains));
355 #virtual_mailbox_maps = "hash:/etc/postfix/virtual_mailbox_maps";
356 virtual_transport = "lmtp:unix:private/dovecot-lmtp";
358 #submissionOptions = {
359 # smtpd_tls_security_level = "encrypt";
360 # smtpd_sasl_auth_enable = "yes";
361 # smtpd_sasl_type = "dovecot";
362 # smtpd_sasl_path = "private/auth";
363 # smtpd_sasl_security_options = "noanonymous";
364 # smtpd_sasl_local_domain = "$myhostname";
365 # smtpd_client_restrictions = "permit_sasl_authenticated,reject";
366 # smtpd_sender_login_maps = "hash:/etc/postfix/vaccounts";
367 # smtpd_sender_restrictions = "reject_sender_login_mismatch";
368 # smtpd_recipient_restrictions = "reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject";
369 # cleanup_service_name = "submission-header-cleanup";
372 #spfcheck unix - n n - 0 spawn
373 # user=policyd-spf argv=/usr/sbin/postfix-policyd-spf-perl
374 465 inet n - - - - smtpd
375 -o milter_macro_daemon_name=ORIGINATING
376 -o smtpd_client_restrictions=permit_sasl_authenticated,reject
377 -o smtpd_recipient_restrictions=reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject
378 -o smtpd_sasl_auth_enable=yes
379 -o smtpd_sasl_local_domain=$myhostname
380 -o smtpd_sasl_path=private/auth
381 -o smtpd_sasl_security_options=noanonymous
382 -o smtpd_sasl_type=dovecot
383 -o smtpd_tls_ask_ccert=no
384 -o smtpd_tls_auth_only=yes
385 -o smtpd_tls_ccert_verifydepth=0
386 -o smtpd_tls_loglevel=1
387 -o smtpd_tls_req_ccert=no
388 -o smtpd_tls_security_level=encrypt
389 -o smtpd_tls_wrappermode=yes
390 # -o smtpd_sender_restrictions=reject_sender_login_mismatch
391 # -o smtpd_sender_login_maps=hash:/etc/postfix/vaccounts
392 # -o cleanup_service_name=submission-header-cleanup
393 submission-header-cleanup unix n - n - 0 cleanup
394 -o header_checks=pcre:${submissionHeaderCleanupRules}
395 #spfcheck unix - n n - 0 spawn
396 # user=policyd-spf argv=/usr/bin/postfix-policyd-spf-perl
397 #uucp unix - n n - - pipe
398 # flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
399 #smtp inet n - - - - smtpd
400 # -o cleanup_service_name=pre-cleanup
401 # -o content_filter=amavis:[127.0.0.1]:10024
402 # -o smtpd_sender_restrictions=reject_unauth_pipelining,reject_non_fqdn_sender,permit
403 # -o receive_override_options=no_address_mappings
404 #amavis unix - - n - 2 lmtp
405 # -o lmtp_data_done_timeout=1200
406 # -o lmtp_send_xforward_command=yes
407 # -o lmtp_tls_note_starttls_offer=no
408 #127.0.0.1:10025 inet n - n - - smtpd
410 # -o local_header_rewrite_clients=
411 # -o local_recipient_maps=
412 # -o mynetworks=127.0.0.0/8
413 # -o receive_override_options=no_header_body_checks,no_milters,no_unknown_recipient_checks
414 # -o relay_recipient_maps=
415 # -o smtpd_client_connection_count_limit=0
416 # -o smtpd_client_connection_rate_limit=0
417 # -o smtpd_client_restrictions=permit_mynetworks,reject
418 # -o smtpd_data_restrictions=reject_unauth_pipelining
419 # -o smtpd_delay_reject=no
420 # -o smtpd_end_of_data_restrictions=
421 # -o smtpd_error_sleep_time=0
422 # -o smtpd_hard_error_limit=1000
423 # -o smtpd_helo_restrictions=
425 # -o smtpd_recipient_restrictions=permit_mynetworks,reject
426 # -o smtpd_restriction_classes=
427 # -o smtpd_sender_restrictions=
428 # -o smtpd_soft_error_limit=1001
429 # -o strict_rfc821_envelopes=yes
430 #submission inet n - - - - smtpd
431 # -o cleanup_service_name=pre-cleanup
432 # -o content_filter=amavis:[127.0.0.1]:10024
433 # -o milter_macro_daemon_name=ORIGINATING
434 # -o receive_override_options=no_address_mappings
435 # -o smtpd_sender_restrictions=permit_tls_clientcerts,reject
436 # -o smtpd_tls_ask_ccert=yes
437 # -o smtpd_tls_auth_only=yes
438 # -o smtpd_tls_ccert_verifydepth=2
439 # -o smtpd_tls_loglevel=1
440 # -o smtpd_tls_req_ccert=yes
441 # -o smtpd_tls_security_level=encrypt
442 #smtps inet n - - - - smtpd
443 # -o milter_macro_daemon_name=ORIGINATING
444 # -o smtpd_client_restrictions=permit_sasl_authenticated,reject
445 # -o smtpd_sasl_auth_enable=yes
446 # -o smtpd_tls_ask_ccert=yes
447 # -o smtpd_tls_auth_only=yes
448 # -o smtpd_tls_ccert_verifydepth=0
449 # -o smtpd_tls_loglevel=1
450 # -o smtpd_tls_req_ccert=no
451 # -o smtpd_tls_security_level=encrypt
452 # -o smtpd_tls_wrappermode=yes
453 #pickup fifo n - - 60 1 pickup
454 # -o cleanup_service_name=pre-cleanup
455 # -o content_filter=amavis:[127.0.0.1]:10024
456 #pre-cleanup unix n - - - 0 cleanup
457 # -o virtual_alias_maps=
458 #cleanup unix n - - - 0 cleanup
459 # -o mime_header_checks=
460 # -o nested_header_checks=
464 #sympa unix - n n - - pipe
465 # flags=R user=sympa argv=/usr/lib/sympa/bin/queue ''${recipient}
466 #sympabounce unix - n n - - pipe
467 # flags=R user=sympa argv=/usr/lib/sympa/bin/bouncequeue ''${recipient}
470 #noclue unix - n n - - pipe
471 # flags=q user=noclue argv=/usr/local/bin/noclue-delivery ${recipient} ${sender}