@@ -60,6 +60,39 @@ let
6060 }
6161 '')
6262
63+ ''
64+ protocol imap {
65+ namespace inbox {
66+ inbox=yes
67+ ${concatStringsSep "\n" (map mailboxConfig cfg.mailboxes)}
68+ }
69+ }
70+ ''
71+
72+ (optionalString cfg.enableQuota ''
73+ mail_plugins = $mail_plugins quota
74+ service quota-status {
75+ executable = ${dovecotPkg}/libexec/dovecot/quota-status -p postfix
76+ inet_listener {
77+ port = ${cfg.quotaPort}
78+ }
79+ client_limit = 1
80+ }
81+
82+ protocol imap {
83+ mail_plugins = $mail_plugins imap_quota
84+ }
85+
86+ plugin {
87+ quota_rule = *:storage=${cfg.quotaGlobalPerUser}
88+ quota = maildir:User quota # per virtual mail user quota # BUG/FIXME broken, we couldn't get this working
89+ quota_status_success = DUNNO
90+ quota_status_nouser = DUNNO
91+ quota_status_overquota = "552 5.2.2 Mailbox is full"
92+ quota_grace = 10%%
93+ }
94+ '')
95+
6396 cfg.extraConfig
6497 ];
6598
@@ -68,6 +101,34 @@ let
68101 paths = map (pkg: "${pkg}/lib/dovecot") ([ dovecotPkg ] ++ map (module: module.override { dovecot = dovecotPkg; }) cfg.modules);
69102 };
70103
104+ mailboxConfig = mailbox: ''
105+ mailbox ${mailbox.name} {
106+ auto = ${toString mailbox.auto}
107+ '' + optionalString (mailbox.specialUse != null) ''
108+ special_use = \${toString mailbox.specialUse}
109+ '' + "}";
110+
111+ mailboxes = { lib, pkgs, ... }: {
112+ options = {
113+ name = mkOption {
114+ type = types.str;
115+ example = "Spam";
116+ description = "The name of the mailbox.";
117+ };
118+ auto = mkOption {
119+ type = types.enum [ "no" "create" "subscribe" ];
120+ default = "no";
121+ example = "subscribe";
122+ description = "Whether to automatically create or create and subscribe to the mailbox or not.";
123+ };
124+ specialUse = mkOption {
125+ type = types.nullOr (types.enum [ "All" "Archive" "Drafts" "Flagged" "Junk" "Sent" "Trash" ]);
126+ default = null;
127+ example = "Junk";
128+ description = "Null if no special use flag is set. Other than that every use flag mentioned in the RFC is valid.";
129+ };
130+ };
131+ };
71132in
72133{
73134
@@ -76,7 +137,7 @@ in
76137
77138 enablePop3 = mkOption {
78139 type = types.bool;
79− default = true;
140+ default = false;
80141 description = "Start the POP3 listener (when Dovecot is enabled).";
81142 };
82143
@@ -145,6 +206,14 @@ in
145206 description = "Default group to store mail for virtual users.";
146207 };
147208
209+ createMailUser = mkOption {
210+ type = types.bool;
211+ default = true;
212+ description = ''Whether to automatically create the user
213+ given in <option>services.dovecot.user</option> and the group
214+ given in <option>services.dovecot.group</option>.'';
215+ };
216+
148217 modules = mkOption {
149218 type = types.listOf types.package;
150219 default = [];
@@ -191,6 +260,36 @@ in
191260 default = false;
192261 description = "Show the PAM failure message on authentication error (useful for OTPW).";
193262 };
263+
264+ mailboxes = mkOption {
265+ type = types.listOf (types.submodule mailboxes);
266+ default = [];
267+ example = [ { name = "Spam"; specialUse = "Junk"; auto = "create"; } ];
268+ description = "Configure mailboxes and auto create or subscribe them.";
269+ };
270+
271+ enableQuota = mkOption {
272+ type = types.bool;
273+ default = false;
274+ example = true;
275+ description = "Whether to enable the dovecot quota service.";
276+ };
277+
278+ quotaPort = mkOption {
279+ type = types.str;
280+ default = "12340";
281+ description = ''
282+ The Port the dovecot quota service binds to.
283+ If using postfix, add check_policy_service inet:localhost:12340 to your smtpd_recipient_restrictions in your postfix config.
284+ '';
285+ };
286+ quotaGlobalPerUser = mkOption {
287+ type = types.str;
288+ default = "100G";
289+ example = "10G";
290+ description = "Quota limit for the user in bytes. Supports suffixes b, k, M, G, T and %.";
291+ };
292+
194293 };
195294
196295
@@ -214,11 +313,19 @@ in
214313 uid = config.ids.uids.dovecot2;
215314 description = "Dovecot user";
216315 group = cfg.group;
316+ }
317+ ++ optional cfg.createMailUser
318+ { name = cfg.mailUser;
319+ description = "Virtual Mail User";
320+ group = cfg.mailGroup;
217321 };
218322
219323 users.extraGroups = optional (cfg.group == "dovecot2")
220324 { name = "dovecot2";
221325 gid = config.ids.gids.dovecot2;
326+ }
327+ ++ optional cfg.createMailUser
328+ { name = cfg.mailGroup;
222329 };
223330
224331 environment.etc."dovecot/modules".source = modulesDir;