How to use nix-shell when the "nixpkgs" channel has been removed

I am using the NIx package manager (without NixOS). The default channel is nixpkgs-unstable release nixpkgs-24.05pre583512.01737456dfe4. I have changed it to a stable channel (nixos-20.09 release nixos-20.09.4407.1c1f5649bb9) like this:

$ nix-channel --list  # Show the default channel.
nixpkgs https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --add https://nixos.org/channels/nixos-20.09  # Add stable channel.
$ nix-channel --update
unpacking channels...
created 2 symlinks in user environment
$ nix-channel --remove nixpkgs  # Remove the default channel.
uninstalling 'nixpkgs-21.05pre272788.870dbb751f4'
building '/nix/store/h11za3c6ja8hgrlkcs8nznib02wlp9cs-user-environment.drv'...
$ nix-channel --list
nixos-20.09 https://nixos.org/channels/nixos-20.09

Now I encounter an error when I try to use nix-shell:

$ nix-shell --pure --packages htop hello
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I), at (string):1:13

It appears that nix-shell expects a nixpkgs channel. I no longer have a channel named nixpkgs because I deleted it when I switched to a stable channel. How can I run nix-shell in this case?

1 Like

Here’s a couple of ways:

  1. You could get a hash from https://status.nixos.org/ and then use it like this

    nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/15a64b2facc1b91f4361bdd101576e8886ef834b.tar.gz -p cowsay
    
  2. You could clone GitHub - NixOS/nixpkgs: Nix Packages collection & NixOS and then use it like this

    nix-shell -I nixpkgs=path/to/your/clone/of/nixpkgs -p cowsay
    

Edit: Aah, but that’s not what you’re after, is it? What I’ve shown you allows you to use specific versions of nixpkgs, but I think you want to use the channels for their automatic update. As I avoid channels precisely because they change implicitly, I don’t know how to do what you want off the top of my head … and my vague recollections aren’t borne out by the docs: I thought you had to add the nixpkgs at the end with something like this:

nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs

.

1 Like

There is no expectation of any channels, though instead it assumes that the path <nixpkgs> resolves. Read more about the resolution process in man nix, somewhere near the -I and NIX_PATH.

You might be able to use nix-shell -p '"nixos-20.09".hello' in your current situation.

Though the better way is probably to “rename” your current nixos-20.09 channel to nixpkgs as briefly described in How to switch to a stable channel and install using nix-env without needing the -f option? - #3 by NobbZ.