nvm icon indicating copy to clipboard operation
nvm copied to clipboard

nvm on nixos and bash hashing

Open ORESoftware opened this issue 5 years ago • 18 comments

Bug/something when install node version on NixOS NVM seemed to install fine on NixOS but when:

  • Operating system and version: nixos
[root@ip-172-31-18-139:~]# cat /etc/os-release
NAME=NixOS
ID=nixos
VERSION="19.03.172286.8ea36d73256 (Koi)"
VERSION_CODENAME=koi
VERSION_ID="19.03.172286.8ea36d73256"
PRETTY_NAME="NixOS 19.03.172286.8ea36d73256 (Koi)"
LOGO="nix-snowflake"
HOME_URL="https://nixos.org/"
SUPPORT_URL="https://nixos.org/nixos/support.html"
BUG_REPORT_URL="https://github.com/NixOS/nixpkgs/issues"
  • nvm debug output:
nvm debug
nvm --version: v0.34.0
$SHELL: /run/current-system/sw/bin/bash
$SHLVL: 1
$HOME: /root
$NVM_DIR: '$HOME/.nvm'
$PATH: $HOME/bin:/run/wrappers/bin:$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/etc/profiles/per-user$HOME/bin
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu)'
uname -a: 'Linux 4.19.35 #1-NixOS SMP Wed Apr 17 06:38:55 UTC 2019 x86_64 GNU/Linux'
OS version: NixOS 19.03.172286.8ea36d73256 (Koi)
curl: /run/current-system/sw/bin/curl, curl 7.64.0 (x86_64-pc-linux-gnu) libcurl/7.64.0 OpenSSL/1.0.2r zlib/1.2.11 libssh2/1.8.1 nghttp2/1.36.0
wget: not found
git: not found
grep: /run/current-system/sw/bin/grep, grep (GNU grep) 3.3
awk: /run/current-system/sw/bin/awk, GNU Awk 4.2.1, API: 2.0
sed: /run/current-system/sw/bin/sed, sed (GNU sed) 4.7
cut: /run/current-system/sw/bin/cut, cut (GNU coreutils) 8.30
basename: /run/current-system/sw/bin/basename, basename (GNU coreutils) 8.30
rm: /run/current-system/sw/bin/rm, rm (GNU coreutils) 8.30
mkdir: /run/current-system/sw/bin/mkdir, mkdir (GNU coreutils) 8.30
xargs: /run/current-system/sw/bin/xargs, xargs (GNU findutils) 4.6.0
nvm current: none
which node: which: no node in ($HOME/bin:/run/wrappers/bin:$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/etc/profiles/per-user$HOME/bin)
which iojs: which: no iojs in ($HOME/bin:/run/wrappers/bin:$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/etc/profiles/per-user$HOME/bin)
which npm: which: no npm in ($HOME/bin:/run/wrappers/bin:$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/etc/profiles/per-user$HOME/bin)
npm config get prefix: The program ‘npm’ is currently not installed. You can install it by typing:
  nix-env -iA nixos.nodejs
npm root -g: The program ‘npm’ is currently not installed. You can install it by typing:
  nix-env -iA nixos.nodejs

So I did this:

[root@ip-172-31-18-139:~]# nvm install 11
Downloading and installing node v11.15.0...
Downloading https://nodejs.org/dist/v11.15.0/node-v11.15.0-linux-x64.tar.xz...
####################################################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
-bash: hash: hashing disabled  #### <<<<<<<< ! here is where it got weird
/usr/bin/env: ‘node’: No such file or directory
nvm is not compatible with the npm config "prefix" option: currently set to ""
Run `nvm use --delete-prefix v11.15.0` to unset it.

See here is where it got weird above

ORESoftware avatar Jun 14 '19 01:06 ORESoftware

I’m not sure what “hashing disabled” means - is nixOS posix compliant?

ljharb avatar Jun 14 '19 05:06 ljharb

It looks like this can happen if you’ve set +h. Have you done that?

ljharb avatar Jun 14 '19 05:06 ljharb

I havent actively set +h, but maybe thats the default in the shell, idk if nixos is posix compliant, i just started using it at work b/c I have to

ORESoftware avatar Jun 14 '19 07:06 ORESoftware

nvm requires posix; but i'm not familiar with the h option.

ljharb avatar Jun 14 '19 07:06 ljharb

@ljharb from a google search it appears to be posix compliant, but not 100% sure

ORESoftware avatar Jun 14 '19 17:06 ORESoftware

Can you try set -h prior to running nvm?

ljharb avatar Jun 14 '19 22:06 ljharb

sort of worked at first, but failure happened later? see complete linear history:

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# set -h

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# nvm install 11
nvm: command not found

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13226  100 13226    0     0   187k      0 --:--:-- --:--:-- --:--:--  187k
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 278, done.
remote: Counting objects: 100% (278/278), done.
remote: Compressing objects: 100% (249/249), done.
remote: Total 278 (delta 33), reused 88 (delta 16), pack-reused 0
Receiving objects: 100% (278/278), 142.36 KiB | 6.19 MiB/s, done.
Resolving deltas: 100% (33/33), done.
=> Compressing and cleaning up git repository

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# export NVM_DIR="$HOME/.nvm"

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# nvm install 11
Downloading and installing node v11.15.0...
Downloading https://nodejs.org/dist/v11.15.0/node-v11.15.0-linux-x64.tar.xz...
####################################################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
nvm is not compatible with the npm config "prefix" option: currently set to "/nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1"
Run `npm config delete prefix` or `nvm use --delete-prefix v11.15.0` to unset it.

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# nvm use 11
nvm is not compatible with the npm config "prefix" option: currently set to "/nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1"
Run `npm config delete prefix` or `nvm use --delete-prefix v11.15.0` to unset it.

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# npm config delete prefix

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# nvm use 11
nvm is not compatible with the npm config "prefix" option: currently set to "/nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1"
Run `npm config delete prefix` or `nvm use --delete-prefix v11.15.0` to unset it.

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# node -v
v8.15.1

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# nvm use --delete-prefix v11.15.0
Now using node v11.15.0 (npm v6.7.0)

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# node -v
-bash: /root/.nvm/versions/node/v11.15.0/bin/node: No such file or directory

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# set +h

[root@ec2-xxx-xxx-92-143:/var/lib/backend]# node -v
-bash: /root/.nvm/versions/node/v11.15.0/bin/node: No such file or directory


ORESoftware avatar Jun 14 '19 23:06 ORESoftware

That’s very strange.

I don’t think anyone’s reported trying on nix before; I’m not sure how to debug it.

ljharb avatar Jun 15 '19 02:06 ljharb

I bet it is because NixOS doesn't have a standard /lib folder in the root of the filesystem.

And because libs aren't in a standard place in NixOS, this generic Linux issue happens: https://unix.stackexchange.com/questions/18061/why-does-sh-say-not-found-when-its-definitely-there

Here's a similar issue I opened on the n package: https://github.com/tj/n/issues/603

I haven't found a workaround yet, but I bet it involves somehow telling binaries (binaries that aren't installed the standard NixOS-way) where to find library files, perhaps by setting LD_LIBRARY_PATH to some value that I'm not sure of yet.

trusktr avatar Jan 08 '20 08:01 trusktr

Per https://github.com/tj/n/issues/603#issuecomment-573132830, it seems like NixOS doesn’t have the typical kind of filesystem in a POSIX system, and so there’s a workaround needed.

@trusktr would you be willing to close this by PRring into the docs a section on NixOS?

ljharb avatar Jan 10 '20 17:01 ljharb

I looked at README on GitHub, then realized I had to clone it and run stuff. I'll just leave something here:

The suggestion in https://github.com/tj/n/issues/603#issuecomment-573132830 is doable (I'd have to try it to figure the details), but it will be fighting against the grain of NixOS.

I've realized in NixOS it is far better to manage anything installed in the system by embracing NixOS's nix package manager.

I recommend not faking an FHS filesystem: one would have to stick all their projects in this faked environment, then they'd need to stick any other dependencies in there as well when the need arrises. In the end they may as well not use NixOS and just switch to a traditional linux distro. This approach is a last resort.

Instead, nix can be used to package and install different versions of node (though I haven't done that yet, so I don't have the exact steps needed to do so). It'll take a learning curve to learn how to package arbitrary things with nix, but it'll be worth the effort (in a similar vain as learning Vim makes it worth the effort for those who do).

trusktr avatar Jan 10 '20 17:01 trusktr

If you could edit and make a PR in the web UI i can easily clean that up for you :-)

ljharb avatar Jan 10 '20 18:01 ljharb

As for packaging up node with nix, one of the major reasons nvm is so widely used is that it uses the only official distribution channel, nodejs.org. Packaging up node makes it just as problematic as apt and yum repos hosting node versions.

ljharb avatar Jan 10 '20 18:01 ljharb

I got the same issue running python virtual env

source /home/mudrii/src/github/.venv/bin/activate
bash: hash: hashing disabled
bash: hash: hashing disabled

Sems activate using hash -r

    # This should detect bash and zsh, which have a hash command that must
    # be called to get it to forget past commands.  Without forgetting
    # past commands the $PATH changes we made may not be respected
    if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
        hash -r
    fi

mudrii avatar Feb 14 '20 11:02 mudrii

https://github.com/nvm-sh/nvm/issues/2065#issuecomment-573140907

Altough you said it should be the last resort to fake a fhs fs in nixos, i think its a valid option and depents on peoples use case. I use nvm primarily for work and dependent of the project type - i dont need a globaly working instance - and i came across the nix-shell command which is kind of exactly what i needed. You don't need to create one fhs filesystem for everything, you can create different nix-shells for different projects. This is really awesome for keeping your base system tidy, since you can specify environment dependencies (e.g. python, node, libs etc..) on a project basis in a shell.nix and login to that shell by calling nix-shell

I personally find this a lot better than going back to a regular distro since nixos solves alot of other problems for me :slightly_smiling_face:

Btw. using nix to manage node versions is no real option here, at least not for me. The nix pkgs are sometimes kind of outdated and incomplete (e.g. some node versions are missing)

mara-schulke avatar Feb 17 '21 18:02 mara-schulke

Weirdly enough, I've been using nvm with nix (on macOS) for a while without issues. Just jumped onto nixOS and I'm seeing problems (but not the same as @ORESoftware).

I assumed everything would "just work" because the installation is to the home directory but I'm guessing it's not as simple as this.

Installation works fine which is what I would expect given installation is in the home directory

$ nvm install 15                                                                                                                                                                                                                                                                   
Downloading and installing node v15.14.0...
Local cache found: ${NVM_DIR}/.cache/bin/node-v15.14.0-linux-x64/node-v15.14.0-linux-x64.tar.xz
Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/bin/node-v15.14.0-linux-x64/node-v15.14.0-linux-x64.tar.xz
Now using node v15.14.0 (npm v)

Adopting the installed nvm package also works fine

$ nvm use 15
Now using node v15.14.0 (npm v)

$ which node
/home/andy/.nvm/versions/node/v15.14.0/bin/node

It's only at execution time where this starts to become a problem.

$ node
zsh: command not found: node

$ npm
/usr/bin/env: ‘node’: No such file or directory

$ which node
/home/andy/.nvm/versions/node/v15.14.0/bin/node

$ /home/andy/.nvm/versions/node/v15.14.0/bin/node
zsh: no such file or directory: /home/andy/.nvm/versions/node/v15.14.0/bin/node

$ ls /home/andy/.nvm/versions/node/v15.14.0/bin/node
/home/andy/.nvm/versions/node/v15.14.0/bin/node

The node binary is definitely there, it's in the path, ls confirms it exists, but for some reason I'm getting reports that it isn't there when trying to execute it.

Sorry I don't have much to contribute - let me know if you have any suggestions!

Additional info

I'm using zsh-nvm

nvm debug output
nvm --version: v0.38.0
$SHELL: /run/current-system/sw/bin/bash
$SHLVL: 1
whoami: 'andy'
${HOME}: /home/andy
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: ${NVM_DIR}/versions/node/v15.14.0/bin::/run/wrappers/bin:${HOME}/.nix-profile/bin:/etc/profiles/per-user/andy/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:${HOME}/.zsh/plugins/node-path:${HOME}/.zsh/plugins/zsh-completions:${HOME}/.zsh/plugins/zsh-autosuggestions:${HOME}/.zsh/plugins/zsh-syntax-highlighting:${HOME}/.zsh/plugins/zsh-256color:${HOME}/.zsh/plugins/zsh-nvm:${HOME}/.zsh/plugins/zsh-better-npm-completion:${HOME}/.zsh/plugins/zsh-autoswitch-virtualenv:${HOME}/.zsh/plugins/zsh-powerline-powerlevel10k:${HOME}/.zsh/plugins/zsh-history-substring-search
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu)'
uname -a: 'Linux 5.10.40 #1-NixOS SMP Wed May 26 10:06:57 UTC 2021 x86_64 GNU/Linux'
checksum binary: 'sha256sum'
OS version: NixOS 21.11.20210603.95222ea (Porcupine)
curl: /run/current-system/sw/bin/curl, curl 7.76.1 (x86_64-pc-linux-gnu) libcurl/7.76.1 OpenSSL/1.1.1k zlib/1.2.11 brotli/1.0.9 libssh2/1.9.0 nghttp2/1.43.0
wget: /run/current-system/sw/bin/wget, GNU Wget 1.21.1 built on linux-gnu.
git: /etc/profiles/per-user/andy/bin/git, git version 2.31.1
ls: cannot access 'grep:': No such file or directory
grep: grep: aliased to grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}), grep (GNU grep) 3.6
awk: /run/current-system/sw/bin/awk, GNU Awk 5.1.0, API: 3.0
sed: /run/current-system/sw/bin/sed, sed (GNU sed) 4.8
cut: /run/current-system/sw/bin/cut, cut (GNU coreutils) 8.32
basename: /run/current-system/sw/bin/basename, basename (GNU coreutils) 8.32
rm: /run/current-system/sw/bin/rm, rm (GNU coreutils) 8.32
mkdir: /run/current-system/sw/bin/mkdir, mkdir (GNU coreutils) 8.32
xargs: /run/current-system/sw/bin/xargs, xargs (GNU findutils) 4.7.0
nvm current:
which node: ${NVM_DIR}/versions/node/v15.14.0/bin/node
which iojs: iojs not found
which npm: ${NVM_DIR}/versions/node/v15.14.0/bin/npm
npm config get prefix: /usr/bin/env: ‘node’: No such file or directory
npm root -g: /usr/bin/env: ‘node’: No such file or directory

andyrichardson avatar Jun 03 '21 10:06 andyrichardson

@andyrichardson it's to the home dir, but it still requires POSIX compliance.

This seems like a zsh-nvm issue, since invoking node is attempting to invoke zsh - that's not how normal node works.

ljharb avatar Jun 03 '21 16:06 ljharb

Prebuilt Node.js binaries will never work out of the box on NixOS, because of non-standard libraries paths, as it was mentioned here before. On macOS its fine, because system libraries are in place where they're expected.

One workaround what nvm could do on NixOS, is to invoke patchelf or nixpkgs' autoPatchelf on downloaded Node.js binary, but it's tricky to set up.

I have a very WIP Node.js versions overlay which patches Node.js as mentioned above - https://github.com/mikroskeem/node-overlay

mikroskeem avatar Jan 09 '22 12:01 mikroskeem