Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List-generations and rollback to any configuration #24374

Open
danbst opened this issue Mar 27, 2017 · 24 comments
Open

List-generations and rollback to any configuration #24374

danbst opened this issue Mar 27, 2017 · 24 comments

Comments

@danbst
Copy link
Contributor

danbst commented Mar 27, 2017

Issue description

I'd like to nixos-rebuild switch --rollback system to an old configuration, but current interface allows rollback only to previous one.

In NixOps, for-example, you can specify the generation number you'd like to rollback to. The generations and their numbers are shown with nixops list-generations

Doing things manually show an error:

# ln -sf /nix/store/5y0ypnyz6mrh95a71qk82kx5cwxfy1dl-nixos-system-nixos-16.09.773.b8ede35 /nix/var/nix/profiles/system
ln: failed to create symbolic link '/nix/var/nix/profiles/system/5y0ypnyz6mrh95a71qk82kx5cwxfy1dl-nixos-system-nixos-16.09.773.b8ede35': Read-only file system

Technical details

  • System: nixos-16.09
@danbst
Copy link
Contributor Author

danbst commented Mar 28, 2017

#24280 is a bit related. Strange part is ln -s works, but ln -sf - doesn't.

@RonnyPfannschmidt
Copy link
Contributor

there is a -T missing so ln will not resolve the target as a folder

@jonas-schievink
Copy link
Contributor

This would still be really nice to have

@JosephLucas
Copy link
Contributor

Yes this would be a nice feature

@thanegill
Copy link
Contributor

You can list system generations with this:

sudo nix-env --list-generations --profile /nix/var/nix/profiles/system

@Atemu
Copy link
Member

Atemu commented Nov 17, 2019

@thanegill awesome, thank you!

Is it possible to list a bit more information like NixOS channel of the generation and Kernel version like it's shown in the bootloader?

Edit: Found a way.

/nix/var/nix/profiles/system contains links to all the generations, you can just ls -l it and the nix store paths they point to each contain channel and version.

These nix store paths then contain a kernel link which you can also ls -l or readlink to print that generation's kernel's nix store path which contains the kernel version of course.

@stale
Copy link

stale bot commented Jun 1, 2020

Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the
    related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on
    irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 1, 2020
@lboklin
Copy link

lboklin commented Jul 6, 2020

I'd still like to see a way to switch between existing generations. Last night I had to get back to a generation associated to a different profile and what I had to do was build a new generation of that profile and then rollback over and over until I reached the desired one. That was not a smooth experience.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 6, 2020
@miallo
Copy link
Contributor

miallo commented Nov 30, 2020

I would love to be able to revert to absolute generations (e.g. nixos-rebuild switch --rollback=2) or relative (maybe in a git style nixos-rebuild switch --rollback=current~2).

@mixmixmix
Copy link

Is there a technical challenge in getting that implemented, anyone knows?

@miallo
Copy link
Contributor

miallo commented Dec 3, 2020

I took a short look into the code and as far as I can see, nixos-rebuild --rollback uses nix-env --rollback, which has a hardcoded relative previous generation. There is also nix-env --switch-generation which could go to an absolute generation instead. So from what I can see, at least going to absolute generations should be relatively straight forward. For a unified interface, rollback to a fixed generation should be done with nixos-rebuild --switch-generation <generation> as well I think

@miallo
Copy link
Contributor

miallo commented Dec 3, 2020

I have just implemented an untested version and I will try it out tomorrow or so. Be carefull if you use it - it was just done by copying the --rollback option and it has been some months since I wrote my last bigger shell script!

@miallo
Copy link
Contributor

miallo commented Dec 4, 2020

Seems to work :)
If you don't want to fiddle with your core system, you could use this for now:

sudo nix-env --switch-generation 12345 -p /nix/var/nix/profiles/system
sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch

(Replace the 12345 with the generation you want)

@miallo
Copy link
Contributor

miallo commented Jan 3, 2021

I added list-generations action in #105910 as well which has some additional output like Kernel version and NixOS version compared to the solution by @thanegill.
Many ideas come from the script that builds the grub-menu

@stale
Copy link

stale bot commented Jul 4, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2021
@miallo
Copy link
Contributor

miallo commented Aug 14, 2021

Is anyone here in for a review of #105910?

@edrex
Copy link
Contributor

edrex commented Oct 21, 2021

For those looking for a way to do this now:

  • Boot into the desired generation via the boot menu
  • Run /run/current-system/bin/switch-to-configuration boot (source code)

This sets the current generation as the active one, with the advantage that you've just tested it's bootable.

via: https://www.reddit.com/r/NixOS/comments/ni9u5l/nixos_rollback_questions/

Added to wiki: https://nixos.wiki/wiki/NixOS#Generations

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 21, 2021
@stale
Copy link

stale bot commented Apr 25, 2022

I marked this as stale due to inactivity. → More info

@Sepero
Copy link

Sepero commented May 20, 2022

Seems to work :) If you don't want to fiddle with your core system, you could use this for now:

sudo nix-env --switch-generation 12345 -p /nix/var/nix/profiles/system
sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch

(Replace the 12345 with the generation you want)

Also to list generations.

sudo nix-env --list-generations -p /nix/var/nix/profiles/system

Thank you. This fix works with a headless system. Until this is implemented officially, I will have to put these commands in a script.

@tobiasBora
Copy link
Contributor

Also it would be great if nixos-rebuild boot could be set to an old derivation. For some reasons I can't always choose that I startup (not sure why the raspberry pi does not always recognize the keyboard) and it would be great to have.

@Sepero
Copy link

Sepero commented Oct 22, 2022

Also it would be great if nixos-rebuild boot could be set to an old derivation. For some reasons I can't always choose that I startup (not sure why the raspberry pi does not always recognize the keyboard) and it would be great to have.

Use this to boot to a previous generation. Replace 28 with your generation.

nix-env --switch-generation 28 -p /nix/var/nix/profiles/system

@euonymos
Copy link

Use this to boot to a previous generation. Replace 28 with your generation.

nix-env --switch-generation 28 -p /nix/var/nix/profiles/system

Didn't work for me for some reason. It said it did switch, but I still saw a newer version from the last profile. nixos-rebuild switch --rollback did the job.

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/docker-run-and-docker-exec-error/30848/4

@miallo
Copy link
Contributor

miallo commented Sep 10, 2023

The list-generations is now merged into master #250501 🎉
Next step is the specific generation #105910 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests