Node2Nix Issues

I’ve been trying all day to get a node/React app (https://github.com/dekkerglen/CubeCobra) running on my new NixOS install. It has been hanging up on canvas. My branch with my attempts at getting it working is https://github.com/ruler501/CubeCobra/tree/fix-install which used node2nix with a wrapper and a flakes.nix. So my first issue was that canvas couldn’t find node-pre-gyp to build with so I had to add it to the buildInputs. Then it was failing to build canvas since it couldn’t download the prebuilt and I hadn’t included the dependencies for it. After spending quite a while down a rabbit hole trying to get the prebuilt working I realized that when building flakes it blocks internet access from the build. In the meantime I tried building new versions of node-pre-gyp with supplement.json to try and get a fix to an issue with needle not working. It wouldn’t compile so I ended up using @discordjs/node-pre-gyp which did work. I finally ended up having to also put all the dependencies for canvas in the buildInputs.

Then I got the environment built, but when trying to run it with npm run devstart it errored out that it couldn’t find babel-loader which was missing from the .bin even though it did seem to list it as a dependency in the generated nix files. I’ve tried adding it to supplement.json where it failed to build due to missing yarn and then needing an internet connection.

I can get a little farther by running npm install inside of the nix develop environment which I know is antithetical to the Nix paradigm. That errors out on devstart as well just this time due to canvas not being able to find libuuid.so.1 even though there is libuuid in the build inputs.

Anyone have any ideas on what I’m doing wrong? I’m quite new to Nix.

1 Like

This is my build.nix (you only need libuuid I suppose):

with import <nixpkgs> { };
stdenv.mkDerivation {
  name = "env";
  nativeBuildInputs = [ pkg-config ];
  buildInputs = [
    autoreconfHook
    xorg.libX11
    xorg.libXi
    xorg.libXext
    libGLU
    zlib
    glibc.out
    glibc.static
    libpng
    nasm
    cairo
    pango
    libuuid # canvas
  ];

  # workaround for npm dep compilation
  # https://github.com/imagemin/optipng-bin/issues/108
  shellHook = ''
    LD=$CC
  '';
}
$ nix-shell build.shell
$ npm install

and this is my run.nix:

{ pkgs ? import <nixpkgs> { } }:
with pkgs; mkShell {
  name = "node-dev-shell";
  # Attributes aren't interpolated by the shell, so $LD_LIBRARY_PATH ends up verbatim in your environment. You could remove it (if no users of this expr need it) or convert it to an export statement in shellHook, which runs as regular bash, including interpolation. https://stackoverflow.com/questions/69953573/nodejs-headless-gl-null-in-nixos/69953610?noredirect=1#comment123682825_69953610
  # https://github.com/albertgoncalves/ranim/blob/e59ee646c155fefba69b6f3b9aaad0402d360c2e/shell.nix#L37
  # test with `echo $LD_LIBRARY_PATH` after entering with nix-shell
  APPEND_LIBRARY_PATH = "${lib.makeLibraryPath [ libGL libuuid ]}";
  shellHook = ''
    export LD_LIBRARY_PATH="$APPEND_LIBRARY_PATH:$LD_LIBRARY_PATH"
  '';
}
$ nix-shell run.nix

$ echo $LD_LIBRARY_PATH | tr ':' '\n'
/nix/store/8pj8n7g8cfbbra79lmhc93nk5nv92drk-util-linux-2.37.2/lib
/nix/store/5icl3gj83q5ar5klx2lc61qyll669inw-libGL-1.3.4/lib
/nix/store/yxflij8cg4fgnzqmda91jx4d94jvkjf5-util-linux-2.37.2-lib/lib
/nix/store/ikh86qfbsi23iajdpbrf642q6v612cw8-telepathy-glib-0.24.2/lib
/nix/store/m8bmifhlakz1mlsz37ki6cwfw2nxry66-telepathy-logger-0.8.2/lib

$ node index.js

util-linux contains libuuid.

I see that discord also uses lib.makeLibraryPath:

1 Like