1 {pkgs, lib, config, system, ...}:
2 let inherit (builtins) toString toFile;
4 inherit (pkgs.lib) unlines;
5 inherit (config.services) dovecot2 postfix x509 openldap;
6 when = x: y: if x == null then "" else y;
7 extSep = postfix.recipientDelimiter;
9 libDir = "/var/lib/dovecot";
10 mailDir = "${libDir}/mail";
11 sieveDir = "${libDir}/sieve";
12 authDir = "${libDir}/auth";
13 authUser = dovecot2.mailUser; # TODO: php_roundcube
14 authGroup = dovecot2.mailGroup; # TODO: php_roundcube
15 escapeGroup = lib.stringAsChars (c: if "a"<=c && c<="z"
20 { target = "dovecot/${config.networking.domain}/dovecot-ldap.conf";
21 source = pkgs.writeText "dovecot-ldap.conf" ''
22 ${lib.optionalString dovecot2.debug ''
28 base = ou=posix,${openldap.domainSuffix}
32 # NOTE: sufficient for small systems and uses less resources.
37 #dn = cn=admin,${openldap.domainSuffix}
38 #dnpass = useless with sasl_mech=EXTERNAL
40 #auth_bind_userdn = cn=%n,ou=accounts,ou=posix,dc=${openldap.domainSuffix}
42 # dovecot passdb query
43 pass_filter = (&(objectClass=posixAccount)(uid=%n))
44 pass_attrs = userPassword=password,\
45 uidNumber=userdb_uid,\
46 gidNumber=userdb_gid,\
47 mailGroupMember=mail_access_groups,\
49 #homeDirectory=userdb_home
50 #quotaBytes=userdb_quota_rule=*:bytes=%{ldap:quotaBytes}
51 # TODO: userdb_quota_rule=*:storage=
52 # TODO: userdb_mail_access_groups
53 # DOC: http://wiki2.dovecot.org/PasswordDatabase/ExtraFields
54 default_pass_scheme = CRYPT
56 # dovecot userdb query
57 user_filter = (&(objectClass=posixAccount)(uid=%n))
58 #user_filter = (&(objectClass=inetOrgPerson)(uid=%n)(mailEnabled=TRUE))
59 # # DOC: http://wiki2.dovecot.org/Variables
60 #user_attrs = homeDirectory=home,uidNumber=uid,gidNumber=gid
61 #user_attrs = mailHomeDirectory=home,\
62 # mailStorageDirectory=mail,\
65 # mailQuota=quota_rule=*:bytes=%$
66 # # DOC: http://wiki2.dovecot.org/UserDatabase/ExtraFields
69 iterate_attrs = =user=%{ldap:uid}@${config.networking.domain}
70 iterate_filter = (objectClass=posixAccount)
74 dovecot-virtual = pkgs.writeTextFile {
75 name = "dovecot-virtual";
76 destination = "/pop3/INBOX/dovecot-virtual";
86 dovecot/autoconfig.nix
89 systemd.services.dovecot2.after = [ "postfix.service" ];
90 systemd.services.dovecot2.restartTriggers =
91 map (f: f.source) etc_dovecot;
92 environment.etc = etc_dovecot;
93 #services.postfix.mapFiles."transport-dovecot" =
94 # toFile "transport-dovecot"
97 # (dom: {...}: "${transportSubDomain}.${dom} lmtp:unix:private/dovecot-lmtp")
100 # { name = "dovecot";
101 # uid = config.ids.uids.dovecot2;
102 # description = "Dovecot user";
103 # group = dovecot2.group;
109 { name = escapeGroup "${dovecot2.mailGroup}-${domain}";
112 systemd.services.dovecot2.preStart =
114 pkgs.writeText "list.sieve" ''
122 if currentdate :matches "year" "*" { set "year" "''${1}"; }
123 if currentdate :matches "month" "*" { set "month" "''${1}"; }
125 if exists "List-ID" {
126 if header :matches "List-ID" "*<*.*.*.*>*" {
128 set "domain" "''${4}";
130 elsif header :matches "List-ID" "*<*.*.*>*" {
132 set "domain" "''${3}";
134 fileinto :create "Listes+''${domain}+''${list}+''${year}+''${month}";
139 pkgs.writeText "spam.sieve" ''
144 if header :contains "X-Spam-Level" "***" {
149 pkgs.writeText "extension.sieve" ''
157 if envelope :matches :detail "TO" "*" {
158 set "extension" "''${1}";
160 if not string :is "''${extension}" "" {
161 fileinto :create "Plus+''${extension}";
166 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
167 # NOTE: the sticky bit is to allow the acl.db{.lock,} done by dovecot
168 install -D -d -m 2771 \
169 -o ${dovecot2.mailUser} \
170 -g ${dovecot2.mailGroup} \
173 # Install global sieves
174 install -D -d -m 0755 \
178 ${sieveDir}/after.d \
179 ${sieveDir}/before.d \
181 ln -fns ${sieveList} ${sieveDir}/global.d/list.sieve
182 ln -fns ${sieveExtension} ${sieveDir}/global.d/extension.sieve
183 ln -fns ${sieveSpam} ${sieveDir}/global.d/spam.sieve
184 for f in ${sieveDir}/*/*.sieve
185 do ${pkgs.dovecot_pigeonhole}/bin/sievec $f
192 + unlines (lib.mapAttrsToList (domain: {accounts, ...}:
193 let domainGroup = escapeGroup "${dovecot2.mailGroup}-${domain}"; in
195 install -D -d -m 1770 \
196 -o ${dovecot2.mailUser} \
198 ${mailDir}/${domain} \
199 ${libDir}/control/${domain} \
200 ${libDir}/index/${domain}
201 install -D -d -m 1770 \
202 -o ${dovecot2.mailUser} \
205 ${libDir}/auth/${domain}
206 '') dovecot2.domains);
212 services.dovecot2 = {
215 mailUser = "dovemail";
216 mailGroup = "dovemail";
218 #pkgs.dovecot_antispam
219 pkgs.dovecot_pigeonhole
221 configFile = toString (pkgs.writeText "dovecot.conf" ''
224 args = /etc/dovecot/${config.networking.domain}/dovecot-ldap.conf
232 # NOTE: this userdb is only used by lda.
234 args = /etc/dovecot/${config.networking.domain}/dovecot-ldap.conf
240 # driver = passwd-file
241 # args = scheme=crypt username_format=%n ${authDir}/%d/passwd
244 # # NOTE: this userdb is only used by lda.
245 # driver = passwd-file
246 # args = username_format=%n ${authDir}/%d/passwd
247 # #default_fields = home=${mailDir}/%d/%n
250 mail_home = ${mailDir}/%d/%n
251 # NOTE: if needed, may be overrided by userdb_mail
252 mail_location = maildir:${mailDir}/%d/%n/Maildir:LAYOUT=fs:INDEX=${libDir}/index/%d/%n:CONTROL=${libDir}/control/%d/%n
253 # NOTE: if needed, may be overrided by userdb_mail
254 # NOTE: INDEX and CONTROL are on a partition without quota, as explain in the doc.
255 # SEE: http://wiki2.dovecot.org/Quota/FS
256 auth_mechanisms = plain login
257 auth_ssl_require_client_cert = no
258 # NOTE: postfix does not supply a client cert.
259 auth_ssl_username_from_cert = yes
261 auth_username_format = %Lu
262 # NOTE: lowercase the username, help with LDAP?
263 ${lib.optionalString dovecot2.debug ''
268 default_internal_user = ${dovecot2.user}
269 default_internal_group = ${dovecot2.group}
270 disable_plaintext_auth = yes
271 first_valid_uid = 1000
272 lda_mailbox_autocreate = yes
273 lda_mailbox_autosubscribe = yes
275 log_timestamp = "%Y-%m-%d %H:%M:%S "
276 #maildir_copy_with_hardlinks = yes
278 # NOTE: here because protocol sieve {namespace inbox{}} does not seem to work.
283 separator = ${dirSep}
288 location = maildir:${mailDir}/%d/%n/Maildir:LAYOUT=fs:INDEX=${libDir}/index/%d/%n/Shared/%n:CONTROL=${libDir}/control/%d/%n/Shared/%n
289 # FIXME: %d not working
290 prefix = Partages+%%n+
291 separator = ${dirSep}
295 mail_plugins = $mail_plugins acl quota virtual
296 #mail_uid = ${dovecot2.mailUser}
297 #mail_gid = ${dovecot2.mailGroup}
298 # NOTE: each user has a dedicated (uid,gid) pair
299 #mail_privileged_group = mail
300 #mail_access_groups =
302 acl = vfile:/etc/dovecot/acl/global.d
304 acl_shared_dict = file:${mailDir}/%d/acl.db
305 # NOTE: to let users LIST mailboxes shared by other users,
306 # Dovecot needs a shared mailbox dictionary.
307 # FIXME: %d not working with userdb ldap
308 ##antispam_allow_append_to_spam = yes
309 # # NOTE: pour offlineimap
310 #antispam_backend = pipe
311 ##antispam_crm_args = -u;${mailDir}/%d/.crm114;/usr/share/crm114/mailfilter.crm
312 #antispam_crm_args = -u;${mailDir}/crm114;/usr/share/crm114/mailfilter.crm
313 #antispam_crm_binary = /usr/bin/crm
314 #antispam_debug_target = syslog
315 ##antispam_crm_env = HOME=%h;USER=%u
316 #antispam_ham_keywords = NonJunk
317 #antispam_pipe_program = /usr/bin/crm
318 #antispam_pipe_program_args = -u;${mailDir}/crm114;/usr/share/crm114/mailfilter.crm;--stats_only;--force
319 #antispam_pipe_program_notspam_arg = --learnnonspam
320 #antispam_pipe_program_spam_arg = --learnspam
321 #antispam_pipe_program_unlearn_spam_args = --unlearn;--learnspam
322 #antispam_pipe_program_unlearn_notspam_args = --unlearn;--learnnonspam
323 #antispam_pipe_tmpdir = ${mailDir}/crm114/tmp
324 #antispam_signature = X-CRM114-CacheID
325 #antispam_signature_missing = move
326 #antispam_spam = Junk
327 #antispam_spam_keywords = Junk
328 #antispam_trash = Trash
329 #antispam_unsure = Unsure
330 #antispam_verbose_debug = 0
331 quota = maildir:User quota
332 quota_rule = *:storage=256M
333 quota_rule2 = Trash:storage=+64M
334 quota_max_mail_size = 20M
335 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
336 quota_warning = storage=95%% quota-warning 95 %u
337 quota_warning2 = storage=80%% quota-warning 80 %u
338 quota_warning3 = -storage=100%% quota-warning below %u
339 # NOTE: user is no longer over quota
340 recipient_delimiter = ${extSep}
341 sieve = file:${mailDir}/%d/%n/sieve;active=${mailDir}/%d/%n/sieve/main.sieve
342 #sieve_default = file:${mailDir}/%u/default.sieve
343 #sieve_default_name = default
344 sieve_after = ${sieveDir}/after.d/
345 sieve_before = ${sieveDir}/before.d/
346 sieve_dir = ${mailDir}/%d/%n/sieve/
347 #sieve_extensions = +spamtest +spamtestplus
348 sieve_global_dir = ${sieveDir}/global.d/
349 sieve_max_script_size = 1M
350 sieve_quota_max_scripts = 0
351 sieve_quota_max_storage = 10M
352 sieve_spamtest_max_value = 10
353 sieve_spamtest_status_header = X-Spam-Score
354 sieve_spamtest_status_type = strlen
355 sieve_user_log = /var/log/dovecot/%d/sieve.%n.log
357 service quota-warning {
358 executable = script ${
359 pkgs.writeScript "quota-warning" ''
363 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
364 "plugin/quota=maildir:User quota:noenforcing"
365 From: postmaster@${config.networking.domain}
366 Subject: [WARNING] your mailbox is now $PERCENT% full.
368 Please remove some mails to make room for new ones.
372 # use some unprivileged user for executing the quota warnings
373 user = ${dovecot2.user}
374 unix_listener quota-warning {
378 #mail_max_userip_connections = 10
379 mail_plugins = $mail_plugins imap_acl imap_quota # antispam
385 special_use = \Drafts
393 mailbox "Sent Messages" {
400 separator = ${dirSep}
404 auth_socket_path = /var/run/dovecot/auth-userdb
405 hostname = ${config.networking.domain}
408 mail_plugins = $mail_plugins sieve
414 separator = ${dirSep}
416 postmaster_address = postmaster${extSep}dovecot${extSep}lda@${config.networking.domain}
417 syslog_facility = mail
420 #info_log_path = /tmp/dovecot-lmtp.log
421 mail_plugins = $mail_plugins sieve
427 separator = ${dirSep}
429 postmaster_address = postmaster${extSep}dovecot${extSep}lmtp@${config.networking.domain}
432 #mail_max_userip_connections = 10
434 # NOTE: used by ${dovecot-virtual}/pop3/INBOX/dovecot-virtual
439 separator = ${dirSep}
441 # Virtual namespace for the virtual INBOX.
442 # Use a global directory for dovecot-virtual files.
447 location = virtual:${dovecot-virtual}/pop3:INDEX=${libDir}/index/%d/%n/POP3:LAYOUT=fs
449 separator = ${dirSep}
451 pop3_client_workarounds =
452 pop3_fast_size_lookups = yes
453 pop3_lock_session = yes
454 pop3_no_flag_updates = yes
455 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
456 pop3_uidl_format = %g
459 #mail_max_userip_connections = 10
460 #managesieve_implementation_string = Dovecot Pigeonhole
461 managesieve_max_compile_errors = 5
462 #managesieve_max_line_length = 65536
463 #managesieve_notify_capability = mailto
464 #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
466 protocols = imap lmtp pop3 sieve
468 #executable = lmtp -L
469 process_min_avail = 2
470 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
471 user = ${postfix.user}
472 group = ${postfix.group}
479 # FIXME: may be user=dovecot-auth with LDAP?
480 unix_listener auth-userdb {
481 user = ${dovecot2.user}
482 group = ${dovecot2.group}
485 unix_listener /var/lib/postfix/queue/private/auth {
486 user = ${postfix.user}
487 group = ${postfix.group}
492 # Most of the memory goes to mmap()ing files.
493 # You may need to increase this limit if you have huge mailboxes.
498 #inet_listener imap {
499 # address = 127.0.0.1
503 inet_listener imaps {
512 inet_listener pop3s {
518 ssl_dh = <${x509.dir}/dh.pem
519 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
520 ssl_cert = <${x509.cert}
521 ssl_key = <${x509.key}
522 #ssl_ca = <''${caPath}
523 #ssl_verify_client_cert = yes
525 #${lib.concatMapStringsSep "\n"
527 # local_name mail.${dom} {
528 # #ssl_ca = <''${caPath}
529 # ssl_cert = <${x509.cert dom}
530 # ssl_key = <${x509.key dom}