I wanted to make a script that updated to the latest nix channel automatically when it was released, the same way I do with my Debian boxes.
Nix doesn’t have any nice way of querying the list of channels programmatically, but you can query their git repository since releases are created as branches there:
git ls-remote --sort=-v:refname https://github.com/NixOS/nixpkgs 'nixos-??.??' | awk '{ sub("^.*/","",$2); print $2; exit}'
This leverages the git ls-remote
command. Here’s an explanation of the arguments:
https://github.com/NixOS/nixpkgs
: The main nixpkgs git repo.-
nixos-??.??
: only show remote branches/tags that match this pattern. If you wanted to match a different official channel type, this is what you’d change. For instance you might wantnixos-??.??-small
ornixpkgs-??.??-darwin
. -
--sort=-v:refname
: Print the branch names in reverse (-
) version order (v:
) of their refname. Version sort correctly sorts numbers amid letters so nix-1.10 would sort before nix-10.1. -
awk '{ sub("^.*/","",$2); print $2; exit}'
:git ls-remote
outputs something like this:c5f08b62ed75415439d48152c2a784e36909b1bc refs/heads/nixos-25.05 50ab793786d9de88ee30ec4e4c24fb4236fc2674 refs/heads/nixos-24.11
This awk script takes the 2nd column (in awk, commands are run for each line and columns are loaded into
$1
,$2
,$3
, etc.) and erases the text matched by the regular expression^.*/
. This clears out therefs/heads/
text. Theexit
make awk quit the script immediately after printing once, so we only get the first line (equivalent tohead -1
). Since we’re sorting by reverse order of version, the latest release (the one with the highest number) gets printed.