nix-community / nixGL

A wrapper tool for nix OpenGL application [maintainer=@guibou]
712 stars 80 forks source link

fix: remove redundant suffix colons at end of overrided $LD_LIBRARY_PATH #103

Closed blurgyy closed 2 years ago

blurgyy commented 2 years ago

Hi, thank you for this great project!

Signed-off-by: Gaoyang Zhang gy@blurgy.xyz

blurgyy commented 2 years ago

Oops, I hit ctrl+enter too fast, I'll add a descript below:

blurgyy commented 2 years ago

Sorry for the clutter! Here's the full description:

I am recently using pyrender with nixGL, the latest pyrender uses an earlier version of pyopengl (3.1.0), when I try to use the offscreen rendering according to their documentation, I got a wierd error saying:

Traceback (most recent call last):
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/egl.py", line 67, in EGL
    return ctypesloader.loadLibrary(
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/ctypesloader.py", line 45, in loadLibrary
    return dllType( name, mode )
  File "/nix/store/553d7c4xcwp9j1a1gb9cb1s9ry3x1pi9-python3-3.9.13/lib/python3.9/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ('EGL: cannot open shared object file: No such file or directory', 'EGL', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/EGL/__init__.py", line 2, in <module>
    from OpenGL.raw.EGL._types import *
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/raw/EGL/_types.py", line 4, in <module>
    from OpenGL import platform as _p
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/__init__.py", line 35, in <module>
    _load()
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/__init__.py", line 32, in _load
    plugin.install(globals())
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/baseplatform.py", line 92, in install
    namespace[ name ] = getattr(self,name,None)
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/baseplatform.py", line 14, in __get__
    value = self.fget( obj )
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/egl.py", line 93, in GetCurrentContext
    return self.EGL.eglGetCurrentContext
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/baseplatform.py", line 14, in __get__
    value = self.fget( obj )
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/egl.py", line 73, in EGL
    raise ImportError("Unable to load EGL library", *err.args)
ImportError: ('Unable to load EGL library', 'EGL: cannot open shared object file: No such file or directory', 'EGL', None)

After some digging I managed to reproduce it with:

$ PYOPENGL_PLATFORM=egl nixGLIntel python -c 'from OpenGL import EGL; print(EGL)'
Traceback (most recent call last):
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/egl.py", line 67, in EGL
    return ctypesloader.loadLibrary(
  File "/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/platform/ctypesloader.py", line 45, in loadLibrary
    return dllType( name, mode )
  File "/nix/store/553d7c4xcwp9j1a1gb9cb1s9ry3x1pi9-python3-3.9.13/lib/python3.9/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ('EGL: cannot open shared object file: No such file or directory', 'EGL', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
...

The error is apparently caused by the redundant suffix colon in $LD_LIBRARY_PATH, because below code runs without problem:

$ PYOPENGL_PLATFORM=egl nixGLIntel python -c 'import os; os.environ["LD_LIBRARY_PATH"] = os.environ["LD_LIBRARY_PATH"][:-1]; from OpenGL import EGL; print(EGL)'
<module 'OpenGL.EGL' from '/nix/store/q2izq7b3mi40zqf09lld4qz6lmdvzffs-python3-3.9.13-env/lib/python3.9/site-packages/OpenGL/EGL/__init__.py'>

So removing the colon nixGLIntel appends at the end of $LD_LIBRARY_PATH solves the problem. Below is a minimal flake.nix to reproduce the problem:

{
  inputs.nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
  inputs.flake-utils.url = github:numtide/flake-utils;
  inputs.nixgl.url = github:guibou/nixGL;
  /* inputs.nixgl.url = github:blurgyy/nixGL/remove-redundant-suffix-colon;  # This fixes the issue */
  inputs.nixgl.inputs.nixpkgs.follows = "nixpkgs";

  outputs = inputs@{ nixpkgs, flake-utils, nixgl, ... }: flake-utils.lib.eachDefaultSystem (system: let
    pkgs = import nixpkgs {
      inherit system;
      config = {
        allowUnfree = true;
        cudaSupport = true;
      };
      overlays = [
        nixgl.overlay
      ];
    };
  in with pkgs; {
    devShells.default = let
      pyopengl310 = python3Packages.pyopengl.overridePythonAttrs (o: rec{
        version = "3.1.0";
        src = python3Packages.fetchPypi {
          pname = "PyOpenGL";
          inherit version;
          sha256 = "sha256-m0fFw6CU+lGMqIru01rnWDTVPkKFUSxhh59npIyU3a8=";
        };
      });
      thisPython = python3.withPackages (python-packages: with python-packages; [
        pyopengl310
      ]);
    in mkShell {
      name = "ogl";
      buildInputs = [
        pkgs.nixgl.nixGLIntel
        /* pkgs.nixgl.auto.nixGLDefault */
        thisPython
     ];
    };
  });
}

To reproduce, run:

$ nix develop
$ PYOPENGL_PLATFORM=egl nixGLIntel python -c 'from OpenGL import EGL; print(EGL)'

I have not observed this issue with the latest pyopengl (3.1.6) though.

SuperSandro2000 commented 2 years ago

Oops, I hit ctrl+enter too fast, I'll add a descript below:

You can edit comments 🙃

guibou commented 2 years ago

Thank you,

I'm sure that's a "bug" in pyopengl, a redundant colon should work, but hey, let's clean all of that.

blurgyy commented 2 years ago

Thank you for your reply.

I'm sure that's a "bug" in pyopengl

Yes, I think it definitely has something to do with pyopengl.

a redundant colon should work

But in this case removing the redundant colon fixed the issue, sorry if I did not express it clearer before.

let's clean all of that

I just made every modified environment variable to be prepended instead of being overrided, I'm not quite sure if that's what should be done?

guibou commented 2 years ago

@blurgyy sorry for the delay, but yes, I was totally agreeing with you. In summary:

guibou commented 2 years ago

There is a conflict, if you can rebase and fix it, and we'll merge.

blurgyy commented 2 years ago

Sure it's done, I just rebased changes onto the latest HEAD.

blurgyy commented 2 years ago

Hey @guibou I realized the quotes were misplaced so the CI failed earlier, now it should work fine!

guibou commented 2 years ago

Thank you, it is now merged.

Sorry for the time it took. I'm waiting for holidays to work on my open source projects ;(