nixlib

Alyssa's collection of Nix expressions
git clone git clone https://edef.eu/~qyliss/nixlib.git
Log | Files | Refs | README | LICENSE

default.nix (7429B)


      1 { stdenv, lib, fetchurl, fetchpatch
      2 , pkgconfig, intltool, ninja, meson
      3 , file, flex, bison, expat, libdrm, xorg, wayland, wayland-protocols, openssl
      4 , llvmPackages, libffi, libomxil-bellagio, libva-minimal
      5 , libelf, libvdpau, python3Packages
      6 , libglvnd
      7 , enableRadv ? true
      8 , galliumDrivers ? ["auto"]
      9 , driDrivers ? ["auto"]
     10 , vulkanDrivers ? ["auto"]
     11 , eglPlatforms ? [ "x11" "surfaceless" ] ++ lib.optionals stdenv.isLinux [ "wayland" "drm" ]
     12 , OpenGL, Xplugin
     13 , withValgrind ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAarch32, valgrind-light
     14 }:
     15 
     16 /** Packaging design:
     17   - The basic mesa ($out) contains headers and libraries (GLU is in libGLU now).
     18     This or the mesa attribute (which also contains GLU) are small (~ 2 MB, mostly headers)
     19     and are designed to be the buildInput of other packages.
     20   - DRI drivers are compiled into $drivers output, which is much bigger and
     21     depends on LLVM. These should be searched at runtime in
     22     "/run/opengl-driver{,-32}/lib/*" and so are kind-of impure (given by NixOS).
     23     (I suppose on non-NixOS one would create the appropriate symlinks from there.)
     24   - libOSMesa is in $osmesa (~4 MB)
     25 */
     26 
     27 with stdenv.lib;
     28 
     29 let
     30   version = "19.1.5";
     31   branch  = head (splitString "." version);
     32 in
     33 
     34 stdenv.mkDerivation {
     35   pname = "mesa";
     36   inherit version;
     37 
     38   src =  fetchurl {
     39     urls = [
     40       "ftp://ftp.freedesktop.org/pub/mesa/mesa-${version}.tar.xz"
     41       "ftp://ftp.freedesktop.org/pub/mesa/${version}/mesa-${version}.tar.xz"
     42       "ftp://ftp.freedesktop.org/pub/mesa/older-versions/${branch}.x/${version}/mesa-${version}.tar.xz"
     43       "https://mesa.freedesktop.org/archive/mesa-${version}.tar.xz"
     44     ];
     45     sha256 = "1d3frncljickn5yi2ch1w2phwxhxpi6diyac3cbin9f76m7f2m3v";
     46   };
     47 
     48   prePatch = "patchShebangs .";
     49 
     50   # TODO:
     51   #  revive ./dricore-gallium.patch when it gets ported (from Ubuntu), as it saved
     52   #  ~35 MB in $drivers; watch https://launchpad.net/ubuntu/+source/mesa/+changelog
     53   patches = [
     54     ./missing-includes.patch # dev_t needs sys/stat.h, time_t needs time.h, etc.-- fixes build w/musl
     55     ./opencl-install-dir.patch
     56     ./disk_cache-include-dri-driver-path-in-cache-key.patch
     57   ] # do not prefix user provided dri-drivers-path
     58     ++ lib.optional (lib.versionOlder version "19.0.0") (fetchpatch {
     59       url = "https://gitlab.freedesktop.org/mesa/mesa/commit/f6556ec7d126b31da37c08d7cb657250505e01a0.patch";
     60       sha256 = "0z6phi8hbrbb32kkp1js7ggzviq7faz1ria36wi4jbc4in2392d9";
     61     })
     62     ++ lib.optionals (lib.versionOlder version "19.1.0") [
     63       # do not prefix user provided d3d-drivers-path
     64       (fetchpatch {
     65         url = "https://gitlab.freedesktop.org/mesa/mesa/commit/dcc48664197c7e44684ccfb970a4ae083974d145.patch";
     66         sha256 = "1nhs0xpx3hiy8zfb5gx1zd7j7xha6h0hr7yingm93130a5902lkb";
     67       })
     68 
     69       # don't build libGLES*.so with GLVND
     70       (fetchpatch {
     71         url = "https://gitlab.freedesktop.org/mesa/mesa/commit/b01524fff05eef66e8cd24f1c5aacefed4209f03.patch";
     72         sha256 = "1pszr6acx2xw469zq89n156p3bf3xf84qpbjw5fr1sj642lbyh7c";
     73       })
     74     ];
     75 
     76   outputs = [ "out" "dev" "drivers" "osmesa" ];
     77 
     78   # TODO: Figure out how to enable opencl without having a runtime dependency on clang
     79   mesonFlags = [
     80     "--sysconfdir=/etc"
     81 
     82     # Don't build in debug mode
     83     # https://gitlab.freedesktop.org/mesa/mesa/blob/master/docs/meson.html#L327
     84     "-Db_ndebug=true"
     85 
     86     "-Ddisk-cache-key=${placeholder "drivers"}"
     87     "-Ddri-search-path=${libglvnd.driverLink}/lib/dri"
     88 
     89     "-Dplatforms=${concatStringsSep "," eglPlatforms}"
     90     "-Ddri-drivers=${concatStringsSep "," driDrivers}"
     91     "-Dgallium-drivers=${concatStringsSep "," galliumDrivers}"
     92     "-Dvulkan-drivers=${concatStringsSep "," vulkanDrivers}"
     93 
     94     "-Ddri-drivers-path=${placeholder "drivers"}/lib/dri"
     95     "-Dvdpau-libs-path=${placeholder "drivers"}/lib/vdpau"
     96     "-Dxvmc-libs-path=${placeholder "drivers"}/lib"
     97     "-Domx-libs-path=${placeholder "drivers"}/lib/bellagio"
     98     "-Dva-libs-path=${placeholder "drivers"}/lib/dri"
     99     "-Dd3d-drivers-path=${placeholder "drivers"}/lib/d3d"
    100   ] ++ optionals stdenv.isLinux [
    101     "-Dglvnd=true"
    102     "-Dosmesa=gallium" # used by wine
    103     "-Dgallium-nine=true" # Direct3D in Wine
    104   ];
    105 
    106   buildInputs = with xorg; [
    107     expat llvmPackages.llvm libglvnd xorgproto
    108     libX11 libXext libxcb libXt libXfixes libxshmfence libXrandr
    109     libffi libvdpau libelf libXvMC
    110     libpthreadstubs openssl /*or another sha1 provider*/
    111   ] ++ lib.optionals (elem "wayland" eglPlatforms) [ wayland wayland-protocols ]
    112     ++ lib.optionals stdenv.isLinux [ libomxil-bellagio libva-minimal ]
    113     ++ lib.optional withValgrind valgrind-light;
    114 
    115   nativeBuildInputs = [
    116     pkgconfig meson ninja
    117     intltool bison flex file
    118     python3Packages.python python3Packages.Mako
    119   ];
    120 
    121   propagatedBuildInputs = with xorg; [
    122     libXdamage libXxf86vm
    123   ] ++ optional stdenv.isLinux libdrm
    124     ++ optionals stdenv.isDarwin [ OpenGL Xplugin ];
    125 
    126   enableParallelBuilding = true;
    127   doCheck = false;
    128 
    129   postInstall = ''
    130     # Some installs don't have any drivers so this directory is never created.
    131     mkdir -p $drivers $osmesa
    132   '' + optionalString stdenv.isLinux ''
    133     mkdir -p $drivers/lib
    134 
    135     # move gallium-related stuff to $drivers, so $out doesn't depend on LLVM
    136     mv -t $drivers/lib       \
    137       $out/lib/libxatracker* \
    138       $out/lib/libvulkan_*
    139 
    140     # Move other drivers to a separate output
    141     mv $out/lib/lib*_mesa* $drivers/lib
    142 
    143     # move libOSMesa to $osmesa, as it's relatively big
    144     mkdir -p $osmesa/lib
    145     mv -t $osmesa/lib/ $out/lib/libOSMesa*
    146 
    147     # move vendor files
    148     mv $out/share/ $drivers/
    149 
    150     # Update search path used by glvnd
    151     for js in $drivers/share/glvnd/egl_vendor.d/*.json; do
    152       substituteInPlace "$js" --replace '"libEGL_' '"'"$drivers/lib/libEGL_"
    153     done
    154 
    155     # Update search path used by Vulkan (it's pointing to $out but
    156     # drivers are in $drivers)
    157     for js in $drivers/share/vulkan/icd.d/*.json; do
    158       substituteInPlace "$js" --replace "$out" "$drivers"
    159     done
    160   '';
    161 
    162   # TODO:
    163   #  check $out doesn't depend on llvm: builder failures are ignored
    164   #  for some reason grep -qv '${llvmPackages.llvm}' -R "$out";
    165   postFixup = optionalString stdenv.isLinux ''
    166     # set the default search path for DRI drivers; used e.g. by X server
    167     substituteInPlace "$dev/lib/pkgconfig/dri.pc" --replace "$drivers" "${libglvnd.driverLink}"
    168 
    169     # remove pkgconfig files for GL/EGL; they are provided by libGL.
    170     rm $dev/lib/pkgconfig/{gl,egl}.pc
    171 
    172     # Update search path used by pkg-config
    173     for pc in $dev/lib/pkgconfig/{d3d,dri,xatracker}.pc; do
    174       substituteInPlace "$pc" --replace $out $drivers
    175     done
    176 
    177     # add RPATH so the drivers can find the moved libgallium and libdricore9
    178     # moved here to avoid problems with stripping patchelfed files
    179     for lib in $drivers/lib/*.so* $drivers/lib/*/*.so*; do
    180       if [[ ! -L "$lib" ]]; then
    181         patchelf --set-rpath "$(patchelf --print-rpath $lib):$drivers/lib" "$lib"
    182       fi
    183     done
    184   '';
    185 
    186   NIX_CFLAGS_COMPILE = stdenv.lib.optionalString stdenv.isDarwin "-fno-common";
    187 
    188   passthru = {
    189     inherit libdrm;
    190     inherit (libglvnd) driverLink;
    191   };
    192 
    193   meta = with stdenv.lib; {
    194     description = "An open source implementation of OpenGL";
    195     homepage = https://www.mesa3d.org/;
    196     license = licenses.mit; # X11 variant, in most files
    197     platforms = platforms.mesaPlatforms;
    198     maintainers = with maintainers; [ vcunat ];
    199   };
    200 }