1 { inputs, pkgs, lib, config, system, ... }:
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 #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'";
83 # Dovecot2 does not work with environment.memoryAllocator.provider="scudo"
84 # https://wiki.dovecot.org/Design/Memory
85 # Scudo ERROR: CHECK failed at /build/compiler-rt-7.1.0.src/lib/scudo/../sanitizer_common/sanitizer_allocator_primary64.h:
86 # 644 ((beg)) == ((address_range.MapOrDie(beg, size))) (4398046511092, 4398046507008)
87 BindReadOnlyPaths = [ "/dev/null:/etc/ld-nix.so.preload" ];
90 #users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
91 networking.nftables.ruleset = ''
92 add rule inet filter net2fw tcp dport 993 counter accept comment "IMAPS"
93 #add rule inet filter net2fw tcp dport 995 counter accept comment "POP3S"
94 add rule inet filter net2fw tcp dport 4190 counter accept comment "Sieve"
99 pkgs.dovecot_pigeonhole
100 pkgs.dovecot_fts_xapian
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;
117 global = dovecot/sieve/global;
124 log_timestamp = "%Y-%m-%d %H:%M:%S "
127 ssl_dh = <${inputs.secrets + "/openssl/dh.pem"}
128 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
129 ssl_prefer_server_ciphers = yes
130 ssl_cert = </var/lib/acme/${networking.domain}/fullchain.pem
131 ssl_key = </var/lib/acme/${networking.domain}/key.pem
132 #ssl_ca = <''${caPath}
133 #ssl_verify_client_cert = yes
134 # FIXME: change to TLSv1.3 when supported by K-9 Mail
135 ssl_min_protocol = TLSv1.2
138 mail_home = ${stateDir}/home/%d/%n
139 # Read multiple mails in parallel, improves performance
140 mail_prefetch_count = 20
141 # Default VSZ (virtual memory size) limit for service processes. This is mainly
142 # intended to catch and kill processes that leak memory before they eat up everything.
143 # Increased for fts_xapian.
144 default_vsz_limit = 1G
146 # DOC: https://doc.dovecot.org/configuration_manual/authentication/authentication_mechanisms/
147 # DOC: https://ldapwiki.com/wiki/DIGEST-MD5
148 # The misunderstanding is the notion "password sent over the network in plain-text" is not secure,
149 # when in fact, storing the password in the directory server using a Salted SHA-2 hash
150 # and transmitting the clear text password over a secure connection
151 # is more secure than having the directory server store the password in clear text
152 # or in a reversible encryption scheme.
153 # Therefore, SASL with DIGEST-MD5 (or CRAM-MD5) should be avoided.
154 # Use doveadm pw -s SSHA
155 # or doveadm pw -s PBKDF2 -r 10000
156 # or slappasswd -o module-load=pw-pbkdf2 -h "{PBKDF2-SHA256}"
157 auth_mechanisms = plain login
158 auth_username_format = %Ln
160 user = ${dovecot2.user}
161 # auth_socket_path points to this userdb socket by default. It's typically
162 # used by dovecot-lda, doveadm, possibly imap process, etc.
163 # Users that have access to this socket are able to get a list
164 # of all usernames and get results of everyone's userdb lookups.
165 unix_listener auth-userdb {
166 user = ${dovecot2.user}
167 group = ${dovecot2.group}
170 unix_listener /var/lib/postfix/queue/private/auth {
171 user = ${postfix.user}
172 group = ${postfix.group}
176 service auth-worker {
177 user = ${dovecot2.user}
179 auth_cache_verify_password_with_worker = yes
180 # SEE: http://wiki2.dovecot.org/Quota/FS
181 # NOTE: postfix does not supply a client cert.
182 auth_ssl_require_client_cert = no
183 #auth_ssl_username_from_cert = yes
184 # NOTE: lowercase the username, help with LDAP?
185 auth_username_format = %Lu
186 # NOTE: sync with LDAP's data.
187 first_valid_uid = 1000
188 disable_plaintext_auth = yes
192 # A different path than passdb's args enables non-blocking LDAP requests
193 args = ${pkgs.symlinkJoin {name="ldap"; paths=[./dovecot];}}/ldap.conf
198 # No dirty syncs for maildir: locations which may be used directly by neomutt
199 maildir_very_dirty_syncs = no
211 # NOTE: always listed in the LIST command.
213 # NOTE: how to access the other users' mailboxes.
214 # NOTE: %var expands to the logged in user's variable, while
215 # %%var expands to the other users' variables.
216 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
217 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
218 prefix = Partages+%%n+
222 mail_plugins = $mail_plugins virtual
229 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
232 mail_plugins = $mail_plugins acl
234 acl = vfile:/etc/dovecot/acl/global
236 # NOTE: to let users LIST mailboxes shared by other users,
237 # Dovecot needs a shared mailbox dictionary.
238 # FIXME: %d not working with userdb ldap
239 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
242 mail_plugins = $mail_plugins fts fts_xapian
244 # WARNING: doveadm fts rescan is buggy, it will delete the index:
245 # https://dovecot.org/pipermail/dovecot/2019-February/114709.html
246 # If need be, use rather: doveadm index -u "*@sourcephile.fr" -q '*'
247 plugin = fts fts_xapian
250 fts_autoindex_exclude = \Junk
251 fts_autoindex_exclude2 = \Trash
253 # 2 and 20 are the NGram values for header fields, which means the
254 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
255 # Full words are also added by default.
256 fts_xapian = partial=2 full=20 attachments=1 verbose=0
257 fts_languages = en fr
258 #fts_dovecot_fs = posix:prefix=%h/fts/
261 mail_plugins = $mail_plugins quota
263 quota = maildir:User quota
264 quota_rule = *:storage=256M
265 quota_rule2 = Trash:storage=+64M
266 quota_max_mail_size = 20M
267 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
268 quota_warning = storage=95%% quota-warning 95 %u
269 quota_warning2 = storage=80%% quota-warning 80 %u
270 quota_warning3 = -storage=100%% quota-warning below %u
274 hostname = ${networking.domain}
275 mail_plugins = $mail_plugins sieve
276 postmaster_address = postmaster+dovecot+lda@${networking.domain}
277 syslog_facility = mail
279 lda_mailbox_autocreate = yes
280 lda_mailbox_autosubscribe = yes
283 mail_plugins = $mail_plugins sieve
284 postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
287 process_min_avail = ${toString config.nix.maxJobs}
288 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
289 user = ${postfix.user}
290 group = ${postfix.group}
295 # Let extension.sieve do the handling of the detail
296 #lmtp_save_to_detail_mailbox = yes
297 recipient_delimiter = ${postfix.recipientDelimiter}
301 # Most of the memory goes to mmap()ing files.
302 # You may need to increase this limit if you have huge mailboxes.
307 #inet_listener imap {
308 # address = 127.0.0.1
312 inet_listener imaps {
317 # Store METADATA information within user's HOME directory
318 mail_attribute_dict = file:%h/dovecot-attributes
319 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
320 imapc_features = $imapc_features fetch-headers
322 #mail_max_userip_connections = 10
323 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve virtual
326 # DOC: https://wiki.dovecot.org/MailboxSettings
327 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
328 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
329 imap_capability = +SPECIAL-USE
334 special_use = \Archive
338 special_use = \Drafts
340 mailbox "Brouillons" {
342 special_use = \Drafts
357 mailbox "Pourriels" {
369 mailbox "Sent Items" {
373 mailbox "Sent Messages" {
386 mailbox "Corbeille" {
394 comment = All messages from all mailboxes
395 # This flag is confused with \Archives by K9-Mail
400 comment = All messages from all mailboxes, arrived in the past 48h
405 sieve_plugins = sieve_imapsieve sieve_extprograms
406 sieve_global_extensions = +vnd.dovecot.environment +vnd.dovecot.pipe
407 #sieve_extensions = +editheader
408 sieve = file:~/sieve;active=~/active.sieve
409 sieve_default = file:${stateDir}/sieve/global/default.sieve
410 sieve_default_name = main
411 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
412 sieve_max_script_size = 1M
413 sieve_quota_max_scripts = 0
414 sieve_quota_max_storage = 10M
415 #sieve_spamtest_max_value = 10
416 #sieve_spamtest_status_header = X-Spam-Score
417 #sieve_spamtest_status_type = strlen
418 #sieve_user_log = ~/sieve.log
419 # Enables support for user Sieve scripts in IMAP
420 #imapsieve_url = sieve://mail.${networking.domain}:4190
422 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
423 imapsieve_mailbox1_name = *
424 imapsieve_mailbox1_causes = FLAG
425 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
427 # From elsewhere to Junk folder
428 imapsieve_mailbox2_name = Pourriel
429 imapsieve_mailbox2_causes = COPY APPEND
430 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
432 # From Junk folder to elsewhere
433 imapsieve_mailbox3_name = *
434 imapsieve_mailbox3_from = Pourriel
435 imapsieve_mailbox3_causes = COPY
436 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
439 # Maximum number of ManageSieve connections allowed for a user from each IP address.
440 # NOTE: The username is compared case-sensitively.
441 mail_max_userip_connections = 10
443 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
444 # the IMPLEMENTATION capability that the dovecot reports to clients.
445 # For example: 'Cyrus timesieved v2.2.13'
446 managesieve_implementation_string = Dovecot Pigeonhole
448 # The maximum number of compile errors that are returned to the client upon script
449 # upload or script verification.
450 managesieve_max_compile_errors = 5
452 #mail_max_userip_connections = 10
453 #managesieve_implementation_string = Dovecot Pigeonhole
454 managesieve_max_compile_errors = 5
455 #managesieve_max_line_length = 65536
456 #managesieve_notify_capability = mailto
457 #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
459 service managesieve-login {
460 inet_listener sieve {
465 # Number of connections to handle before starting a new process. Typically
466 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
467 # is faster. <doc/wiki/LoginProcess.txt>
470 # Number of processes to always keep waiting for more connections.
471 process_min_avail = 0
473 # If you set service_count=0, you probably need to grow this.
477 service quota-warning {
478 executable = script ${
479 pkgs.writeShellScript "quota-warning" ''
483 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
484 "plugin/quota=maildir:User quota:noenforcing"
485 From: postmaster@${networking.domain}
486 Subject: [WARNING] your mailbox is now $PERCENT% full.
488 Please remove some mails to make room for new ones.
492 # use some unprivileged user for executing the quota warnings
493 user = ${dovecot2.user}
494 unix_listener quota-warning {
499 mail_plugins = $mail_plugins virtual
500 #mail_max_userip_connections = 10
501 # Virtual namespace for the virtual INBOX.
502 # Use a global directory for dovecot-virtual files.
506 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
509 pop3_client_workarounds =
510 pop3_fast_size_lookups = yes
511 pop3_lock_session = yes
512 pop3_no_flag_updates = yes
513 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
514 pop3_uidl_format = %g
520 inet_listener pop3s {