【问题标题】:nodejs headless-gl null in nixosnixos中的nodejs headless-gl null
【发布时间】:2021-12-25 10:20:30
【问题描述】:

我有这个 shell.nix:

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

然后我用nix-shell shell.nix

进入shell
$ npm install gl

我认为它构建正确。

我已经使用项目主页 README 中的示例创建了 index.js

// 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] + ' ')
  }
}

当我运行这个时:

$ node index.js

我收到错误:

\/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

【问题讨论】:

    标签: node.js webgl nixos


    【解决方案1】:

    有效。现在我已经 run.nix:

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

    nix-shell run.nix

    $ nix-shell run.nix
    $ echo $LD_LIBRARY_PATH | tr ':' '\n'
    /nix/store/hwnjiqml5vvlngdj9nrnwg0qzglhgk2r-util-linux-2.36.2/lib
    /nix/store/c35w5n0prvq4v4priyi8fiiq361pmyvp-libGL-1.3.3/lib
    /nix/store/a1xkyw98lgj38kymim78a7xjk50wqg6k-telepathy-glib-0.24.2/lib
    /nix/store/nzp3cc7bsj83dn23b4vvrvsp9psgg50m-telepathy-logger-0.8.2/lib
    $ 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 
    ...
    

    【讨论】:

    • 属性不是由 shell 插入的,所以 $LD_LIBRARY_PATH 在您的环境中逐字结束。您可以删除它(如果没有此 expr 的用户需要它)或将其转换为 shellHook 中的 export 语句,该语句作为常规 bash 运行,包括插值。
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2014-07-07
    • 2014-12-07
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2015-12-16
    相关资源
    最近更新 更多