Help fixing a broken nix-env installation on NixOS

So something went wrong for me after running some nix-env --switch-generation and nix-env --profile commands. I can’t recall exactly what I ran and what happened next, but I got this nasty error after these commands when I tried to garbage collect:

Mar 24 20:25:55 NUX 1hdw8pxfay0zx2mb6pbnlhczgbzqw2j8-unit-script-nix-gc-start[30461]: error: cannot unlink '/nix/var/nix/profiles/per-user/doron/profile-782-link': No such file or directory

So I removed that link manually :grimacing: and all other links as well. And now I can’t install anything to my user’s profile via nix-env.

Is there a way to recreate all of these directories just like in a fresh NixOS installation??

What output do you get when trying to install something?

I’ve managed to install something with:

/var/src/nixpkgs $ nix-env -f. -iA

And now my profile at least seems to be active - nix-env -q works. But outside nixpkgs and without -f., I get:

error: attribute 'nerdfonts' in selection path 'nerdfonts' not found

What is the output of ‘echo $NIX_PATH’

nixos=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix

I’ve had a discussion about this $NIX_PATH in Use only 1 channel for my user's `nix-env` and root's `nixos-rebuild` - #10 by doronbehar .

And here: /nix/var/nix/profiles/per-user/root/channels/nixos I have:

lrwxrwxrwx 2 root root 82 Jan  1  1970 /nix/var/nix/profiles/per-user/root/channels/nixos -> /nix/store/3m9z2vfr0fnfdgrshyy70cidhyp5wbkz-nixos-20.09pre217537.d96bd3394b7/nixos

Try ‘nix-env - f “<nixos>” -iA package’

Well that sort of works: I can install packages using their attribute but without the regular nixos. prefix, which is kind of weird but I guess it’s better then nothing. I would like though to not need that -f <nixos> argument.

I also ran (to debug):

$ nix-env -vq
evaluating file '/nix/store/83f38s2vp9nlkh3akilbhw2869xy0b9l-nix-2.3.3/share/nix/corepkgs/derivation.nix'
evaluating file '/nix/store/w213fg5zb7df8h1wsldqpqi7yk9d80rk-env-manifest.nix'

And I’ve found out that the last file is empty which is interesting…

I’ve fixed this by re-running:

ln -s /nix/var/nix/profiles/per-user/root/channels .nix-defexpr

As explaind in Use only 1 channel for my user's `nix-env` and root's `nixos-rebuild` - #10 by doronbehar .