NixOS + Nodejs

In NixOS, you install the specific Node.js version according to your requirements, such as Node.js version 18, 20, 21, 22, etc.

Check out the following example to install node.js in your NixOS.

# installing nodejs package in system profile.
environment.systemPackages = with pkgs; [ nodejs_23 ];
# nodejs_22
environment.systemPackages = with pkgs; [ nodejs_22 ];
# nodejs_20
environment.systemPackages = with pkgs; [ nodejs_20 ];

However, you can not install multiple node versions in NixOS. The following example explains this better.

environment.systemPackages = with pkgs; [ nodejs_22 nodejs_20 ]; # it not work.

The most effective method for installing the Node version in NixOS is to utilise the Node.js version manager.

Several Node.js version managers are accessible in the open-source server environment, including NVM, N, fnm, asdf, and Volta.

In this tutorial, we use the Fnm Node.js version manager, which is faster, simpler, and easier to use than other Node version managers. Fnm works with .node-version and .nvmrc files.

If you want to learn a comparison between different node versions, check out this tutorial: Node.js Version Managers — Install and Run Multiple Node.js Versions.

Step up

  1. How to install the Fnm Node.js version manager in NixOS? · Shell SetupBash ShellZsh ShellFish shellConfiguration Option
  2. How to use the Fnm Node.js version manager in NixOS?list-remotelistinstall (Important)use (Important)defaultcurrentuninstall
  3. Conclusion

Demo

Fnm Demo
Fnm Demo

How to install the Fnm Node.js version manager in NixOS?

To use The Fnm Nodejs version, first install the Fnm package in NixOS.

// configuration.nix

environment.systemPackages = with pkgs; [
    fnm
];

// home-manager

home.packages = with pkgs; [ fnm ]

// nix-env

nix-env -iA nixos.fnm

Shell Setup

The next step is to set environment variables must be set up before using fnm in NixOS according to your shell, such as bash, zsh, etc.

Bash Shell

Add the following to your .bashrc profile:

eval "$(fnm env --use-on-cd --shell bash)"

Zsh Shell

Add the following to your .zshrc profile:

eval "$(fnm env --use-on-cd --shell zsh)"

Fish shell

Create ~/.config/fish/conf.d/fnm.fish and add this line to it:

fnm env --use-on-cd --shell fish | source

Fnm has another configuration to enable highly recommended features, like automatic version switching.

If you face the following error when you use fnm

➜  ~ fnm use "v22.13.1"    
error: We can't find the necessary environment variables to replace the Node version.
You should setup your shell profile to evaluate `fnm env`, see https://github.com/Schniz/fnm#shell-setup on how to do this
Check out our documentation for more information: https://fnm.vercel.app

That means you don't configure the fnm in the shell; first, complete the Shell Setup, then use it, and your error is solved.

Configuration Option

The --use-on-cd option is useful when changing directories. Fnm switches the Node.js version according to the current directory's requirements based on .node-version or .nvmrc (or packages.json#engines#node if — resolve-engines is enabled).

Check out the fnm configuration documentation for the other --version-file-strategy=recursive , --corepack-enabled , and --resolve-enginesoptions.

How to use the Fnm Node.js version manager in NixOS?

You can use Fnm very easily using Fnm CLI. With the Fnm CLI, you can install and manage the Node version in NixOS.

Check out all options in the fnm option using the fnm --help command.


➜  ~ fnm --help       
A fast and simple Node.js manager

Usage: fnm [OPTIONS] <COMMAND>

Commands:
  list-remote  List all remote Node.js versions [aliases: ls-remote]
  list         List all locally installed Node.js versions [aliases: ls]
  install      Install a new Node.js version
  use          Change Node.js version
  env          Print and set up required environment variables for fnm
  completions  Print shell completions to stdout
  alias        Alias a version to a common name
  unalias      Remove an alias definition
  default      Set a version as the default version
  current      Print the current Node.js version
  exec         Run a command within fnm context
  uninstall    Uninstall a Node.js version
  help         Print this message or the help of the given subcommand(s)

Options:
      --node-dist-mirror <NODE_DIST_MIRROR>
          <https://nodejs.org/dist/> mirror
          
          [env: FNM_NODE_DIST_MIRROR]
          [default: https://nodejs.org/dist]

      --fnm-dir <BASE_DIR>
          The root directory of fnm installations
          
          [env: FNM_DIR]

      --log-level <LOG_LEVEL>
          The log level of fnm commands
          
          [env: FNM_LOGLEVEL]
          [default: info]
          [possible values: quiet, error, info]

      --arch <ARCH>
          Override the architecture of the installed Node binary. Defaults to arch of fnm binary
          
          [env: FNM_ARCH]

      --version-file-strategy <VERSION_FILE_STRATEGY>
          A strategy for how to resolve the Node version. Used whenever `fnm use` or `fnm install` is called without a version, or when `--use-on-cd` is configured on evaluation
          
          [env: FNM_VERSION_FILE_STRATEGY]
          [default: local]

          Possible values:
          - local:     Use the local version of Node defined within the current directory
          - recursive: Use the version of Node defined within the current directory and all parent directories

      --corepack-enabled
          Enable corepack support for each new installation. This will make fnm call `corepack enable` on every Node.js installation. For more information about corepack see <https://nodejs.org/api/corepack.html>
          
          [env: FNM_COREPACK_ENABLED]

      --resolve-engines
          Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
          Experimental: This feature is subject to change.
          Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
          
          [env: FNM_RESOLVE_ENGINES]

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

In this tutorial, we learn only the most important options that you use daily in your life.

  1. list-remote
  2. list
  3. install
  4. use
  5. default
  6. current
  7. uninstall

list-remote

The list-remote option lists all remote Node.js versions.

fnm list-remote

The command output looks like this.

fnm list-remote
...
v22.10.0
v22.11.0 (Jod)
v22.12.0 (Jod)
v22.13.0 (Jod)
v22.13.1 (Jod)
v23.0.0
v23.1.0
v23.2.0
v23.3.0
v23.4.0
v23.5.0
v23.6.0
v23.6.1
v23.7.0

list

The list option lists all locally installed Node.js versions in your NixOS.

fnm list

The command output looks like this.

fnm list             
* v22.13.1 default
* v23.0.0
* v23.0.2
* system

install (Important)

The install option installs a new Node.js version in NixOS.

# Fnm Syntax
fnm install "node-version"

# Install nodejs "v23.0.0" version in your NixOS.
fnm install "v23.0.0"

The command output looks like this.

fnm install "v23.0.0"
Installing Node v23.0.0 (x64)
00:00:08 ███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ 28.75 MiB/28.75 MiB (3.36 MiB/s, 0s)

use (Important)

The use option is used to change or switch different Node.js versions in NixOS based on your requirement or use case.

# Syntax: (Switching between one version or another.)
fnm use "node-version"

# Use option install nodejs "v23.0.0" version in your NixOS.
fnm use "v23.0.0"

The command output looks like this.

fnm use "v23.0.0"          
Using Node v23.0.0

default

The default option helps you set up a default version in your NixOS.

fnm default "v22.0.0"

The command output looks like this.

fnm default "v22.0.0"
fnm ls               
* v22.0.0 default
* v22.13.1
* v23.0.0
* system

current

The current option helps you to print the current active Node.js version.

fnm current

The command output looks like this.

fnm current                  
v22.0.0

uninstall

The uninstall option allows you to remove the locally installed node version that you no longer need.

# Syntax: (Uninstall or Remove the node version.)
fnm uninstall "node-version"

# Uninstall the node version "v22.0.0" 
fnm uninstall "v22.0.0"

The command output looks like this.

fnm uninstall "v22.0.0"
Node version v22.0.0 was removed successfully.

Error

Installing fnm, using fnm, and installing multiple Node packages; after restarting your NixOS, if you encounter the following error:

➜  ~ node -v
Could not start dynamically linked executable: node
NixOS cannot run dynamically linked executables intended for generic
linux environments out of the box. For more information, see:
https://nix.dev/permalink/stub-ld

Then, add the following code to your configuration.nix file and rebuild your NixOS.

# configuration.nix
programs.nix-ld.enable = true;
programs.nix-ld.libraries = with pkgs; [
   fnm # Add fnm here
];

The nix-ld package helps to run the dynamically linked executable file. In our case, it is a nodejs binary file.

If you have any problems, check out my NixOS configuration.

Conclusion

The Fnm node version manager can be used in NixOS and with other Linux distros, such as Ubuntu, Debian, Arch, Windows, and MacOS.

It is easy to use, has easy installation, and installs the node version package very fast compared to others. It supports cross-platform (MacOS, Windows, Linux) and works with .node-version and .nvmrc files as well.

You can use Fnm, or if you don't like it, you can try other node package managers.

To learn more about Nixos and Linux stuff, follow the Linux publication on Medium and other updates. Follow me on Twitter (X) and Medium.