refactor(mx): extract disk check services into disk-check.nix

Share the check script via a parameterized mkDiskCheck function over
{ name, mountPoint, label } and iterate an attrset to emit the boot
and root services plus their daily timers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Harald Hoyer 2026-04-20 10:28:27 +02:00
parent 6d0186eadb
commit 4045aa1859
2 changed files with 66 additions and 69 deletions

View file

@ -1,10 +1,16 @@
{ pkgs, lib, config, ... }:
{
pkgs,
lib,
config,
...
}:
{
imports = [
# ./goaccess.nix
./acme.nix
./backup.nix
./coturn.nix
./disk-check.nix
./forgejo.nix
./hardware-configuration.nix
./headscale.nix
@ -103,73 +109,5 @@
];
};
systemd.services = {
check_boot = {
serviceConfig = {
Type = "oneshot";
Environment = "PATH=/run/current-system/sw/bin";
ExecStart = toString (
pkgs.writeShellScript "check_boot.sh" ''
CURRENT=$(df /boot | grep /boot | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=85
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
${pkgs.mailutils}/bin/mail -s '/boot Disk Space Alert' harald << EOF
Your /boot partition remaining free space is critically low. Used: $CURRENT%
EOF
TOKEN=$(cat ${config.sops.secrets.ntfy.path})
${pkgs.curl}/bin/curl -s -H "Authorization: Bearer $TOKEN" \
-H "Title: /boot Disk Space Alert" \
-H "Priority: high" \
-d "Boot partition at $CURRENT%" \
http://127.0.0.1:2586/alerts
fi
''
);
};
wantedBy = [ "default.target" ];
};
check_root = {
serviceConfig = {
Type = "oneshot";
Environment = "PATH=/run/current-system/sw/bin";
ExecStart = toString (
pkgs.writeShellScript "check_root.sh" ''
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=85
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
${pkgs.mailutils}/bin/mail -s '/ Disk Space Alert' harald << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
TOKEN=$(cat ${config.sops.secrets.ntfy.path})
${pkgs.curl}/bin/curl -s -H "Authorization: Bearer $TOKEN" \
-H "Title: / Disk Space Alert" \
-H "Priority: high" \
-d "Root partition at $CURRENT%" \
http://127.0.0.1:2586/alerts
fi
''
);
};
wantedBy = [ "default.target" ];
};
};
systemd.timers = {
check_boot = {
timerConfig = {
OnCalendar = "daily";
};
wantedBy = [ "timers.target" ];
};
check_root = {
timerConfig = {
OnCalendar = "daily";
};
wantedBy = [ "timers.target" ];
};
};
system.stateVersion = "23.05";
}