diff --git a/options/auto-upgrade.nix b/options/auto-upgrade.nix index 3c14c5b..a98fde1 100644 --- a/options/auto-upgrade.nix +++ b/options/auto-upgrade.nix @@ -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"; diff --git a/options/email.nix b/options/email.nix index dcc79e4..62f631c 100644 --- a/options/email.nix +++ b/options/email.nix @@ -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; + }; + }; + }; } diff --git a/options/status-email.nix b/options/status-email.nix index 43ab30f..4dfaa74 100644 --- a/options/status-email.nix +++ b/options/status-email.nix @@ -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 - <