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.strings) concatStringsSep;
|
|
|
|
inherit (lib.attrsets) mapAttrsToList;
|
2022-06-22 18:05:02 +01:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
mailConfig = config.services.mailserver;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
2024-03-10 17:26:18 +00:00
|
|
|
inherit (mailConfig) vmail;
|
2023-09-18 03:56:58 +01:00
|
|
|
vmailUser = vmail.user;
|
|
|
|
vmailGroup = vmail.group;
|
|
|
|
|
2024-03-10 17:26:18 +00:00
|
|
|
inherit (mailConfig) sieveDirectory;
|
2023-09-18 03:56:58 +01:00
|
|
|
|
|
|
|
scriptForUser = name: config:
|
|
|
|
if builtins.isString config.sieveScript
|
|
|
|
then ''
|
|
|
|
cat ${builtins.toFile "default.sieve" config.sieveScript} > "${sieveDirectory}/${name}/default.sieve"
|
|
|
|
chown "${vmailUser}:${vmailGroup}" "${sieveDirectory}/${name}/default.sieve"
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
if [ -f "${sieveDirectory}/${name}/default.sieve" ]; then
|
|
|
|
rm "${sieveDirectory}/${name}/default.sieve"
|
|
|
|
fi
|
|
|
|
if [ -f "${sieveDirectory}/${name}.svbin" ]; then
|
|
|
|
rm "${sieveDirectory}/${name}/default.svbin"
|
|
|
|
fi
|
|
|
|
'';
|
2022-06-22 18:05:02 +01:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
virtualMailUsersActivationScript = pkgs.writeScript "activate-virtual-mail-users" ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
${concatStringsSep "\n" (mapAttrsToList (name: config: scriptForUser name config) mailConfig.accounts)}
|
2022-12-04 13:45:43 +00:00
|
|
|
'';
|
2022-06-22 15:51:22 +01:00
|
|
|
in {
|
2024-03-10 17:26:18 +00:00
|
|
|
config = mkIf mailConfig.enable {
|
2023-09-18 03:56:58 +01:00
|
|
|
users.users."${vmailUser}" = {
|
2022-11-17 12:06:16 +00:00
|
|
|
isSystemUser = true;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
home = vmail.directory;
|
2022-11-17 12:06:16 +00:00
|
|
|
createHome = true;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
uid = vmail.userID;
|
|
|
|
group = "${vmailGroup}";
|
2022-11-17 12:06:16 +00:00
|
|
|
};
|
2023-09-18 03:56:58 +01:00
|
|
|
|
|
|
|
users.groups."${vmailGroup}" = {
|
|
|
|
gid = vmail.groupID;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules =
|
|
|
|
[
|
|
|
|
"d '${sieveDirectory}' - ${vmailUser} ${vmailGroup} - -"
|
|
|
|
]
|
|
|
|
++ (map (
|
|
|
|
email: "d '${sieveDirectory}/${email}' 770 ${vmailUser} ${vmailGroup} - -"
|
|
|
|
) (builtins.attrNames mailConfig.accounts));
|
|
|
|
|
2022-11-17 12:06:16 +00:00
|
|
|
systemd.services.activate-virtual-mail-users = {
|
2022-12-04 13:45:43 +00:00
|
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
before = ["dovecot2.service"];
|
2023-09-18 03:56:58 +01:00
|
|
|
serviceConfig.ExecStart = virtualMailUsersActivationScript;
|
2022-11-17 12:06:16 +00:00
|
|
|
enable = true;
|
|
|
|
};
|
2022-12-04 13:45:43 +00:00
|
|
|
};
|
2022-06-22 15:51:22 +01:00
|
|
|
}
|