No Suitable Graphics Device Exception (OpenGL missconfigured?)

Hello everyone,
for a project, I have to develop with csharp, dotnet, and a library called Monogame on my NixOS machine.
For my development environment I am using a flake that looks something like this

{
  description = "dotnet flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };
  
  outputs = { self, nixpkgs }: 
  let 
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
  in
  {
    devShells.${system}.default = pkgs.mkShell {
      buildInputs = with pkgs; [
          dotnetCorePackages.dotnet_8.sdk
      ];
    };
  };
}

As intended the nix develop provides me with the dotnet command, and compiling the project with dotnet build returns with no errors.
But running dotnet run results in the following stack trace.

Unhandled exception. Microsoft.Xna.Framework.Graphics.NoSuitableGraphicsDeviceException: Failed to create graphics device!
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at MonoGame.OpenGL.GL.LoadExtensions()
   at MonoGame.OpenGL.GraphicsContext..ctor(IWindowInfo info)
   at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformSetup()
   at Microsoft.Xna.Framework.Graphics.GraphicsDevice.Setup()
   at Microsoft.Xna.Framework.Graphics.GraphicsDevice..ctor(GraphicsAdapter adapter, GraphicsProfile graphicsProfile, Boolean preferHalfPixelOffset, PresentationParameters presentationParameters)
   at Microsoft.Xna.Framework.GraphicsDeviceManager.CreateDevice(GraphicsDeviceInformation gdi)
   at Microsoft.Xna.Framework.GraphicsDeviceManager.CreateDevice()
   --- End of inner exception stack trace ---
   at Microsoft.Xna.Framework.GraphicsDeviceManager.CreateDevice()
   at Microsoft.Xna.Framework.Game.DoInitialize()
   at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
   at Program.<Main>$(String[] args) in /path/to/project/Program.cs:line 2

Does anyone have an idea why this error could occur?
I am using NixOS with the i3 window manager.

Thanks for any help.

I found an issue with the same error. I think, probably you need the LD_LIBRARY_PATH hack from the first comment. Or you can LD_DEBUG it as suggested later.

Thank you so much for pointing me to this issue.
So yes adding LD_LIBRARY_PATH fixed the issue for me.
The flake now looks something like this

{
  description = "dotnet monogame flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };
  
  outputs = { self, nixpkgs }: 
  let 
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
  in
  {
    devShells.${system}.default = pkgs.mkShell {
      buildInputs = with pkgs; [
        dotnet-sdk_8
      ];
      LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
          pkgs.freetype
          pkgs.libGL
          pkgs.pulseaudio
          pkgs.xorg.libX11
          pkgs.xorg.libXrandr
      ];
    };
  };
}
1 Like