GLFW not showing window under Wayland or X11 on NixOS

Hi Community,

This is my first time posting here. I’ve just starting learning GLFW via this tutorial. However, after initially setting up a very basic project the window would not show up at all under Wayland. I’m using a flake-based project on NixOS.

Relevant part of my flake.nix - this basically pulls in all the build dependencies:

          nativeBuildInputs = with pkgs; [
            # Development Tools
            llvm.lldb
            cmake
            cmakeCurses
            boost
            glfw-wayland

            clang-tools
            llvm.clang
          ];

My root level CMakeLists.txt which mentions glfw:

set(GLFW_USE_WAYLAND ON)
find_package(glfw3 REQUIRED)

My src level CMakeLists.txt:

find_package(glfw3 REQUIRED)
add_executable(App)
target_sources(App PRIVATE main.cpp)
target_link_libraries(App glfw)

My main.cpp:

#include <GLFW/glfw3.h>
#include <iostream>

int main() {
  // Any call to GLFW must happen between its initialization and termination
  if (!glfwInit()) {
    std::cerr << "Could NOT initialize GLFW!" << std::endl;
    return 1;
  }

  GLFWwindow *window = glfwCreateWindow(640, 480, "WebGPU for C++", NULL, NULL);
  if (!window) {
    std::cerr << "Could NOT open window!" << std::endl;
    glfwTerminate();
    return 1;
  }
  std::cout << "window:" << window << std::endl;

  glfwMakeContextCurrent(window);

  while (!glfwWindowShouldClose(window)) {
    glfwPollEvents();
  }

  glfwTerminate();
}

The project built successfully but when I ran the executable it just got stuck there forever (because of the while loop) and no window showed up at all. It printed the window pointer so I assume it at least successfully initialized.

After having no luck under Wayland, I switched to X11 desktop, changed the glfw-wayland in flake.nix to the statndard glfw, and removed the set(GLFW_USE_WAYLAND ON) flag. But this time the execution actually errored out with Could NOT open window! - it did not even initialize properly. So it’s not working for me under X11 either.

Where did I do wrong? Am I missing anything? Please help, thank you so much!

EDIT:
The tutorial I mentioned at the beginning of this post doesn’t use any other libraries. But I did see some other places using OpenGL and even having find_package(OpenGL REQUIRED) in CMakeLists.txt. Do I need to do that?

Have you tried running the GLFW examples or tests? I would recommend doing that as a first step.

You can also try using:

OpenGL is only required if you want to use OpenGL (easiest option for graphics), otherwise you can use Vulkan (harder option for graphics as very low level). If you’re calling glfwMakeContextCurrent this is for OpenGL.

On Wayland specifically, you need to swap the buffers of a window for it to become visible.

1 Like

This was my issue, thanks.