2025-01-20 22:35:40 -05:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.nmasur.presets.services.nix-autoupgrade;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2025-02-05 17:25:29 -05:00
|
|
|
options.nmasur.presets.services.nix-autoupgrade = {
|
|
|
|
enable = lib.mkEnableOption "Nix auto upgrade";
|
|
|
|
repo = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
description = "Git URL of the flake repository to use for upgrades";
|
|
|
|
default = "git@github.com:nmasur/dotfiles";
|
|
|
|
};
|
|
|
|
};
|
2025-01-20 22:35:40 -05:00
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
|
|
|
|
# Update the system daily by pointing it at the flake repository
|
|
|
|
system.autoUpgrade = {
|
|
|
|
enable = true;
|
|
|
|
dates = "09:33";
|
2025-02-05 17:25:29 -05:00
|
|
|
flake = "git+${cfg.repo}";
|
2025-01-20 22:35:40 -05:00
|
|
|
randomizedDelaySec = "25min";
|
|
|
|
operation = "switch";
|
|
|
|
allowReboot = true;
|
|
|
|
rebootWindow = {
|
|
|
|
lower = "09:01";
|
|
|
|
upper = "11: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
|
2025-02-05 17:25:29 -05:00
|
|
|
${lib.getExe pkgs.msmtp} \
|
|
|
|
--file=${config.home-manager.users.${config.user}.xdg.configDir}/msmtp/config \
|
2025-01-20 22:35:40 -05:00
|
|
|
--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"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|