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 (asrootwhen rebuilding the system) then you might get a different commit hash than what you need. (Especially if you ever issuednix-channel --updatewithoutsudo, 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.09So if you just executed
nix-channel --updatebeforenix-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-envoperations (via the symlink~/.nix-defexpr/channels)”.To see where the
~/.nix-defexpr/channelssymlink points to, usereadlink -fto follow the symlink chain and combine it withlsto 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.nixMy channel’s name is
nixos, and it points to/nix/store/k7..4w-nixos-20.09.3009.8e78c2cfbae/nixos/ ----------- ^ | channel-commitand the commit hash is right after the MAJOR.MINOR.PATCH version number.
Aside: To construct the tarball URL for
fetchTarballin the question, use the following template:https://github.com/<user>/<repo>/archive/<full-or-abbr-commit-hash>.tar.gzFor example:
https://github.com/NixOS/nixpkgs/archive/8e78c2cfbae.tar.gzAlternatively, click the green “Code” button, and copy the URL of the “Download ZIP” link (and change the
zipextension totar.gz).
Fun fact: if you did
nix-channel --updatebefore 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.