Nodejs application in nix


I’m ventured into npm and nodejs hell… i can feel the temperature rising.

i’m using node2nix, things seem to go well (after creating a 89mb node-packages.nix) , which seems to make a FOD every node dependency in the known universe.

Things seem to go pretty well until i hit node-gyp-build , which can’t find /usr/bin/env/, however Nixos does actually have that, available, unless it’s red herring.

So, what my options on packaging a node app? Or is fact on dream2nix can help me :-).

> node-gyp-build

sh: /nix/store/pf5dl9n7hsrfzamm16wpzjbf65ic471i-application-0.1.0/lib/node_modules/application/node_modules/.bin/node-gyp-build: /usr/bin/env: bad interpreter: No such file or directory
npm ERR! errno 126
npm ERR! bufferutil@4.0.5 install: `node-gyp-build`
npm ERR! Exit status 126
npm ERR! 
npm ERR! Failed at the bufferutil@4.0.5 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
1 Like

Right, so what you’re running into is that JavaScript packages on npm are often not purely JavaScript - usually not even purely scripted languages.

Node-gyp is a C application that builds “native addons”, i.e., things that node will run natively on your computer.

npm permits these kinds of dependencies, and often npm build scripts will download binaries from who-knows-where on the internet to run on your computer (yes, npm build scripts have full internet access, because who cares about reproducibility or source caching, right?).

Naturally, because these binaries are all built for ubuntu 10.04, you can’t directly run them on NixOS.

Luckily, npm2nix does give us the option to override dependencies though.

If you are lucky, something like this will do the trick:

{ pkgs ? import <nixpkgs> { inherit system; }, system ? builtins.currentSystem

let nodePackages = import ./default.nix { inherit pkgs system; };
in nodePackages // {
  shell = nodePackages.bufferutil.override {
    buildInputs = with pkgs; [

And then you use this override as your package, instead of the generated default.nix - that way you can continue letting npm2nix handle most of the horror.

If you’re not lucky, this is in a deep, nested dependency, or the script calls the binary directly with an absolute path, or the hack that works for my shell doesn’t work with generic packages. In the former two cases, you’ll need to figure out how to patch the correct dependencies or override the build script contents (the npm2nix readme explains that a little), in the latter case I’d suggest searching through the upstream issues; I cobbled my hack together from info found there and some reading of the generated files.

Someone should probably document this upstream, mind you :slight_smile:

1 Like

i did your suggested workaround, but alas, the problem persists.

I think i’m starting to really really dislike npm, and the way it does things.

Wether it’s design to be nix unfriendly, by accident or on purpose , i will never know.

Thanks for trying to assist. I will battle some more with it.

For anyone having the same problem, there’s a workaround documented in the node2nix issue.