Search code examples
nixnixpkgs

How does Nix able to install individual NPM packages as standalone software?


CAVEAT: If you would like to use Serverless Framework with Nix/NixOS, this is not the way to do it: the package you end up with is outdated, and (as stated below) it probably won't work anyway. See thread on NixOS Discourse.

Wanted to try out Serverless via nix-shell so I looked it up, ran the command

nix-shell -v -p nodePackages.serverless

a̶n̶d̶ i̶t̶ w̶o̶r̶k̶s̶ f̶l̶a̶w̶l̶e̶s̶s̶l̶y̶.1

What makes this possible without requiring me to install Node manually to be able to run npm install -g serverless? (E.g., Is the node_modules folder somewhere in the Nix store? What happens if I nix-shell another Node package - will they share that same directory?)


[1]: It does not... See this Reddit thread; probable setuid issue. Still interested in the behind the scene stuff though.

This question is more like a todo because I really would like to figure it out myself but don't have the time for it right now...


Solution

  • This is possible because it was packaged with node2nix. This tool generates expressions that fetch the various packages and put them in a node_modules directory. Indeed it's not perfect and some package need some extra fixing up to make them work well. The node2nix tooling could 'learn' from the cabal2nix integration in Nixpkgs to improve the quality of packaging and the Nixpkgs developer experience.