dotfiles/modules/nixos/system/auto-upgrade.nix

55 lines
1.5 KiB
Nix
Raw Normal View History

2024-04-20 13:42:06 +00:00
{
config,
pkgs,
lib,
...
}:
{
2023-07-31 00:26:23 +00:00
# This setting only applies to NixOS, different on Darwin
2024-03-24 17:16:20 +00:00
nix.gc.dates = "09:03"; # Run every morning (but before upgrade)
2023-07-31 00:26:23 +00:00
# Update the system daily by pointing it at the flake repository
system.autoUpgrade = {
enable = lib.mkDefault false; # Don't enable by default
2024-03-24 17:16:20 +00:00
dates = "09:33";
2023-07-31 00:26:23 +00:00
flake = "git+${config.dotfilesRepo}";
randomizedDelaySec = "25min";
operation = "switch";
allowReboot = true;
rebootWindow = {
2024-03-24 17:16:20 +00:00
lower = "09:01";
upper = "11:00";
2023-07-31 00:26:23 +00:00
};
};
# Create an email notification service for failed jobs
systemd.services."notify-email@" =
2024-04-20 13:42:06 +00:00
let
address = "system@${config.mail.server}";
in
{
2023-07-31 00:26:23 +00:00
enable = config.mail.enable;
environment.SERVICE_ID = "%i";
script = ''
TEMPFILE=$(mktemp)
echo "From: ${address}" > $TEMPFILE
echo "To: ${address}" >> $TEMPFILE
echo "Subject: Failure in $SERVICE_ID" >> $TEMPFILE
echo -e "\nGot an error with $SERVICE_ID\n\n" >> $TEMPFILE
set +e
systemctl status $SERVICE_ID >> $TEMPFILE
set -e
${pkgs.msmtp}/bin/msmtp \
--file=${config.homePath}/.config/msmtp/config \
--account=system \
${address} < $TEMPFILE
'';
};
# Send an email whenever auto upgrade fails
2024-04-20 13:42:06 +00:00
systemd.services.nixos-upgrade.onFailure = lib.mkIf config.systemd.services."notify-email@".enable [
"notify-email@%i.service"
];
2023-07-31 00:26:23 +00:00
}