Skip to content

Commit e26124a

Browse files
committed
autoupgrade: add ntfy notifications
1 parent 5629210 commit e26124a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

hosts/framework/nixos/autoupgrade.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
pkgs,
33
lib,
44
config,
5+
secrets,
56
...
67
}:
7-
88
{
99
my.system.autoUpgrade = {
1010
enable = true;
1111
dates = "hourly";
1212
flake = "github:LorenzBischof/nixfiles";
13+
ntfyTopic = secrets.ntfy-alertmanager;
1314
};
1415

1516
systemd.services = lib.mkIf config.my.system.autoUpgrade.enabled {

modules/nixos/autoupgrade.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ in
116116
system was powered down.
117117
'';
118118
};
119+
ntfyUrl = lib.mkOption {
120+
type = lib.types.str;
121+
default = "https://ntfy.sh";
122+
description = "ntfy server URL";
123+
};
124+
ntfyTopic = lib.mkOption {
125+
type = lib.types.nullOr lib.types.str;
126+
default = null;
127+
description = "ntfy topic for notifications";
128+
};
119129
};
120130
config = lib.mkIf (cfg.enabled) {
121131
environment.etc.git-revision.text = inputs.self.rev;
@@ -159,7 +169,33 @@ in
159169
};
160170
# Prefer not to autoupgrade when on battery
161171
unitConfig.ConditionACPower = true;
172+
173+
onSuccess = lib.mkIf (cfg.ntfyTopic != null) [ "ntfy-upgrade-success.service" ];
174+
onFailure = lib.mkIf (cfg.ntfyTopic != null) [ "ntfy-upgrade-failure.service" ];
162175
};
163176

177+
systemd.services."ntfy-upgrade-success" = lib.mkIf (cfg.ntfyTopic != null) {
178+
script = ''
179+
if [ "$(systemctl show nixos-upgrade -p ConditionResult --value)" = "no" ]; then
180+
exit 0
181+
fi
182+
${pkgs.curl}/bin/curl -s \
183+
-H "Title: NixOS Upgrade Succeeded on $(${pkgs.nettools}/bin/hostname)" \
184+
-H "Tags: white_check_mark" \
185+
-d "System updated successfully to ${inputs.self.rev or "unknown"}" \
186+
"${cfg.ntfyUrl}/${cfg.ntfyTopic}"
187+
'';
188+
};
189+
190+
systemd.services."ntfy-upgrade-failure" = lib.mkIf (cfg.ntfyTopic != null) {
191+
script = ''
192+
${pkgs.curl}/bin/curl -s \
193+
-H "Title: NixOS Upgrade Failed on $(${pkgs.nettools}/bin/hostname)" \
194+
-H "Tags: warning" \
195+
-H "Priority: high" \
196+
-d "System upgrade failed!" \
197+
"${cfg.ntfyUrl}/${cfg.ntfyTopic}"
198+
'';
199+
};
164200
};
165201
}

0 commit comments

Comments
 (0)