Install npm packages globally via Nix using node2nix.
Inspired by Nick Novitski's blog post.
n2n is a CLI tool that acts like npm install -g but uses Nix instead. It allows you to install npm packages into your Nix profile without relying on a global npm installation.
# Run directly
nix run github:clord/n2n -- <package-name>
# Install to your profile
nix profile install github:clord/n2n# Install a package
n2n cowsay
n2n typescript
n2n @fission-ai/openspec
# The binary is now available
cowsay "Hello from Nix!"Packages are installed to ~/.nix-profile/bin/.
- Creates a temp directory at
/tmp/nix-npm-install/<package> - Runs
node2nix --nodejs-18to generate Nix expressions from the npm package - Runs
nix-env --installto install the package to your profile
The tool is self-contained: node2nix and nix are bundled via Nix's wrapProgram.
- Supports scoped packages (
@scope/name) - Input validation to prevent injection attacks
- Works in pure flake environments (no channels required)
- Uses Node.js 18 (compatible with current nixpkgs)
# Enter dev shell
nix develop
# Build
cargo build
# Run locally
nix run . -- cowsayMIT