1 { lib, pkgs, config, ... }:
3 sieve_bin = pkgs.runCommand "sieve_bin" {
4 buildInputs = [ pkgs.makeWrapper ];
6 cp -a ${./sieve_bin} $out
10 wrapProgram "$i" --prefix PATH : ${lib.makeBinPath [ pkgs.coreutils ]}
15 config = lib.mkIf config.myServices.mail.enable {
16 systemd.services.dovecot2.serviceConfig.Slice = "mail.slice";
17 secrets.keys."dovecot/ldap" = {
18 user = config.services.dovecot2.user;
19 group = config.services.dovecot2.group;
22 hosts = ${config.myEnv.mail.dovecot.ldap.host}
25 dn = ${config.myEnv.mail.dovecot.ldap.dn}
26 dnpass = ${config.myEnv.mail.dovecot.ldap.password}
32 base = ${config.myEnv.mail.dovecot.ldap.base}
35 pass_filter = ${config.myEnv.mail.dovecot.ldap.filter}
36 pass_attrs = ${config.myEnv.mail.dovecot.ldap.pass_attrs}
38 user_attrs = ${config.myEnv.mail.dovecot.ldap.user_attrs}
39 user_filter = ${config.myEnv.mail.dovecot.ldap.filter}
40 iterate_attrs = ${config.myEnv.mail.dovecot.ldap.iterate_attrs}
41 iterate_filter = ${config.myEnv.mail.dovecot.ldap.iterate_filter}
47 uid = config.ids.uids.vhost;
49 users.groups.vhost.gid = config.ids.gids.vhost;
51 # https://blog.zeninc.net/index.php?post/2018/04/01/Un-annuaire-pour-les-gouverner-tous.......
58 protocols = [ "sieve" ];
60 pkgs.dovecot_pigeonhole
61 pkgs.dovecot_fts-xapian
65 createMailUser = false;
67 Trash = { auto = "subscribe"; specialUse = "Trash"; };
68 Junk = { auto = "subscribe"; specialUse = "Junk"; };
69 Sent = { auto = "subscribe"; specialUse = "Sent"; };
70 Drafts = { auto = "subscribe"; specialUse = "Drafts"; };
72 mailLocation = "mbox:~/Mail:INBOX=~/Mail/Inbox:INDEX=~/.imap";
73 sslServerCert = "/var/lib/acme/mail/fullchain.pem";
74 sslServerKey = "/var/lib/acme/mail/key.pem";
75 sslCACert = "/var/lib/acme/mail/fullchain.pem";
76 extraConfig = builtins.concatStringsSep "\n" [
77 # For printer which doesn’t support elliptic curve
79 ssl_alt_cert = </var/lib/acme/mail-rsa/fullchain.pem
80 ssl_alt_key = </var/lib/acme/mail-rsa/key.pem
84 postmaster_address = postmaster@immae.eu
85 mail_attribute_dict = file:%h/dovecot-attributes
86 imap_idle_notify_interval = 20 mins
97 mail_plugins = $mail_plugins acl
99 acl = vfile:${pkgs.writeText "dovecot-acl" ''
102 acl_globals_only = yes
108 # needs to be bigger than any mailbox size
109 default_vsz_limit = 2GB
110 mail_plugins = $mail_plugins fts fts_xapian
112 plugin = fts fts_xapian
114 fts_xapian = partial=2 full=20
116 fts_autoindex_exclude = \Junk
117 fts_autoindex_exclude2 = \Trash
118 fts_autoindex_exclude3 = Virtual/*
123 # https://docs.iredmail.org/dovecot.imapsieve.html
125 # imap_sieve plugin added below
128 sieve_plugins = sieve_imapsieve sieve_extprograms
129 imapsieve_url = sieve://127.0.0.1:4190
131 sieve_before = file:${./sieve_scripts}/backup.sieve;bindir=/var/lib/vhost/.sieve_bin
133 # From elsewhere to Junk folder
134 imapsieve_mailbox1_name = Junk
135 imapsieve_mailbox1_causes = COPY APPEND
136 imapsieve_mailbox1_before = file:${./sieve_scripts}/report_spam.sieve;bindir=/var/lib/vhost/.imapsieve_bin
138 # From Junk folder to elsewhere
139 imapsieve_mailbox2_name = *
140 imapsieve_mailbox2_from = Junk
141 imapsieve_mailbox2_causes = COPY
142 imapsieve_mailbox2_before = file:${./sieve_scripts}/report_ham.sieve;bindir=/var/lib/vhost/.imapsieve_bin
144 # From anywhere to NoJunk folder
145 imapsieve_mailbox3_name = NoJunk
146 imapsieve_mailbox3_causes = COPY APPEND
147 imapsieve_mailbox3_before = file:${./sieve_scripts}/report_ham.sieve;bindir=/var/lib/vhost/.imapsieve_bin
149 sieve_pipe_bin_dir = ${sieve_bin}
151 sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.environment
159 inet_listener imaps {
165 inet_listener pop3s {
173 unix_listener auth-userdb {
175 unix_listener ${config.services.postfix.config.queue_directory}/private/auth {
179 service auth-worker {
186 unix_listener stats-reader {
191 unix_listener stats-writer {
201 first_valid_uid = ${toString config.ids.uids.vhost}
202 disable_plaintext_auth = yes
205 args = ${config.secrets.fullPaths."dovecot/ldap"}
209 args = ${config.secrets.fullPaths."dovecot/ldap"}
215 mail_plugins = $mail_plugins zlib
225 sieve = file:~/sieve;bindir=~/.sieve-bin;active=~/.dovecot.sieve
227 service managesieve-login {
229 service managesieve {
235 mail_plugins = $mail_plugins virtual
238 location = virtual:~/Virtual
242 # Protocol specific configuration
243 # Needs to come last if there are mail_plugins entries
246 mail_plugins = $mail_plugins imap_sieve imap_acl
249 mail_plugins = $mail_plugins sieve
254 networking.firewall.allowedTCPPorts = [ 110 143 993 995 4190 ];
255 system.activationScripts.dovecot = {
258 install -m 0755 -o vhost -g vhost -d /var/lib/vhost
262 services.cron.systemCronJobs = let
263 cron_script = pkgs.writeScriptBin "cleanup-imap-folders" ''
264 ${pkgs.dovecot}/bin/doveadm expunge -A MAILBOX "Backup/*" NOT FLAGGED BEFORE 8w 2>&1 > /dev/null | grep -v "Mailbox doesn't exist:" | grep -v "Info: Opening DB"
265 ${pkgs.dovecot}/bin/doveadm expunge -A MAILBOX Junk SEEN NOT FLAGGED BEFORE 4w 2>&1 > /dev/null | grep -v "Mailbox doesn't exist:" | grep -v "Info: Opening DB"
266 ${pkgs.dovecot}/bin/doveadm expunge -A MAILBOX Trash NOT FLAGGED BEFORE 4w 2>&1 > /dev/null | grep -v "Mailbox doesn't exist:" | grep -v "Info: Opening DB"
270 "0 2 * * * root ${cron_script}/bin/cleanup-imap-folders"
272 security.acme.certs."mail-rsa" = {
274 systemctl restart dovecot2.service
277 "imap.immae.eu" = null;
278 "pop3.immae.eu" = null;
281 security.acme.certs."mail" = {
283 systemctl restart dovecot2.service
286 "imap.immae.eu" = null;
287 "pop3.immae.eu" = null;