1 { pkgs, lib, config, system, ... }:
2 let inherit (builtins) toString toFile;
3 inherit (builtins.extraBuiltins) pass;
5 inherit (pkgs.lib) loadFile unlines unlinesAttrs unlinesValues unwords;
6 inherit (config) networking;
7 inherit (config.services) dovecot2 postfix openldap;
8 when = x: y: if x == null then "" else y;
9 extSep = postfix.recipientDelimiter;
11 # NOTE: nixpkgs' dovecot2.stateDir is currently not exported
12 stateDir = "/var/lib/dovecot";
13 mailDir = "${stateDir}/mail";
14 sieveDir = "${stateDir}/sieve";
15 authDir = "${stateDir}/auth";
16 authUser = dovecot2.mailUser; # TODO: php_roundcube
17 authGroup = dovecot2.mailGroup; # TODO: php_roundcube
18 escapeGroup = lib.stringAsChars (c: if "a"<=c && c<="z"
22 domainGroup = escapeGroup "${networking.domainBase}";
24 { target = "dovecot/${networking.domain}/dovecot-ldap.conf";
25 source = pkgs.writeText "dovecot-ldap.conf" ''
26 ${lib.optionalString dovecot2.debug ''
32 base = ou=posix,${openldap.domainSuffix}
35 # NOTE: sufficient for small systems and uses less resources.
41 #dn = cn=admin,${openldap.domainSuffix}
42 #dnpass = useless with sasl_mech=EXTERNAL
44 #auth_bind_userdn = cn=%n,ou=accounts,ou=posix,dc=${openldap.domainSuffix}
46 # dovecot passdb query
47 # DOC: http://wiki2.dovecot.org/PasswordDatabase/ExtraFields
48 pass_filter = (&(objectClass=posixAccount)(uid=%n)(mailEnabled=TRUE))
49 # TODO: userdb_quota_rule=*:storage=
50 pass_attrs = userPassword=password,\
51 uidNumber=userdb_uid,\
52 gidNumber=userdb_gid,\
53 mailGroupMember=userdb_mail_access_groups=${domainGroup},\
54 quotaBytes=userdb_quota_rule=*:bytes=%{ldap:quotaBytes},\
56 #homeDirectory=userdb_home
57 default_pass_scheme = CRYPT
59 # dovecot userdb query
60 # DOC: http://wiki2.dovecot.org/UserDatabase/ExtraFields
61 user_filter = (&(objectClass=posixAccount)(uid=%n)(mailEnabled=TRUE))
62 #user_filter = (&(objectClass=inetOrgPerson)(uid=%n))
63 #user_attrs = homeDirectory=home,uidNumber=uid,gidNumber=gid
64 #user_attrs = mailHomeDirectory=home,\
65 # mailStorageDirectory=mail,\
68 # mailQuota=quota_rule=*:bytes=%$
71 iterate_attrs = =user=%{ldap:uid}@${networking.domain}
72 iterate_filter = (&(objectClass=posixAccount)(mailEnabled=TRUE))
76 dovecot-virtual = pkgs.writeTextFile {
77 name = "dovecot-virtual";
78 destination = "/pop3/INBOX/dovecot-virtual";
86 learn-spam = pkgs.writeShellScriptBin "learn-spam.sh" ''
87 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_spam
89 learn-ham = pkgs.writeShellScriptBin "learn-ham.sh" ''
90 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_ham
92 sieve_pipe_bin_dir = pkgs.buildEnv {
93 name = "sieve_pipe_bin_dir";
94 pathsToLink = [ "/bin" ];
103 dovecot/autoconfig.nix
105 #services.postfix.mapFiles."transport-dovecot" =
106 # toFile "transport-dovecot"
108 # (lib.mapAttrsToList
109 # (dom: {...}: "${transportSubDomain}.${dom} lmtp:unix:private/dovecot-lmtp")
110 # dovecot2.domains));
111 systemd.services.dovecot2 = {
115 "dovecot.${networking.domainBase}.key.pem-key.service"
117 restartTriggers = map (f: f.source) etc_dovecot;
120 "dovecot.${networking.domainBase}.key.pem" = {
121 text = pass "x509/${networking.domainBase}/key.pem";
122 user = dovecot2.user;
124 destDir = "/run/keys/";
125 permissions = "0400"; # WARNING: not enforced when deployment.storeKeysOnMachine = true
128 environment.etc = etc_dovecot;
129 users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
130 services.dovecot2 = {
133 mailUser = "dovemail";
134 mailGroup = "dovemail";
136 pkgs.dovecot_pigeonhole
137 pkgs.dovecot_fts_xapian
149 if currentdate :matches "year" "*" { set "year" "''${1}"; }
150 if currentdate :matches "month" "*" { set "month" "''${1}"; }
152 if exists "List-ID" {
153 if header :matches "List-ID" "*<*.*.*.*>*" {
155 set "domain" "''${4}";
157 elsif header :matches "List-ID" "*<*.*.*>*" {
159 set "domain" "''${3}";
161 fileinto :create "Listes+''${domain}+''${list}+''${year}+''${month}";
166 require [ "imap4flags" ];
168 if header :contains "X-Spam-Level" "***" {
180 if envelope :matches :detail "TO" "*" {
181 set "extension" "''${1}";
183 if not string :is "''${extension}" "" {
184 fileinto :create "Plus+''${extension}";
189 require ["vnd.dovecot.pipe", "copy", "imapsieve", "variables", "imap4flags", "environment"];
191 if environment :is "imap.changedflags" "Junk" {
192 if hasflag :is "Junk" {
193 pipe :copy :try "learn-spam.sh";
194 } elsif not hasflag :is "Junk" {
195 pipe :copy :try "learn-ham.sh";
200 require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
202 if environment :matches "imap.user" "*" {
203 set "username" "''${1}";
206 pipe :copy :try "learn-spam.sh" [ "''${username}" ];
209 require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];
211 if environment :matches "imap.mailbox" "*" {
212 set "mailbox" "''${1}";
215 if string "''${mailbox}" "Trash" {
219 if environment :matches "imap.user" "*" {
220 set "username" "''${1}";
223 pipe :copy :try "learn-ham.sh" [ "''${username}" ];
227 configFile = toString ( pkgs.writeText "dovecot.conf" ''
230 args = /etc/dovecot/${networking.domain}/dovecot-ldap.conf
231 default_fields = userdb_mail_access_groups=${domainGroup}
238 # NOTE: this userdb is only used by lda.
240 args = /etc/dovecot/${networking.domain}/dovecot-ldap.conf
241 default_fields = mail_access_groups=${domainGroup}
246 # driver = passwd-file
247 # args = scheme=crypt username_format=%n ${authDir}/%d/passwd
250 # # NOTE: this userdb is only used by lda.
251 # driver = passwd-file
252 # args = username_format=%n ${authDir}/%d/passwd
253 # #default_fields = home=${mailDir}/%d/%n
256 # Store METADATA information within user's HOME directory
257 mail_attribute_dict = file:%h/dovecot-attributes
258 # If needed, may be overrided by userdb_mail
259 mail_home = ${mailDir}/%d/%n
260 # NOTE: if needed, may be overrided by userdb_mail
261 # NOTE: INDEX and CONTROL are on a partition without quota, as explain in the doc.
262 # SEE: http://wiki2.dovecot.org/Quota/FS
263 mail_location = maildir:${mailDir}/%d/%n/mail.d:LAYOUT=fs:UTF-8:INDEX=${stateDir}/index/%d/%n:INDEXPVT=${stateDir}/index/%d/%n:CONTROL=${stateDir}/control/%d/%n
264 auth_mechanisms = plain login
265 # NOTE: postfix does not supply a client cert.
266 auth_ssl_require_client_cert = no
267 #auth_ssl_username_from_cert = yes
269 # NOTE: lowercase the username, help with LDAP?
270 auth_username_format = %Lu
271 ${lib.optionalString dovecot2.debug ''
276 default_internal_user = ${dovecot2.user}
277 default_internal_group = ${dovecot2.group}
278 disable_plaintext_auth = yes
279 # NOTE: sync with LDAP's data.
280 first_valid_uid = 1000
281 lda_mailbox_autocreate = yes
282 lda_mailbox_autosubscribe = yes
284 log_timestamp = "%Y-%m-%d %H:%M:%S "
285 #maildir_copy_with_hardlinks = yes
287 # NOTE: here because protocol sieve {namespace inbox{}} does not seem to work.
293 separator = ${dirSep}
298 # NOTE: always listed in the LIST command.
300 # NOTE: how to access the other users' mailboxes.
301 # NOTE: %var expands to the logged in user's variable, while
302 # %%var expands to the other users' variables.
303 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
304 location = maildir:${mailDir}/%%d/%%n/mail.d:LAYOUT=fs:INDEX=${stateDir}/index/%%d/%%n/Shared:INDEXPVT=${stateDir}/index/%d/%n/Shared/%%n:CONTROL=${stateDir}/control/%d/%n/Shared
305 prefix = Partages+%%n+
306 separator = ${dirSep}
309 # Default VSZ (virtual memory size) limit for service processes. This is mainly
310 # intended to catch and kill processes that leak memory before they eat up everything.
311 # Increased for fts_xapian.
312 default_vsz_limit = 1G
313 mail_plugins = $mail_plugins acl quota virtual fts fts_xapian
314 #mail_uid = ${dovecot2.mailUser}
315 #mail_gid = ${dovecot2.mailGroup}
316 # NOTE: each user has a dedicated (uid,gid) pair
317 #mail_privileged_group = mail
318 #mail_access_groups =
320 plugin = fts fts_xapian
322 acl = vfile:/etc/dovecot/acl/global.d
324 # NOTE: to let users LIST mailboxes shared by other users,
325 # Dovecot needs a shared mailbox dictionary.
326 # FIXME: %d not working with userdb ldap
327 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
331 fts_autoindex_exclude = \Junk
332 fts_autoindex_exclude2 = \Trash
334 # 2 and 20 are the NGram values for header fields, which means the
335 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
336 # Full words are also added by default.
337 fts_xapian = partial=2 full=20 verbose=${if dovecot2.debug then "2" else "1"}
339 quota = maildir:User quota
340 quota_rule = *:storage=256M
341 quota_rule2 = Trash:storage=+64M
342 quota_max_mail_size = 20M
343 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
344 quota_warning = storage=95%% quota-warning 95 %u
345 quota_warning2 = storage=80%% quota-warning 80 %u
346 quota_warning3 = -storage=100%% quota-warning below %u
348 recipient_delimiter = ${extSep}
350 #sieve_default = file:${mailDir}/%u/default.sieve
351 #sieve_default_name = default
352 sieve_after = ${sieveDir}/after.d/
353 sieve_before = ${sieveDir}/before.d/
354 sieve = file:~/sieve.d;active=~/sieve
355 #sieve_extensions = +editheader
356 sieve_global = ${sieveDir}/global.d/
357 sieve_max_script_size = 1M
358 sieve_quota_max_scripts = 0
359 sieve_quota_max_storage = 10M
360 sieve_spamtest_max_value = 10
361 sieve_spamtest_status_header = X-Spam-Score
362 sieve_spamtest_status_type = strlen
363 sieve_user_log = /var/log/dovecot/%d/sieve.%n.log
364 sieve_plugins = sieve_imapsieve sieve_extprograms
365 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
366 sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.environment
367 # Enables support for user Sieve scripts in IMAP
368 #imapsieve_url = sieve://mail.${networking.domain}:4190
370 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
371 imapsieve_mailbox1_name = *
372 imapsieve_mailbox1_causes = FLAG
373 imapsieve_mailbox1_before = file:${sieveDir}/global.d/spam-or-ham.sieve
375 # From elsewhere to Junk folder
376 imapsieve_mailbox2_name = Pourriel
377 imapsieve_mailbox2_causes = COPY APPEND
378 imapsieve_mailbox2_before = file:${sieveDir}/global.d/report-spam.sieve
380 # From Junk folder to elsewhere
381 imapsieve_mailbox3_name = *
382 imapsieve_mailbox3_from = Pourriel
383 imapsieve_mailbox3_causes = COPY
384 imapsieve_mailbox3_before = file:${sieveDir}/global.d/report-ham.sieve
386 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
387 imapc_features = $imapc_features fetch-headers
388 # Read multiple mails in parallel, improves performance
389 mail_prefetch_count = 20
390 service quota-warning {
391 executable = script ${
392 pkgs.writeScript "quota-warning" ''
396 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
397 "plugin/quota=maildir:User quota:noenforcing"
398 From: postmaster@${networking.domain}
399 Subject: [WARNING] your mailbox is now $PERCENT% full.
401 Please remove some mails to make room for new ones.
405 # use some unprivileged user for executing the quota warnings
406 user = ${dovecot2.user}
407 unix_listener quota-warning {
411 #mail_max_userip_connections = 10
412 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve
413 # DOC: https://wiki.dovecot.org/MailboxSettings
414 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
415 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
416 imap_capability = +SPECIAL-USE
422 separator = ${dirSep}
425 special_use = \Archive
429 special_use = \Archive
433 special_use = \Drafts
435 mailbox "Brouillons" {
437 special_use = \Drafts
452 mailbox "Pourriels" {
464 mailbox "Sent Items" {
468 mailbox "Sent Messages" {
481 mailbox "Corbeille" {
488 auth_socket_path = /var/run/dovecot/auth-userdb
489 hostname = ${networking.domain}
492 mail_plugins = $mail_plugins sieve
498 separator = ${dirSep}
500 postmaster_address = postmaster${extSep}dovecot${extSep}lda@${networking.domain}
501 syslog_facility = mail
504 #info_log_path = /tmp/dovecot-lmtp.log
505 mail_plugins = $mail_plugins sieve
511 separator = ${dirSep}
513 postmaster_address = postmaster${extSep}dovecot${extSep}lmtp@${networking.domain}
516 #mail_max_userip_connections = 10
518 # NOTE: used by ${dovecot-virtual}/pop3/INBOX/dovecot-virtual
523 separator = ${dirSep}
525 # Virtual namespace for the virtual INBOX.
526 # Use a global directory for dovecot-virtual files.
531 location = virtual:${dovecot-virtual}/pop3:INDEX=${stateDir}/index/%d/%n/POP3:LAYOUT=fs
533 separator = ${dirSep}
535 pop3_client_workarounds =
536 pop3_fast_size_lookups = yes
537 pop3_lock_session = yes
538 pop3_no_flag_updates = yes
539 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
540 pop3_uidl_format = %g
543 #mail_max_userip_connections = 10
544 #managesieve_implementation_string = Dovecot Pigeonhole
545 managesieve_max_compile_errors = 5
546 #managesieve_max_line_length = 65536
547 #managesieve_notify_capability = mailto
548 #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
550 # DOC: https://wiki2.dovecot.org/Pigeonhole/ManageSieve/Configuration
552 # Maximum number of ManageSieve connections allowed for a user from each IP address.
553 # NOTE: The username is compared case-sensitively.
554 mail_max_userip_connections = 10
556 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
557 # the IMPLEMENTATION capability that the dovecot reports to clients.
558 # For example: 'Cyrus timsieved v2.2.13'
559 managesieve_implementation_string = Dovecot Pigeonhole
561 # The maximum number of compile errors that are returned to the client upon script
562 # upload or script verification.
563 managesieve_max_compile_errors = 5
565 protocols = imap lmtp pop3 sieve
567 #executable = lmtp -L
568 process_min_avail = 2
569 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
570 user = ${postfix.user}
571 group = ${postfix.group}
577 # FIXME: may be user=dovecot-auth with LDAP?
579 unix_listener auth-userdb {
580 user = ${dovecot2.user}
581 group = ${dovecot2.group}
584 unix_listener /var/lib/postfix/queue/private/auth {
585 user = ${postfix.user}
586 group = ${postfix.group}
591 # Most of the memory goes to mmap()ing files.
592 # You may need to increase this limit if you have huge mailboxes.
597 #inet_listener imap {
598 # address = 127.0.0.1
602 inet_listener imaps {
611 inet_listener pop3s {
616 service managesieve-login {
617 inet_listener sieve {
622 # Number of connections to handle before starting a new process. Typically
623 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
624 # is faster. <doc/wiki/LoginProcess.txt>
627 # Number of processes to always keep waiting for more connections.
628 process_min_avail = 0
630 # If you set service_count=0, you probably need to grow this.
634 ssl_dh = <${../../../sec/openssl/dh.pem}
635 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
636 ssl_cert = <${loadFile (../../../sec + "/openssl/${networking.domainBase}/cert.self-signed.pem")}
637 ssl_key = </run/keys/dovecot.${networking.domainBase}.key.pem
638 #ssl_ca = <''${caPath}
639 #ssl_verify_client_cert = yes
641 #${lib.concatMapStringsSep "\n"
643 # local_name mail.${dom} {
644 # #ssl_ca = <''${caPath}
645 # ssl_cert = <${x509.cert dom}
646 # ssl_key = <${x509.key dom}