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.

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

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