One of the main reasons my laptop/desktop run NixOS is the ability to roll back if an update goes bad. For years, I’ve had updates automated (primarily to keep my browser up to date; though I reocgnize there are better methods).

I recently found my laptop dead, and upon plugging it in, I was unable to run a rebuild as it errored out for many packages with an error like this: ...disappeared, but it still has valid referrers

The fix

The first few forum posts suggested reinstalling, but I’m far too lazy for that. Admittedly, I used generative AI to cook up a fix which ended up working:

sudo nix-store --verify --check-contents 2>&1 \
  | awk -F"'" '/disappeared, but it still has valid referrers/ {print $2}' \
  | while read p; do
      echo "Repairing $p"
      sudo nix-store --repair-path "$p"
    done

This rebuilt what I needed, and afterwards I was able to run a rebuild normally.