8 libGLSupported ? lib.elem stdenv.hostPlatform.system mesa.meta.platforms,
9 openglSupport ? libGLSupported,
11 alsaSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
13 x11Support ? !stdenv.hostPlatform.isWindows && !stdenv.hostPlatform.isAndroid,
24 waylandSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
32 dbusSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
34 udevSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
38 libdecorSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
40 pipewireSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
41 pipewire, # NOTE: must be built with SDL2 without pipewire support
43 config.pulseaudio or stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
53 withStatic ? stdenv.hostPlatform.isMinGW,
67 # NOTE: When editing this expression see if the same change applies to
70 stdenv.mkDerivation (finalAttrs: {
74 src = fetchFromGitHub {
77 rev = "release-${finalAttrs.version}";
78 hash = "sha256-ij9/VhSacUaPbMGX1hx2nz0n8b1tDb1PnC7IO9TlNhE=";
80 dontDisableStatic = if withStatic then 1 else 0;
85 outputBin = "dev"; # sdl-config
88 # `sdl2-config --cflags` from Nixpkgs returns include path to just SDL2.
89 # On a normal distro this is enough for includes from all SDL2* packages to work,
90 # but on NixOS they're spread across different paths.
91 # This patch + the setup-hook will ensure that `sdl2-config --cflags` works correctly.
96 # Fix running wayland-scanner for the build platform when cross-compiling.
97 # See comment here: https://github.com/libsdl-org/SDL/issues/4860#issuecomment-1119003545
98 substituteInPlace configure \
99 --replace '$(WAYLAND_SCANNER)' 'wayland-scanner'
104 depsBuildBuild = [ pkg-config ];
108 ++ lib.optionals waylandSupport [
113 dlopenPropagatedBuildInputs =
115 # Propagated for #include <GLES/gl.h> in SDL_opengles.h.
116 ++ lib.optional (openglSupport && !stdenv.hostPlatform.isDarwin) libGL
117 # Propagated for #include <X11/Xlib.h> and <X11/Xatom.h> in SDL_syswm.h.
118 ++ lib.optionals x11Support [ libX11 ];
120 propagatedBuildInputs =
121 lib.optionals x11Support [ xorgproto ] ++ finalAttrs.dlopenPropagatedBuildInputs;
124 lib.optionals alsaSupport [
128 ++ lib.optional dbusSupport dbus
129 ++ lib.optional libdecorSupport libdecor
130 ++ lib.optional pipewireSupport pipewire
131 ++ lib.optional pulseaudioSupport libpulseaudio
132 ++ lib.optional udevSupport udev
133 ++ lib.optionals waylandSupport [
137 ++ lib.optionals x11Support [
147 ++ lib.optionals drmSupport [
154 ++ finalAttrs.dlopenBuildInputs
155 ++ lib.optional ibusSupport ibus
156 ++ lib.optionals waylandSupport [ wayland-protocols ]
157 ++ lib.optionals stdenv.hostPlatform.isDarwin [
166 enableParallelBuilding = true;
170 ++ lib.optional (!x11Support) "--without-x"
171 ++ lib.optional alsaSupport "--with-alsa-prefix=${alsa-lib.out}/lib"
172 ++ lib.optional stdenv.hostPlatform.isWindows "--disable-video-opengles"
173 ++ lib.optional stdenv.hostPlatform.isDarwin "--disable-sdltest";
175 # We remove libtool .la files when static libs are requested,
176 # because they make the builds of downstream libs like `SDL_tff`
177 # fail with `cannot find -lXext, `-lXcursor` etc. linker errors
178 # because the `.la` files are not pruned if static libs exist
179 # (see https://github.com/NixOS/nixpkgs/commit/fd97db43bcb05e37f6bb77f363f1e1e239d9de53)
180 # and they also don't carry the necessary `-L` paths of their
182 # For static linking, it is better to rely on `pkg-config` `.pc`
185 if [ "$dontDisableStatic" -eq "1" ]; then
190 moveToOutput bin/sdl2-config "$dev"
193 # SDL is weird in that instead of just dynamically linking with
194 # libraries when you `--enable-*` (or when `configure` finds) them
195 # it `dlopen`s them at runtime. In principle, this means it can
196 # ignore any missing optional dependencies like alsa, pulseaudio,
197 # some x11 libs, wayland, etc if they are missing on the system
198 # and/or work with wide array of versions of said libraries. In
199 # nixpkgs, however, we don't need any of that. Moreover, since we
200 # don't have a global ld-cache we have to stuff all the propagated
201 # libraries into rpath by hand or else some applications that use
202 # SDL API that requires said libraries will fail to start.
204 # You can grep SDL sources with `grep -rE 'SDL_(NAME|.*_SYM)'` to
205 # list the symbols used in this way.
208 rpath = lib.makeLibraryPath (
209 finalAttrs.dlopenPropagatedBuildInputs ++ finalAttrs.dlopenBuildInputs
212 lib.optionalString (stdenv.hostPlatform.extensions.sharedLibrary == ".so") ''
213 for lib in $out/lib/*.so* ; do
214 if ! [[ -L "$lib" ]]; then
215 patchelf --set-rpath "$(patchelf --print-rpath $lib):${rpath}" "$lib"
220 setupHook = ./setup-hook.sh;
223 inherit openglSupport;
224 updateScript = nix-update-script {
231 pkg-config = testers.hasPkgConfigModules { package = finalAttrs.finalPackage; };
242 inherit (python3Packages) pygame pygame-ce pygame-sdl2;
247 description = "Cross-platform multimedia library";
248 mainProgram = "sdl2-config";
249 homepage = "http://www.libsdl.org/";
250 changelog = "https://github.com/libsdl-org/SDL/releases/tag/release-${finalAttrs.version}";
251 license = licenses.zlib;
252 platforms = platforms.all;
253 maintainers = lib.teams.sdl.members;
254 pkgConfigModules = [ "sdl2" ];