Problems using SDL2 with CMake and flakes

Hello! I’m new to all this. I have a very simple toy SDL2 app I’m making to learn how to use nix. I’m on NixOS. The project is a flake that uses CMake; I must be doing something abnormal, because when I try to run nix develop && cmake ., cmake fails with this error, which I haven’t been able to find online:

-- The C compiler identification is GNU 9.5.0
-- The CXX compiler identification is GNU 9.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /nix/store/qa00jx7v8r3z6kaqhc8kg3bybk801pwp-gcc-wrapper-9.5.0/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /nix/store/qa00jx7v8r3z6kaqhc8kg3bybk801pwp-gcc-wrapper-9.5.0/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:6 (find_package):
  Found package configuration file:

    /nix/store/pl6vn3iqs7icf2czwblf1kmkyqr5xx0i-SDL2-2.28.5-dev/lib/cmake/SDL2/sdl2-config.cmake

  but it set SDL2_FOUND to FALSE so package "SDL2" is considered to be NOT
  FOUND.

What is happening, and what should I do?

Here is my flake.nix:

{
  description = "A simple SDL app, written in C";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      # This is the list of architectures that work with this project
      systems = [
        "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin"
      ];
      perSystem = { config, self', inputs', pkgs, system, ... }: {

        # devShells.default describes the default shell with C++, cmake, boost,
        # and catch2
        devShells.default = pkgs.mkShell {
          packages = with pkgs; [
            # C++ Compiler is already part of stdenv
            cmake
            gcc9
            SDL2
            SDL2.dev
          ];
        };
      };
    };
}

Here is my CMakeLists.txt (more or less a carbon copy of the template given by the SDL wiki):

cmake_minimum_required(VERSION 3.20)

project(SDLApp)

find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)

add_executable(SDLApp main.c)

if(TARGET SDL2::SDL2main)
        target_link_libraries(SDLApp PRIVATE SDL2::SDL2main)
endif()

target_link_libraries(SDLApp PRIVATE SDL2::SDL2)

Thanks in advance?

I believe the reason why you are getting this error lies on the package itself.
The file sdl2-config.cmake reads.

...
set(libdir "/nix/store/21ps3g6b908wxj8q6ghh1drjm4idsw4z-SDL2-2.30.2/lib")
...
set_and_check(SDL2_LIBDIR         "${libdir}")
...
set(_sdl2main_library ${SDL2_LIBDIR}/libSDL2main.a)
if(EXISTS "${_sdl2main_library}")
  ...
  set(SDL2_SDL2main_FOUND TRUE)
else()
  set(SDL2_SDL2main_FOUND FALSE)
endif()
unset(_sdl2main_library)

However, the SDL2_LIBDIR folder does not contain the file libSDL2main.a, but the file libSDL2main.la (see /nix/store/21ps3g6b908wxj8q6ghh1drjm4idsw4z-SDL2-2.30.2/lib/libSDL2main.la).

If that is the case, the solution would be suggesting a package fix. Maybe a more experienced user could help out.

A potential workaround could be using the SDL source as a input.

1 Like

I was able to work around it by overriding SDL2 like this:

let staticSDL2 = SDL2.overrideAttrs (old: { dontDisableStatic = true; });
in
1 Like

Has anyone found a fix? I tried @DoomHammer’s workaround but still getting the same issue. Should I open an issue on github? Here is my flake:

{
  description = "suna sand falling game";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 
  };

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
      staticSDL2 = pkgs.SDL2.overrideAttrs (old: {
        dontDisableStatic = true;
      });
    in {
      devShells.x86_64-linux.default = pkgs.mkShell {
        nativeBuildInputs = [
          staticSDL2 
          staticSDL2.dev
          # SDL2_image
        ];
      }; 
    };
}

and the same error:

CMake Error at CMakeLists.txt:7 (find_package):
  Found package configuration file:

    /nix/store/arszgqa52qjp3bqpnqkbhnvlzacl5wlv-SDL2-2.30.4-dev/lib/cmake/SDL2/sdl2-config.cmake

  but it set SDL2_FOUND to FALSE so package "SDL2" is considered to be NOT
  FOUND.


-- Configuring incomplete, errors occurred!
make: *** [Makefile:233: cmake_check_build_system] Error 1

Hi,

I understand that the root cause of the issue lies within the package itself. If that is the case, I would be confident that this will be resolved in future versions. In the meantime, as a workaround, you may add the following to your CMakeLists.txt file, it has worked on my end:

Your nix flake should have

            buildInputs = with pkgs; [
              ...
              SDL2
              xorg.libXext
              xorg.libXcursor
              xorg.libXi
              xorg.libXfixes
              xorg.libXrandr
            ];
 
find_package(SDL2 REQUIRED)

set(_sdl2_static_private_libs_in "-lm  -lX11 -lXext -lXcursor -lXi -lXfixes -lXrandr -lpthread")
set(_sdl2_static "${SDL2_LIBDIR}/libSDL2.so")
if(EXISTS "${_sdl2_static}")
  if(NOT TARGET SDL2::SDL2-static2)
    add_library(SDL2::SDL2-static2 STATIC IMPORTED)
    set_target_properties(SDL2::SDL2-static2
      PROPERTIES
      IMPORTED_LOCATION "${_sdl2_static}"
      INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
      INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries};${_sdl2_static_private_libs_in}"
      INTERFACE_LINK_DIRECTORIES "${_sdl2_libdirs};${_sdl2_static_private_libdirs}"
      IMPORTED_LINK_INTERFACE_LANGUAGES "C"
      COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
      INTERFACE_SDL_VERSION "SDL2"
    )
  endif()
  set(SDL2_SDL2-static_FOUND TRUE)
else()
  set(SDL2_SDL2-static_FOUND FALSE)
endif()
...
target_link_libraries(target PRIVATE SDL2::SDL2-static2)