Here is the full version. The commands are update, logs and break up the logs daily, and clean (both old logs and the system). I wanted the logs broken up into their own independent daily logs as not to be greeted with a wall of text.
Part of this code was me (my work), and part of this was ChatGPT.
# ----------------------------------------
# π Automatic NixOS Upgrades.
# ----------------------------------------
# Automatically checks for and installs system updates daily at 3:00 AM.
# It runs `nixos-rebuild switch --upgrade` and saves the output to a log file.
#
# β
What it does:
# β’ Downloads the latest NixOS packages.
# β’ Rebuilds your system with updates applied immediately (no reboot needed).
# β’ Saves upgrade logs to: /var/log/nixos-upgrade.log
#
# β οΈ What it *doesn't* do:
# β’ β Won't reboot your system β you'll need to restart manually.
# β’ β Doesn't update Flatpak or home-manager apps.
#
# π Tip: Change the time below if 3 AM doesnβt suit your schedule.
system.autoUpgrade = {
enable = true; # Enable automatic system upgrades.
persistent = true; # Remember missed upgrades and run ASAP after reboot.
dates = "03:00"; # When upgrades run daily β customize this time.
};
systemd.services.nixos-upgrade.serviceConfig = {
StandardOutput = "file:/var/log/nixos-upgrade.log"; # Logs upgrade output here.
StandardError = "inherit"; # Shows errors in system logs.
};
systemd.timers.nixos-upgrade.timerConfig.WakeSystem = true; # Wake system to run upgrades if asleep.
# ----------------------------------------
# π Logrotate for Upgrade Logs.
# ----------------------------------------
# Rotates `/var/log/nixos-upgrade.log` daily to keep logs manageable.
# Keeps the last 7 days of logs (you can change `rotate 7` below).
#
# π Logrotate state is stored at: /var/lib/logrotate/status
# Rotation runs daily at 4:30 AM β 90 minutes after the system upgrade.
# π§Ύ Define logrotate rules.
# Change "rotate 7" below to keep more or fewer days.
environment.etc."logrotate.d/nixos-upgrade".text = ''
/var/log/nixos-upgrade.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 644 root root
}
'';
# π οΈ Define the logrotate service.
systemd.services.logrotate-nixos-upgrade = {
description = "Logrotate for NixOS upgrade logs.";
enable = true;
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.logrotate}/bin/logrotate /etc/logrotate.d/nixos-upgrade --state /var/lib/logrotate/status";
};
};
# β° Set the daily timer.
systemd.timers.logrotate-nixos-upgrade = {
description = "Daily Logrotate for NixOS upgrade logs.";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "*-*-* 04:30:00"; # Log rotation happens 90 minutes after the upgrade to avoid overlap.
Persistent = true;
WakeSystem = true;
};
};
# ----------------------------------------
# ποΈ Weekly Garbage Collection.
# ----------------------------------------
# Cleans unused packages every Monday at 4 PM, waiting 24+ hours after updates to avoid issues.
#
# β
What it does:
# β’ Runs: "nix-collect-garbage -d".
# β’ Deletes unused system generations and packages.
# β’ Frees up disk space safely.
#
# β οΈ What it doesn't do:
# β’ β Won't remove your currently active or booted system configuration.
# β’ β Doesn't reboot your machine.
# π Tip: Adjust the times below to fit your schedule or preferences.
# Enable automatic garbage collection in Nix.
# This cleans up unused packages and old system generations regularly.
nix.gc = {
automatic = true;
dates = "Mon *-*-* 16:00"; # Monday, 4:00 PM - Change if you want a different day and time.
};
# Timer for running Nix's built-in automatic garbage collection
systemd.timers.nix-gc.timerConfig = {
WakeSystem = true;
Persistent = true;
};
# Custom service to wipe ALL old system generations manually.
# This is a more thorough cleanup, safely removing all old system snapshots.
systemd.services.nix-gc-wipe = {
description = "Wipe all old system generations";
serviceConfig = {
Type = "oneshot";
Environment = "PATH=/run/current-system/sw/bin";
ExecStart = "/bin/sh -c 'nix-env --delete-generations old --profile /nix/var/nix/profiles/system && nix-collect-garbage -d'";
};
};
# Timer to run the custom wipe service every Monday at 4:15 PM
systemd.timers.nix-gc-wipe = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "Mon *-*-* 16:15:00"; # Monday, 4:15 PM - Change if you want a different day and time.
WakeSystem = true;
Persistent = true;
};
};
#NixOs #Nix #Linux #Forums #SupportForums #AI