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}'
565

Or use readlinks

$ readlinks ~/.nix-profile 
/home/layus/.nix-profile
/nix/var/nix/profiles/per-user/layus/profile
/nix/var/nix/profiles/per-user/layus/profile-965-link
/nix/store/98g9gp49d7rq9s4gi0cnvzsppa1pq15l-user-environment

or use tree

$ tree /nix/var/nix/profiles                    
/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.

> 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:

nix-build https://github.com/layus/readlinks/archive/master.tar.gz
1 Like