Rolling back a channel

Today I’ve spent some time looking for a way to rollback NixOS, and did a couple of mistakes along the way. So let me write the things I did down, so that, the next time, I don’t have to make these mistakes again :slight_smile:

Earlier today, I’ve run sudo nixos-rebuild switch --upgrade , and got a semi-broken system (some weird error during boot and mouse not working). I’ve booted to the previous generation, and it worked.

So I decided to make the previous generation current. I also wanted to change configuration.nix, and get the new generation based on the old state of channels. I’ve tried sudo nixos-rebuild switch --rollback: that did make the previous generation default, but it didn’t removed the new, broken-one, from boot menu.

So, the next thing I did, which was a mistake, was to run sudo nix-collect-garbadge -d. This didn’t remove the broken generation, but it did remove old version of channels. So, sudo nix-channel --rollback, which is the thing I should be using in the first place, was broken now.

I however have a local copy of nixpkgs repository, so I figured I just use it instead of a channel. To do this, I needed two things:

  • figure out which commit of nixpkgs I should use (it should be the one that was used by the working generation)
  • figure out how to make nixos-rebuild use my local copy of nixpkgs.

To do the first, I run nixos-version, which gave me a version, last component of which was the desired commit hash.

To do the second, I run

sudo nixos-rebuild switch \
    -I nixpkgs=/home/matklad/projects/nixpkgs/ \
    -I nixos=/home/matklad/projects/nixpkgs/nixos/

And now it all seems working :slight_smile:


You can simply remove particular symlinks by hand – from inside /nix/var/nix/profiles/**, only note that won’t change the boot menu until you do a switch or boot.

EDIT: I personally prefer to keep quite a long history of working systems. Sometimes you just don’t immediately notice that something less important is broken.


@matklad did you also blindly follow these instructions: nixos - How to undo `nix-channel --update` - Stack Overflow ? I would really like to downvote that answerbut I don’t have enough rep on stackoverflow…

I don’t remember really, but I think I only did things explicitelly mentioned in the OP :slight_smile:

Sorry for the necrobump but got confused by the comment to downvote because both answers are great - but only because Lily Ballard superbly rewrote both from scratch. Thank you!

FWIW, I’ve just had to rollback a second time (discourse topic), and I discovered /run/current-system/bin/switch-to-configuration boot which is better than sudo nixos-rebuild switch --rollback.

Rollback rolls you back from the current, broken generation, to the previous one, which is hopefully working. This isn’t an ideal workflow, because:

  • you need to be booted up into the broken gen,
  • you need to remember that the previous gen is OK (which it might actually not be)

What switch-to-configuration does is it makes the current configuration default. So the workflow is:

  • you do upgrade
  • you reboot
  • you notice stuff is broken
  • you reboot into the generation which works for you (not necessary the previous one)
  • you verify that it indeed works
  • you make it current by running /run/current-system/bin/switch-to-configuration boot