6 , libGLSupported ? lib.elem stdenv.hostPlatform.system lib.platforms.mesaPlatforms
7 , openglSupport ? libGLSupported
9 , alsaSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
11 , x11Support ? !stdenv.targetPlatform.isWindows && !stdenv.hostPlatform.isAndroid
22 , waylandSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
26 , drmSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
30 , dbusSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
32 , udevSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
36 , fcitxSupport ? false
38 , libdecorSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
40 , pipewireSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
41 , pipewire # NOTE: must be built with SDL2 without pipewire support
42 , pulseaudioSupport ? config.pulseaudio or stdenv.isLinux && !stdenv.hostPlatform.isAndroid
55 # NOTE: When editing this expression see if the same change applies to
60 stdenv.mkDerivation rec {
65 url = "https://www.libsdl.org/release/${pname}-${version}.tar.gz";
66 sha256 = "sha256-s17wqAKwnZDtOt0NysDpWCCAQgKRT1u3sP63EPGhMp8=";
68 dontDisableStatic = if withStatic then 1 else 0;
69 outputs = [ "out" "dev" ];
70 outputBin = "dev"; # sdl-config
73 # `sdl2-config --cflags` from Nixpkgs returns include path to just SDL2.
74 # On a normal distro this is enough for includes from all SDL2* packages to work,
75 # but on NixOS they're spread across different paths.
76 # This patch + the setup-hook will ensure that `sdl2-config --cflags` works correctly.
81 # Fix running wayland-scanner for the build platform when cross-compiling.
82 # See comment here: https://github.com/libsdl-org/SDL/issues/4860#issuecomment-1119003545
83 substituteInPlace configure \
84 --replace '$(WAYLAND_SCANNER)' 'wayland-scanner'
89 depsBuildBuild = [ pkg-config ];
91 nativeBuildInputs = [ pkg-config ] ++ optionals waylandSupport [ wayland wayland-scanner ];
93 propagatedBuildInputs = dlopenPropagatedBuildInputs;
95 dlopenPropagatedBuildInputs = [ ]
96 # Propagated for #include <GLES/gl.h> in SDL_opengles.h.
97 ++ optional openglSupport libGL
98 # Propagated for #include <X11/Xlib.h> and <X11/Xatom.h> in SDL_syswm.h.
99 ++ optionals x11Support [ libX11 xorgproto ];
101 dlopenBuildInputs = optionals alsaSupport [ alsa-lib audiofile ]
102 ++ optional dbusSupport dbus
103 ++ optional libdecorSupport libdecor
104 ++ optional pipewireSupport pipewire
105 ++ optional pulseaudioSupport libpulseaudio
106 ++ optional udevSupport udev
107 ++ optionals waylandSupport [ wayland wayland-protocols libxkbcommon ]
108 ++ optionals x11Support [ libICE libXi libXScrnSaver libXcursor libXinerama libXext libXrandr libXxf86vm ]
109 ++ optionals drmSupport [ libdrm mesa ];
111 buildInputs = [ libiconv ]
113 ++ optional ibusSupport ibus
114 ++ optional fcitxSupport fcitx
115 ++ optionals stdenv.isDarwin [ AudioUnit Cocoa CoreAudio CoreServices ForceFeedback OpenGL ];
117 enableParallelBuilding = true;
121 ] ++ optional (!x11Support) "--without-x"
122 ++ optional alsaSupport "--with-alsa-prefix=${alsa-lib.out}/lib"
123 ++ optional stdenv.targetPlatform.isWindows "--disable-video-opengles"
124 ++ optional stdenv.isDarwin "--disable-sdltest";
126 # We remove libtool .la files when static libs are requested,
127 # because they make the builds of downstream libs like `SDL_tff`
128 # fail with `cannot find -lXext, `-lXcursor` etc. linker errors
129 # because the `.la` files are not pruned if static libs exist
130 # (see https://github.com/NixOS/nixpkgs/commit/fd97db43bcb05e37f6bb77f363f1e1e239d9de53)
131 # and they also don't carry the necessary `-L` paths of their
133 # For static linking, it is better to rely on `pkg-config` `.pc`
136 if [ "$dontDisableStatic" -eq "1" ]; then
141 moveToOutput bin/sdl2-config "$dev"
144 # SDL is weird in that instead of just dynamically linking with
145 # libraries when you `--enable-*` (or when `configure` finds) them
146 # it `dlopen`s them at runtime. In principle, this means it can
147 # ignore any missing optional dependencies like alsa, pulseaudio,
148 # some x11 libs, wayland, etc if they are missing on the system
149 # and/or work with wide array of versions of said libraries. In
150 # nixpkgs, however, we don't need any of that. Moreover, since we
151 # don't have a global ld-cache we have to stuff all the propagated
152 # libraries into rpath by hand or else some applications that use
153 # SDL API that requires said libraries will fail to start.
155 # You can grep SDL sources with `grep -rE 'SDL_(NAME|.*_SYM)'` to
156 # list the symbols used in this way.
159 rpath = makeLibraryPath (dlopenPropagatedBuildInputs ++ dlopenBuildInputs);
161 optionalString (stdenv.hostPlatform.extensions.sharedLibrary == ".so") ''
162 for lib in $out/lib/*.so* ; do
163 if ! [[ -L "$lib" ]]; then
164 patchelf --set-rpath "$(patchelf --print-rpath $lib):${rpath}" "$lib"
169 setupHook = ./setup-hook.sh;
171 passthru = { inherit openglSupport; };
174 description = "A cross-platform multimedia library";
175 homepage = "http://www.libsdl.org/";
176 license = licenses.zlib;
177 platforms = platforms.all;
178 maintainers = with maintainers; [ cpages ];