]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/mail/dovecot.nix
Remove deleted-to-trash dovecot plugin
[perso/Immae/Config/Nix.git] / modules / private / mail / dovecot.nix
1 { lib, pkgs, config, myconfig, ... }:
2 let
3 sieve_bin = pkgs.runCommand "sieve_bin" {
4 buildInputs = [ pkgs.makeWrapper ];
5 } ''
6 cp -a ${./sieve_bin} $out
7 chmod -R u+w $out
8 patchShebangs $out
9 for i in $out/*; do
10 wrapProgram "$i" --prefix PATH : ${lib.makeBinPath [ pkgs.coreutils ]}
11 done
12 '';
13 in
14 {
15 config.secrets.keys = [
16 {
17 dest = "dovecot/ldap";
18 user = config.services.dovecot2.user;
19 group = config.services.dovecot2.group;
20 permissions = "0400";
21 text = ''
22 hosts = ${myconfig.env.mail.dovecot.ldap.host}
23 tls = yes
24
25 dn = ${myconfig.env.mail.dovecot.ldap.dn}
26 dnpass = ${myconfig.env.mail.dovecot.ldap.password}
27
28 auth_bind = yes
29
30 ldap_version = 3
31
32 base = ${myconfig.env.mail.dovecot.ldap.base}
33 scope = subtree
34
35 user_filter = ${myconfig.env.mail.dovecot.ldap.filter}
36 pass_filter = ${myconfig.env.mail.dovecot.ldap.filter}
37
38 user_attrs = ${myconfig.env.mail.dovecot.ldap.user_attrs}
39 pass_attrs = ${myconfig.env.mail.dovecot.ldap.pass_attrs}
40 '';
41 }
42 ];
43
44 config.users.users.vhost = {
45 group = "vhost";
46 uid = config.ids.uids.vhost;
47 };
48 config.users.groups.vhost.gid = config.ids.gids.vhost;
49
50 # https://blog.zeninc.net/index.php?post/2018/04/01/Un-annuaire-pour-les-gouverner-tous.......
51 config.services.dovecot2 = {
52 enable = true;
53 enablePAM = false;
54 enablePop3 = true;
55 enableImap = true;
56 enableLmtp = true;
57 protocols = [ "sieve" ];
58 modules = [
59 pkgs.dovecot_pigeonhole
60 pkgs.dovecot_fts-xapian
61 ];
62 mailUser = "vhost";
63 mailGroup = "vhost";
64 createMailUser = false;
65 mailboxes = [
66 { name = "Trash"; auto = "subscribe"; specialUse = "Trash"; }
67 { name = "Junk"; auto = "subscribe"; specialUse = "Junk"; }
68 { name = "Sent"; auto = "subscribe"; specialUse = "Sent"; }
69 { name = "Drafts"; auto = "subscribe"; specialUse = "Drafts"; }
70 ];
71 mailLocation = "mbox:~/Mail:INBOX=~/Mail/Inbox:INDEX=~/.imap";
72 sslServerCert = "/var/lib/acme/mail/fullchain.pem";
73 sslServerKey = "/var/lib/acme/mail/key.pem";
74 sslCACert = "/var/lib/acme/mail/fullchain.pem";
75 extraConfig = builtins.concatStringsSep "\n" [
76 ''
77 postmaster_address = postmaster@immae.eu
78 mail_attribute_dict = file:%h/dovecot-attributes
79 imap_idle_notify_interval = 20 mins
80 namespace inbox {
81 type = private
82 separator = /
83 inbox = yes
84 list = yes
85 }
86 ''
87
88 # Full text search
89 ''
90 # needs to be bigger than any mailbox size
91 default_vsz_limit = 2GB
92 mail_plugins = $mail_plugins fts fts_xapian
93 plugin {
94 plugin = fts fts_xapian
95 fts = xapian
96 fts_xapian = partial=2 full=20
97 fts_autoindex = yes
98 fts_autoindex_exclude = \Junk
99 fts_autoindex_exclude2 = \Trash
100 fts_autoindex_exclude3 = Virtual/*
101 }
102 ''
103
104 # Antispam
105 # https://docs.iredmail.org/dovecot.imapsieve.html
106 ''
107 # imap_sieve plugin added below
108
109 plugin {
110 sieve_plugins = sieve_imapsieve sieve_extprograms
111 imapsieve_url = sieve://127.0.0.1:4190
112
113 # From elsewhere to Junk folder
114 imapsieve_mailbox1_name = Junk
115 imapsieve_mailbox1_causes = COPY APPEND
116 imapsieve_mailbox1_before = file:${./sieve_scripts}/report_spam.sieve;bindir=/var/lib/vhost/.imapsieve_bin
117
118 # From Junk folder to elsewhere
119 imapsieve_mailbox2_name = *
120 imapsieve_mailbox2_from = Junk
121 imapsieve_mailbox2_causes = COPY
122 imapsieve_mailbox2_before = file:${./sieve_scripts}/report_ham.sieve;bindir=/var/lib/vhost/.imapsieve_bin
123
124 sieve_pipe_bin_dir = ${sieve_bin}
125
126 sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.environment
127 }
128 ''
129 # Services to listen
130 ''
131 service imap-login {
132 inet_listener imap {
133 }
134 inet_listener imaps {
135 }
136 }
137 service pop3-login {
138 inet_listener pop3 {
139 }
140 inet_listener pop3s {
141 }
142 }
143 service imap {
144 }
145 service pop3 {
146 }
147 service auth {
148 unix_listener auth-userdb {
149 }
150 unix_listener ${config.services.postfix.config.queue_directory}/private/auth {
151 mode = 0666
152 }
153 }
154 service auth-worker {
155 }
156 service dict {
157 unix_listener dict {
158 }
159 }
160 service stats {
161 unix_listener stats-reader {
162 user = vhost
163 group = vhost
164 mode = 0660
165 }
166 unix_listener stats-writer {
167 user = vhost
168 group = vhost
169 mode = 0660
170 }
171 }
172 ''
173
174 # Authentification
175 ''
176 first_valid_uid = ${toString config.ids.uids.vhost}
177 disable_plaintext_auth = yes
178 passdb {
179 driver = ldap
180 args = ${config.secrets.fullPaths."dovecot/ldap"}
181 }
182 userdb {
183 driver = static
184 args = user=%u uid=vhost gid=vhost home=/var/lib/vhost/%d/%n/ mail=mbox:~/Mail:INBOX=~/Mail/Inbox:INDEX=~/.imap
185 }
186 ''
187
188 # Zlib
189 ''
190 mail_plugins = $mail_plugins zlib
191 plugin {
192 zlib_save_level = 6
193 zlib_save = gz
194 }
195 ''
196
197 # Sieve
198 ''
199 plugin {
200 sieve = file:~/sieve;bindir=~/.sieve-bin;active=~/.dovecot.sieve
201 }
202 service managesieve-login {
203 }
204 service managesieve {
205 }
206 ''
207
208 # Virtual mailboxes
209 ''
210 mail_plugins = $mail_plugins virtual
211 namespace Virtual {
212 prefix = Virtual/
213 location = virtual:~/Virtual
214 }
215 ''
216
217 # Protocol specific configuration
218 # Needs to come last if there are mail_plugins entries
219 ''
220 protocol imap {
221 mail_plugins = $mail_plugins imap_sieve
222 }
223 protocol lda {
224 mail_plugins = $mail_plugins sieve
225 }
226 ''
227 ];
228 };
229 config.networking.firewall.allowedTCPPorts = [ 110 143 993 995 4190 ];
230 config.system.activationScripts.dovecot = {
231 deps = [ "users" ];
232 text =''
233 install -m 0755 -o vhost -g vhost -d /var/lib/vhost
234 '';
235 };
236
237 config.security.acme.certs."mail" = {
238 postRun = ''
239 systemctl restart dovecot2.service
240 '';
241 extraDomains = {
242 "imap.immae.eu" = null;
243 "pop3.immae.eu" = null;
244 };
245 };
246 }
247