NixOS Upgrades On Shutdown Flake Module

I stripped the code I had for my machines to update on shutdown into its own module that people can use. Periodically it activates, and if the laptop/computer is connected to AC or has enough battery on shutdown it updates:

It only gets activated on shutdown, not on reboots (this is not configurable yet, if anyone uses this and wants more options or a feature like this please make an issue so I can gauge interest!). But queues the update again after reboots.

Updating randomly during usage often would slow the computer down a lot, especially if there were unfree packages that had to be built (cuda), custom packages, or overrides. So this was a much better way for me to handle updates.

It was non-trivial so I decided to create a module for it. It uses a lingering systemd stop script to achieve updates only on shutdown, and has a bunch of services it keeps around during the process. Everything else gets killed by systemd so it runs in a very minimal environment with (hopefully) a lot of RAM available.

It is expected to be ran with a remote flake (maybe that updates itself through for example a GitHub action).

By default it uses nix output monitor, so as long as the shutdown info isn’t set to “quiet” we can see what it does:
gif4

After being imported, it can be enabled with:

system.autoUpgradeOnShutdown = {
  enable = true;
  flake  = "github:youruser/nixos-config";
  host = "yourhost";
};

By default it activates roughly twice per month.

9 Likes

Why would you delay updates for weeks? :wink:

2 Likes

Fair point ahaha, I guess it’s a byproduct of having a Surface Pro 7, it’s using the surface Kernel from nixos-hardware, and what that meant is that more often than not I’d have to compile a whole kernel every time I upgraded.
But I now managed to pin it so I guess I could update more often :stuck_out_tongue: