How to see what commit is my channel on?

I’m trying to trace the breaking commit in some package. How can I find on which commit is my nixos-19.10 currently on, so that I could checkout to the same commit in my git clone of nixpkgs?

Thanks!

3 Likes

use nixos-version. Mine gives 20.03pre194293.2436c27541b (Markhor) where the last part is the commit hash. Not suer if it aslo works on stable, but it should.

1 Like

Thanks, that works. But how to get the hash if I have several channels (I have separate nixos-unstable to install stuff not in stable yet)?

Found the answer myself. You can use readlink /nix/var/nix/profiles/per-user/root/channels/<channel name>, which gives the actual path in nix store, along with the commit hash. This works at least on NixOS.

5 Likes

Just posting this here because I need things explained like to a 5 year old (and SO threads are prone to be closed/deleted/etc.):

Note: Before trying any of these methods, make sure that you are issuing commands with the right user!
No harm done either way, but if you are on NixOS and manage things declaratively (as root when rebuilding the system) then you might get a different commit hash than what you need. (Especially if you ever issued nix-channel --update without sudo, which will set up a channel for your user profile as well.)

Method 0

According to the NixOS wiki’s Nix channels entry, “a channel is a name for the latest “verified” git commits in Nixpkgs”. That is, at any given time, a channel points to a specific commit in the Nixpkgs git repository on Github; each Nix channel is actually a git branch in the repo:

$ nix-channel --list
nixos https://nixos.org/channels/nixos-20.09
-----                            ----------- 
(name)                          (branch-name)
#                                     |
#                                     V
#              https://github.com/NixOS/nixpkgs/tree/<branch-name>
#         i.e. https://github.com/NixOS/nixpkgs/tree/nixos-20.09

So if you just executed nix-channel --update before nix-shell, and it works, just look up the last commit in the channel branch.

Method 1

Chapter 12. Channels” of the Nix manual mentions that nix-channel --updatemakes the union of each channel’s Nix expressions available by default to nix-env operations (via the symlink ~/.nix-defexpr/channels)”.

To see where the ~/.nix-defexpr/channels symlink points to, use readlink -f to follow the symlink chain and combine it with ls to get straight to the point:

$ ls -l $(readlink -f ~/.nix-defexpr/channels)
total 6432
dr-xr-xr-x    2 root root      4096 Jan  1  1970 ./
drwxrwxr-t 8191 root nixbld 6569984 Feb  9 15:51 ../
lrwxrwxrwx    1 root root        78 Jan  1  1970 nixos -> /nix/store/k737c631q19n54fhjmnf68frg5dar14w-nixos-20.09.3009.8e78c2cfbae/nixos/
lrwxrwxrwx    1 root root        60 Jan  1  1970 manifest.nix -> /nix/store/a5wl1fri2sasnsb1i5zscni5h7kjg7d6-env-manifest.nix

My channel’s name is nixos, and it points to

/nix/store/k7..4w-nixos-20.09.3009.8e78c2cfbae/nixos/
                                   -----------
                                        ^
                                        |
                                   channel-commit

and the commit hash is right after the MAJOR.MINOR.PATCH version number.


Aside: To construct the tarball URL for fetchTarball in the question, use the following template:

https://github.com/<user>/<repo>/archive/<full-or-abbr-commit-hash>.tar.gz

For example:

https://github.com/NixOS/nixpkgs/archive/8e78c2cfbae.tar.gz

Alternatively, click the green “Code” button, and copy the URL of the “Download ZIP” link (and change the zip extension to tar.gz).


Fun fact: if you did nix-channel --update before method 1, then URLs https://github.com/NixOS/nixpkgs/tree/<branch-name> and https://github.com/NixOS/nixpkgs/tree/<channel-commit> will point to the same place in the Nixpkgs repo.

5 Likes