Can't load/use libGL on nixOS unstable

I am trying to use GLFW to make a context window for OpenGL.
This is my shell.nix
{ pkgs ? import {} }:

pkgs.mkShell {
  buildInputs = with pkgs; [

But while importing glfw using #include<GLFW/glfw3.h>, I get the following error:

/nix/store/0airi7ppyn8qrf9xp1m1wx03qq6ragkg-glfw-3.4/include/GLFW/glfw3.h:241:12: fatal error: GL/gl.h: No such file or directory
[build]   241 |   #include <GL/gl.h>
[build]       |            ^~~~~~~~~
[build] compilation terminated.
cmake_minimum_required(VERSION 3.29.2)
project(opengl_project VERSION 1.0.0)
add_executable(opengl_project main.cpp)
find_package(glfw3 REQUIRED)
target_link_libraries(opengl_project glfw

This is the CMakeLists.txt file

Is there any workaround for the same? Thanks in advance for any help!

You didn’t include any information about how you’re compiling your project. What Nixpkgs’ setup-hooks do with buildInputs = [ ... libGL ... ] is they make the following things work:

  1. find_package(OpenGL) in CMake;
  2. dependency('gl') (or something like that) in meson;
  3. pkg-config --cflags gl egl ... in the shell;
  4. et cetera.

What Nixpkgs doesn’t do is it doesn’t include any setup hooks to automatically force -I flags into your compiler invocations, at least not derived from buildInputs. If you think of this as analogous to “dependency injection”, Nixpkgs is the part that “injects” values into constructors when constructors declare them. The project being built still has to declare them. E.g. if you were using CMake, you’d have to additionally target_link_libraries(... OpenGL::GL). I’m not sure if meson infers from you linking glfw that you want the dependency('gl') linked too

1 Like

Thanks a lot! That worked!