1{ lib
2, stdenv
3, config
4, fetchFromGitHub
5, nix-update-script
6, pkg-config
7, libGLSupported ? lib.elem stdenv.hostPlatform.system lib.platforms.mesaPlatforms
8, openglSupport ? libGLSupported
9, libGL
10, alsaSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
11, alsa-lib
12, x11Support ? !stdenv.hostPlatform.isWindows && !stdenv.hostPlatform.isAndroid
13, libX11
14, xorgproto
15, libICE
16, libXi
17, libXScrnSaver
18, libXcursor
19, libXinerama
20, libXext
21, libXxf86vm
22, libXrandr
23, waylandSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
24, wayland
25, wayland-protocols
26, wayland-scanner
27, drmSupport ? false
28, libdrm
29, mesa
30, libxkbcommon
31, dbusSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
32, dbus
33, udevSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
34, udev
35, ibusSupport ? false
36, ibus
37, libdecorSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
38, libdecor
39, pipewireSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
40, pipewire # NOTE: must be built with SDL2 without pipewire support
41, pulseaudioSupport ? config.pulseaudio or stdenv.isLinux && !stdenv.hostPlatform.isAndroid
42, libpulseaudio
43, AudioUnit
44, Cocoa
45, CoreAudio
46, CoreServices
47, ForceFeedback
48, OpenGL
49, audiofile
50, libiconv
51, withStatic ? false
52# passthru.tests
53, testers
54}:
55
56# NOTE: When editing this expression see if the same change applies to
57# SDL expression too
58
59stdenv.mkDerivation (finalAttrs: {
60 pname = "SDL2";
61 version = "2.28.4";
62
63 src = fetchFromGitHub {
64 owner = "libsdl-org";
65 repo = "SDL";
66 rev = "release-${finalAttrs.version}";
67 hash = "sha256-1+1m0s3pBCTu924J/4aIu4IHk/N88x2djWDEsDpAJn4=";
68 };
69 dontDisableStatic = if withStatic then 1 else 0;
70 outputs = [ "out" "dev" ];
71 outputBin = "dev"; # sdl-config
72
73 patches = [
74 # `sdl2-config --cflags` from Nixpkgs returns include path to just SDL2.
75 # On a normal distro this is enough for includes from all SDL2* packages to work,
76 # but on NixOS they're spread across different paths.
77 # This patch + the setup-hook will ensure that `sdl2-config --cflags` works correctly.
78 ./find-headers.patch
79 ];
80
81 postPatch = ''
82 # Fix running wayland-scanner for the build platform when cross-compiling.
83 # See comment here: https://github.com/libsdl-org/SDL/issues/4860#issuecomment-1119003545
84 substituteInPlace configure \
85 --replace '$(WAYLAND_SCANNER)' 'wayland-scanner'
86 '';
87
88 strictDeps = true;
89
90 depsBuildBuild = [ pkg-config ];
91
92 nativeBuildInputs = [ pkg-config ] ++ lib.optionals waylandSupport [ wayland wayland-scanner ];
93
94 dlopenPropagatedBuildInputs = [ ]
95 # Propagated for #include <GLES/gl.h> in SDL_opengles.h.
96 ++ lib.optional (openglSupport && !stdenv.isDarwin) libGL
97 # Propagated for #include <X11/Xlib.h> and <X11/Xatom.h> in SDL_syswm.h.
98 ++ lib.optionals x11Support [ libX11 ];
99
100 propagatedBuildInputs = lib.optionals x11Support [ xorgproto ]
101 ++ finalAttrs.dlopenPropagatedBuildInputs;
102
103 dlopenBuildInputs = lib.optionals alsaSupport [ alsa-lib audiofile ]
104 ++ lib.optional dbusSupport dbus
105 ++ lib.optional libdecorSupport libdecor
106 ++ lib.optional pipewireSupport pipewire
107 ++ lib.optional pulseaudioSupport libpulseaudio
108 ++ lib.optional udevSupport udev
109 ++ lib.optionals waylandSupport [ wayland libxkbcommon ]
110 ++ lib.optionals x11Support [ libICE libXi libXScrnSaver libXcursor libXinerama libXext libXrandr libXxf86vm ]
111 ++ lib.optionals drmSupport [ libdrm mesa ];
112
113 buildInputs = [ libiconv ]
114 ++ finalAttrs.dlopenBuildInputs
115 ++ lib.optional ibusSupport ibus
116 ++ lib.optionals waylandSupport [ wayland-protocols ]
117 ++ lib.optionals stdenv.isDarwin [ AudioUnit Cocoa CoreAudio CoreServices ForceFeedback OpenGL ];
118
119 enableParallelBuilding = true;
120
121 configureFlags = [
122 "--disable-oss"
123 ] ++ lib.optional (!x11Support) "--without-x"
124 ++ lib.optional alsaSupport "--with-alsa-prefix=${alsa-lib.out}/lib"
125 ++ lib.optional stdenv.hostPlatform.isWindows "--disable-video-opengles"
126 ++ lib.optional stdenv.isDarwin "--disable-sdltest";
127
128 # We remove libtool .la files when static libs are requested,
129 # because they make the builds of downstream libs like `SDL_tff`
130 # fail with `cannot find -lXext, `-lXcursor` etc. linker errors
131 # because the `.la` files are not pruned if static libs exist
132 # (see https://github.com/NixOS/nixpkgs/commit/fd97db43bcb05e37f6bb77f363f1e1e239d9de53)
133 # and they also don't carry the necessary `-L` paths of their
134 # X11 dependencies.
135 # For static linking, it is better to rely on `pkg-config` `.pc`
136 # files.
137 postInstall = ''
138 if [ "$dontDisableStatic" -eq "1" ]; then
139 rm $out/lib/*.la
140 else
141 rm $out/lib/*.a
142 fi
143 moveToOutput bin/sdl2-config "$dev"
144 '';
145
146 # SDL is weird in that instead of just dynamically linking with
147 # libraries when you `--enable-*` (or when `configure` finds) them
148 # it `dlopen`s them at runtime. In principle, this means it can
149 # ignore any missing optional dependencies like alsa, pulseaudio,
150 # some x11 libs, wayland, etc if they are missing on the system
151 # and/or work with wide array of versions of said libraries. In
152 # nixpkgs, however, we don't need any of that. Moreover, since we
153 # don't have a global ld-cache we have to stuff all the propagated
154 # libraries into rpath by hand or else some applications that use
155 # SDL API that requires said libraries will fail to start.
156 #
157 # You can grep SDL sources with `grep -rE 'SDL_(NAME|.*_SYM)'` to
158 # list the symbols used in this way.
159 postFixup =
160 let
161 rpath = lib.makeLibraryPath (finalAttrs.dlopenPropagatedBuildInputs ++ finalAttrs.dlopenBuildInputs);
162 in
163 lib.optionalString (stdenv.hostPlatform.extensions.sharedLibrary == ".so") ''
164 for lib in $out/lib/*.so* ; do
165 if ! [[ -L "$lib" ]]; then
166 patchelf --set-rpath "$(patchelf --print-rpath $lib):${rpath}" "$lib"
167 fi
168 done
169 '';
170
171 setupHook = ./setup-hook.sh;
172
173 passthru = {
174 inherit openglSupport;
175 updateScript = nix-update-script { extraArgs = ["--version-regex" "release-(.*)"]; };
176 tests.pkg-config = testers.hasPkgConfigModules {
177 package = finalAttrs.finalPackage;
178 };
179 };
180
181 meta = with lib; {
182 description = "A cross-platform multimedia library";
183 homepage = "http://www.libsdl.org/";
184 changelog = "https://github.com/libsdl-org/SDL/releases/tag/release-${version}";
185 license = licenses.zlib;
186 platforms = platforms.all;
187 maintainers = with maintainers; [ cpages ];
188 pkgConfigModules = [ "sdl2" ];
189 };
190})