Nix-env error opening lock file

I’m new to NixOS and am getting a strange error when trying to install packages as a normal user. I created a normal user account, but when trying to use nix-env to install packages as that user it seems to be trying to open the root user’s nix profile:

$ nix-env -iA nixos.hello
installing ‘hello-2.10’
error: opening lock file ‘/nix/var/nix/profiles/per-user/root/profile.lock’: Permission denied

Any idea why this is happening?

I bet what happened was the very first time you ran a login shell after installing Nix (and setting up the bash profile integration) was with sudo. When the bash setup script is sourced, if your ~/.nix-profile doesn’t exist, it’s created for you, and I expect that if you run ls -l ~/.nix-profile right now you’ll see that it points at /nix/var/nix/profiles/per-user/root/profile. Try deleting ~/.nix-profile and spawning a new shell, or just run nix-env --switch-profile /nix/var/nix/profiles/per-user/$USER/profile

Looks like that was exactly it. Thanks!

I went ahead and filed