From c0e55961e32b9221bd2f4deca7449f58163adc44 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 30 Jul 2017 19:09:34 +0200 Subject: Refactor status mails --- custom/unit-status-mail.nix | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 custom/unit-status-mail.nix (limited to 'custom') diff --git a/custom/unit-status-mail.nix b/custom/unit-status-mail.nix new file mode 100644 index 00000000..54e5cb39 --- /dev/null +++ b/custom/unit-status-mail.nix @@ -0,0 +1,63 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.systemd.status-mail; + + systemdCfg = foldr singleCfg {} cfg.onFailure; + singleCfg = unitName: attrs: attrs // (setAttrByPath ["systemd" "services" unitName "onFailure"] ["unit-status-mail@%n.service"]); +in { + options = { + systemd.status-mail = { + onFailure = mkOption { + default = []; + type = types.listOf types.str; + description = '' + Send status mail when these units fail + ''; + }; + + recipient = mkOption { + default = "root"; + type = types.str; + description = '' + Recipient of status mails + ''; + }; + }; + }; + + config = mkIf (cfg.onFailure != []) { + systemd.services."unit-status-mail@" = { + serviceConfig = { + Type = "oneshot"; + }; + scriptArgs = "%I \"Hostname: %H\" \"Machine-ID: %m\" \"Boot-ID: %b\""; + script = '' + #!${pkgs.stdenv.shell} + MAILTO="root" + MAILFROM="unit-status-mailer" + UNIT=$1 + + EXTRA="" + for e in "''${@:2}"; do + EXTRA+="$e"$'\n' + done + + UNITSTATUS=$(systemctl status $UNIT) + + ${config.security.wrapperDir}/sendmail $MAILTO <