"nix search" duplicate results in brand-new NixOS install. How to fix?

I’m totally new to Nix. I followed the steps in 2.5.4. Installing from another Linux distribution, and when I boot into the brand new Nix install that’s what nix search shows (the results from two channels).

Basically this is what I did. From Ubuntu that I booted into from a thumb drive:

$ curl https://nixos.org/nix/install | sh
$ . $HOME/.nix-profile/etc/profile.d/nix.sh
$ nix-channel --list
nixpkgs https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --add https://nixos.org/channels/nixos-19.09 nixpkgs
$ nix-channel --update
$ nix-env -iE "_: with import <nixpkgs/nixos> { configuration = {}; }; with config.system.build; [ nixos-generate-config nixos-install nixos-enter manual.manpages ]"

Then after mounting the partitions from my target disk,

$ sudo `which nixos-generate-config` --root /mnt
$ sudo groupadd -g 30000 nixbld
$ sudo useradd -u 30000 -g nixbld -G nixbld nixbld

And then after editing the /mnt/etc/nixos/configuration.nix file to add programs that I need,

$ sudo PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt

Finally, I rebooted into the new Nix OS, and then sudo nix search <anything> shows the results from both channels.

Do any steps there create both channels on the target?

Could you check your nix-defexpr ? And give a look at the documentation at Introduction - Nix Reference Manual if you want to know more ?

Mine has

$ tree ~/.nix-defexpr 
/home/gmaudoux/.nix-defexpr
└── channels_root -> /nix/var/nix/profiles/per-user/root/channels

I guess yours could have two entries. I have no idea what this does, except that it is precisely linked to the behavior of these top-level nixos and nixpkgs attributes in some cases.

If you have two entries, it should be safe to keep only the channels_root to fall back to a “proper” nixos distribution setup.

Here’s what I see:

[trusktr@starnix:~]$ tree ~/.nix-defexpr
The program ‘tree’ is currently not installed. You can install it by typing:
  nix-env -iA nixos.tree

[trusktr@starnix:~]$ ll ~/.nix-defexpr
ls: cannot access '/home/trusktr/.nix-defexpr': No such file or directory

[trusktr@starnix:~]$ sudo bash
[sudo] password for trusktr: 

[root@starnix:/home/trusktr]# ll ~/.nix-defexpr
total 0
lrwxrwxrwx 1 root root 44 Jan  6 23:46 channels -> /nix/var/nix/profiles/per-user/root/channels

[root@starnix:/home/trusktr]# ll /nix/var/nix/profiles/per-user/root/channels
lrwxrwxrwx 1 root root 15 Jan  6 23:46 /nix/var/nix/profiles/per-user/root/channels -> channels-1-link

[root@starnix:/home/trusktr]# ll /nix/var/nix/profiles/per-user/root/channels-1-link
lrwxrwxrwx 1 root root 60 Jan  6 23:46 /nix/var/nix/profiles/per-user/root/channels-1-link -> /nix/store/b5x66dl1arb9s8brrk4lsgysmwny4nd6-user-environment

[root@starnix:/home/trusktr]# ll /nix/store/b5x66dl1arb9s8brrk4lsgysmwny4nd6-user-environment
total 8
lrwxrwxrwx 1 root root 60 Dec 31  1969 manifest.nix -> /nix/store/ybia58aj8cmyacpzd58ysdy9y0bd961k-env-manifest.nix
lrwxrwxrwx 1 root root 78 Dec 31  1969 nixos -> /nix/store/8v0n7zn9hpmqays85zwibyfivr4ddyy4-nixos-19.09.1778.db3e8325a9b/nixos

I edited the above, had a mistake

Seems like there is only one channel there.

Aha! Now see this:

[root@starnix:/home/trusktr]# ll /nix/store/8v0n7zn9hpmqays85zwibyfivr4ddyy4-nixos-19.09.1778.db3e8325a9b/nixos
total 3592
-r--r--r--  1 root root    1097 Dec 31  1969 COPYING
-r--r--r--  1 root root     968 Dec 31  1969 default.nix
dr-xr-xr-x  8 root root    4096 Dec 31  1969 doc
dr-xr-xr-x  4 root root    4096 Dec 31  1969 lib
dr-xr-xr-x  3 root root    4096 Dec 31  1969 maintainers
dr-xr-xr-x  7 root root    4096 Dec 31  1969 nixos
lrwxrwxrwx  1 root root       1 Dec 31  1969 nixpkgs -> .
dr-xr-xr-x 17 root root    4096 Dec 31  1969 pkgs
-r--r--r--  1 root root 3637248 Dec 31  1969 programs.sqlite
-r--r--r--  1 root root    5726 Dec 31  1969 README.md
-r--r--r--  1 root root      18 Dec 31  1969 svn-revision

Note the link nixpkgs -> .

I’m wondering why the default steps in the manual lead to this being the case.

I wonder what happens if in the install steps of section 2.5.4 we replace

$ nix-channel --add https://nixos.org/channels/nixos-19.09 nixpkgs
$ nix-channel --update

with

$ nix-channel --remove nixpkgs
$ nix-channel --add https://nixos.org/channels/nixos-19.09 nixos
$ nix-channel --update

Maybe that’s the issue?

Just as a sanity check, does the following print the same path twice? nix eval '([<nixpkgs> <nixos>])'

1 Like

Looks like it does, and in the case of running as root user, it does not, but the root user’s is symlinked to the same place.

[trusktr@starnix:~]$ nix eval '([<nixpkgs> <nixos>])'
[ /nix/var/nix/profiles/per-user/root/channels/nixos /nix/var/nix/profiles/per-user/root/channels/nixos ]

[trusktr@starnix:~]$ sudo nix eval '([<nixpkgs> <nixos>])'
[sudo] password for trusktr: 
[ /nix/var/nix/profiles/per-user/root/channels/nixos /nix/var/nix/profiles/per-user/root/channels/nixos ]

[trusktr@starnix:~]$ sudo bash

[root@starnix:/home/trusktr]# nix eval '([<nixpkgs> <nixos>])'
[ /nix/var/nix/profiles/per-user/root/channels/nixos /root/.nix-defexpr/channels/nixos ]

@lilyball Should I remove that symlink? Is this a bug in the installation procedure? Or did I mess up?

The nixpkgs -> . symlink? I have it too. I assume it’s there for a reason.

@layus @jonringer Do one of you know about that symlink? Is it supposed to be there? Seems like it causes the same channel to be treated like two channels, and shows the same results for “both channels”.

No, it’s so that it can resolve nixpkgs or the directory directly. Just a convenience to allow for some conventions to work

Oh ok. Is nixpkgs is the conventional one? Does that mean if I remove the symlink then things like let pkgs = import <nixpkgs> {}; won’t work?

this was discussed in detail at the office hours today, i would suggest taking a look:

Still trying to get to the bottom of this. Here is an excerpt of the git log for nixos/modules/services/misc/nix-daemon.nix. This show that nixos should not appear, at least when using the new nix xxx interface.

commit 9c53116d499f930f3b79a11ba1fa696623ebfcab
Author: Eelco Dolstra <edolstra@gmail.com>
Date:   Mon Sep 24 10:42:01 2018 +0200

    Revert "nixos: set nixos in nixPath"
    
    This reverts commit 67c8c4917799496dcf2ad6a500441fbbaeb918c9.
    
    'nix run nixos.firefox' is *not* supposed to work - the Nix 2.x
    interface attempts to standardize on nixpkgs.*, to get rid of the
    nixos/nixpkgs confusion that existed with the channels interface. So
    let's not bring that confusion back.

commit 67c8c4917799496dcf2ad6a500441fbbaeb918c9
Author: Matthew Bauer <mjbauer95@gmail.com>
Date:   Sun Sep 23 00:14:27 2018 -0500

    nixos: set nixos in nixPath
    
    This makes using the nixos channel work out of the box with the new
    Nix commands. For example:
    
    $ nix run nixos.firefox -c firefox
    
    Fixes #46536

I would bet that on your system the following command works ?

nix run nixos.firefox -c firefox

On mine it fails with

$ nix run nixos.firefox -c firefox
error: attribute 'nixos' in selection path 'nixos.firefox' not found

For you it should build firefox and run it. If it is indeed the case, I have a long and complex command that could get us a bit further :wink:

nix run nixos.firefox actually starts compiling something on my machine. I guess I have this problem too. Unfortunately nix search -u runs out of memory and crashes on my machine (it does this when I have 2 channels set up; I don’t remember what the output looked like when I only had 1 channel) so I can’t verify that it’s showing duplicate output.

Looking at that commit, I’m assuming this is the code that populates $NIX_PATH? My $NIX_PATH doesn’t contain a nixos=… entry, so this commit is working, and yet nixos.firefox still resolves.

This is what I got as root:

# nix run nixos.firefox -c firefox
[1 copied, 0.0 MiB DL]
Running Firefox as root in a regular user's session is not supported.  ($XAUTHORITY is /run/user/1000/gdm/Xauthority which is owned by trusktr.)

EDIT: works fine as normal user. But either way it resolve nixos.firefox.

firefox, as an application, will fail if it’s started by root

you probably want:

$ nix run nixpkgs.firefox -c firefox

however, this is starting to greatly diverge from the original thread topic