How to include a package from another channel?

I wanted to use Terraform 0.13.4 so I wanted to get it from nixos-unstable channel.
I’ve added the channel like so:

 > sudo nix-channel --add nixos-unstable
 > sudo nix-channel --list                                                        

And I tried getting the package using import:

  unstablePkgs = import <nixos-unstable> { };

But when I try to build that I get an error:

error: file 'nixos-unstable' was not found in the Nix search path (add it using $NIX_PATH or -I), at /etc/nixos/roles/work.nix:6:25

My $NIX_PATH looks like this:

 > echo $NIX_PATH | tr ':' '\n'

And I can see that the channel is there in the first path:

 > ls -l /home/jakubgs/.nix-defexpr/channels/
total 2
lrwxrwxrwx 1 root root  60 Jan  1  1970 manifest.nix -> /nix/store/dkcsmb9gc5r43g97l1wbhz2qhxrz7lj5-env-manifest.nix
lrwxrwxrwx 1 root root  78 Jan  1  1970 nixos -> /nix/store/f227g1ph32pxl06h71x9aair6z65syc6-nixos-20.03.3129.d3784204ba1/nixos
lrwxrwxrwx 1 root root 100 Jan  1  1970 nixos-unstable -> /nix/store/liasszqdxazz50b7i15lmzjifyazjm16-nixos-unstable-21.03pre246543.24c9b05ac53/nixos-unstable

So I’m not sure what I’m missing to make this work.

What I found out is that if I use the -i flag - which stands for --login and adds user env too, in this case that of root - with sudo makes it work:

sudo -i nixos-rebuild switch --upgrade

Example: /etc/nixos/unstable-packages.nix:

{ config, pkgs, ... }:
  baseconfig = { allowUnfree = true; };
  unstable = import <nixos-unstable> { config = baseconfig; };
in {
  environment.systemPackages = with pkgs; [

OR (as user):

nix-env -iA nixos-unstable.terraform_0_13
1 Like

Oh, interesting point. I forgot that doing a separate import would not include allowUnfree. But right now I have Terraform installed without providing that in config and it still works. So not sure if that’s necessary.

You have added the channel as your user, though you rebuild the system as root, you should add the unstable channel to your roots environment using sudo nix-channel --add nixos-unstable.

Also, to avoid warnings about that channel name beeing used twice, you should remove it from your user.

Except I didn’t It was added as root. See my post, I used sudo for nix-channel commands.

EDIT: It was actually added as both, since I thought that was an issue beforehand.

Indeed, I was just confused by the fact that it worked with passing in the users environment.

Have you run sudo nix-channel --update after adding the channel?

The channel isn’t available without it…

By running sudo -i nixos-rebuild switch --upgrade (which implies a nix-channel --update) you might even have caused that nix updated and created the channels in your users environment.

Yeah. It’s weird. But what’s even weirder is that I did nix-channel --update without sudo and now sudo nixos-rebuild switch --upgrade works without the -i.

I really don’t get how that nix-channel --update fixed anything since as you can see in my OP the channel symlinks were clearly visible in the /home/jakubgs/.nix-defexpr/channels folder.

I’m not 100% sure but I think nix-channel --update without sudo fixed it.

But why? How? No idea.

They are within your users folder, which nixos-rebuild should not respect, but you forced it to, by using sudo -i. That together with the implied update of the channels probably confused the system.

No, as I stated before, the channels were added BOTH for me and root.

You have never shown roots defexpr, and that’s where nix channel would create them.