Getting libGL in a devshell produced from a flake

I am trying to get a gui rust program to run however while it does compile running produces:

Error: CreationErrors([NotSupported("lacking either libGL or libEGL so could not fallback to other"), NotSupported("both libGL and libEGL are not present")])

Looking around the net for similar issues point to OpenGL not being found in my environment. While OpenGL is enabled on a system level, I am using a devShell to create an isolated development environment. Hence, I am asking what is needed to be done to allow libGL be part of the development environment.

Adding the nixpkgs’ attribute libGL to buildInputs should be sufficient for any reasonable build system. Seeing how yours is a rust project and fails at runtime, they probably just try to dlopen libGL without ever asking about it at build time. In that case you can add ${libGL}/libto the resulting binary’s runpath (patchelf --add-rpath) or expose “${libGL}/lib” via LD_LIBRARY_PATH at runtime

Could you please share, with what you have tried this far? It may or may not help people to answer… :slight_smile:

Here is one flake that works to a degree. I was playing with egui rust lib and examples and most of the examples work with this flake. I’ve got some problems with any example that tries to use “egl”, probably my nvidia settings are not yet good enough. (GL works, e.g. glxgears and such.)

The below flake is not trying to be minimal in anyway yet. Depending on what you are trying, you may want to replace that “crateName” with you project name. Or just take the pkg list and put the into the shell.nix and try with nix-shell. (The env vars I found from other discussion topics here - and possibly somewhere else, didn’t save the links to those.)

# from https://serokell.io/blog/practical-nix-flakes
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    crate2nix = {
      url = "github:kolloch/crate2nix";
      flake = false;
    };
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, crate2nix, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        crateName = "gui3"; 

        inherit (import "${crate2nix}/tools.nix" { inherit pkgs; })
          generatedCargoNix;

        project = pkgs.callPackage (generatedCargoNix {
          name = crateName;
          src = ./.;
        }) {
          defaultCrateOverrides = pkgs.defaultCrateOverrides // {
            # Crate dependency overrides go here
          };
        };
        bInputs = with pkgs; [ 
          cargo rust-analyzer clippy 
          clang
          pkg-config
          xorg.libX11
          xorg.libXcursor
          xorg.libXrandr
          xorg.libXi
          xorg.libxcb 
          libGL
          vulkan-headers
          vulkan-loader
          # vulkan-validation-layers
          fontconfig
          libxkbcommon
          wayland
          #
          python3
          openssl
          # libGLU
          # wgpu-utils
          # egl-wayland
          # libglvnd
          # glslang
          # mesa 
          # freeglut
          # fontconfig
          libxkbcommon
          wayland
          # udev alsa-lib
          at-spi2-atk # for one example (file dialog)
          gdk-pixbuf # for one example (file dialog)
          cairo # for one example (file dialog)
          pango # for one example (file dialog)
          gtk3 # for one example (file dialog)
          gtk3.dev
          gtk3-x11
          gtk3-x11.dev
          gsettings-desktop-schemas # for one example
          #
          # wxGTK32
        ];
      in {
        packages.${crateName} = project.rootCrate.build;

        defaultPackage = self.packages.${system}.${crateName};

        devShell = pkgs.mkShell {
          inputsFrom = builtins.attrValues self.packages.${system};
          nativeBuildInputs = with pkgs; [
            pkg-config
            cmake
            addOpenGLRunpath
            makeWrapper
          ];
          buildInputs = bInputs;
          shellHook = ''
            export XDG_DATA_DIRS=${pkgs.gsettings-desktop-schemas}/share/gsettings-schemas/${pkgs.gsettings-desktop-schemas.name}:${pkgs.gtk3}/share/gsettings-schemas/${pkgs.gtk3.name}:$XDG_DATA_DIRS
            export LD_LIBRARY_PATH="/run/opengl-driver/lib/:${pkgs.lib.makeLibraryPath bInputs}";
          '';
        };
      });
}

Looking at your code it seems that graphics library are not automatically brought into the sell. Using this code

{
  description = "A Nix-flake-based Rust development environment";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
      
  };

  outputs = { self, nixpkgs, rust-overlay,   }:
    let
      overlays = [
        rust-overlay.overlays.default
        (final: prev: {
          rustToolchain =
            let
              rust = prev.rust-bin;
            in
            if builtins.pathExists ./rust-toolchain.toml then
              rust.fromRustupToolchainFile ./rust-toolchain.toml
            else if builtins.pathExists ./rust-toolchain then
              rust.fromRustupToolchainFile ./rust-toolchain
            else
              rust.stable.latest.default;
        })       
      ];
      supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
      forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
        pkgs = import nixpkgs { inherit overlays system; };
      });
    in
    {
      devShells = forEachSupportedSystem ({ pkgs }: {
        default = pkgs.mkShell {
          packages = with pkgs; [
            rustToolchain
            openssl
            pkg-config
            cargo-deny
            cargo-edit
            cargo-watch
            rust-analyzer
	        cmake
	        fontconfig
            wayland
            xorg.libX11
            xorg.libXcursor
            xorg.libXrandr
            xorg.libXi
            mesa
            libGL
            ];
          shellHook = ''
            export LD_LIBRARY_PATH="/run/opengl-driver/lib/:${pkgs.lib.makeLibraryPath [pkgs.libGL]  }";
            '';
        };
      });
    };
}

results in the following errors:

Error: CreationErrors([OsError("Could not create EGL display object"), OsError("`glXQueryExtensionsString` found no glX extensions")])