]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/mermet/dovecot.nix
nix: update flake.lock
[sourcephile-nix.git] / hosts / mermet / dovecot.nix
1 { inputs, pkgs, lib, config, system, host, ... }:
2 let
3 inherit (builtins) toString toFile readFile;
4 inherit (lib) types;
5 inherit (pkgs.lib) loadFile unlines unlinesAttrs unlinesValues unwords;
6 inherit (config) networking;
7 inherit (config.services) dovecot2 postfix openldap;
8
9 # NOTE: nixpkgs' dovecot2.stateDir is currently not exported
10 stateDir = "/var/lib/dovecot";
11
12 sieve_pipe_bin_dir = pkgs.buildEnv {
13 name = "sieve_pipe_bin_dir";
14 pathsToLink = [ "/bin" ];
15 paths = [
16 learn-spam
17 learn-ham
18 ];
19 };
20 learn-spam = pkgs.writeShellScriptBin "learn-spam.sh" ''
21 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_spam
22 '';
23 learn-ham = pkgs.writeShellScriptBin "learn-ham.sh" ''
24 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_ham
25 '';
26
27 dovecot-virtual = pkgs.buildEnv {
28 name = "dovecot-virtual";
29 pathsToLink = [ "/" ];
30 paths = [
31 dovecot-virtual-all
32 dovecot-virtual-recents
33 ];
34 };
35 dovecot-virtual-all = pkgs.writeTextFile {
36 name = "dovecot-virtual-all";
37 destination = "/All/dovecot-virtual";
38 text = ''
39 *
40 all
41 '';
42 };
43 dovecot-virtual-recents = pkgs.writeTextFile {
44 name = "dovecot-virtual-recents";
45 destination = "/Recents/dovecot-virtual";
46 text = ''
47 *
48 all younger 172800
49 '';
50 };
51 dovecot-virtual-pop3 = pkgs.writeTextFile {
52 name = "dovecot-virtual-pop3";
53 destination = "/pop3/INBOX/dovecot-virtual";
54 text = ''
55 All
56 All+*
57 all
58 '';
59 };
60 in
61 {
62 imports = [
63 dovecot/sourcephile.fr.nix
64 dovecot/autogeree.net.nix
65 ];
66 users.groups.acme.members = [ dovecot2.user ];
67 systemd.services.dovecot2 = {
68 after = [
69 "postfix.service"
70 "openldap.service"
71 ];
72 /*
73 preStart = ''
74 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
75 install -D -d -m 0771 \
76 -o "${dovecot2.user}" \
77 -g "${dovecot2.group}" \
78 ${stateDir}/mail
79 '';
80 */
81 preStart = ''
82 ln -fns -t ${stateDir}/virtual/ \
83 ${dovecot-virtual-all}/All \
84 ${dovecot-virtual-recents}/Recents
85 '';
86 serviceConfig = {
87 #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'";
88 # Dovecot2 does not work with environment.memoryAllocator.provider="scudo"
89 # https://wiki.dovecot.org/Design/Memory
90 # Scudo ERROR: CHECK failed at /build/compiler-rt-7.1.0.src/lib/scudo/../sanitizer_common/sanitizer_allocator_primary64.h:
91 # 644 ((beg)) == ((address_range.MapOrDie(beg, size))) (4398046511092, 4398046507008)
92 BindReadOnlyPaths = [ "/dev/null:/etc/ld-nix.so.preload" ];
93 StateDirectory = ["dovecot/virtual"];
94 };
95 };
96 #users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
97 networking.nftables.ruleset = ''
98 add rule inet filter net2fw tcp dport 993 counter accept comment "IMAPS"
99 #add rule inet filter net2fw tcp dport 995 counter accept comment "POP3S"
100 add rule inet filter net2fw tcp dport 4190 counter accept comment "Sieve"
101 '';
102 fileSystems."/var/lib/dovecot" = {
103 device = "rpool/var/mail";
104 fsType = "zfs";
105 };
106 services.dovecot2 = {
107 enable = true;
108 modules = [
109 pkgs.dovecot_pigeonhole
110 pkgs.dovecot_fts_xapian
111 ];
112 enablePAM = false;
113 enableImap = true;
114 enableLmtp = true;
115 enablePop3 = false;
116 protocols = [ "sieve" ];
117 # If needed, may be overrided by userdb_mail= in passdb, or mail= in userdb
118 # Here INDEX and CONTROL are separated,
119 # it's not useful since there is no quota at the filesystem level
120 # it's just to let this possibility later on.
121 mailLocation = "sdbox:${stateDir}/home/%d/%n/mail:UTF-8:CONTROL=${stateDir}/control/%d/%n:INDEX=${stateDir}/index/%d/%n";
122 createMailUser = false;
123 mailUser = "";
124 mailGroup = "";
125 sslServerCert = null;
126 sieveScripts = {
127 global = dovecot/sieve/global;
128 };
129 extraConfig = ''
130 auth_verbose = no
131 auth_debug = no
132 mail_debug = no
133 verbose_ssl = no
134 log_timestamp = "%Y-%m-%d %H:%M:%S "
135
136 ssl = required
137 ssl_dh = <${inputs.secrets + "/openssl/dh.pem"}
138 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
139 ssl_prefer_server_ciphers = yes
140 ssl_cert = </var/lib/acme/${networking.domain}/fullchain.pem
141 ssl_key = </var/lib/acme/${networking.domain}/key.pem
142 #ssl_ca = <''${caPath}
143 #ssl_verify_client_cert = yes
144 # FIXME: change to TLSv1.3 when supported by K-9 Mail
145 ssl_min_protocol = TLSv1.2
146
147 listen = *
148 mail_home = ${stateDir}/home/%d/%n
149 # Read multiple mails in parallel, improves performance
150 mail_prefetch_count = 20
151 # Default VSZ (virtual memory size) limit for service processes. This is mainly
152 # intended to catch and kill processes that leak memory before they eat up everything.
153 # Increased for fts_xapian.
154 default_vsz_limit = 1G
155
156 # DOC: https://doc.dovecot.org/configuration_manual/authentication/authentication_mechanisms/
157 # DOC: https://ldapwiki.com/wiki/DIGEST-MD5
158 # The misunderstanding is the notion "password sent over the network in plain-text" is not secure,
159 # when in fact, storing the password in the directory server using a Salted SHA-2 hash
160 # and transmitting the clear text password over a secure connection
161 # is more secure than having the directory server store the password in clear text
162 # or in a reversible encryption scheme.
163 # Therefore, SASL with DIGEST-MD5 (or CRAM-MD5) should be avoided.
164 # Use doveadm pw -s SSHA
165 # or doveadm pw -s PBKDF2 -r 10000
166 # or slappasswd -o module-load=pw-pbkdf2 -h "{PBKDF2-SHA256}"
167 auth_mechanisms = plain login
168 auth_username_format = %Ln
169 service auth {
170 user = ${dovecot2.user}
171 # auth_socket_path points to this userdb socket by default. It's typically
172 # used by dovecot-lda, doveadm, possibly imap process, etc.
173 # Users that have access to this socket are able to get a list
174 # of all usernames and get results of everyone's userdb lookups.
175 unix_listener auth-userdb {
176 user = ${dovecot2.user}
177 group = ${dovecot2.group}
178 mode = 0660
179 }
180 unix_listener /var/lib/postfix/queue/private/auth {
181 user = ${postfix.user}
182 group = ${postfix.group}
183 mode = 0660
184 }
185 }
186 service auth-worker {
187 user = ${dovecot2.user}
188 }
189 auth_cache_verify_password_with_worker = yes
190 # SEE: http://wiki2.dovecot.org/Quota/FS
191 # NOTE: postfix does not supply a client cert.
192 auth_ssl_require_client_cert = no
193 #auth_ssl_username_from_cert = yes
194 # NOTE: lowercase the username, help with LDAP?
195 auth_username_format = %Lu
196 # NOTE: sync with LDAP's data.
197 first_valid_uid = 1000
198 disable_plaintext_auth = yes
199
200 userdb {
201 driver = ldap
202 # A different path than passdb's args enables non-blocking LDAP requests
203 args = ${pkgs.symlinkJoin {name="ldap"; paths=[./dovecot];}}/ldap.conf
204 default_fields =
205 override_fields =
206 }
207
208 # No dirty syncs for maildir: locations which may be used directly by neomutt
209 maildir_very_dirty_syncs = no
210 namespace Inbox {
211 type = private
212 inbox = yes
213 hidden = no
214 list = yes
215 prefix =
216 separator = +
217 }
218 namespace Shared {
219 type = shared
220 #list = children
221 # NOTE: always listed in the LIST command.
222 list = yes
223 # NOTE: how to access the other users' mailboxes.
224 # NOTE: %var expands to the logged in user's variable, while
225 # %%var expands to the other users' variables.
226 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
227 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
228 prefix = Partages+%%n+
229 separator = +
230 subscriptions = yes
231 }
232 mail_plugins = $mail_plugins virtual
233 namespace Virtual {
234 prefix = Virtual+
235 separator = +
236 hidden = no
237 list = yes
238 subscriptions = no
239 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
240 }
241
242 mail_plugins = $mail_plugins acl
243 plugin {
244 acl = vfile:/etc/dovecot/acl/global
245 acl_anyone = allow
246 # NOTE: to let users LIST mailboxes shared by other users,
247 # Dovecot needs a shared mailbox dictionary.
248 # FIXME: %d not working with userdb ldap
249 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
250 }
251
252 mail_plugins = $mail_plugins fts fts_xapian
253 plugin {
254 # WARNING: doveadm fts rescan is buggy, it will delete the index:
255 # https://dovecot.org/pipermail/dovecot/2019-February/114709.html
256 # If need be, use rather: doveadm index -u "*@sourcephile.fr" -q '*'
257 plugin = fts fts_xapian
258 fts = xapian
259 fts_autoindex = yes
260 fts_autoindex_exclude = \Junk
261 fts_autoindex_exclude2 = \Trash
262 fts_enforced = yes
263 # 2 and 20 are the NGram values for header fields, which means the
264 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
265 # Full words are also added by default.
266 fts_xapian = partial=2 full=20 attachments=1 verbose=0
267 fts_languages = en fr
268 #fts_dovecot_fs = posix:prefix=%h/fts/
269 }
270
271 mail_plugins = $mail_plugins quota
272 plugin {
273 quota = maildir:User quota
274 quota_rule = *:storage=256M
275 quota_rule2 = Trash:storage=+64M
276 quota_max_mail_size = 20M
277 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
278 quota_warning = storage=95%% quota-warning 95 %u
279 quota_warning2 = storage=80%% quota-warning 80 %u
280 quota_warning3 = -storage=100%% quota-warning below %u
281 }
282
283 protocol lda {
284 hostname = ${networking.domain}
285 mail_plugins = $mail_plugins sieve
286 postmaster_address = postmaster+dovecot+lda@${networking.domain}
287 syslog_facility = mail
288 }
289 lda_mailbox_autocreate = yes
290 lda_mailbox_autosubscribe = yes
291
292 protocol lmtp {
293 mail_plugins = $mail_plugins sieve
294 postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
295 }
296 service lmtp {
297 process_min_avail = ${toString host.CPUs}
298 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
299 user = ${postfix.user}
300 group = ${postfix.group}
301 mode = 0600
302 }
303 }
304 plugin {
305 # Let extension.sieve do the handling of the detail
306 #lmtp_save_to_detail_mailbox = yes
307 recipient_delimiter = ${postfix.recipientDelimiter}
308 }
309
310 service imap {
311 # Most of the memory goes to mmap()ing files.
312 # You may need to increase this limit if you have huge mailboxes.
313 #vsz_limit =
314 process_limit = 1024
315 }
316 service imap-login {
317 #inet_listener imap {
318 # address = 127.0.0.1
319 # port = 143
320 # ssl = no
321 #}
322 inet_listener imaps {
323 port = 993
324 ssl = yes
325 }
326 }
327 # Store METADATA information within user's HOME directory
328 mail_attribute_dict = file:%h/dovecot-attributes
329 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
330 imapc_features = $imapc_features fetch-headers
331 protocol imap {
332 #mail_max_userip_connections = 10
333 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve virtual
334 imap_metadata = yes
335
336 # DOC: https://wiki.dovecot.org/MailboxSettings
337 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
338 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
339 imap_capability = +SPECIAL-USE
340
341 namespace Inbox {
342 mailbox "Archives" {
343 auto = subscribe
344 special_use = \Archive
345 }
346 mailbox "Drafts" {
347 auto = no
348 special_use = \Drafts
349 }
350 mailbox "Brouillons" {
351 auto = subscribe
352 special_use = \Drafts
353 }
354 mailbox "Junk" {
355 auto = no
356 #autoexpunge = 30d
357 special_use = \Junk
358 }
359 mailbox "Spams" {
360 auto = no
361 special_use = \Junk
362 }
363 mailbox "Spam" {
364 auto = no
365 special_use = \Junk
366 }
367 mailbox "Pourriels" {
368 auto = no
369 special_use = \Junk
370 }
371 mailbox "Pourriel" {
372 auto = no
373 special_use = \Junk
374 }
375 mailbox "Sent" {
376 auto = no
377 special_use = \Sent
378 }
379 mailbox "Sent Items" {
380 auto = no
381 special_use = \Sent
382 }
383 mailbox "Sent Messages" {
384 auto = no
385 special_use = \Sent
386 }
387 mailbox "Envoyés" {
388 auto = no
389 special_use = \Sent
390 }
391 mailbox "Trash" {
392 auto = no
393 special_use = \Trash
394 #autoexpunge = 30d
395 }
396 mailbox "Corbeille" {
397 auto = no
398 special_use = \Trash
399 }
400 }
401 namespace Virtual {
402 mailbox All {
403 auto = no
404 comment = All messages from all mailboxes
405 # This flag is confused with \Archives by K9-Mail
406 special_use = \All
407 }
408 mailbox Recents {
409 auto = no
410 comment = All messages from all mailboxes, arrived in the past 48h
411 }
412 }
413 }
414 plugin {
415 sieve_plugins = sieve_imapsieve sieve_extprograms
416 sieve_global_extensions = +vnd.dovecot.environment +vnd.dovecot.pipe
417 #sieve_extensions = +editheader
418 sieve = file:~/sieve;active=~/active.sieve
419 sieve_default = file:${stateDir}/sieve/global/default.sieve
420 sieve_default_name = main
421 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
422 sieve_max_script_size = 1M
423 sieve_quota_max_scripts = 0
424 sieve_quota_max_storage = 10M
425 #sieve_spamtest_max_value = 10
426 #sieve_spamtest_status_header = X-Spam-Score
427 #sieve_spamtest_status_type = strlen
428 #sieve_user_log = ~/sieve.log
429 # Enables support for user Sieve scripts in IMAP
430 #imapsieve_url = sieve://mail.${networking.domain}:4190
431
432 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
433 imapsieve_mailbox1_name = *
434 imapsieve_mailbox1_causes = FLAG
435 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
436
437 # From elsewhere to Junk folder
438 imapsieve_mailbox2_name = Pourriel
439 imapsieve_mailbox2_causes = COPY APPEND
440 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
441
442 # From Junk folder to elsewhere
443 imapsieve_mailbox3_name = *
444 imapsieve_mailbox3_from = Pourriel
445 imapsieve_mailbox3_causes = COPY
446 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
447 }
448 protocol sieve {
449 # Maximum number of ManageSieve connections allowed for a user from each IP address.
450 # NOTE: The username is compared case-sensitively.
451 mail_max_userip_connections = 10
452
453 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
454 # the IMPLEMENTATION capability that the dovecot reports to clients.
455 # For example: 'Cyrus timesieved v2.2.13'
456 managesieve_implementation_string = Dovecot Pigeonhole
457
458 # The maximum number of compile errors that are returned to the client upon script
459 # upload or script verification.
460 managesieve_max_compile_errors = 5
461
462 #mail_max_userip_connections = 10
463 #managesieve_implementation_string = Dovecot Pigeonhole
464 managesieve_max_compile_errors = 5
465 #managesieve_max_line_length = 65536
466 #managesieve_notify_capability = mailto
467 #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
468 }
469 service managesieve-login {
470 inet_listener sieve {
471 port = 4190
472 #ssl = yes
473 }
474
475 # Number of connections to handle before starting a new process. Typically
476 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
477 # is faster. <doc/wiki/LoginProcess.txt>
478 service_count = 1
479
480 # Number of processes to always keep waiting for more connections.
481 process_min_avail = 0
482
483 # If you set service_count=0, you probably need to grow this.
484 #vsz_limit = 64M
485 }
486
487 service quota-warning {
488 executable = script ${
489 pkgs.writeShellScript "quota-warning" ''
490 set -eu
491 PERCENT=$1
492 USER=$2
493 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
494 "plugin/quota=maildir:User quota:noenforcing"
495 From: postmaster@${networking.domain}
496 Subject: [WARNING] your mailbox is now $PERCENT% full.
497
498 Please remove some mails to make room for new ones.
499 EOF
500 ''
501 }
502 # use some unprivileged user for executing the quota warnings
503 user = ${dovecot2.user}
504 unix_listener quota-warning {
505 }
506 }
507
508 protocol pop3 {
509 mail_plugins = $mail_plugins virtual
510 #mail_max_userip_connections = 10
511 # Virtual namespace for the virtual INBOX.
512 # Use a global directory for dovecot-virtual files.
513 #namespace Inbox {
514 # hidden = yes
515 # list = no
516 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
517 # prefix = pop3+
518 #}
519 pop3_client_workarounds =
520 pop3_fast_size_lookups = yes
521 pop3_lock_session = yes
522 pop3_no_flag_updates = yes
523 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
524 pop3_uidl_format = %g
525 }
526 service pop3 {
527 process_limit = 1024
528 }
529 service pop3-login {
530 inet_listener pop3s {
531 port = 995
532 ssl = yes
533 }
534 }
535 '';
536 };
537 }