1 { stdenv, lib, fetchurl
2 , meson, pkg-config, ninja
3 , intltool, bison, flex, file, python3Packages, wayland-scanner
4 , expat, libdrm, xorg, wayland, wayland-protocols, openssl
5 , llvmPackages, libffi, libomxil-bellagio, libva-minimal
8 , vulkan-loader, glslang
9 , galliumDrivers ? ["auto"]
10 # upstream Mesa defaults to only enabling swrast (aka lavapipe) on aarch64 for some reason, so force building the others
11 , vulkanDrivers ? [ "auto" ] ++ lib.optionals (stdenv.isLinux && stdenv.isAarch64) [ "broadcom" "freedreno" "panfrost" ]
12 , eglPlatforms ? [ "x11" ] ++ lib.optionals stdenv.isLinux [ "wayland" ]
13 , vulkanLayers ? lib.optionals (!stdenv.isDarwin) [ "device-select" "overlay" ] # No Vulkan support on Darwin
15 , withValgrind ? lib.meta.availableOn stdenv.hostPlatform valgrind-light && !valgrind-light.meta.broken, valgrind-light
16 , enableGalliumNine ? stdenv.isLinux
17 , enableOSMesa ? stdenv.isLinux
18 , enableOpenCL ? stdenv.isLinux && stdenv.isx86_64
19 , enablePatentEncumberedCodecs ? true
25 - The basic mesa ($out) contains headers and libraries (GLU is in libGLU now).
26 This or the mesa attribute (which also contains GLU) are small (~ 2 MB, mostly headers)
27 and are designed to be the buildInput of other packages.
28 - DRI drivers are compiled into $drivers output, which is much bigger and
29 depends on LLVM. These should be searched at runtime in
30 "/run/opengl-driver{,-32}/lib/*" and so are kind-of impure (given by NixOS).
31 (I suppose on non-NixOS one would create the appropriate symlinks from there.)
32 - libOSMesa is in $osmesa (~4 MB)
38 # Release calendar: https://www.mesa3d.org/release-calendar.html
39 # Release frequency: https://www.mesa3d.org/releasing.html#schedule
41 branch = versions.major version;
43 self = stdenv.mkDerivation {
49 "https://archive.mesa3d.org/mesa-${version}.tar.xz"
50 "https://mesa.freedesktop.org/archive/mesa-${version}.tar.xz"
51 "ftp://ftp.freedesktop.org/pub/mesa/mesa-${version}.tar.xz"
52 "ftp://ftp.freedesktop.org/pub/mesa/${version}/mesa-${version}.tar.xz"
53 "ftp://ftp.freedesktop.org/pub/mesa/older-versions/${branch}.x/${version}/mesa-${version}.tar.xz"
55 sha256 = "ee7d026f7b1991dbae0861d359b671145c3a86f2a731353b885d2ea2d5c098d6";
59 # revive ./dricore-gallium.patch when it gets ported (from Ubuntu), as it saved
60 # ~35 MB in $drivers; watch https://launchpad.net/ubuntu/+source/mesa/+changelog
62 # fixes pkgsMusl.mesa build
66 ./disk_cache-include-dri-driver-path-in-cache-key.patch
72 # The drirc.d directory cannot be installed to $drivers as that would cause a cyclic dependency:
73 substituteInPlace src/util/xmlconfig.c --replace \
74 'DATADIR "/drirc.d"' '"${placeholder "out"}/share/drirc.d"'
75 substituteInPlace src/util/meson.build --replace \
76 "get_option('datadir')" "'${placeholder "out"}/share'"
77 substituteInPlace src/amd/vulkan/meson.build --replace \
78 "get_option('datadir')" "'${placeholder "out"}/share'"
81 outputs = [ "out" "dev" "drivers" ]
82 ++ lib.optional enableOSMesa "osmesa"
83 ++ lib.optional stdenv.isLinux "driversdev"
84 ++ lib.optional enableOpenCL "opencl";
87 PATH=${llvmPackages.libllvm.dev}/bin:$PATH
90 # TODO: Figure out how to enable opencl without having a runtime dependency on clang
93 "--datadir=${placeholder "drivers"}/share" # Vendor files
95 # Don't build in debug mode
96 # https://gitlab.freedesktop.org/mesa/mesa/blob/master/docs/meson.html#L327
99 "-Ddisk-cache-key=${placeholder "drivers"}"
100 "-Ddri-search-path=${libglvnd.driverLink}/lib/dri"
102 "-Dplatforms=${concatStringsSep "," eglPlatforms}"
103 "-Dgallium-drivers=${concatStringsSep "," galliumDrivers}"
104 "-Dvulkan-drivers=${concatStringsSep "," vulkanDrivers}"
106 "-Ddri-drivers-path=${placeholder "drivers"}/lib/dri"
107 "-Dvdpau-libs-path=${placeholder "drivers"}/lib/vdpau"
108 "-Dxvmc-libs-path=${placeholder "drivers"}/lib"
109 "-Domx-libs-path=${placeholder "drivers"}/lib/bellagio"
110 "-Dva-libs-path=${placeholder "drivers"}/lib/dri"
111 "-Dd3d-drivers-path=${placeholder "drivers"}/lib/d3d"
112 "-Dgallium-nine=${boolToString enableGalliumNine}" # Direct3D in Wine
113 "-Dosmesa=${boolToString enableOSMesa}" # used by wine
114 "-Dmicrosoft-clc=disabled" # Only relevant on Windows (OpenCL 1.2 API on top of D3D12)
116 # To enable non-mesa gbm backends to be found (e.g. Nvidia)
117 "-Dgbm-backends-path=${libglvnd.driverLink}/lib/gbm:${placeholder "out"}/lib/gbm"
118 ] ++ optionals stdenv.isLinux [
120 ] ++ optionals enableOpenCL [
121 "-Dgallium-opencl=icd" # Enable the gallium OpenCL frontend
122 "-Dclang-libdir=${llvmPackages.clang-unwrapped.lib}/lib"
123 ] ++ optional enablePatentEncumberedCodecs
124 "-Dvideo-codecs=h264dec,h264enc,h265dec,h265enc,vc1dec"
125 ++ optional (vulkanLayers != []) "-D vulkan-layers=${builtins.concatStringsSep "," vulkanLayers}";
127 buildInputs = with xorg; [
128 expat llvmPackages.libllvm libglvnd xorgproto
129 libX11 libXext libxcb libXt libXfixes libxshmfence libXrandr
130 libffi libvdpau libelf libXvMC
131 libpthreadstubs openssl /*or another sha1 provider*/
132 ] ++ lib.optionals (elem "wayland" eglPlatforms) [ wayland wayland-protocols ]
133 ++ lib.optionals stdenv.isLinux [ libomxil-bellagio libva-minimal ]
134 ++ lib.optionals stdenv.isDarwin [ libunwind ]
135 ++ lib.optionals enableOpenCL [ libclc llvmPackages.clang llvmPackages.clang-unwrapped ]
136 ++ lib.optional withValgrind valgrind-light
137 # Mesa will not build zink when gallium-drivers=auto
138 ++ lib.optional (elem "zink" galliumDrivers) vulkan-loader;
140 depsBuildBuild = [ pkg-config ];
142 nativeBuildInputs = [
143 meson pkg-config ninja
144 intltool bison flex file
145 python3Packages.python python3Packages.Mako
147 ] ++ lib.optionals (elem "wayland" eglPlatforms) [
151 propagatedBuildInputs = with xorg; [
152 libXdamage libXxf86vm
153 ] ++ optional stdenv.isLinux libdrm
154 ++ optionals stdenv.isDarwin [ OpenGL Xplugin ];
159 # Some installs don't have any drivers so this directory is never created.
160 mkdir -p $drivers $osmesa
161 '' + optionalString stdenv.isLinux ''
162 mkdir -p $drivers/lib
164 if [ -n "$(shopt -s nullglob; echo "$out/lib/libxatracker"*)" -o -n "$(shopt -s nullglob; echo "$out/lib/libvulkan_"*)" ]; then
165 # move gallium-related stuff to $drivers, so $out doesn't depend on LLVM
167 $out/lib/libxatracker* \
171 if [ -n "$(shopt -s nullglob; echo "$out"/lib/lib*_mesa*)" ]; then
172 # Move other drivers to a separate output
173 mv -t $drivers/lib $out/lib/lib*_mesa*
176 # Update search path used by glvnd
177 for js in $drivers/share/glvnd/egl_vendor.d/*.json; do
178 substituteInPlace "$js" --replace '"libEGL_' '"'"$drivers/lib/libEGL_"
181 # Update search path used by Vulkan (it's pointing to $out but
182 # drivers are in $drivers)
183 for js in $drivers/share/vulkan/icd.d/*.json; do
184 substituteInPlace "$js" --replace "$out" "$drivers"
186 '' + optionalString enableOpenCL ''
189 mv -t "$opencl/lib/" \
190 $out/lib/gallium-pipe \
191 $out/lib/libMesaOpenCL*
193 # We construct our own .icd file that contains an absolute path.
194 rm -r $out/etc/OpenCL
195 mkdir -p $opencl/etc/OpenCL/vendors/
196 echo $opencl/lib/libMesaOpenCL.so > $opencl/etc/OpenCL/vendors/mesa.icd
197 '' + lib.optionalString enableOSMesa ''
198 # move libOSMesa to $osmesa, as it's relatively big
200 mv -t $osmesa/lib/ $out/lib/libOSMesa*
201 '' + lib.optionalString (vulkanLayers != []) ''
202 mv -t $drivers/lib $out/lib/libVkLayer*
203 for js in $drivers/share/vulkan/{im,ex}plicit_layer.d/*.json; do
204 substituteInPlace "$js" --replace '"libVkLayer_' '"'"$drivers/lib/libVkLayer_"
208 postFixup = optionalString stdenv.isLinux ''
209 # set the default search path for DRI drivers; used e.g. by X server
210 substituteInPlace "$dev/lib/pkgconfig/dri.pc" --replace "$drivers" "${libglvnd.driverLink}"
211 [ -f "$dev/lib/pkgconfig/d3d.pc" ] && substituteInPlace "$dev/lib/pkgconfig/d3d.pc" --replace "$drivers" "${libglvnd.driverLink}"
213 # remove pkgconfig files for GL/EGL; they are provided by libGL.
214 rm -f $dev/lib/pkgconfig/{gl,egl}.pc
216 # Move development files for libraries in $drivers to $driversdev
217 mkdir -p $driversdev/include
218 mv $dev/include/xa_* $dev/include/d3d* -t $driversdev/include || true
219 mkdir -p $driversdev/lib/pkgconfig
220 for pc in lib/pkgconfig/{xatracker,d3d}.pc; do
221 if [ -f "$dev/$pc" ]; then
222 substituteInPlace "$dev/$pc" --replace $out $drivers
223 mv $dev/$pc $driversdev/$pc
227 # NAR doesn't support hard links, so convert them to symlinks to save space.
228 jdupes --hard-links --link-soft --recurse "$drivers"
230 # add RPATH so the drivers can find the moved libgallium and libdricore9
231 # moved here to avoid problems with stripping patchelfed files
232 for lib in $drivers/lib/*.so* $drivers/lib/*/*.so*; do
233 if [[ ! -L "$lib" ]]; then
234 patchelf --set-rpath "$(patchelf --print-rpath $lib):$drivers/lib" "$lib"
239 NIX_CFLAGS_COMPILE = optionals stdenv.isDarwin [ "-fno-common" ] ++ lib.optionals enableOpenCL [
241 "-DPIPE_SEARCH_DIR=\"${placeholder "opencl"}/lib/gallium-pipe\""
246 inherit (libglvnd) driverLink;
247 inherit llvmPackages;
249 tests = lib.optionalAttrs stdenv.isLinux {
250 devDoesNotDependOnLLVM = stdenv.mkDerivation {
251 name = "mesa-dev-does-not-depend-on-llvm";
253 echo ${self.dev} >>$out
255 disallowedRequisites = [ llvmPackages.llvm self.drivers ];
261 description = "An open source 3D graphics library";
263 The Mesa project began as an open-source implementation of the OpenGL
264 specification - a system for rendering interactive 3D graphics. Over the
265 years the project has grown to implement more graphics APIs, including
266 OpenGL ES (versions 1, 2, 3), OpenCL, OpenMAX, VDPAU, VA API, XvMC, and
267 Vulkan. A variety of device drivers allows the Mesa libraries to be used
268 in many different environments ranging from software emulation to
269 complete hardware acceleration for modern GPUs.
271 homepage = "https://www.mesa3d.org/";
272 changelog = "https://www.mesa3d.org/relnotes/${version}.html";
273 license = licenses.mit; # X11 variant, in most files
274 platforms = platforms.mesaPlatforms;
275 maintainers = with maintainers; [ primeos vcunat ]; # Help is welcome :)