Mach-nix/python with opengl

Hello, I am trying to get openai gym working. Inspired by Install atari[gym] with mach-nix (i.e. build-time cmake availability) I have the following in a shell.nix file:

{ pkgs ? import <nixpkgs> { } }:

let
  mach-nix = import
    (
      builtins.fetchGit {
        url = "https://github.com/DavHau/mach-nix/";
        ref = "refs/tags/3.3.0";
      }
    )
    {
      inherit pkgs;
      pypiDataRev = "9699653ccac8681ce92004b210b73f5d929d6e41"; # 2021-08-30T20:17:04Z
      pypiDataSha256 = "0zayz2flj46gf6gc3h9ip9i2fkpff50qiyjnrrpcgxjyxnq4rndz";
    };
  custom-python = mach-nix.mkPython {
    python = "python38Full";
    requirements = ''
      gym
      matplotlib
      tqdm
      pyglet
      PyOpenGL
      numpy
    '';
  };
in
with pkgs; mkShell {
  buildInputs = [
    custom-python
  ];
}

Many of the things in the file are probably not needed, but it was a starting point.
I want this to be able to run mountaincar.py:

import gym
import numpy as np
import matplotlib.pyplot as plt
env = gym.make('MountainCar-v0')
a = env.reset()
env.step(env.action_space.sample())
env.render(mode="human")

Entering a nix-shell and running the file.

> nix-shell shell.nix
>python mountaincar.py
python mountaincar.py 
Traceback (most recent call last):
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/gym/envs/classic_control/rendering.py", line 27, in <module>
    from pyglet.gl import *
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/pyglet/gl/__init__.py", line 95, in <module>
    from pyglet.gl.gl import *
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/pyglet/gl/gl.py", line 45, in <module>
    from pyglet.gl.lib import link_GL as _link_function
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/pyglet/gl/lib.py", line 149, in <module>
    from pyglet.gl.lib_glx import link_GL, link_GLU, link_GLX
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/pyglet/gl/lib_glx.py", line 45, in <module>
    gl_lib = pyglet.lib.load_library('GL')
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/pyglet/lib.py", line 164, in load_library
    raise ImportError('Library "%s" not found.' % names[0])
ImportError: Library "GL" not found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mountaincar.py", line 7, in <module>
    env.render(mode="human")
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/gym/core.py", line 254, in render
    return self.env.render(mode, **kwargs)
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/gym/envs/classic_control/mountain_car.py", line 118, in render
    from gym.envs.classic_control import rendering
  File "/nix/store/hmc86a13mcq3csfhgiffcg8dm97j6h4s-python3-3.8.12-env/lib/python3.8/site-packages/gym/envs/classic_control/rendering.py", line 29, in <module>
    raise ImportError(
ImportError: 
    Error occurred while running `from pyglet.gl import *`
    HINT: make sure you have OpenGL installed. On Ubuntu, you can run 'apt-get install python-opengl'.
    If you're running on a server, you may need a virtual frame buffer; something like this should work:
    'xvfb-run -s "-screen 0 1400x900x24" python <your_script.py>'

So it’s complaining about opengl/pyopengl. I tried to add them to the requirements in the shell.nix-file (as you can see - PyOpenGl.
I also tried to add python39Packages.pyopengl to buildInputs, and finally I tried to add hardware.opengl.enable= true; to configuration.nix. Neither of these worked. Is there something else I can try?

>nixos-version
22.05pre333986.73369f8d086 (Quokka)
1 Like