{ config, lib, pkgs, ... }: with lib; let cfg = config.systemd.status-mail; 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 = let singleServiceCfg = attrs: unitName: attrs // (setAttrByPath [unitName "onFailure"] ["unit-status-mail@%n.service"]); in foldl singleServiceCfg {} 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="${cfg.recipient}" 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 <