1 { inputs, pkgs, lib, config, system, host, ... }:
3 inherit (builtins) toString toFile readFile;
5 inherit (pkgs.lib) loadFile unlines unlinesAttrs unlinesValues unwords;
6 inherit (config) networking;
7 inherit (config.services) dovecot2 postfix openldap;
9 # NOTE: nixpkgs' dovecot2.stateDir is currently not exported
10 stateDir = "/var/lib/dovecot";
12 sieve_pipe_bin_dir = pkgs.buildEnv {
13 name = "sieve_pipe_bin_dir";
14 pathsToLink = [ "/bin" ];
20 learn-spam = pkgs.writeShellScriptBin "learn-spam.sh" ''
21 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_spam
23 learn-ham = pkgs.writeShellScriptBin "learn-ham.sh" ''
24 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_ham
27 dovecot-virtual = pkgs.buildEnv {
28 name = "dovecot-virtual";
29 pathsToLink = [ "/" ];
32 dovecot-virtual-recents
35 dovecot-virtual-all = pkgs.writeTextFile {
36 name = "dovecot-virtual-all";
37 destination = "/All/dovecot-virtual";
43 dovecot-virtual-recents = pkgs.writeTextFile {
44 name = "dovecot-virtual-recents";
45 destination = "/Recents/dovecot-virtual";
51 dovecot-virtual-pop3 = pkgs.writeTextFile {
52 name = "dovecot-virtual-pop3";
53 destination = "/pop3/INBOX/dovecot-virtual";
63 dovecot/sourcephile.fr.nix
64 dovecot/autogeree.net.nix
66 users.groups.acme.members = [ dovecot2.user ];
67 systemd.services.dovecot2 = {
74 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
75 install -D -d -m 0771 \
76 -o "${dovecot2.user}" \
77 -g "${dovecot2.group}" \
82 ln -fns -t ${stateDir}/virtual/ \
83 ${dovecot-virtual-all}/All \
84 ${dovecot-virtual-recents}/Recents
87 #ExecStart = lib.mkForce "${pkgs.utillinux}/bin/setarch x86_64 --addr-no-randomize /bin/sh -c 'LD_PRELOAD=${pkgs.gcc-unwrapped.lib}/lib/libasan.so ${pkgs.dovecot}/sbin/dovecot -F'";
88 # Dovecot2 does not work with environment.memoryAllocator.provider="scudo"
89 # https://wiki.dovecot.org/Design/Memory
90 # Scudo ERROR: CHECK failed at /build/compiler-rt-7.1.0.src/lib/scudo/../sanitizer_common/sanitizer_allocator_primary64.h:
91 # 644 ((beg)) == ((address_range.MapOrDie(beg, size))) (4398046511092, 4398046507008)
92 BindReadOnlyPaths = [ "/dev/null:/etc/ld-nix.so.preload" ];
93 StateDirectory = ["dovecot/virtual"];
96 #users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
97 networking.nftables.ruleset = ''
98 add rule inet filter net2fw tcp dport 993 counter accept comment "IMAPS"
99 #add rule inet filter net2fw tcp dport 995 counter accept comment "POP3S"
100 add rule inet filter net2fw tcp dport 4190 counter accept comment "Sieve"
102 fileSystems."/var/lib/dovecot" = {
103 device = "rpool/var/mail";
106 services.dovecot2 = {
109 pkgs.dovecot_pigeonhole
110 pkgs.dovecot_fts_xapian
116 protocols = [ "sieve" ];
117 # If needed, may be overrided by userdb_mail= in passdb, or mail= in userdb
118 # Here INDEX and CONTROL are separated,
119 # it's not useful since there is no quota at the filesystem level
120 # it's just to let this possibility later on.
121 mailLocation = "sdbox:${stateDir}/home/%d/%n/mail:UTF-8:CONTROL=${stateDir}/control/%d/%n:INDEX=${stateDir}/index/%d/%n";
122 createMailUser = false;
125 sslServerCert = null;
127 global = dovecot/sieve/global;
134 log_timestamp = "%Y-%m-%d %H:%M:%S "
137 ssl_dh = <${inputs.secrets + "/openssl/dh.pem"}
138 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
139 ssl_prefer_server_ciphers = yes
140 ssl_cert = </var/lib/acme/${networking.domain}/fullchain.pem
141 ssl_key = </var/lib/acme/${networking.domain}/key.pem
142 #ssl_ca = <''${caPath}
143 #ssl_verify_client_cert = yes
144 # FIXME: change to TLSv1.3 when supported by K-9 Mail
145 ssl_min_protocol = TLSv1.2
148 mail_home = ${stateDir}/home/%d/%n
149 # Read multiple mails in parallel, improves performance
150 mail_prefetch_count = 20
151 # Default VSZ (virtual memory size) limit for service processes. This is mainly
152 # intended to catch and kill processes that leak memory before they eat up everything.
153 # Increased for fts_xapian.
154 default_vsz_limit = 1G
156 # DOC: https://doc.dovecot.org/configuration_manual/authentication/authentication_mechanisms/
157 # DOC: https://ldapwiki.com/wiki/DIGEST-MD5
158 # The misunderstanding is the notion "password sent over the network in plain-text" is not secure,
159 # when in fact, storing the password in the directory server using a Salted SHA-2 hash
160 # and transmitting the clear text password over a secure connection
161 # is more secure than having the directory server store the password in clear text
162 # or in a reversible encryption scheme.
163 # Therefore, SASL with DIGEST-MD5 (or CRAM-MD5) should be avoided.
164 # Use doveadm pw -s SSHA
165 # or doveadm pw -s PBKDF2 -r 10000
166 # or slappasswd -o module-load=pw-pbkdf2 -h "{PBKDF2-SHA256}"
167 auth_mechanisms = plain login
168 auth_username_format = %Ln
170 user = ${dovecot2.user}
171 # auth_socket_path points to this userdb socket by default. It's typically
172 # used by dovecot-lda, doveadm, possibly imap process, etc.
173 # Users that have access to this socket are able to get a list
174 # of all usernames and get results of everyone's userdb lookups.
175 unix_listener auth-userdb {
176 user = ${dovecot2.user}
177 group = ${dovecot2.group}
180 unix_listener /var/lib/postfix/queue/private/auth {
181 user = ${postfix.user}
182 group = ${postfix.group}
186 service auth-worker {
187 user = ${dovecot2.user}
189 auth_cache_verify_password_with_worker = yes
190 # SEE: http://wiki2.dovecot.org/Quota/FS
191 # NOTE: postfix does not supply a client cert.
192 auth_ssl_require_client_cert = no
193 #auth_ssl_username_from_cert = yes
194 # NOTE: lowercase the username, help with LDAP?
195 auth_username_format = %Lu
196 # NOTE: sync with LDAP's data.
197 first_valid_uid = 1000
198 disable_plaintext_auth = yes
202 # A different path than passdb's args enables non-blocking LDAP requests
203 args = ${pkgs.symlinkJoin {name="ldap"; paths=[./dovecot];}}/ldap.conf
208 # No dirty syncs for maildir: locations which may be used directly by neomutt
209 maildir_very_dirty_syncs = no
221 # NOTE: always listed in the LIST command.
223 # NOTE: how to access the other users' mailboxes.
224 # NOTE: %var expands to the logged in user's variable, while
225 # %%var expands to the other users' variables.
226 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
227 location = sdbox:${stateDir}/home/%%d/%%n/${stateDir}/mail:UTF-8:CONTROL=${stateDir}/control/%%d/%%n/shared:INDEX=${stateDir}/index/%%d/%%n/shared:INDEXPVT=${stateDir}/index/%d/%n/shared/%%n
228 prefix = Partages+%%n+
232 mail_plugins = $mail_plugins virtual
239 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
242 mail_plugins = $mail_plugins acl
244 acl = vfile:/etc/dovecot/acl/global
246 # NOTE: to let users LIST mailboxes shared by other users,
247 # Dovecot needs a shared mailbox dictionary.
248 # FIXME: %d not working with userdb ldap
249 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
252 mail_plugins = $mail_plugins fts fts_xapian
254 # WARNING: doveadm fts rescan is buggy, it will delete the index:
255 # https://dovecot.org/pipermail/dovecot/2019-February/114709.html
256 # If need be, use rather: doveadm index -u "*@sourcephile.fr" -q '*'
257 plugin = fts fts_xapian
260 fts_autoindex_exclude = \Junk
261 fts_autoindex_exclude2 = \Trash
263 # 2 and 20 are the NGram values for header fields, which means the
264 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
265 # Full words are also added by default.
266 fts_xapian = partial=2 full=20 attachments=1 verbose=0
267 fts_languages = en fr
268 #fts_dovecot_fs = posix:prefix=%h/fts/
271 mail_plugins = $mail_plugins quota
273 quota = maildir:User quota
274 quota_rule = *:storage=256M
275 quota_rule2 = Trash:storage=+64M
276 quota_max_mail_size = 20M
277 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
278 quota_warning = storage=95%% quota-warning 95 %u
279 quota_warning2 = storage=80%% quota-warning 80 %u
280 quota_warning3 = -storage=100%% quota-warning below %u
284 hostname = ${networking.domain}
285 mail_plugins = $mail_plugins sieve
286 postmaster_address = postmaster+dovecot+lda@${networking.domain}
287 syslog_facility = mail
289 lda_mailbox_autocreate = yes
290 lda_mailbox_autosubscribe = yes
293 mail_plugins = $mail_plugins sieve
294 postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
297 process_min_avail = ${toString host.CPUs}
298 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
299 user = ${postfix.user}
300 group = ${postfix.group}
305 # Let extension.sieve do the handling of the detail
306 #lmtp_save_to_detail_mailbox = yes
307 recipient_delimiter = ${postfix.recipientDelimiter}
311 # Most of the memory goes to mmap()ing files.
312 # You may need to increase this limit if you have huge mailboxes.
317 #inet_listener imap {
318 # address = 127.0.0.1
322 inet_listener imaps {
327 # Store METADATA information within user's HOME directory
328 mail_attribute_dict = file:%h/dovecot-attributes
329 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
330 imapc_features = $imapc_features fetch-headers
332 #mail_max_userip_connections = 10
333 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve virtual
336 # DOC: https://wiki.dovecot.org/MailboxSettings
337 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
338 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
339 imap_capability = +SPECIAL-USE
344 special_use = \Archive
348 special_use = \Drafts
350 mailbox "Brouillons" {
352 special_use = \Drafts
367 mailbox "Pourriels" {
379 mailbox "Sent Items" {
383 mailbox "Sent Messages" {
396 mailbox "Corbeille" {
404 comment = All messages from all mailboxes
405 # This flag is confused with \Archives by K9-Mail
410 comment = All messages from all mailboxes, arrived in the past 48h
415 sieve_plugins = sieve_imapsieve sieve_extprograms
416 sieve_global_extensions = +vnd.dovecot.environment +vnd.dovecot.pipe
417 #sieve_extensions = +editheader
418 sieve = file:~/sieve;active=~/active.sieve
419 sieve_default = file:${stateDir}/sieve/global/default.sieve
420 sieve_default_name = main
421 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
422 sieve_max_script_size = 1M
423 sieve_quota_max_scripts = 0
424 sieve_quota_max_storage = 10M
425 #sieve_spamtest_max_value = 10
426 #sieve_spamtest_status_header = X-Spam-Score
427 #sieve_spamtest_status_type = strlen
428 #sieve_user_log = ~/sieve.log
429 # Enables support for user Sieve scripts in IMAP
430 #imapsieve_url = sieve://mail.${networking.domain}:4190
432 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
433 imapsieve_mailbox1_name = *
434 imapsieve_mailbox1_causes = FLAG
435 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
437 # From elsewhere to Junk folder
438 imapsieve_mailbox2_name = Pourriel
439 imapsieve_mailbox2_causes = COPY APPEND
440 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
442 # From Junk folder to elsewhere
443 imapsieve_mailbox3_name = *
444 imapsieve_mailbox3_from = Pourriel
445 imapsieve_mailbox3_causes = COPY
446 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
449 # Maximum number of ManageSieve connections allowed for a user from each IP address.
450 # NOTE: The username is compared case-sensitively.
451 mail_max_userip_connections = 10
453 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
454 # the IMPLEMENTATION capability that the dovecot reports to clients.
455 # For example: 'Cyrus timesieved v2.2.13'
456 managesieve_implementation_string = Dovecot Pigeonhole
458 # The maximum number of compile errors that are returned to the client upon script
459 # upload or script verification.
460 managesieve_max_compile_errors = 5
462 #mail_max_userip_connections = 10
463 #managesieve_implementation_string = Dovecot Pigeonhole
464 managesieve_max_compile_errors = 5
465 #managesieve_max_line_length = 65536
466 #managesieve_notify_capability = mailto
467 #managesieve_sieve_capability = fileinto reject envelope encoded-character vacation subaddress comparator-i;ascii-numeric relational regex imap4flags copy include variables body enotify environment mailbox date ihave
469 service managesieve-login {
470 inet_listener sieve {
475 # Number of connections to handle before starting a new process. Typically
476 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
477 # is faster. <doc/wiki/LoginProcess.txt>
480 # Number of processes to always keep waiting for more connections.
481 process_min_avail = 0
483 # If you set service_count=0, you probably need to grow this.
487 service quota-warning {
488 executable = script ${
489 pkgs.writeShellScript "quota-warning" ''
493 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
494 "plugin/quota=maildir:User quota:noenforcing"
495 From: postmaster@${networking.domain}
496 Subject: [WARNING] your mailbox is now $PERCENT% full.
498 Please remove some mails to make room for new ones.
502 # use some unprivileged user for executing the quota warnings
503 user = ${dovecot2.user}
504 unix_listener quota-warning {
509 mail_plugins = $mail_plugins virtual
510 #mail_max_userip_connections = 10
511 # Virtual namespace for the virtual INBOX.
512 # Use a global directory for dovecot-virtual files.
516 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
519 pop3_client_workarounds =
520 pop3_fast_size_lookups = yes
521 pop3_lock_session = yes
522 pop3_no_flag_updates = yes
523 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
524 pop3_uidl_format = %g
530 inet_listener pop3s {