Current generation number

If I do nix-env --list-generations, I have to page through all the previous generations to get to the current one.

Is there a way to see the current generation number without listing all the previous ones?

You could always press G to jump to the end.

Alternatively you could run

> readlink "$(readlink ~/.nix-profile)"

which should spit out something like profile-66-link, which means generation 66.

1 Like

Or you could use grep as in

$ nix-env --list-generations | grep current | awk '{print $1}'

Or use readlinks

$ readlinks ~/.nix-profile 

or use tree

$ tree /nix/var/nix/profiles                    
β”œβ”€β”€ per-user
β”‚   β”œβ”€β”€ layus
β”‚   β”‚   β”œβ”€β”€ channels -> channels-8-link
β”‚   β”‚   β”œβ”€β”€ channels-8-link -> /nix/store/jnqmlqai4zsh9jl7nxxgrbq7hi08w7g4-user-environment
β”‚   β”‚   β”œβ”€β”€ profile -> profile-965-link
β”‚   β”‚   β”œβ”€β”€ profile-961-link -> /nix/store/j47259hsbcfyhfbi3cm2hhj1mdzk4a6h-user-environment
β”‚   β”‚   β”œβ”€β”€ profile-962-link -> /nix/store/cy87889ln7a64g43zpz0k8k6a2843gv3-user-environment
β”‚   β”‚   β”œβ”€β”€ profile-963-link -> /nix/store/2cbcd3n2zpdyjhli3qaa0p9yqbycm0h2-user-environment
β”‚   β”‚   β”œβ”€β”€ profile-964-link -> /nix/store/fvn0wnx0gr0mbq4src9pv2dj5ssnscmv-user-environment
β”‚   β”‚   └── profile-965-link -> /nix/store/98g9gp49d7rq9s4gi0cnvzsppa1pq15l-user-environment
β”‚   └── root
β”‚       β”œβ”€β”€ channels -> channels-52-link
β”‚       └── channels-52-link -> /nix/store/41nvsyg6sjdswywmicr478wphqsgi5nc-user-environment
β”œβ”€β”€ system -> system-225-link
β”œβ”€β”€ system-223-link -> /nix/store/zav2p8819dbs1y80hvvf4dpkk45lp12n-nixos-system-ankh-morpork-19.09pre177651.aeb464dfd37
β”œβ”€β”€ system-224-link -> /nix/store/mjxa261axs2zsv3biyhi1xs7238va0g9-nixos-system-ankh-morpork-19.09pre177651.aeb464dfd37
β”œβ”€β”€ system-225-link -> /nix/store/kvh5fc2a57yi9m7nmkqdkkpkf33vc55i-nixos-system-ankh-morpork-19.09pre177651.aeb464dfd37
└── system-profiles

This long post as a funny way to show you that this information is stored in the /nix/var/nix/profiles/per-user/$USER/profile symlink ;-). Pick your way to extract it. I usually use tree for that, as it also shows you when it is time to collect garbage somewhere.

1 Like
> nix search -u readlinks
error: no results for the given search term(s)!
1 Like

Thanks for all the suggestions! Maybe this functionality could be added to Nix as:

nix-env --current-generation

Still needs a bit of polish, but it is now ready for packaging.
As you asked for it, here comes nix β€œintegration” :star_struck:


Sorry to answer to this old question, but I took me a few minutes to understand why nixos-rebuild switch was not creating new generation, and since this answer is the first one in google search I was thinking that it could be useful to others as well.

So the above code gives you the generations of the user profile (usually installed with nix-env), but not the system generations (usually installed with nixos-rebuild switch). To see list the generations of the system, use:

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

or if you just want to see the current one, you can use the trick given above in the thread, like:

sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}'