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