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