Selecting a channel with nix-shell

I have multiple channels:

$ nix-channel --list                                                           
nixpkgs https://nixos.org/channels/nixpkgs-19.03-darwin
nixpkgs-new https://nixos.org/channels/nixpkgs-19.09-darwin

How would I select nixpkgs-new when opening a nix-shell?

(I know you can do nix-shell -I nixpkgs=channel:nixpkgs-19.09-darwin -p ... - really wondering how to select a local channel).

2 Likes

I only have one channel but I expect you can write something like nix-shell -p hello '<nixpkgs>'

That said, if you’re trying to create a shell with the given set of packages available, nix run is the better tool for that.

3 Likes

I’m not using channels, but I use this command every now and than

nix-shell -I nixpkgs=/var/src/nixpkgs-unstable -p git

when I want to install something from the unstable branch.
For that I have to clone the repository in /var/src/nixpkgs-unstable.

1 Like

The channels are located under /nix/var/nix/profiles/per-user/$USER/channels. So for example if the user was root:

nix-shell -I nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixpkgs-new

or this works too:

NIX_PATH=nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixpkgs-new nix-shell
5 Likes

I’m having trouble getting this to work actually. I have the nixpkgs-unstable channel:

$ nix-channel --list
home-manager https://github.com/nix-community/home-manager/archive/release-20.09.tar.gz
nixpkgs-unstable https://nixos.org/channels/nixpkgs-unstable

But when I go to use it:

$ nix-shell -p julia_15 '<nixpkgs-unstable>'
error: undefined variable 'julia_15' at (string):1:94
(use '--show-trace' to show detailed location information)

even though julia_15 is in the unstable channel. Am I doing something wrong here?

I’m on NixOS 20.09.1632.a6a3a368dda (Nightingale).

I’d recommend to avoid using channels for the following reasons: Anti pattern: nix-channel command · Issue #16 · NixOS/nix.dev · GitHub

4 Likes

Oh interesting, I had no idea that channels were on the way out!

1 Like

You can also pass nix code to the nix-shell -p flag:

$ nix-shell -p '(import <unstable> {}).hello'
11 Likes

According to the doc of common eval option --include for nix3-commands nix shell (not old nix-shell) before 2.34: reword documentation on `nix-path` config option by fricklerhandwerk · Pull Request #7772 · NixOS/nix · GitHub, this could also be

nix-shell -I nixpkgs=flake:github/NixOS/nixpkgs/nixos-unstable -p hello

that set search paths of nixpkgs directly (installable hello is impilcitly nixpkgs#hello) if you are not using channels or there’s no channel named unstable, so lookup path <unstable> won’t match anything in search paths.

Somehow it wouldn’t work with nix3-commands like nix shell or nix run:

nix shell -I nixpkgs=flake:github:NixOS/nixpkgs/nixos-unstable nixpkgs#hello

nixpkgs#hello still resolves to hello in the nixpkgs from nix-path

1 Like

Of course not, to be able to access nix path you’d need to be using impure.

You’re anyway grabbing hello from the nixpkgs flake registry entry, so even if you were using impure, you wouldn’t be using the flake from the nix path.

The intended nix 3 CLI version of this is simply:

$ nix shell github:NixOS/nixpkgs/nixos-unstable#hello

I’m way more surprised setting a channel to a flake URI is working for you, are you sure it is? Your explanation kinda makes no sense, so I’m not sure you’ve correctly asserted this.

1 Like

nix-shell has a --pure option, so it seems to be impure by default, and the $IN_NIX_SHELL in nix-shell will be impure.

That makes sense.
I have two entry named nixpkgs:

$ nix registry list | grep nixpkgs
system flake:nixpkgs path:/nix/store/m5dr0w2wb9lca77ar3idab31szd6mix7-source
global flake:nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable

the system one that takes precedence seems to be the one used by my NixOS configuration that set by nixpkgs.flake.setFlakeRegistry

That should be an lookup path instead of channel:

Like nixpkgs=flake:nixpkgs in NixOS config nix.nixPath that set by nixpkgs.flake.setNixPath

1 Like

Yes, I meant that the 3.0 nix shell variant obviously doesn’t work, since it is pure by default.

Yep, I understand, I’m simply surprised that flake URIs are valid lookup paths. But yes, apparently that’s permitted.

I have also finally managed to grok your explanation; you’re referring to other users in this thread who are using channels.