diff --git a/custom/packages/maxwell-notify.nix b/custom/packages/maxwell-notify.nix index a736066..a464501 100644 --- a/custom/packages/maxwell-notify.nix +++ b/custom/packages/maxwell-notify.nix @@ -1,20 +1,35 @@ -{ writeScriptBin, fish, curl +{ lib +, writers +, curl +, jq , homeserver , roomId , authToken }: -writeScriptBin "notify" '' - #!${fish}/bin/fish +writers.writeDashBin "notify" '' + export PATH="$PATH:${lib.makeBinPath [ curl jq ]}" - set token (cat ${authToken}) - - if test (id -u) != 0 + if test $(id -u) != 0; then echo 'you must be root to send a notice' exit 1 - end + fi - set url '${homeserver}/rooms/${roomId}/send/m.room.message?access_token='$token - set msg '{"msgtype":"m.text", "body": "'$argv[1]'"}' - ${curl}/bin/curl -s -XPOST -d $msg $url + token=$(cat ${authToken}) + url="${homeserver}/rooms/${roomId}/send/m.room.message?access_token=$token" + + if test $# -eq 1; then + # send first arg as text + msg=$(printf "%s" "$1" | jq -Rsc '{ "msgtype": "m.text", "body": . }') + else + # send stdin formatted as code + msg=$(jq -Rsc '{ + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "body": "", + "formatted_body": ("
" + . + "
") + }') + fi + + curl -s "$url" -d "$msg" '' diff --git a/jobs.nix b/jobs.nix index 73d0676..2c712dd 100644 --- a/jobs.nix +++ b/jobs.nix @@ -4,6 +4,17 @@ with lib; { + systemd.services."notify-failed@" = { + description = "notify that %i has failed"; + scriptArgs = "%i"; + path = [ pkgs.maxwell-notify ]; + script = '' + unit=$1 + notify "$unit: failed. last log lines:" + journalctl -u "$unit" -o cat -n 15 | notify + ''; + }; + systemd.services.ydns = { description = "update ydns address record"; after = [ "network-online.target" ]; @@ -63,8 +74,9 @@ with lib; in { description = "system backup"; - after = [ "network-online.target" ]; - startAt = "*-*-* 03:00"; # every day at 3:00 + after = [ "network-online.target" ]; + startAt = "*-*-* 03:00"; # every day at 3:00 + onFailure = [ "notify-failed@backup.service" ]; serviceConfig = { Type = "oneshot"; @@ -133,6 +145,7 @@ with lib; description = "update namecoin names"; after = [ "namecoind.service" ]; startAt = "hourly"; + onFailure = [ "notify-failed@namecoin-update.service" ]; path = [ pkgs.namecoind ]; serviceConfig.Type = "oneshot";