So after a few days of my server not succeeding to upgrade, trying and failing to rebuild some package, I tried to change something in my conf.
My goal was to rollback to the last channel which had seen a successful nixos-rebuild, to permit me to change things in my configuration. I think this is a common experience: a channel update breaks something, so I can’t build anything until I fix it, but I want to quick fix something before, and I can’t.
So I tried to guess what last channel matched a successful nixos build, following symlinks from /nix/var/nix and trying some nix-store -q --referrers / --references, but failed to find a correlation.
I ended up looping on
nix-channel --rollback
nixos-rebuild switch
until I the build succeeded. It worked, but it was tedious.
How can I find which /nix/var/nix/profiles/per-user/root/channels* matches /nix/var/nix/profiles/system?
Just compare the svn-revision file in the channel to the nixos-version file in the profile. When the former is a suffix of the latter, you have a match.
Great, thanks a lot!
So wrapping up, this Bash script permits to know on which channel the current system runs:
for channel in /nix/var/nix/profiles/per-user/root/channels-*; do grep -q $(cat $channel/nixos/svn-revision) /run/current-system/nixos-version && echo $channel; done
Assuming the system channel is named nixos (which is the default on NixOS).
And there can be multiple results if you have multiple channels, in which case any of them is fine. The nixos will be the same in both results.