How to use `gh` with nix-shell without -E?

Tried to “install” gh (the GitHub CLI tool) with nix-shell as I usually do with other commands but all my attempts failed. I tried these at first:

$ gh
The program ‘gh’ is currently not installed. You can install it by typing:
  nix-env -iA nixos.gitAndTools.gh

$ nix-shell -p nixos.gitAndTools.gh
error: value is a function while a set was expected, at (string):1:94
(use '--show-trace' to show detailed location information)

$ nix-shell -p gh
error: undefined variable 'gh' at (string):1:94
(use '--show-trace' to show detailed location information)

$ nix-shell --show-trace -p gh
error: while evaluating the attribute 'buildInputs' of the derivation 'shell' at /nix/store/rvpdr9qywd7fsz624a5c255b5w5frpyd-nixos-20.09.3346.4d0ee90c6e2/nixos/pkgs/build-support/trivial-builders.nix:7:7:
while evaluating 'chooseDevOutputs' at /nix/store/rvpdr9qywd7fsz624a5c255b5w5frpyd-nixos-20.09.3346.4d0ee90c6e2/nixos/lib/attrsets.nix:475:22, called from undefined position:
undefined variable 'gh' at (string):1:94

The nix-env command would install it without a hitch but I don’t like imperative Nix. I was able to get this to work simply by re-using an example from man nix-shell

nix-shell -E 'with import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/4d46cb67ad21c068b28618dfa0ffa6fa1aa8b057.tar.gz") { }; runCommand "dummy" { buildInputs = [ gitAndTools.gh  ]; } ""'

but I wonder why the above nix-shell commands failed?

Try:

nix-shell -p gitAndTools.gh

nix-shell -p automatically adds the “nixos.” or “nixpkgs.” (according to your default channels, I think?)
I agree it’s inconsistent, though it does save typing.

1 Like

Was about to reply that I tried that without success, and that I only forgot to add it to the list above, but it turns out that what I tried had a typo in it (nix-shell -p gitAndtools.gh) - never underestimate the power of human fallibility… Thank you!

We also get rid of gitAndTools so in future also just using nix-shell -p gh should work.

3 Likes