{ 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 <