1 { pkgs, lib, config, system, ... }:
3 inherit (builtins) toString toFile;
4 inherit (builtins.extraBuiltins) pass;
6 inherit (pkgs.lib) loadFile unlines unlinesAttrs unlinesValues unwords;
7 inherit (config) networking;
8 inherit (config.services) dovecot2 postfix openldap;
10 # NOTE: nixpkgs' dovecot2.stateDir is currently not exported
11 stateDir = "/var/lib/dovecot";
13 sieve_pipe_bin_dir = pkgs.buildEnv {
14 name = "sieve_pipe_bin_dir";
15 pathsToLink = [ "/bin" ];
21 learn-spam = pkgs.writeShellScriptBin "learn-spam.sh" ''
22 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_spam
24 learn-ham = pkgs.writeShellScriptBin "learn-ham.sh" ''
25 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_ham
28 dovecot-virtual = pkgs.buildEnv {
29 name = "dovecot-virtual";
30 pathsToLink = [ "/" ];
33 dovecot-virtual-recents
36 dovecot-virtual-all = pkgs.writeTextFile {
37 name = "dovecot-virtual-all";
38 destination = "/All/dovecot-virtual";
44 dovecot-virtual-recents = pkgs.writeTextFile {
45 name = "dovecot-virtual-recents";
46 destination = "/Recents/dovecot-virtual";
52 dovecot-virtual-pop3 = pkgs.writeTextFile {
53 name = "dovecot-virtual-pop3";
54 destination = "/pop3/INBOX/dovecot-virtual";
64 dovecot/autoconfig.nix
66 systemd.services.dovecot2 = {
70 "dovecot.${networking.domainBase}.key.pem-key.service"
74 "dovecot.${networking.domainBase}.key.pem" = {
75 text = pass "x509/${networking.domainBase}/key.pem";
78 destDir = "/run/keys/";
79 permissions = "0400"; # WARNING: not enforced when deployment.storeKeysOnMachine = true
82 #users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
86 pkgs.dovecot_pigeonhole
87 pkgs.dovecot_fts_xapian
93 protocols = [ "sieve" ];
94 mailLocation = "sdbox:${stateDir}/mail/%d/%n/mail.d:UTF-8:CONTROL=${stateDir}/control/%d/%n:INDEX=${stateDir}/index/%d/%n";
95 createMailUser = false;
100 global = dovecot/sieve/global;
107 log_timestamp = "%Y-%m-%d %H:%M:%S "
110 ssl_dh = <${../../../sec/openssl/dh.pem}
111 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
112 ssl_cert = <${loadFile (../../../sec + "/openssl/${networking.domainBase}/cert.self-signed.pem")}
113 ssl_key = </run/keys/dovecot.${networking.domainBase}.key.pem
114 #ssl_ca = <''${caPath}
115 #ssl_verify_client_cert = yes
118 # If needed, may be overrided by userdb_mail
119 mail_home = ${stateDir}/mail/%d/%n
120 # Read multiple mails in parallel, improves performance
121 mail_prefetch_count = 20
122 # Default VSZ (virtual memory size) limit for service processes. This is mainly
123 # intended to catch and kill processes that leak memory before they eat up everything.
124 # Increased for fts_xapian.
125 default_vsz_limit = 1G
129 args = ${loadFile (dovecot/ldap + "/${networking.domainBase}.conf")}
130 default_fields = userdb_mail_access_groups=${networking.domainBase}
137 # NOTE: this userdb is only used by lda.
139 args = ${loadFile (dovecot/ldap + "/${networking.domainBase}.conf")}
140 default_fields = mail_access_groups=${networking.domainBase}
145 # FIXME: may be user=dovecot-auth with LDAP?
147 unix_listener auth-userdb {
148 user = ${dovecot2.user}
149 group = ${dovecot2.group}
152 unix_listener /var/lib/postfix/queue/private/auth {
153 user = ${postfix.user}
154 group = ${postfix.group}
158 auth_cache_verify_password_with_worker = yes
159 # NOTE: if needed, may be overrided by userdb_mail
160 # NOTE: INDEX and CONTROL are on a partition without quota, as explain in the doc.
161 # SEE: http://wiki2.dovecot.org/Quota/FS
162 auth_mechanisms = plain login
163 # NOTE: postfix does not supply a client cert.
164 auth_ssl_require_client_cert = no
165 #auth_ssl_username_from_cert = yes
166 # NOTE: lowercase the username, help with LDAP?
167 auth_username_format = %Lu
168 # NOTE: sync with LDAP's data.
169 first_valid_uid = 1000
170 disable_plaintext_auth = yes
173 # driver = passwd-file
174 # args = scheme=crypt username_format=%n ${stateDir}/passwd/%d
177 # # NOTE: this userdb is only used by LDA.
178 # driver = passwd-file
179 # args = username_format=%n ${stateDir}/passwd/%d
180 # #default_fields = home=${stateDir}/mail/%d/%n
184 # No dirty syncs for maildir: locations which may be used directly by neomutt
185 maildir_very_dirty_syncs = no
197 # NOTE: always listed in the LIST command.
199 # NOTE: how to access the other users' mailboxes.
200 # NOTE: %var expands to the logged in user's variable, while
201 # %%var expands to the other users' variables.
202 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
203 location = sdbox:${stateDir}/mail/%%d/%%n/${stateDir}/mail.d:UTF-8:CONTROL=${stateDir}/control/%%d/%%n/Shared:INDEX=${stateDir}/index/%%d/%%n/Shared:INDEXPVT=${stateDir}/index/%d/%n/Shared/%%n
204 prefix = Partages+%%n+
208 mail_plugins = $mail_plugins virtual
215 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
218 mail_plugins = $mail_plugins acl
220 acl = vfile:/etc/dovecot/acl/global.d
222 # NOTE: to let users LIST mailboxes shared by other users,
223 # Dovecot needs a shared mailbox dictionary.
224 # FIXME: %d not working with userdb ldap
225 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
228 mail_plugins = $mail_plugins fts fts_xapian
230 plugin = fts fts_xapian
233 fts_autoindex_exclude = \Junk
234 fts_autoindex_exclude2 = \Trash
236 # 2 and 20 are the NGram values for header fields, which means the
237 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
238 # Full words are also added by default.
239 fts_xapian = partial=2 full=20 verbose=0
242 mail_plugins = $mail_plugins quota
244 quota = maildir:User quota
245 quota_rule = *:storage=256M
246 quota_rule2 = Trash:storage=+64M
247 quota_max_mail_size = 20M
248 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
249 quota_warning = storage=95%% quota-warning 95 %u
250 quota_warning2 = storage=80%% quota-warning 80 %u
251 quota_warning3 = -storage=100%% quota-warning below %u
255 auth_socket_path = /var/run/dovecot/auth-userdb
256 hostname = ${networking.domain}
259 mail_plugins = $mail_plugins sieve
260 postmaster_address = postmaster+dovecot+lda@${networking.domain}
261 syslog_facility = mail
263 lda_mailbox_autocreate = yes
264 lda_mailbox_autosubscribe = yes
267 #info_log_path = /tmp/dovecot-lmtp.log
268 mail_plugins = $mail_plugins sieve
269 postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
272 #executable = lmtp -L
273 process_min_avail = ${toString config.nix.maxJobs}
274 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
275 user = ${postfix.user}
276 group = ${postfix.group}
282 # Let extension.sieve do the handling of the detail
283 #lmtp_save_to_detail_mailbox = yes
284 recipient_delimiter = ${postfix.recipientDelimiter}
288 # Most of the memory goes to mmap()ing files.
289 # You may need to increase this limit if you have huge mailboxes.
294 #inet_listener imap {
295 # address = 127.0.0.1
299 inet_listener imaps {
304 # Store METADATA information within user's HOME directory
305 mail_attribute_dict = file:%h/dovecot-attributes
306 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
307 imapc_features = $imapc_features fetch-headers
309 #mail_max_userip_connections = 10
310 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve virtual
313 # DOC: https://wiki.dovecot.org/MailboxSettings
314 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
315 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
316 imap_capability = +SPECIAL-USE
321 special_use = \Archive
325 special_use = \Drafts
327 mailbox "Brouillons" {
329 special_use = \Drafts
344 mailbox "Pourriels" {
356 mailbox "Sent Items" {
360 mailbox "Sent Messages" {
373 mailbox "Corbeille" {
381 comment = All messages from all mailboxes
386 comment = All messages from all mailboxes arrived in the past 48h
391 #sieve_default = file:${stateDir}/mail/%u/default.sieve
392 #sieve_default_name = default
393 sieve_plugins = sieve_imapsieve sieve_extprograms
394 sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.environment
395 #sieve_extensions = +editheader
396 sieve = file:~/sieve.d;active=~/sieve
397 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
398 sieve_max_script_size = 1M
399 sieve_quota_max_scripts = 0
400 sieve_quota_max_storage = 10M
401 #sieve_spamtest_max_value = 10
402 #sieve_spamtest_status_header = X-Spam-Score
403 #sieve_spamtest_status_type = strlen
404 sieve_user_log = /var/log/dovecot/%d/sieve.%n.log
405 # Enables support for user Sieve scripts in IMAP
406 #imapsieve_url = sieve://mail.${networking.domain}:4190
408 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
409 imapsieve_mailbox1_name = *
410 imapsieve_mailbox1_causes = FLAG
411 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
413 # From elsewhere to Junk folder
414 imapsieve_mailbox2_name = Pourriel
415 imapsieve_mailbox2_causes = COPY APPEND
416 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
418 # From Junk folder to elsewhere
419 imapsieve_mailbox3_name = *
420 imapsieve_mailbox3_from = Pourriel
421 imapsieve_mailbox3_causes = COPY
422 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
425 # Maximum number of ManageSieve connections allowed for a user from each IP address.
426 # NOTE: The username is compared case-sensitively.
427 mail_max_userip_connections = 10
429 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
430 # the IMPLEMENTATION capability that the dovecot reports to clients.
431 # For example: 'Cyrus timsieved v2.2.13'
432 managesieve_implementation_string = Dovecot Pigeonhole
434 # The maximum number of compile errors that are returned to the client upon script
435 # upload or script verification.
436 managesieve_max_compile_errors = 5
438 #mail_max_userip_connections = 10
439 #managesieve_implementation_string = Dovecot Pigeonhole
440 managesieve_max_compile_errors = 5
441 #managesieve_max_line_length = 65536
442 #managesieve_notify_capability = mailto
443 #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
445 service managesieve-login {
446 inet_listener sieve {
451 # Number of connections to handle before starting a new process. Typically
452 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
453 # is faster. <doc/wiki/LoginProcess.txt>
456 # Number of processes to always keep waiting for more connections.
457 process_min_avail = 0
459 # If you set service_count=0, you probably need to grow this.
463 service quota-warning {
464 executable = script ${
465 pkgs.writeScript "quota-warning" ''
469 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
470 "plugin/quota=maildir:User quota:noenforcing"
471 From: postmaster@${networking.domain}
472 Subject: [WARNING] your mailbox is now $PERCENT% full.
474 Please remove some mails to make room for new ones.
478 # use some unprivileged user for executing the quota warnings
479 user = ${dovecot2.user}
480 unix_listener quota-warning {
485 mail_plugins = $mail_plugins virtual
486 #mail_max_userip_connections = 10
487 # Virtual namespace for the virtual INBOX.
488 # Use a global directory for dovecot-virtual files.
492 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
495 pop3_client_workarounds =
496 pop3_fast_size_lookups = yes
497 pop3_lock_session = yes
498 pop3_no_flag_updates = yes
499 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
500 pop3_uidl_format = %g
506 inet_listener pop3s {
512 #${lib.concatMapStringsSep "\n"
514 # local_name mail.${dom} {
515 # #ssl_ca = <''${caPath}
516 # ssl_cert = <${x509.cert dom}
517 # ssl_key = <${x509.key dom}
523 #services.postfix.mapFiles."transport-dovecot" =
524 # toFile "transport-dovecot"
526 # (lib.mapAttrsToList
527 # (dom: {...}: "${transportSubDomain}.${dom} lmtp:unix:private/dovecot-lmtp")
528 # dovecot2.domains));