2022-12-04 13:45:43 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
2023-09-08 21:29:08 +01:00
|
|
|
mail_config = config.services.mailserver;
|
2022-06-22 18:05:02 +01:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
vmail_config = mail_config.vmail_config;
|
|
|
|
vmail_user = vmail_config.user;
|
|
|
|
vmail_group = vmail_config.group;
|
|
|
|
|
2022-06-22 18:05:02 +01:00
|
|
|
sieve_directory = mail_config.sieve_directory;
|
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
virtualMailUsersActivationScript = pkgs.writeScript "activate-virtual-mail-users" ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
# Create directory to store user sieve scripts if it doesn't exist
|
|
|
|
if (! test -d "${sieve_directory}"); then
|
|
|
|
mkdir "${sieve_directory}"
|
|
|
|
chown "${vmail_user}:${vmail_group}" "${sieve_directory}"
|
|
|
|
chmod 770 "${sieve_directory}"
|
|
|
|
fi
|
2022-06-22 18:05:02 +01:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
# Copy user's sieve script to the correct location (if it exists). If it
|
|
|
|
# is null, remove the file.
|
|
|
|
${lib.concatMapStringsSep "\n" ({
|
|
|
|
name,
|
|
|
|
sieveScript,
|
|
|
|
}:
|
|
|
|
if lib.isString sieveScript
|
|
|
|
then ''
|
|
|
|
if (! test -d "${sieve_directory}/${name}"); then
|
|
|
|
mkdir -p "${sieve_directory}/${name}"
|
|
|
|
chown "${vmail_user}:${vmail_group}" "${sieve_directory}/${name}"
|
|
|
|
chmod 770 "${sieve_directory}/${name}"
|
|
|
|
fi
|
|
|
|
cat << 'EOF' > "${sieve_directory}/${name}/default.sieve"
|
|
|
|
${sieveScript}
|
|
|
|
EOF
|
|
|
|
chown "${vmail_user}:${vmail_group}" "${sieve_directory}/${name}/default.sieve"
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
if (test -f "${sieve_directory}/${name}/default.sieve"); then
|
|
|
|
rm "${sieve_directory}/${name}/default.sieve"
|
|
|
|
fi
|
|
|
|
if (test -f "${sieve_directory}/${name}.svbin"); then
|
|
|
|
rm "${sieve_directory}/${name}/default.svbin"
|
|
|
|
fi
|
|
|
|
'') (map (user: {inherit (user) name sieveScript;})
|
|
|
|
(lib.attrValues mail_config.accounts))}
|
|
|
|
'';
|
2022-06-22 15:51:22 +01:00
|
|
|
in {
|
2022-12-04 13:45:43 +00:00
|
|
|
config = lib.mkIf (mail_config.enable) {
|
|
|
|
users.users."${vmail_user}" = {
|
2022-11-17 12:06:16 +00:00
|
|
|
isSystemUser = true;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
|
|
|
home = vmail_config.directory;
|
2022-11-17 12:06:16 +00:00
|
|
|
createHome = true;
|
2022-12-04 13:45:43 +00:00
|
|
|
|
|
|
|
uid = vmail_config.user_id;
|
|
|
|
group = "${vmail_group}";
|
2022-11-17 12:06:16 +00:00
|
|
|
};
|
2022-12-04 13:45:43 +00:00
|
|
|
users.groups."${vmail_group}" = {gid = vmail_config.group_id;};
|
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"];
|
|
|
|
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
|
|
|
}
|