9 inherit (builtins) toString;
10 inherit (config) networking;
11 inherit (config.services) dovecot2 postfix;
13 stateDir = "/var/lib/dovecot";
15 learn-spam = pkgs.writeShellScriptBin "learn-spam.sh" ''
16 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_spam
18 learn-ham = pkgs.writeShellScriptBin "learn-ham.sh" ''
19 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_ham
22 dovecot-virtual = pkgs.buildEnv {
23 name = "dovecot-virtual";
24 pathsToLink = [ "/" ];
27 dovecot-virtual-recents
30 dovecot-virtual-all = pkgs.writeTextFile {
31 name = "dovecot-virtual-all";
32 destination = "/All/dovecot-virtual";
35 dovecot-virtual-recents = pkgs.writeTextFile {
36 name = "dovecot-virtual-recents";
37 destination = "/Recents/dovecot-virtual";
46 dovecot/sourcephile.fr.nix
47 dovecot/autogeree.net.nix
49 environment.systemPackages = [
50 pkgs.dovecot_pigeonhole
52 users.groups.acme.members = [ dovecot2.user ];
53 systemd.services.dovecot2 = {
60 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
61 install -D -d -m 0771 \
62 -o "${dovecot2.user}" \
63 -g "${dovecot2.group}" \
68 ln -fns -t ${stateDir}/virtual/ \
69 ${dovecot-virtual-all}/All \
70 ${dovecot-virtual-recents}/Recents
73 #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'";
74 # Dovecot2 does not work with environment.memoryAllocator.provider="scudo"
75 # https://wiki.dovecot.org/Design/Memory
76 # Scudo ERROR: CHECK failed at /build/compiler-rt-7.1.0.src/lib/scudo/../sanitizer_common/sanitizer_allocator_primary64.h:
77 # 644 ((beg)) == ((address_range.MapOrDie(beg, size))) (4398046511092, 4398046507008)
78 BindReadOnlyPaths = [ "/dev/null:/etc/ld-nix.so.preload" ];
79 StateDirectory = [ "dovecot/virtual" ];
82 #users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
83 networking.nftables.ruleset = ''
86 tcp dport imaps counter accept comment "dovecot: IMAPS"
87 #tcp dport 995 counter accept comment "dovecot: POP3S"
88 tcp dport sieve counter accept comment "dovecot: Sieve"
92 fileSystems."/var/lib/dovecot" = {
93 device = "rpool/var/mail";
96 services.sanoid.datasets."rpool/var/mail" = {
97 use_template = [ "snap" ];
100 services.dovecot2 = {
106 protocols = [ "sieve" ];
107 # If needed, may be overrided by userdb_mail= in passdb, or mail= in userdb
108 # Here INDEX and CONTROL are separated,
109 # it's not useful since there is no quota at the filesystem level
110 # it's just to let this possibility later on.
111 mailLocation = "sdbox:${stateDir}/home/%d/%n/mail:UTF-8:CONTROL=${stateDir}/control/%d/%n:INDEX=${stateDir}/index/%d/%n";
112 createMailUser = false;
115 sslServerCert = null;
132 "vnd.dovecot.environment"
137 pipeBins = builtins.map lib.getExe [
142 global = dovecot/sieve/global;
154 lda.enable = [ "sieve" ];
155 lmtp.enable = [ "sieve" ];
162 pop3.enable = [ "virtual" ];
170 log_timestamp = "%Y-%m-%d %H:%M:%S "
173 ssl_dh = <${dovecot/dh4096.pem}
174 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
175 ssl_prefer_server_ciphers = yes
176 ssl_cert = </var/lib/acme/${networking.domain}/fullchain.pem
177 ssl_key = </var/lib/acme/${networking.domain}/key.pem
178 #ssl_ca = <''${caPath}
179 #ssl_verify_client_cert = yes
180 # FIXME: change to TLSv1.3 when supported by K-9 Mail
181 ssl_min_protocol = TLSv1.2
184 mail_home = ${stateDir}/home/%d/%n
185 # Read multiple mails in parallel, improves performance
186 mail_prefetch_count = 20
188 # DOC: https://doc.dovecot.org/configuration_manual/authentication/authentication_mechanisms/
189 # DOC: https://ldapwiki.com/wiki/DIGEST-MD5
190 # The misunderstanding is the notion "password sent over the network in plain-text" is not secure,
191 # when in fact, storing the password in the directory server using a Salted SHA-2 hash
192 # and transmitting the clear text password over a secure connection
193 # is more secure than having the directory server store the password in clear text
194 # or in a reversible encryption scheme.
195 # Therefore, SASL with DIGEST-MD5 (or CRAM-MD5) should be avoided.
196 # Use doveadm pw -s SSHA
197 # or doveadm pw -s PBKDF2 -r 10000
198 # or slappasswd -o module-load=pw-pbkdf2 -h "{PBKDF2-SHA256}"
199 auth_mechanisms = plain login
200 auth_username_format = %Ln
202 user = ${dovecot2.user}
203 # auth_socket_path points to this userdb socket by default. It's typically
204 # used by dovecot-lda, doveadm, possibly imap process, etc.
205 # Users that have access to this socket are able to get a list
206 # of all usernames and get results of everyone's userdb lookups.
207 unix_listener auth-userdb {
208 user = ${dovecot2.user}
209 group = ${dovecot2.group}
212 unix_listener /var/lib/postfix/queue/private/auth {
213 user = ${postfix.user}
214 group = ${postfix.group}
218 service auth-worker {
219 user = ${dovecot2.user}
221 auth_cache_verify_password_with_worker = yes
222 # SEE: http://wiki2.dovecot.org/Quota/FS
223 # NOTE: postfix does not supply a client cert.
224 auth_ssl_require_client_cert = no
225 #auth_ssl_username_from_cert = yes
226 # NOTE: lowercase the username, help with LDAP?
227 auth_username_format = %Lu
228 # NOTE: sync with LDAP's data.
229 first_valid_uid = 1000
230 disable_plaintext_auth = yes
234 # A different path than passdb's args enables non-blocking LDAP requests
238 paths = [ ./dovecot ];
245 # No dirty syncs for maildir: locations which may be used directly by neomutt
246 maildir_very_dirty_syncs = no
258 # NOTE: always listed in the LIST command.
260 # NOTE: how to access the other users' mailboxes.
261 # NOTE: %var expands to the logged in user's variable, while
262 # %%var expands to the other users' variables.
263 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
264 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
265 prefix = Partages+%%n+
275 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
279 acl = vfile:/etc/dovecot/acl/global
281 # NOTE: to let users LIST mailboxes shared by other users,
282 # Dovecot needs a shared mailbox dictionary.
283 # FIXME: %d not working with userdb ldap
284 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
287 # Default VSZ (virtual memory size) limit for service processes. This is mainly
288 # intended to catch and kill processes that leak memory before they eat up everything.
289 # Increased for fts_xapian.
290 #default_vsz_limit = 1G
292 # # WARNING: doveadm fts rescan is buggy, it will delete the index:
293 # # https://dovecot.org/pipermail/dovecot/2019-February/114709.html
294 # # If need be, use rather: doveadm index -u "*@sourcephile.fr" -q '*'
295 # plugin = fts fts_xapian
297 # fts_autoindex = yes
298 # fts_autoindex_exclude = \Junk
299 # fts_autoindex_exclude2 = \Trash
301 # # 2 and 20 are the NGram values for header fields, which means the
302 # # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
303 # # Full words are also added by default.
304 # fts_xapian = partial=2 full=20 attachments=1 verbose=0
305 # fts_languages = en fr
306 # #fts_dovecot_fs = posix:prefix=%h/fts/
310 quota = maildir:User quota
311 quota_rule = *:storage=256M
312 quota_rule2 = Trash:storage=+64M
313 quota_max_mail_size = 20M
314 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
315 quota_warning = storage=95%% quota-warning 95 %u
316 quota_warning2 = storage=80%% quota-warning 80 %u
317 quota_warning3 = -storage=100%% quota-warning below %u
321 hostname = ${networking.domain}
322 postmaster_address = root+dovecot+lda@${networking.domain}
323 syslog_facility = mail
325 lda_mailbox_autocreate = yes
326 lda_mailbox_autosubscribe = yes
329 postmaster_address = root+dovecot+lmtp@${networking.domain}
332 process_min_avail = ${toString host.CPUs}
333 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
334 user = ${postfix.user}
335 group = ${postfix.group}
340 # Let extension.sieve do the handling of the detail
341 #lmtp_save_to_detail_mailbox = yes
342 recipient_delimiter = ${postfix.recipientDelimiter}
346 # Most of the memory goes to mmap()ing files.
347 # You may need to increase this limit if you have huge mailboxes.
352 #inet_listener imap {
353 # address = 127.0.0.1
357 inet_listener imaps {
362 # Store METADATA information within user's HOME directory
363 mail_attribute_dict = file:%h/dovecot-attributes
364 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
365 imapc_features = $imapc_features fetch-headers
367 #mail_max_userip_connections = 10
370 # DOC: https://wiki.dovecot.org/MailboxSettings
371 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
372 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
373 imap_capability = +SPECIAL-USE
378 special_use = \Archive
382 special_use = \Drafts
384 mailbox "Brouillons" {
386 special_use = \Drafts
401 mailbox "Pourriels" {
413 mailbox "Sent Items" {
417 mailbox "Sent Messages" {
430 mailbox "Corbeille" {
438 comment = All messages from all mailboxes
439 # This flag is confused with \Archives by K9-Mail
444 comment = All messages from all mailboxes, arrived in the past 48h
449 sieve = file:~/sieve;active=~/active.sieve
450 sieve_default = file:${stateDir}/sieve/global/default.sieve
451 sieve_default_name = main
452 sieve_max_script_size = 1M
453 sieve_quota_max_scripts = 0
454 sieve_quota_max_storage = 10M
455 #sieve_spamtest_max_value = 10
456 #sieve_spamtest_status_header = X-Spam-Score
457 #sieve_spamtest_status_type = strlen
458 #sieve_user_log = ~/active.sieve.log
459 #sieve_trace_dir = ~/sieve-log
460 #sieve_trace_level = matching
461 # Enables support for user Sieve scripts in IMAP
462 #imapsieve_url = sieve://mail.${networking.domain}:4190
464 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
465 imapsieve_mailbox1_name = *
466 imapsieve_mailbox1_causes = FLAG
467 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
469 # From elsewhere to Junk folder
470 imapsieve_mailbox2_name = Pourriel
471 imapsieve_mailbox2_causes = COPY APPEND
472 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
474 # From Junk folder to elsewhere
475 imapsieve_mailbox3_name = *
476 imapsieve_mailbox3_from = Pourriel
477 imapsieve_mailbox3_causes = COPY
478 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
481 # Maximum number of ManageSieve connections allowed for a user from each IP address.
482 # NOTE: The username is compared case-sensitively.
483 mail_max_userip_connections = 10
485 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
486 # the IMPLEMENTATION capability that the dovecot reports to clients.
487 # For example: 'Cyrus timesieved v2.2.13'
488 managesieve_implementation_string = Dovecot Pigeonhole
490 # The maximum number of compile errors that are returned to the client upon script
491 # upload or script verification.
492 managesieve_max_compile_errors = 5
494 #mail_max_userip_connections = 10
495 #managesieve_implementation_string = Dovecot Pigeonhole
496 managesieve_max_compile_errors = 5
497 #managesieve_max_line_length = 65536
498 #managesieve_notify_capability = mailto
499 #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
501 service managesieve-login {
502 inet_listener sieve {
507 # Number of connections to handle before starting a new process. Typically
508 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
509 # is faster. <doc/wiki/LoginProcess.txt>
512 # Number of processes to always keep waiting for more connections.
513 process_min_avail = 0
515 # If you set service_count=0, you probably need to grow this.
519 service quota-warning {
520 executable = script ${pkgs.writeShellScript "quota-warning" ''
524 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
525 "plugin/quota=maildir:User quota:noenforcing"
526 From: root+docevot@${networking.domain}
527 Subject: [WARNING] your mailbox is now $PERCENT% full.
529 Please remove some mails to make room for new ones.
532 # use some unprivileged user for executing the quota warnings
533 user = ${dovecot2.user}
534 unix_listener quota-warning {
539 #mail_max_userip_connections = 10
540 # Virtual namespace for the virtual INBOX.
541 # Use a global directory for dovecot-virtual files.
545 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
548 pop3_client_workarounds =
549 pop3_fast_size_lookups = yes
550 pop3_lock_session = yes
551 pop3_no_flag_updates = yes
552 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
553 pop3_uidl_format = %g
559 inet_listener pop3s {