From 43d36d9b76971705ae9952ef052ac16e5bb23e9a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20D=C3=B6rfler?= <phdoerfler@gmail.com>
Date: Fri, 2 Feb 2018 20:37:29 +0000
Subject: [PATCH] Dovecot: Mailbox config + hierarchy separator + FS layout. -
 Factored mailbox config into its own option. - Added hierarchy separator
 option. - Added option for using FS layout.

---
 default.nix             | 58 +++++++++++++++++++++++++++++++++++++++++
 mail-server/dovecot.nix | 31 ++++++----------------
 2 files changed, 66 insertions(+), 23 deletions(-)

diff --git a/default.nix b/default.nix
index 7fd29fd..ae51c8d 100644
--- a/default.nix
+++ b/default.nix
@@ -201,6 +201,64 @@ in
       '';
     };
 
+    useFsLayout = mkOption {
+      type = types.bool;
+      default = false;
+      description = ''
+        Sets whether dovecot should organize mail in subdirectories:
+
+        - /var/vmail/example.com/user/.folder.subfolder/ (default layout)
+        - /var/vmail/example.com/user/folder/subfolder/  (FS layout)
+
+        See https://wiki2.dovecot.org/MailboxFormat/Maildir for details.
+      '';
+    };
+
+    hierarchySeparator = mkOption {
+      type = types.string;
+      default = ".";
+      description = ''
+        The hierarchy separator for mailboxes used by dovecot for the namespace 'inbox'.
+        Dovecot defaults to "." but recommends "/".
+        This affects how mailboxes appear to mail clients and sieve scripts.
+        For instance when using "." then in a sieve script "example.com" would refer to the mailbox "com" in the parent mailbox "example".
+        This does not determine the way your mails are stored on disk.
+        See https://wiki.dovecot.org/Namespaces for details.
+      '';
+    };
+
+    mailboxes = mkOption {
+      description = ''
+        The mailboxes for dovecot.
+        Depending on the mail client used it might be necessary to change some mailbox's name.
+      '';
+      default = [
+        {
+          name = "Trash";
+          auto = "no";
+          specialUse = "Trash";
+        }
+
+        {
+          name = "Junk";
+          auto = "subscribe";
+          specialUse = "Junk";
+        }
+
+        {
+          name = "Drafts";
+          auto = "subscribe";
+          specialUse = "Drafts";
+        }
+
+        {
+          name = "Sent";
+          auto = "subscribe";
+          specialUse = "Sent";
+        }
+      ];
+    };
+
     certificateScheme = mkOption {
       type = types.enum [ 1 2 3 ];
       default = 2;
diff --git a/mail-server/dovecot.nix b/mail-server/dovecot.nix
index fd99837..26a8002 100644
--- a/mail-server/dovecot.nix
+++ b/mail-server/dovecot.nix
@@ -21,11 +21,13 @@ with (import ./common.nix { inherit config lib; });
 let
   cfg = config.mailserver;
 
-  # maildir in format "/${domain}/${user}"
-  dovecot_maildir = "maildir:${cfg.mailDirectory}/%d/%n";
+  maildirLayoutAppendix = lib.optionalString cfg.useFsLayout ":LAYOUT=fs";
 
   dovecotVersion = builtins.fromJSON
     (builtins.readFile (pkgs.callPackage ./dovecot-version.nix {}));
+
+  # maildir in format "/${domain}/${user}"
+  dovecotMaildir = "maildir:${cfg.mailDirectory}/%d/%n${maildirLayoutAppendix}";
 in
 {
   config = with cfg; lib.mkIf enable {
@@ -37,7 +39,7 @@ in
       enableQuota = true;
       mailGroup = vmailGroupName;
       mailUser = vmailUserName;
-      mailLocation = dovecot_maildir;
+      mailLocation = dovecotMaildir;
       sslServerCert = certificatePath;
       sslServerKey = keyPath;
       enableLmtp = true;
@@ -55,6 +57,8 @@ in
         '';
       };
 
+      mailboxes = cfg.mailboxes;
+
       extraConfig = ''
         #Extra Config
         ${lib.optionalString debug ''
@@ -102,27 +106,8 @@ in
         auth_mechanisms = plain login
 
         namespace inbox {
+          separator = ${cfg.hierarchySeparator}
           inbox = yes
-
-          mailbox "Trash" {
-            auto = no
-            special_use = \Trash
-          }
-
-          mailbox "Junk" {
-            auto = subscribe
-            special_use = \Junk
-          }
-
-          mailbox "Drafts" {
-            auto = subscribe
-            special_use = \Drafts
-          }
-
-          mailbox "Sent" {
-            auto = subscribe
-            special_use = \Sent
-          }
         }
 
         plugin {