Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/spamassassin: Avoid network dependency on boot #103902

Merged
merged 6 commits into from Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 36 additions & 29 deletions nixos/modules/services/mail/spamassassin.nix
Expand Up @@ -126,19 +126,36 @@ in
};

systemd.services.sa-update = {
# Needs to be able to contact the update server.
wants = [ "network-online.target" ];
after = [ "network-online.target" ];

serviceConfig = {
Type = "oneshot";
User = "spamd";
Group = "spamd";
StateDirectory = "spamassassin";
ExecStartPost = "+${pkgs.systemd}/bin/systemctl -q --no-block try-reload-or-restart spamd.service";
};

script = ''
set +e
${pkgs.su}/bin/su -s "${pkgs.bash}/bin/bash" -c "${pkgs.spamassassin}/bin/sa-update --gpghomedir=/var/lib/spamassassin/sa-update-keys/" spamd

v=$?
${pkgs.spamassassin}/bin/sa-update --verbose --gpghomedir=/var/lib/spamassassin/sa-update-keys/
rc=$?
set -e
if [ $v -gt 1 ]; then
echo "sa-update execution error"
exit $v

if [[ $rc -gt 1 ]]; then
# sa-update failed.
exit $rc
fi
if [ $v -eq 0 ]; then
systemctl reload spamd.service

if [[ $rc -eq 1 ]]; then
# No update was available, exit successfully.
exit 0
fi

# An update was available and installed. Compile the rules.
${pkgs.spamassassin}/bin/sa-compile
'';
};

Expand All @@ -153,32 +170,22 @@ in
};

systemd.services.spamd = {
description = "Spam Assassin Server";
description = "SpamAssassin Server";

wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
wants = [ "sa-update.service" ];
after = [
"network.target"
"sa-update.service"
];

serviceConfig = {
ExecStart = "${pkgs.spamassassin}/bin/spamd ${optionalString cfg.debug "-D"} --username=spamd --groupname=spamd --virtual-config-dir=/var/lib/spamassassin/user-%u --allow-tell --pidfile=/run/spamd.pid";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
User = "spamd";
Group = "spamd";
ExecStart = "+${pkgs.spamassassin}/bin/spamd ${optionalString cfg.debug "-D"} --username=spamd --groupname=spamd --virtual-config-dir=%S/spamassassin/user-%u --allow-tell --pidfile=/run/spamd.pid";
ExecReload = "+${pkgs.coreutils}/bin/kill -HUP $MAINPID";
StateDirectory = "spamassassin";
};

# 0 and 1 no error, exitcode > 1 means error:
# https://spamassassin.apache.org/full/3.1.x/doc/sa-update.html#exit_codes
preStart = ''
echo "Recreating '/var/lib/spamasassin' with creating '3.004001' (or similar) and 'sa-update-keys'"
mkdir -p /var/lib/spamassassin
chown spamd:spamd /var/lib/spamassassin -R
set +e
${pkgs.su}/bin/su -s "${pkgs.bash}/bin/bash" -c "${pkgs.spamassassin}/bin/sa-update --gpghomedir=/var/lib/spamassassin/sa-update-keys/" spamd
v=$?
set -e
if [ $v -gt 1 ]; then
echo "sa-update execution error"
exit $v
fi
chown spamd:spamd /var/lib/spamassassin -R
'';
};
};
}
4 changes: 2 additions & 2 deletions pkgs/servers/mail/spamassassin/default.nix
@@ -1,4 +1,4 @@
{ lib, fetchurl, perlPackages, makeWrapper, gnupg }:
{ lib, fetchurl, perlPackages, makeWrapper, gnupg, re2c, gcc, gnumake }:

perlPackages.buildPerlPackage rec {
pname = "SpamAssassin";
Expand Down Expand Up @@ -28,7 +28,7 @@ perlPackages.buildPerlPackage rec {
mv "rules/"* $out/share/spamassassin/

for n in "$out/bin/"*; do
wrapProgram "$n" --prefix PERL5LIB : "$PERL5LIB" --prefix PATH : "${gnupg}/bin"
wrapProgram "$n" --prefix PERL5LIB : "$PERL5LIB" --prefix PATH : ${lib.makeBinPath [ gnupg re2c gcc gnumake ]}
done
'';

Expand Down