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
136 mail_home = ${stateDir}/home/%d/%n
137 # Read multiple mails in parallel, improves performance
138 mail_prefetch_count = 20
139 # Default VSZ (virtual memory size) limit for service processes. This is mainly
140 # intended to catch and kill processes that leak memory before they eat up everything.
141 # Increased for fts_xapian.
142 default_vsz_limit = 1G
144 # DOC: https://doc.dovecot.org/configuration_manual/authentication/authentication_mechanisms/
145 # DOC: https://ldapwiki.com/wiki/DIGEST-MD5
146 # The misunderstanding is the notion "password sent over the network in plain-text" is not secure,
147 # when in fact, storing the password in the directory server using a Salted SHA-2 hash
148 # and transmitting the clear text password over a secure connection
149 # is more secure than having the directory server store the password in clear text
150 # or in a reversible encryption scheme.
151 # Therefore, SASL with DIGEST-MD5 (or CRAM-MD5) should be avoided.
152 # Use doveadm pw -s SSHA
153 # or doveadm pw -s PBKDF2 -r 10000
154 # or slappasswd -o module-load=pw-pbkdf2 -h "{PBKDF2-SHA256}"
155 auth_mechanisms = plain login
156 auth_username_format = %Ln
158 user = ${dovecot2.user}
159 # auth_socket_path points to this userdb socket by default. It's typically
160 # used by dovecot-lda, doveadm, possibly imap process, etc.
161 # Users that have access to this socket are able to get a list
162 # of all usernames and get results of everyone's userdb lookups.
163 unix_listener auth-userdb {
164 user = ${dovecot2.user}
165 group = ${dovecot2.group}
168 unix_listener /var/lib/postfix/queue/private/auth {
169 user = ${postfix.user}
170 group = ${postfix.group}
174 service auth-worker {
175 user = ${dovecot2.user}
177 auth_cache_verify_password_with_worker = yes
178 # SEE: http://wiki2.dovecot.org/Quota/FS
179 # NOTE: postfix does not supply a client cert.
180 auth_ssl_require_client_cert = no
181 #auth_ssl_username_from_cert = yes
182 # NOTE: lowercase the username, help with LDAP?
183 auth_username_format = %Lu
184 # NOTE: sync with LDAP's data.
185 first_valid_uid = 1000
186 disable_plaintext_auth = yes
190 # A different path than passdb's args enables non-blocking LDAP requests
191 args = ${pkgs.symlinkJoin {name="ldap"; paths=[./dovecot];}}/ldap.conf
196 # No dirty syncs for maildir: locations which may be used directly by neomutt
197 maildir_very_dirty_syncs = no
209 # NOTE: always listed in the LIST command.
211 # NOTE: how to access the other users' mailboxes.
212 # NOTE: %var expands to the logged in user's variable, while
213 # %%var expands to the other users' variables.
214 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
215 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
216 prefix = Partages+%%n+
220 mail_plugins = $mail_plugins virtual
227 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
230 mail_plugins = $mail_plugins acl
232 acl = vfile:/etc/dovecot/acl/global
234 # NOTE: to let users LIST mailboxes shared by other users,
235 # Dovecot needs a shared mailbox dictionary.
236 # FIXME: %d not working with userdb ldap
237 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
240 mail_plugins = $mail_plugins fts fts_xapian
242 # WARNING: doveadm fts rescan is buggy, it will delete the index:
243 # https://dovecot.org/pipermail/dovecot/2019-February/114709.html
244 # If need be, use rather: doveadm index -u "*@sourcephile.fr" -q '*'
245 plugin = fts fts_xapian
248 fts_autoindex_exclude = \Junk
249 fts_autoindex_exclude2 = \Trash
251 # 2 and 20 are the NGram values for header fields, which means the
252 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
253 # Full words are also added by default.
254 fts_xapian = partial=2 full=20 attachments=1 verbose=0
255 fts_languages = en fr
256 #fts_dovecot_fs = posix:prefix=%h/fts/
259 mail_plugins = $mail_plugins quota
261 quota = maildir:User quota
262 quota_rule = *:storage=256M
263 quota_rule2 = Trash:storage=+64M
264 quota_max_mail_size = 20M
265 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
266 quota_warning = storage=95%% quota-warning 95 %u
267 quota_warning2 = storage=80%% quota-warning 80 %u
268 quota_warning3 = -storage=100%% quota-warning below %u
272 hostname = ${networking.domain}
273 mail_plugins = $mail_plugins sieve
274 postmaster_address = postmaster+dovecot+lda@${networking.domain}
275 syslog_facility = mail
277 lda_mailbox_autocreate = yes
278 lda_mailbox_autosubscribe = yes
281 mail_plugins = $mail_plugins sieve
282 postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
285 process_min_avail = ${toString config.nix.maxJobs}
286 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
287 user = ${postfix.user}
288 group = ${postfix.group}
293 # Let extension.sieve do the handling of the detail
294 #lmtp_save_to_detail_mailbox = yes
295 recipient_delimiter = ${postfix.recipientDelimiter}
299 # Most of the memory goes to mmap()ing files.
300 # You may need to increase this limit if you have huge mailboxes.
305 #inet_listener imap {
306 # address = 127.0.0.1
310 inet_listener imaps {
315 # Store METADATA information within user's HOME directory
316 mail_attribute_dict = file:%h/dovecot-attributes
317 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
318 imapc_features = $imapc_features fetch-headers
320 #mail_max_userip_connections = 10
321 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve virtual
324 # DOC: https://wiki.dovecot.org/MailboxSettings
325 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
326 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
327 imap_capability = +SPECIAL-USE
332 special_use = \Archive
336 special_use = \Drafts
338 mailbox "Brouillons" {
340 special_use = \Drafts
355 mailbox "Pourriels" {
367 mailbox "Sent Items" {
371 mailbox "Sent Messages" {
384 mailbox "Corbeille" {
392 comment = All messages from all mailboxes
393 # This flag is confused with \Archives by K9-Mail
398 comment = All messages from all mailboxes, arrived in the past 48h
403 sieve_plugins = sieve_imapsieve sieve_extprograms
404 sieve_global_extensions = +vnd.dovecot.environment +vnd.dovecot.pipe
405 #sieve_extensions = +editheader
406 sieve = file:~/sieve;active=~/active.sieve
407 sieve_default = file:${stateDir}/sieve/global/default.sieve
408 sieve_default_name = main
409 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
410 sieve_max_script_size = 1M
411 sieve_quota_max_scripts = 0
412 sieve_quota_max_storage = 10M
413 #sieve_spamtest_max_value = 10
414 #sieve_spamtest_status_header = X-Spam-Score
415 #sieve_spamtest_status_type = strlen
416 #sieve_user_log = ~/sieve.log
417 # Enables support for user Sieve scripts in IMAP
418 #imapsieve_url = sieve://mail.${networking.domain}:4190
420 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
421 imapsieve_mailbox1_name = *
422 imapsieve_mailbox1_causes = FLAG
423 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
425 # From elsewhere to Junk folder
426 imapsieve_mailbox2_name = Pourriel
427 imapsieve_mailbox2_causes = COPY APPEND
428 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
430 # From Junk folder to elsewhere
431 imapsieve_mailbox3_name = *
432 imapsieve_mailbox3_from = Pourriel
433 imapsieve_mailbox3_causes = COPY
434 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
437 # Maximum number of ManageSieve connections allowed for a user from each IP address.
438 # NOTE: The username is compared case-sensitively.
439 mail_max_userip_connections = 10
441 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
442 # the IMPLEMENTATION capability that the dovecot reports to clients.
443 # For example: 'Cyrus timesieved v2.2.13'
444 managesieve_implementation_string = Dovecot Pigeonhole
446 # The maximum number of compile errors that are returned to the client upon script
447 # upload or script verification.
448 managesieve_max_compile_errors = 5
450 #mail_max_userip_connections = 10
451 #managesieve_implementation_string = Dovecot Pigeonhole
452 managesieve_max_compile_errors = 5
453 #managesieve_max_line_length = 65536
454 #managesieve_notify_capability = mailto
455 #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
457 service managesieve-login {
458 inet_listener sieve {
463 # Number of connections to handle before starting a new process. Typically
464 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
465 # is faster. <doc/wiki/LoginProcess.txt>
468 # Number of processes to always keep waiting for more connections.
469 process_min_avail = 0
471 # If you set service_count=0, you probably need to grow this.
475 service quota-warning {
476 executable = script ${
477 pkgs.writeShellScript "quota-warning" ''
481 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
482 "plugin/quota=maildir:User quota:noenforcing"
483 From: postmaster@${networking.domain}
484 Subject: [WARNING] your mailbox is now $PERCENT% full.
486 Please remove some mails to make room for new ones.
490 # use some unprivileged user for executing the quota warnings
491 user = ${dovecot2.user}
492 unix_listener quota-warning {
497 mail_plugins = $mail_plugins virtual
498 #mail_max_userip_connections = 10
499 # Virtual namespace for the virtual INBOX.
500 # Use a global directory for dovecot-virtual files.
504 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
507 pop3_client_workarounds =
508 pop3_fast_size_lookups = yes
509 pop3_lock_session = yes
510 pop3_no_flag_updates = yes
511 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
512 pop3_uidl_format = %g
518 inet_listener pop3s {