2022-12-04 13:45:43 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
2023-09-18 03:56:58 +01:00
|
|
|
inherit (lib.modules) mkIf;
|
|
|
|
inherit (lib.attrsets) mapAttrsToList;
|
|
|
|
inherit (lib.strings) concatStringsSep optionalString;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
mailConfig = config.services.mailserver;
|
|
|
|
vmailConfig = mailConfig.vmail;
|
2022-11-17 12:06:16 +00:00
|
|
|
|
|
|
|
postfixCfg = config.services.postfix;
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
dovecotRuntimeDir = "/run/dovecot2";
|
|
|
|
passwdFile = "${dovecotRuntimeDir}/passwd";
|
|
|
|
|
2022-11-17 12:06:16 +00:00
|
|
|
genPasswdScript = pkgs.writeScript "generate-password-file" ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
${concatStringsSep "\n" (map (userPasswdFile: ''
|
|
|
|
if [ ! -f "${userPasswdFile}" ]; then
|
|
|
|
echo "Expected password hash file ${userPasswdFile} does not exist!"
|
2022-11-17 12:06:16 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2023-09-18 03:56:58 +01:00
|
|
|
'') (mapAttrsToList (_email: config: config.passwordHashFile) mailConfig.accounts))}
|
2022-11-17 12:06:16 +00:00
|
|
|
|
|
|
|
cat <<EOF > ${passwdFile}
|
2023-09-18 03:56:58 +01:00
|
|
|
${concatStringsSep "\n" (mapAttrsToList (
|
|
|
|
email: config: "${email}:$(head -n 1 ${config.passwordHashFile})"
|
|
|
|
)
|
|
|
|
mailConfig.accounts)}
|
2022-11-17 12:06:16 +00:00
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
in {
|
2024-03-10 17:26:18 +00:00
|
|
|
config = mkIf mailConfig.enable {
|
2022-11-17 12:06:16 +00:00
|
|
|
services.dovecot2 = {
|
|
|
|
enable = true;
|
|
|
|
enableImap = true;
|
2022-12-04 13:45:43 +00:00
|
|
|
enableLmtp = true;
|
2022-11-17 12:06:16 +00:00
|
|
|
enableQuota = true;
|
2022-12-04 13:45:43 +00:00
|
|
|
enablePop3 = false;
|
|
|
|
enablePAM = false; # Not using PAM for Auth
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
mailUser = vmailConfig.user;
|
|
|
|
mailGroup = vmailConfig.group;
|
|
|
|
mailLocation = "maildir:${vmailConfig.directory}/%d/%n";
|
2022-12-04 13:45:43 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
sslServerCert = mailConfig.sslConfig.cert;
|
|
|
|
sslServerKey = mailConfig.sslConfig.key;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
|
|
|
# For Sieve
|
2024-02-08 17:46:58 +00:00
|
|
|
modules = with pkgs; [
|
|
|
|
dovecot_pigeonhole
|
|
|
|
];
|
2022-12-04 13:45:43 +00:00
|
|
|
protocols = ["sieve"];
|
2022-11-17 12:06:16 +00:00
|
|
|
|
|
|
|
mailboxes = {
|
|
|
|
Trash = {
|
|
|
|
auto = "no";
|
|
|
|
specialUse = "Trash";
|
|
|
|
};
|
|
|
|
Junk = {
|
|
|
|
auto = "subscribe";
|
|
|
|
specialUse = "Junk";
|
|
|
|
};
|
|
|
|
Drafts = {
|
|
|
|
auto = "subscribe";
|
|
|
|
specialUse = "Drafts";
|
|
|
|
};
|
|
|
|
Sent = {
|
|
|
|
auto = "subscribe";
|
|
|
|
specialUse = "Sent";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = ''
|
2023-09-18 03:56:58 +01:00
|
|
|
${optionalString mailConfig.debugMode ''
|
2022-11-17 12:06:16 +00:00
|
|
|
mail_debug = yes
|
|
|
|
auth_debug = yes
|
|
|
|
verbose_ssl = yes
|
|
|
|
''}
|
|
|
|
|
|
|
|
service imap-login {
|
|
|
|
inet_listener imap {
|
|
|
|
port = 143
|
|
|
|
}
|
|
|
|
inet_listener imaps {
|
|
|
|
port = 993
|
|
|
|
ssl = yes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol imap {
|
|
|
|
mail_max_userip_connections = 100
|
|
|
|
mail_plugins = $mail_plugins imap_sieve
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl = required
|
|
|
|
ssl_min_protocol = TLSv1.2
|
|
|
|
ssl_prefer_server_ciphers = yes
|
|
|
|
|
|
|
|
service lmtp {
|
|
|
|
unix_listener dovecot-lmtp {
|
|
|
|
group = ${postfixCfg.group}
|
|
|
|
mode = 0600
|
|
|
|
user = ${postfixCfg.user}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recipient_delimiter = "+"
|
|
|
|
lmtp_save_to_detail_mailbox = "no"
|
|
|
|
|
|
|
|
protocol lmtp {
|
|
|
|
mail_plugins = $mail_plugins sieve
|
|
|
|
}
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
mail_access_groups = "${vmailConfig.group}"
|
2022-11-17 12:06:16 +00:00
|
|
|
|
|
|
|
userdb {
|
2022-12-04 13:45:43 +00:00
|
|
|
driver = static
|
2023-09-18 03:56:58 +01:00
|
|
|
args = uid=${toString vmailConfig.userID} gid=${toString vmailConfig.groupID}
|
2022-12-04 13:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
passdb {
|
2022-11-17 12:06:16 +00:00
|
|
|
driver = passwd-file
|
|
|
|
args = ${passwdFile}
|
|
|
|
}
|
|
|
|
|
|
|
|
service auth {
|
|
|
|
unix_listener auth {
|
|
|
|
mode = 0660
|
|
|
|
user = ${postfixCfg.user}
|
|
|
|
group = ${postfixCfg.group}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auth_mechanisms = plain login
|
|
|
|
|
|
|
|
namespace inbox {
|
|
|
|
separator = "."
|
|
|
|
inbox = yes
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin {
|
|
|
|
sieve_plugins = sieve_imapsieve sieve_extprograms
|
2023-09-18 03:56:58 +01:00
|
|
|
sieve = file:${mailConfig.sieveDirectory}/%u/scripts;active=${mailConfig.sieveDirectory}/%u/active.sieve
|
|
|
|
sieve_default = file:${mailConfig.sieveDirectory}/%u/default.sieve
|
2022-11-17 12:06:16 +00:00
|
|
|
sieve_default_name = default
|
2024-02-08 17:46:58 +00:00
|
|
|
sieve_global_extensions = +vnd.dovecot.environment
|
2022-11-17 12:06:16 +00:00
|
|
|
}
|
|
|
|
lda_mailbox_autosubscribe = yes
|
|
|
|
lda_mailbox_autocreate = yes
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
systemd = {
|
|
|
|
tmpfiles.rules = [
|
|
|
|
"f ${passwdFile} 600 dovecot2 dovecot2"
|
|
|
|
];
|
|
|
|
services = {
|
|
|
|
dovecot2.preStart = ''
|
|
|
|
${genPasswdScript}
|
|
|
|
'';
|
|
|
|
postfix.restartTriggers = [genPasswdScript];
|
|
|
|
};
|
2022-11-17 12:06:16 +00:00
|
|
|
};
|
2022-12-04 13:45:43 +00:00
|
|
|
};
|
2022-11-17 12:06:16 +00:00
|
|
|
}
|