1 {pkgs, lib, config, system, ...}:
2 let inherit (builtins) toString toFile attrNames;
4 inherit (config.services) dovecot2 postfix x509;
5 unlines = lib.concatStringsSep "\n";
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"
22 environment.etc."nginx/site.d/autoconfig.conf".source =
23 let servers = lib.concatMapStringsSep " "
24 (dom: "autoconfig.${dom}")
25 (attrNames dovecot2.domains);
26 autoconfigSite = pkgs.writeTextFile {
28 destination = "/mail/config-v1.1.xml";
31 <clientConfig version="1.1">
32 <emailProvider id="%EMAILDOMAIN%">
33 <!-- <displayName></displayName> -->
34 <!-- <displayShortName></displayShortName> -->
35 <domain>%EMAILDOMAIN%</domain>
36 <incomingServer type="imap">
37 <hostname>imap.%EMAILDOMAIN%</hostname>
39 <socketType>SSL</socketType>
40 <username>%EMAILADDRESS%</username>
41 <authentication>password-cleartext</authentication>
43 <incomingServer type="pop3">
44 <hostname>pop.%EMAILDOMAIN%</hostname>
46 <socketType>SSL</socketType>
47 <username>%EMAILADDRESS%</username>
48 <authentication>password-cleartext</authentication>
50 <leaveMessagesOnServer>false</leaveMessagesOnServer>
51 <downloadOnBiff>true</downloadOnBiff>
54 <outgoingServer type="smtp">
55 <hostname>smtp.%EMAILDOMAIN%</hostname>
57 <socketType>SSL</socketType> <!-- see above -->
58 <username>%EMAILADDRESS%</username> <!-- if smtp-auth -->
59 <authentication>password-cleartext</authentication>
60 <!-- <restriction>client-IP-address</restriction> -->
61 <addThisServer>true</addThisServer>
62 <useGlobalPreferredServer>false</useGlobalPreferredServer>
65 <!-- <clientConfigUpdate url="https://www.example.com/config/mozilla.xml" /> -->
70 pkgs.writeText "autoconfig.conf" ''
73 server_name ${servers};
74 root ${autoconfigSite};
81 server_name ${servers};
82 root ${autoconfigSite};
87 #services.postfix.mapFiles."transport-dovecot" =
88 # toFile "transport-dovecot"
91 # (dom: {...}: "${transportSubDomain}.${dom} lmtp:unix:private/dovecot-lmtp")
93 systemd.services.dovecot2.after = [ "postfix.service" ];
96 # uid = config.ids.uids.dovecot2;
97 # description = "Dovecot user";
98 # group = dovecot2.group;
101 users.extraGroups = lib.mapAttrs
103 { name = escapeGroup "${dovecot2.mailGroup}-${domain}";
106 systemd.services.dovecot2.preStart =
108 pkgs.writeText "list.sieve" ''
116 if currentdate :matches "year" "*" { set "year" "''${1}"; }
117 if currentdate :matches "month" "*" { set "month" "''${1}"; }
119 if exists "List-ID" {
120 if header :matches "List-ID" "*<*.*.*.*>*" {
122 set "domain" "''${4}";
124 elsif header :matches "List-ID" "*<*.*.*>*" {
126 set "domain" "''${3}";
128 fileinto :create "Listes+''${domain}+''${list}+''${year}+''${month}";
133 pkgs.writeText "spam.sieve" ''
138 if header :contains "X-Spam-Level" "***" {
143 pkgs.writeText "extension.sieve" ''
151 if envelope :matches :detail "TO" "*" {
152 set "extension" "''${1}";
154 if not string :is "''${extension}" "" {
155 fileinto :create "Plus+''${extension}";
160 pkgs.writeText "dovecot-virtual" ''
166 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
167 # 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
193 ${libDir}/pop3/INBOX/dovecot-virtual
199 + unlines (lib.mapAttrsToList (domain: {accounts, ...}:
200 let domainGroup = escapeGroup "${dovecot2.mailGroup}-${domain}"; in
202 install -D -d -m 1770 \
203 -o ${dovecot2.mailUser} \
205 ${mailDir}/${domain} \
206 ${libDir}/control/${domain} \
207 ${libDir}/index/${domain}
208 install -D -d -m 1770 \
209 -o ${dovecot2.mailUser} \
212 ${libDir}/auth/${domain}
213 dir_passwd=${libDir}/auth/${domain}
214 old_passwd=$dir_passwd/passwd
215 new_passwd=$(TMPDIR= mktemp --tmpdir=$dir_passwd -t passwd.XXXXXXXX.tmp)
219 + unlines (lib.mapAttrsToList (user: acct: ''
220 home=${mailDir}/${domain}/${user}
222 shell=/run/current-system/sw/bin/nologin
225 uid=$(stat -c %u $home)
226 gid=$(stat -c %g $home)
228 [ "''${uid:+set}" ] || {
229 while test exists = "$(find $(dirname $home) -mindepth 1 -maxdepth 1 -uid $new_uid -printf exists -quit)"
230 do new_uid=$((new_uid + 1))
235 install -D -d -o $uid -g $gid -m 2770 $home $home/Maildir
236 install -d -o $uid -g $gid -m 0700 $home/sieve
238 + unlines (lib.mapAttrsToList
240 install -D -m 640 -o $uid -g $gid \
241 ${pkgs.writeText "${n}.sieve" v} \
242 $home/sieve/${n}.sieve
243 ${pkgs.dovecot_pigeonhole}/bin/sievec \
244 $home/sieve/${n}.sieve
248 mail_access_groups=${lib.concatStringsSep "," ([domainGroup] ++ acct.groups)}
249 quota=${if lib.isString acct.quota
250 then ''"userdb_quota_rule=*:storage=${acct.quota}"''
252 extra_fields="userdb_uid=$uid userdb_gid=$gid userdb_mail_access_groups=$mail_access_groups $quota"
253 #test ! -e $old_passwd || {
254 # # Preserve password changed by another mechanism, eg. roundcube.
255 # # But this also does not overwrite any old password set by this config.
256 # pass="$(sed -ne "s/^${user}:\([^:]*\):.*/\1/p" $old_passwd)"
258 [ "''${pass:+set}" ] || {
259 pass=${lib.escapeShellArg acct.password}
261 printf '%s\n' >>$new_passwd \
262 "${user}:$pass:$uid:$gid:$gecos:$home:$shell:$extra_fields"
265 install -o ${authUser} -g ${authGroup} -m 0640 $new_passwd $old_passwd
269 services.dovecot2 = {
271 mailUser = "dovemail";
272 mailGroup = "dovemail";
274 #pkgs.dovecot_antispam
275 pkgs.dovecot_pigeonhole
277 # ${lib.concatMapStringsSep "\n"
279 # local_name imap.${dom} {
280 # #ssl_ca = <''${caPath}
281 # ssl_cert = <${x509.cert dom}
282 # ssl_key = <${x509.key dom}
284 # local_name pop.${dom} {
285 # #ssl_ca = <''${caPath}
286 # ssl_cert = <${x509.cert dom}
287 # ssl_key = <${x509.key dom}
293 configFile = toString (pkgs.writeText "dovecot.conf" ''
296 args = scheme=crypt username_format=%n ${authDir}/%d/passwd
302 # NOTE: this userdb is only used by lda.
304 args = username_format=%n ${authDir}/%d/passwd
305 #default_fields = home=${mailDir}/%d/%n
307 mail_home = ${mailDir}/%d/%n
308 auth_mechanisms = plain login
309 # postfix does not supply a client cert.
310 auth_ssl_require_client_cert = no
311 auth_ssl_username_from_cert = yes
313 ${lib.optionalString dovecot2.debug ''
318 default_internal_user = ${dovecot2.user}
319 default_internal_group = ${dovecot2.group}
320 disable_plaintext_auth = yes
321 first_valid_uid = 1000
322 lda_mailbox_autocreate = yes
323 lda_mailbox_autosubscribe = yes
325 log_timestamp = "%Y-%m-%d %H:%M:%S "
326 # NOTE: INDEX and CONTROL are on a partition without quota, as explain in the doc.
327 # SEE: http://wiki2.dovecot.org/Quota/FS
328 mail_location = maildir:${mailDir}/%d/%n/Maildir:LAYOUT=fs:INDEX=${libDir}/index/%d/%n:CONTROL=${libDir}/control/%d/%n
330 # NOTE: here because protocol sieve {namespace inbox{}} does not seem to work.
335 separator = ${dirSep}
340 location = maildir:${mailDir}/%%d/%%n/Maildir:LAYOUT=fs:INDEX=${libDir}/index/%d/%n/Shared/%%n:CONTROL=${libDir}/control/%d/%n/Shared/%%n
341 prefix = Partages+%%n+
342 separator = ${dirSep}
346 mail_plugins = $mail_plugins acl quota virtual
347 #mail_uid = ${dovecot2.mailUser}
348 #mail_gid = ${dovecot2.mailGroup}
349 #mail_privileged_group = mail
350 #mail_access_groups =
352 acl = vfile:/etc/dovecot/acl/global.d
354 acl_shared_dict = file:${mailDir}/%d/acl.db
355 ##antispam_allow_append_to_spam = yes
356 # # NOTE: pour offlineimap
357 #antispam_backend = pipe
358 ##antispam_crm_args = -u;${mailDir}/%d/.crm114;/usr/share/crm114/mailfilter.crm
359 #antispam_crm_args = -u;${mailDir}/crm114;/usr/share/crm114/mailfilter.crm
360 #antispam_crm_binary = /usr/bin/crm
361 #antispam_debug_target = syslog
362 ##antispam_crm_env = HOME=%h;USER=%u
363 #antispam_ham_keywords = NonJunk
364 #antispam_pipe_program = /usr/bin/crm
365 #antispam_pipe_program_args = -u;${mailDir}/crm114;/usr/share/crm114/mailfilter.crm;--stats_only;--force
366 #antispam_pipe_program_notspam_arg = --learnnonspam
367 #antispam_pipe_program_spam_arg = --learnspam
368 #antispam_pipe_program_unlearn_spam_args = --unlearn;--learnspam
369 #antispam_pipe_program_unlearn_notspam_args = --unlearn;--learnnonspam
370 #antispam_pipe_tmpdir = ${mailDir}/crm114/tmp
371 #antispam_signature = X-CRM114-CacheID
372 #antispam_signature_missing = move
373 #antispam_spam = Junk
374 #antispam_spam_keywords = Junk
375 #antispam_trash = Trash
376 #antispam_unsure = Unsure
377 #antispam_verbose_debug = 0
378 quota = maildir:User quota
379 quota_rule = *:storage=256M
380 quota_rule2 = Trash:storage=+64M
381 recipient_delimiter = ${extSep}
382 sieve = file:${mailDir}/%d/%n/sieve;active=${mailDir}/%d/%n/sieve/main.sieve
383 #sieve_default = file:${mailDir}/%u/default.sieve
384 #sieve_default_name = default
385 sieve_after = ${sieveDir}/after.d/
386 sieve_before = ${sieveDir}/before.d/
387 sieve_dir = ${mailDir}/%d/%n/sieve/
388 #sieve_extensions = +spamtest +spamtestplus
389 sieve_global_dir = ${sieveDir}/global.d/
390 sieve_max_script_size = 1M
391 sieve_quota_max_scripts = 0
392 sieve_quota_max_storage = 10M
393 sieve_spamtest_max_value = 10
394 sieve_spamtest_status_header = X-Spam-Score
395 sieve_spamtest_status_type = strlen
396 sieve_user_log = /var/log/dovecot/%d/sieve.%n.log
399 #mail_max_userip_connections = 10
400 mail_plugins = $mail_plugins imap_acl imap_quota # antispam
406 special_use = \Drafts
414 mailbox "Sent Messages" {
421 separator = ${dirSep}
425 auth_socket_path = /var/run/dovecot/auth-userdb
426 hostname = ${config.networking.domain}
429 mail_plugins = $mail_plugins sieve
435 separator = ${dirSep}
437 postmaster_address = postmaster${extSep}dovecot${extSep}lda@${config.networking.domain}
438 syslog_facility = mail
441 #info_log_path = /tmp/dovecot-lmtp.log
442 mail_plugins = $mail_plugins sieve
448 separator = ${dirSep}
450 postmaster_address = postmaster${extSep}dovecot${extSep}lmtp@${config.networking.domain}
453 #mail_max_userip_connections = 10
454 # Used by ${libDir}/pop3/INBOX/dovecot-virtual
460 separator = ${dirSep}
462 # Virtual namespace for the virtual INBOX.
463 # Use a global directory for dovecot-virtual files.
468 location = virtual:${libDir}/pop3:INDEX=${libDir}/index/%d/%n/POP3:LAYOUT=fs
470 separator = ${dirSep}
472 pop3_client_workarounds =
473 pop3_fast_size_lookups = yes
474 pop3_lock_session = yes
475 pop3_no_flag_updates = yes
476 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
477 pop3_uidl_format = %g
480 #mail_max_userip_connections = 10
481 #managesieve_implementation_string = Dovecot Pigeonhole
482 managesieve_max_compile_errors = 5
483 #managesieve_max_line_length = 65536
484 #managesieve_notify_capability = mailto
485 #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
487 protocols = imap lmtp pop3 sieve
489 #executable = lmtp -L
490 process_min_avail = 2
491 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
492 user = ${postfix.user}
493 group = ${postfix.group}
500 unix_listener auth-userdb {
501 user = ${dovecot2.user}
502 group = ${dovecot2.group}
505 unix_listener /var/lib/postfix/queue/private/auth {
506 user = ${postfix.user}
507 group = ${postfix.group}
512 # Most of the memory goes to mmap()ing files.
513 # You may need to increase this limit if you have huge mailboxes.
518 #inet_listener imap {
519 # address = 127.0.0.1
523 inet_listener imaps {
532 inet_listener pop3s {
538 #ssl_ca = <''${caPath}
539 ssl_cert = <${x509.cert}
540 # Only with dovecot >= 2.3
541 #ssl_dh = <${x509.dir}/dh.pem
542 ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
543 ssl_key = <${x509.key}
544 #ssl_verify_client_cert = yes