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

48 lines
1.4 KiB
Nix
Raw Normal View History

2023-07-31 00:26:23 +00:00
{ config, pkgs, lib, ... }: {
# 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 = config.server; # Only auto upgrade servers
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@" =
let address = "system@${config.mail.server}";
in {
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
systemd.services.nixos-upgrade.onFailure =
lib.mkIf config.systemd.services."notify-email@".enable
[ "notify-email@%i.service" ];
}