Enable e-mail services for auto-upgrade
This commit is contained in:
parent
1260f41dbb
commit
99ed3884f8
|
@ -18,6 +18,7 @@ in
|
|||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
email.enable = true;
|
||||
systemd.services.nixos-upgrade.script = mkOverride 50 (
|
||||
let
|
||||
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
{ config, lib, ... }:
|
||||
with lib;
|
||||
let
|
||||
cfg = config.email;
|
||||
in
|
||||
{
|
||||
options.email = {
|
||||
enable = lib.mkEnableOption "Allow sending system status e-mails via sendmail";
|
||||
fromAddress = mkOption {
|
||||
type = types.str;
|
||||
example = "noreply@example.com";
|
||||
|
@ -25,4 +29,21 @@ with lib;
|
|||
'';
|
||||
};
|
||||
};
|
||||
|
||||
config.programs.msmtp = lib.mkIf cfg.enable {
|
||||
enable = true;
|
||||
setSendmail = true;
|
||||
accounts = {
|
||||
default = {
|
||||
auth = true;
|
||||
host = "gvfr.de";
|
||||
passwordeval = "cat /secrets/email_password.txt";
|
||||
user = cfg.fromAddress;
|
||||
from = cfg.fromAddress;
|
||||
port = 465;
|
||||
tls = true;
|
||||
tls_starttls = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -10,86 +10,72 @@ in
|
|||
enable = lib.mkEnableOption "Send systemd status e-mails";
|
||||
};
|
||||
|
||||
config.programs.msmtp = lib.mkIf cfg.enable {
|
||||
enable = true;
|
||||
setSendmail = true;
|
||||
accounts = {
|
||||
default = {
|
||||
auth = true;
|
||||
host = "gvfr.de";
|
||||
passwordeval = "cat /secrets/email_password.txt";
|
||||
user = fromAddress;
|
||||
from = fromAddress;
|
||||
port = 465;
|
||||
tls = true;
|
||||
tls_starttls = false;
|
||||
config = lib.mkIf cfg.enable {
|
||||
email.enable = true;
|
||||
systemd.services."status-email@" = let
|
||||
sendStatusEmail = pkgs.writeScript "send-status-email" ''
|
||||
#!${pkgs.bash}/bin/bash
|
||||
|
||||
from="${fromIdentity}"
|
||||
to="${toAddress}"
|
||||
service="$1"
|
||||
full_status="$(systemctl status --full --lines 200 "$service")"
|
||||
exit_code="$(echo "$full_status" | head -n5 | tail -1 | sed -e 's/.*status=\(.*\))$/\1/g')"
|
||||
# state="$(systemctl is-failed "$service")"
|
||||
|
||||
fail_priority=1
|
||||
fail_subject="Unit \"$service\" failure report (exit code $exit_code)"
|
||||
success_priority=3
|
||||
success_subject="Unit \"$service\" report (success)"
|
||||
|
||||
shift
|
||||
while [ $# -gt 0 ] ; do
|
||||
case "$1" in
|
||||
'-s'|'--fail-subject')
|
||||
fail_subject="$2"
|
||||
shift 2
|
||||
;;
|
||||
'-p'|'--fail-priority')
|
||||
fail_priority="$2"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$exit_code" != "0/SUCCESS" ] ; then
|
||||
subject="$fail_subject"
|
||||
priority="$fail_priority"
|
||||
else
|
||||
subject="$success_subject"
|
||||
priority="$success_priority"
|
||||
fi
|
||||
|
||||
echo "Sending e-mail \"$subject\" to \"$to\"."
|
||||
|
||||
${pkgs.system-sendmail}/bin/sendmail -t -X - <<ERRMAIL
|
||||
To: $to
|
||||
From: $from
|
||||
Subject: $subject
|
||||
Content-Transfer-Encoding: 8bit
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
X-Priority: $priority
|
||||
|
||||
$full_status
|
||||
|
||||
ERRMAIL
|
||||
'';
|
||||
in
|
||||
{
|
||||
unitConfig = {
|
||||
Description = "Send a status e-mail for %I";
|
||||
};
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${sendStatusEmail} %i";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
config.systemd.services."status-email@" = let
|
||||
sendStatusEmail = pkgs.writeScript "send-status-email" ''
|
||||
#!${pkgs.bash}/bin/bash
|
||||
|
||||
from="${fromIdentity}"
|
||||
to="${toAddress}"
|
||||
service="$1"
|
||||
full_status="$(systemctl status --full --lines 200 "$service")"
|
||||
exit_code="$(echo "$full_status" | head -n5 | tail -1 | sed -e 's/.*status=\(.*\))$/\1/g')"
|
||||
# state="$(systemctl is-failed "$service")"
|
||||
|
||||
fail_priority=1
|
||||
fail_subject="Unit \"$service\" failure report (exit code $exit_code)"
|
||||
success_priority=3
|
||||
success_subject="Unit \"$service\" report (success)"
|
||||
|
||||
shift
|
||||
while [ $# -gt 0 ] ; do
|
||||
case "$1" in
|
||||
'-s'|'--fail-subject')
|
||||
fail_subject="$2"
|
||||
shift 2
|
||||
;;
|
||||
'-p'|'--fail-priority')
|
||||
fail_priority="$2"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$exit_code" != "0/SUCCESS" ] ; then
|
||||
subject="$fail_subject"
|
||||
priority="$fail_priority"
|
||||
else
|
||||
subject="$success_subject"
|
||||
priority="$success_priority"
|
||||
fi
|
||||
|
||||
echo "Sending e-mail \"$subject\" to \"$to\"."
|
||||
|
||||
${pkgs.system-sendmail}/bin/sendmail -t -X - <<ERRMAIL
|
||||
To: $to
|
||||
From: $from
|
||||
Subject: $subject
|
||||
Content-Transfer-Encoding: 8bit
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
X-Priority: $priority
|
||||
|
||||
$full_status
|
||||
|
||||
ERRMAIL
|
||||
'';
|
||||
in
|
||||
lib.mkIf cfg.enable {
|
||||
unitConfig = {
|
||||
Description = "Send a status e-mail for %I";
|
||||
};
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${sendStatusEmail} %i";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue