2022-06-22 18:05:02 +01:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
|
|
|
mail_config = (import ./config.nix { });
|
|
|
|
|
|
|
|
v = mail_config.vmail_config;
|
|
|
|
sieve_directory = mail_config.sieve_directory;
|
|
|
|
|
|
|
|
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 "${v.user_group_name}:${v.user_group_name}" "${sieve_directory}"
|
|
|
|
chmod 770 "${sieve_directory}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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 "${v.user_group_name}:${v.user_group_name}" "${sieve_directory}/${name}"
|
|
|
|
chmod 770 "${sieve_directory}/${name}"
|
|
|
|
fi
|
|
|
|
cat << 'EOF' > "${sieve_directory}/${name}/default.sieve"
|
|
|
|
${sieveScript}
|
|
|
|
EOF
|
|
|
|
chown "${v.user_group_name}:${v.user_group_name}" "${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 {
|
|
|
|
users.users."${v.user_group_name}" = {
|
|
|
|
name = "${v.user_group_name}";
|
|
|
|
isSystemUser = true;
|
|
|
|
uid = v.user_group_id;
|
|
|
|
home = v.directory;
|
|
|
|
createHome = true;
|
|
|
|
group = "${v.user_group_name}";
|
|
|
|
};
|
|
|
|
users.groups."${v.user_group_name}" = { gid = v.user_group_id; };
|
2022-06-22 18:05:02 +01:00
|
|
|
systemd.services.activate-virtual-mail-users = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
before = [ "dovecot2.service" ];
|
|
|
|
serviceConfig = { ExecStart = virtualMailUsersActivationScript; };
|
|
|
|
enable = true;
|
|
|
|
};
|
2022-06-22 15:51:22 +01:00
|
|
|
}
|