Linux Is Best<p><span>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. <br><br>Part of this code was me (my work), and part of this was ChatGPT.<br></span></p><pre><code># ----------------------------------------
# 🔄 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;
};
};
</code></pre><span><br></span><a href="https://mk.absturztau.be/tags/NixOs" rel="nofollow noopener" target="_blank">#NixOs</a> <a href="https://mk.absturztau.be/tags/Nix" rel="nofollow noopener" target="_blank">#Nix</a> <a href="https://mk.absturztau.be/tags/Linux" rel="nofollow noopener" target="_blank">#Linux</a> <a href="https://mk.absturztau.be/tags/Forums" rel="nofollow noopener" target="_blank">#Forums</a> <a href="https://mk.absturztau.be/tags/SupportForums" rel="nofollow noopener" target="_blank">#SupportForums</a> <a href="https://mk.absturztau.be/tags/AI" rel="nofollow noopener" target="_blank">#AI</a><p></p>