Nodejs headless-gl does not work

I have this shell.nix:

with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "env";
  nativeBuildInputs = [ pkg-config ];
  buildInputs = [
    xorg.libX11
    xorg.libX11.dev
    xorg.libXi
    xorg.libXext
    libGL
  ];
}

Then I enter the shell with nix-shell shell.nix and

$ npm install gl

It builds correctly I think.

I have created index.js with example from project’s home README

// Create context
var width   = 64
var height  = 64
var gl = require('gl')(width, height, { preserveDrawingBuffer: true })

//Clear screen to red
gl.clearColor(1, 0, 0, 1)
gl.clear(gl.COLOR_BUFFER_BIT)

//Write output as a PPM formatted image
var pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
process.stdout.write(['P3\n# gl.ppm\n', width, " ", height, '\n255\n'].join(''))

for(var i = 0; i < pixels.length; i += 4) {
  for(var j = 0; j < 3; ++j) {
    process.stdout.write(pixels[i + j] + ' ')
  }
}

when I run this:

$ node index.js

I got error:

\/home/roman/personal_projects/nixos/nixos-headless-gl-error-no-webgl/index.js:20
gl.clearColor(1, 0, 0, 1)
   ^

TypeError: Cannot read property 'clearColor' of null
    at Object.<anonymous> (/home/roman/personal_projects/nixos/nixos-headless-gl-error-no-webgl/index.js:20:4)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

Works. Now I have run.nix:

with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "env";
  LD_LIBRARY_PATH = "${lib.makeLibraryPath [ libGL ]}:$LD_LIBRARY_PATH";
}

and nix-shell run.nix and

$ nix-shell run.nix
$ echo $LD_LIBRARY_PATH | tr ':' '\n'
/nix/store/c35w5n0prvq4v4priyi8fiiq361pmyvp-libGL-1.3.3/lib
$LD_LIBRARY_PATH
$ node index.js
P3
# gl.ppm
64 64
255
255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 
...