Linking issues with simple C compilation in nix-shell

I’m at wits end here, as it feels like I must be doing something wrong; It feels like this has worked in the past, but I can’t seem to make it work anymore.

I’m on NixOS unstable. My channels are up to date. I was able to perform an upgrade just fine.

I made a simple test case, composed of…

CMakeLists.txt:

cmake_minimum_required(VERSION 3.24)
project(SDL2Test)
find_package(SDL2 REQUIRED)
add_executable(SDL2Test main.c)
target_link_libraries(SDL2Test SDL2::SDL2)

main.c:

#include <SDL.h>
int main(int argc, char *argv[]) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window *window = SDL_CreateWindow("SDL2Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  SDL_RenderClear(renderer);
  SDL_RenderPresent(renderer);
  SDL_Delay(1000);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

Here’s where things go wrong. I try to run a simple development workflow through nix-shell, and I wind up with linker errors. Here’s what I run:

nix-shell -p cmake SDL2 --command "rm -fr .build && cmake . -B .build && cmake --build .build"

And the output I get looks like this (truncated since there’s little point in showing all of it):

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /nix/store/1y8i61anhs9hh1g5x3zw2fvdbivwixzg-gcc-wrapper-11.3.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/1y8i61anhs9hh1g5x3zw2fvdbivwixzg-gcc-wrapper-11.3.0/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/john/Code/sdl2test/.build
[ 50%] Building C object CMakeFiles/SDL2Test.dir/main.c.o
[100%] Linking C executable SDL2Test
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libm.so.6, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libX11.so.6, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXext.so.6, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXcursor.so.1, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXi.so.6, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXfixes.so.3, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXrandr.so.2, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libXss.so.1, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: libpthread.so.0, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: warning: librt.so.1, needed by /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so, not found (try using -rpath or -rpath-link)
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XAllocClassHint'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XSendEvent'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XReparentWindow'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `_XFlush'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `tanf@GLIBC_2.2.5'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XFreeModifiermap'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XTranslateCoordinates'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XSetTransientForHint'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `Xutf8ResetIC'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XRRFreeScreenConfigInfo'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XESetEventToWire'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XGetDefault'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XSetIOErrorHandler'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XClearWindow'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `XFixesQueryVersion'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `logf@GLIBC_2.27'
/nix/store/r2b9k28c6aghczpqfvh71y9xavm7rr68-binutils-2.39/bin/ld: /nix/store/byw7k5yff2p8cgq5lv1g70fmp82fi162-SDL2-2.24.2/lib/libSDL2.so: undefined reference to `log10f@GLIBC_2.2.5'
...

I’m able to see that using nix-build to compile SDL2 software that uses CMake works perfectly fine, e.g. I can run:

nix-build "<nixpkgs>" -A milkytracker --option build-use-substitutes false

And even if I verbatim copy the CMake flags that are printed out in the logs for that build and use them to try to build my example, it still fails exactly the same way.

So, does anyone see what I am not seeing? I’ve tried to search for anyone hitting a similar problem and tested a few different approaches but I’m just not figuring it out.

FYI, I was running into this same issue (specifically with SDL2) right down to the store paths and was about to go hunting again for a solution (haven’t seen anything in the issue tracker).

However it looks like updating the unstable channel in the last few days has fixed it for me, just thought I’d let you know as well!

I decided to move all of my machines/projects to flakes since this incident, but yes, it did seem to get resolved at some point. I really wonder what the heck happened, I couldn’t trace it down at all.