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 (asroot
when rebuilding the system) then you might get a different commit hash than what you need. (Especially if you ever issuednix-channel --update
withoutsudo
, 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
beforenix-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 --update
“makes the union of each channel’s Nix expressions available by default tonix-env
operations (via the symlink~/.nix-defexpr/channels
)”.To see where the
~/.nix-defexpr/channels
symlink points to, usereadlink -f
to follow the symlink chain and combine it withls
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 totar.gz
).
Fun fact: if you did
nix-channel --update
before method 1, then URLshttps://github.com/NixOS/nixpkgs/tree/<branch-name>
andhttps://github.com/NixOS/nixpkgs/tree/<channel-commit>
will point to the same place in the Nixpkgs repo.