From 72748d7b6d706ca17817389952a3a7d9185b0d5c Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Fri, 23 Jul 2021 22:57:39 +0200 Subject: [PATCH] Use the Junk mailbox name defined in the mailboxes attrs Previously, the static Junk mailbox was used in sieve script to move spam messages. This patch gets the Junk mailbox defined in the dovecot mailboxes attribute instead. Fixes #224 --- mail-server/dovecot.nix | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/mail-server/dovecot.nix b/mail-server/dovecot.nix index 3781bbd..bc947ae 100644 --- a/mail-server/dovecot.nix +++ b/mail-server/dovecot.nix @@ -85,9 +85,22 @@ let chmod 600 ${passwdFile} ''; + + junkMailboxes = builtins.attrNames (lib.filterAttrs (n: v: v ? "specialUse" && v.specialUse == "Junk") cfg.mailboxes); + junkMailboxNumber = builtins.length junkMailboxes; + # The assertion garantees there is exactly one Junk mailbox. + junkMailboxName = if junkMailboxNumber == 1 then builtins.elemAt junkMailboxes 0 else ""; + in { config = with cfg; lib.mkIf enable { + assertions = [ + { + assertion = junkMailboxNumber == 1; + message = "nixos-mailserver requires exactly one dovecot mailbox with the 'special use' flag set to 'Junk' (${builtins.toString junkMailboxNumber} have been found)"; + } + ]; + services.dovecot2 = { enable = true; enableImap = enableImap || enableImapSsl; @@ -109,7 +122,7 @@ in require "fileinto"; if header :is "X-Spam" "Yes" { - fileinto "Junk"; + fileinto "${junkMailboxName}"; stop; } ''; @@ -229,13 +242,13 @@ in sieve_default_name = default # From elsewhere to Spam folder - imapsieve_mailbox1_name = Junk + imapsieve_mailbox1_name = ${junkMailboxName} imapsieve_mailbox1_causes = COPY imapsieve_mailbox1_before = file:${stateDir}/imap_sieve/report-spam.sieve # From Spam folder to elsewhere imapsieve_mailbox2_name = * - imapsieve_mailbox2_from = Junk + imapsieve_mailbox2_from = ${junkMailboxName} imapsieve_mailbox2_causes = COPY imapsieve_mailbox2_before = file:${stateDir}/imap_sieve/report-ham.sieve