nixlib

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

default.nix (14804B)


      1 { stdenv, fetchurl, fetchpatch, lib
      2 , pkgconfig, autoreconfHook, python2, python2Packages
      3 , expat, zlib
      4 , OpenGL, Xplugin
      5 
      6 , enableRadv ? stdenv.isLinux
      7 
      8 , galliumDrivers ? null
      9 , driDrivers ? null
     10 , vulkanDrivers ? null
     11 , eglPlatforms ? [ "x11" ] ++ lib.optionals stdenv.isLinux [ "wayland" "drm" ]
     12 , xorg, wayland, wayland-protocols
     13 
     14 , xvmcSupport ? true
     15 , vdpauSupport ? true, libvdpau
     16 , omxBellagioSupport ? stdenv.isLinux, libomxil-bellagio
     17 , vaSupport ? stdenv.isLinux, libva-minimal
     18 , libdrmSupport ? stdenv.isLinux, libdrm
     19 , libelfSupport ? stdenv.isLinux, libelf
     20 , llvmSupport ? true, llvmPackages
     21 , libglvndSupport ? true, libglvnd
     22 , withValgrind ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAarch32, valgrind-light
     23 }:
     24 
     25 with lib;
     26 
     27 assert elem "drm" eglPlatforms -> libdrmSupport;
     28 assert elem "wayland" eglPlatforms -> libdrmSupport;
     29 assert xvmcSupport -> elem "x11" eglPlatforms;
     30 assert vdpauSupport -> elem "x11" eglPlatforms;
     31 assert enableRadv -> libdrmSupport && llvmSupport && libelfSupport;
     32 
     33 /** Packaging design:
     34   - The basic mesa ($out) contains headers and libraries (GLU is in libGLU now).
     35     This or the mesa attribute (which also contains GLU) are small (~ 2 MB, mostly headers)
     36     and are designed to be the buildInput of other packages.
     37   - DRI drivers are compiled into $drivers output, which is much bigger and
     38     depends on LLVM. These should be searched at runtime in
     39     "/run/opengl-driver{,-32}/lib/*" and so are kind-of impure (given by NixOS).
     40     (I suppose on non-NixOS one would create the appropriate symlinks from there.)
     41   - libOSMesa is in $osmesa (~4 MB)
     42 */
     43 
     44 let
     45   # platforms that have PCIe slots and thus can use most non-integrated GPUs
     46   pciePlatform = !stdenv.hostPlatform.isAarch32 && !stdenv.hostPlatform.isAarch64;
     47   defaultGalliumDrivers = optionals (elem "drm" eglPlatforms) ([ "virgl" ]
     48     ++ optionals (pciePlatform || stdenv.hostPlatform.isAarch32 || stdenv.hostPlatform.isAarch64) [ "nouveau" ]
     49     ++ optionals (stdenv.hostPlatform.isAarch32 || stdenv.hostPlatform.isAarch64) [ "vc4" ]
     50     ++ optionals stdenv.hostPlatform.isAarch64 [ "freedreno" "etnaviv" "imx" ]
     51     ++ optionals stdenv.hostPlatform.isx86 [ "svga" "i915" ]
     52     ++ optional (pciePlatform && llvmSupport) "r300"
     53     ++ optionals (pciePlatform && llvmSupport && libelfSupport) [ "r600" "radeonsi" ]);
     54 
     55   defaultDriDrivers = optionals (elem "drm" eglPlatforms) ([ ]
     56     ++ optionals pciePlatform [ "radeon" "r200" ]
     57     ++ optionals (pciePlatform || stdenv.hostPlatform.isAarch32 || stdenv.hostPlatform.isAarch64) [ "nouveau" ]
     58     ++ optionals stdenv.hostPlatform.isx86 [ "i915" "i965" ]);
     59 
     60   defaultVulkanDrivers = optionals stdenv.hostPlatform.isLinux ([ ]
     61     ++ optional (libdrmSupport && stdenv.hostPlatform.isx86) "intel"
     62     ++ optional enableRadv "radeon");
     63 
     64   gallium_ = galliumDrivers; dri_ = driDrivers; vulkan_ = vulkanDrivers;
     65 in
     66 
     67 let
     68   galliumDrivers =
     69     (if gallium_ == null
     70           then defaultGalliumDrivers
     71           else gallium_)
     72     ++ optional stdenv.isLinux "swrast";
     73   driDrivers =
     74     (if dri_ == null
     75       then optionals (elem "drm" eglPlatforms) defaultDriDrivers
     76       else dri_) ++ optional stdenv.isLinux "swrast";
     77   vulkanDrivers =
     78     if vulkan_ == null
     79     then defaultVulkanDrivers
     80     else vulkan_;
     81 
     82   version = "18.3.4";
     83   branch  = head (splitString "." version);
     84 
     85   self =
     86     assert xvmcSupport ->
     87       elem "r600" galliumDrivers || elem "nouveau" galliumDrivers;
     88     assert omxBellagioSupport ->
     89       intersectLists galliumDrivers [ "r600" "radeonsi" "nouveau" ] != [];
     90     assert vaSupport ->
     91       intersectLists galliumDrivers [ "r600" "radeonsi" "nouveau" ] != [];
     92     assert vdpauSupport ->
     93       intersectLists galliumDrivers [ "r300" "r600" "radeonsi" "nouveau" ] != [];
     94 
     95     stdenv.mkDerivation {
     96       name = "mesa-noglu-${version}";
     97 
     98       src =  fetchurl {
     99         urls = [
    100           "ftp://ftp.freedesktop.org/pub/mesa/mesa-${version}.tar.xz"
    101           "ftp://ftp.freedesktop.org/pub/mesa/${version}/mesa-${version}.tar.xz"
    102           "ftp://ftp.freedesktop.org/pub/mesa/older-versions/${branch}.x/${version}/mesa-${version}.tar.xz"
    103           "https://mesa.freedesktop.org/archive/mesa-${version}.tar.xz"
    104         ];
    105         sha256 = "01xv03ah4l5lcfx015n3fg1620dh4nbbv6gmhh6zhdsx6sj4sc9j";
    106       };
    107 
    108       prePatch = "patchShebangs .";
    109 
    110       # TODO:
    111       #  revive ./dricore-gallium.patch when it gets ported (from Ubuntu), as it saved
    112       #  ~35 MB in $drivers; watch https://launchpad.net/ubuntu/+source/mesa/+changelog
    113       patches = [
    114         ./symlink-drivers.patch
    115         ./missing-includes.patch # dev_t needs sys/stat.h, time_t needs time.h, etc.-- fixes build w/musl
    116         ./disk_cache-include-dri-driver-path-in-cache-key.patch
    117 
    118         (fetchpatch {
    119           url = https://cgit.freedesktop.org/mesa/mesa/patch/?id=eb44c36cf1729e7e200b77cf8ea755dff72d1639;
    120           sha256 = "1izp38yja917241y7qslbkbmxv5ll9746ivgg2q5s64cwiydwrcc";
    121         })
    122         (fetchpatch {
    123           url = https://cgit.freedesktop.org/mesa/mesa/patch/?id=bcc4bfc8e80da5dc4c6ee44f791f2112dac208d1;
    124           sha256 = "0rijkx80anbb7g14cg9g11ha7lcf7sixc7xsjam6myyd68nl3n5n";
    125         })
    126       ];
    127 
    128       outputs = [ "out" "dev" "drivers" ]
    129                 ++ optional (elem "swrast" galliumDrivers) "osmesa";
    130 
    131       # TODO: Figure out how to enable opencl without having a runtime dependency on clang
    132       configureFlags = [
    133         "--sysconfdir=${libglvnd.driverLink}/etc"
    134         "--localstatedir=/var"
    135         "--with-dri-driverdir=$(drivers)/lib/dri"
    136         "--with-dri-searchpath=${libglvnd.driverLink}/lib/dri"
    137         "--with-va-libdir=$(drivers)/lib/dri"
    138         "--with-platforms=${concatStringsSep "," eglPlatforms}"
    139         "--with-gallium-drivers=${concatStringsSep "," galliumDrivers}"
    140         "--with-dri-drivers=${concatStringsSep "," driDrivers}"
    141         "--with-vulkan-drivers=${concatStringsSep "," vulkanDrivers}"
    142         "--with-d3d-libdir=$(drivers)/lib"
    143         "--with-xvmc-libdir=$(drivers)/lib"
    144         "--with-vdpau-libdir=$(drivers)/lib"
    145         "--with-omx-bellagio-libdir=$(drivers)/lib"
    146         "--enable-texture-float"
    147         "--enable-shared-glapi"
    148         "--enable-llvm-shared-libs"
    149 
    150         # XXX: I'm not sure whether x11 is actually required in theory for
    151         #      dri3, but attempting to build with dri3 and without x11
    152         #      results in a compile error. For future versions, check
    153         #      whether this is still the case and remove the check if not:
    154         #
    155         #      Last checked: v18.3.4
    156         (enableFeature (stdenv.isLinux && elem "x11" eglPlatforms && libdrmSupport) "dri3")
    157 
    158         # Direct3D in Wine
    159         (enableFeature (any (d: d != "swrast") galliumDrivers) "nine")
    160 
    161         (enableFeature libglvndSupport "libglvnd")
    162         "--enable-dri"
    163         "--enable-driglx-direct"
    164         "--enable-gles1"
    165         "--enable-gles2"
    166         (enableFeature (elem "x11" eglPlatforms && libdrmSupport) "glx")
    167 
    168         # https://bugs.freedesktop.org/show_bug.cgi?id=35268
    169         (enableFeature (!stdenv.hostPlatform.isMusl) "glx-tls")
    170 
    171         # used by wine
    172         (enableFeature (elem "swrast" galliumDrivers) "gallium-osmesa")
    173 
    174         (enableFeature llvmSupport "llvm")
    175         (enableFeature stdenv.isLinux "egl")
    176 
    177         # used in vmware driver
    178         (enableFeature (any (d: d != "swrast") galliumDrivers) "xa")
    179 
    180         (enableFeature libdrmSupport "gbm")
    181         (enableFeature xvmcSupport "xvmc")
    182         (enableFeature vdpauSupport "vdpau")
    183         (enableFeature omxBellagioSupport "omx-bellagio")
    184         (enableFeature vaSupport "va")
    185         "--disable-opencl"
    186       ];
    187 
    188       nativeBuildInputs = [ autoreconfHook pkgconfig python2 ]
    189         ++ optionals enableRadv [ python2Packages.Mako ];
    190 
    191       propagatedBuildInputs =
    192         optionals (elem "x11" eglPlatforms) (with xorg; [ libXdamage libXxf86vm ])
    193         ++ optional libdrmSupport libdrm
    194         ++ optionals stdenv.isDarwin [ OpenGL Xplugin ];
    195 
    196       buildInputs = [ expat zlib ]
    197         ++ optional libglvndSupport libglvnd
    198         ++ optional llvmSupport llvmPackages.llvm
    199         ++ optional libelfSupport libelf
    200         ++ optionals (elem "wayland" eglPlatforms) [ wayland wayland-protocols ]
    201         ++ optional xvmcSupport xorg.libXvMC
    202         ++ optional vdpauSupport libvdpau
    203         ++ optional vaSupport libva-minimal
    204         ++ optional omxBellagioSupport libomxil-bellagio
    205         ++ optional withValgrind valgrind-light
    206 
    207         ++ optionals (elem "x11" eglPlatforms)
    208           ((with xorg; [ xorgproto libX11 ]))
    209 
    210         ++ optionals (elem "x11" eglPlatforms && libdrmSupport)
    211           (with xorg; [ libXext libxcb libxshmfence ])
    212 
    213         ++ optional (elem "x11" eglPlatforms && elem "drm" eglPlatforms)
    214           xorg.libXrandr;
    215 
    216       enableParallelBuilding = true;
    217       doCheck = false;
    218 
    219       installFlags = [
    220         "sysconfdir=\${drivers}/etc"
    221         "localstatedir=\${TMPDIR}"
    222         "vendorjsondir=\${out}/share/glvnd/egl_vendor.d"
    223       ];
    224 
    225       # TODO: probably not all .la files are completely fixed, but it shouldn't matter;
    226       postInstall = ''
    227         # Some installs don't have any drivers so this directory is never created.
    228         mkdir -p $drivers
    229       '' + optionalString (galliumDrivers != []) ''
    230         # move libOSMesa to $osmesa, as it's relatively big
    231         mkdir -p {$osmesa,$drivers}/lib/
    232         mv -t $osmesa/lib/ $out/lib/libOSMesa*
    233 
    234         # now fix references in .la files
    235         sed "/^libdir=/s,$out,$osmesa," -i $osmesa/lib/libOSMesa*.la
    236 
    237         # set the default search path for DRI drivers; used e.g. by X server
    238         substituteInPlace "$dev/lib/pkgconfig/dri.pc" --replace '$(drivers)' "${libglvnd.driverLink}"
    239 
    240         # remove GLES libraries; they are provided by libglvnd
    241         rm $out/lib/lib{GLESv1_CM,GLESv2}.*
    242 
    243         # remove pkgconfig files for GL/GLES/EGL; they are provided by libGL.
    244         rm -f $dev/lib/pkgconfig/{gl,egl,glesv1_cm,glesv2}.pc
    245 
    246         # move vendor files
    247         mv $out/share/ $drivers/
    248 
    249         # Update search path used by glvnd
    250         for js in $drivers/share/glvnd/egl_vendor.d/*.json; do
    251           substituteInPlace "$js" --replace '"libEGL_' '"'"$drivers/lib/libEGL_"
    252         done
    253 
    254         # Update search path used by pkg-config
    255         substituteInPlace $dev/lib/pkgconfig/dri.pc --replace $out $drivers
    256       '' + optionalString (vulkanDrivers != []) ''
    257         # Update search path used by Vulkan (it's pointing to $out but
    258         # drivers are in $drivers)
    259         for js in $drivers/share/vulkan/icd.d/*.json; do
    260           substituteInPlace "$js" --replace "$out" "$drivers"
    261         done
    262       '' + optionalString (any (d: d != "swrast") galliumDrivers) ''
    263         # Update search path used by pkg-config
    264         for pc in $dev/lib/pkgconfig/{d3d,xatracker}.pc; do
    265           substituteInPlace "$pc" --replace $out $drivers
    266         done
    267         # move gallium-related stuff to $drivers, so $out doesn't depend on LLVM
    268         mv $out/lib/libxatracker* $drivers/lib
    269       '' + optionalString (vulkanDrivers != []) ''
    270         # move gallium-related stuff to $drivers, so $out doesn't depend on LLVM
    271         mv $out/lib/libvulkan_* $drivers/lib
    272       '' + optionalString libglvndSupport ''
    273         # Move other drivers to a separate output
    274         mv $out/lib/lib*_mesa* $drivers/lib
    275       '';
    276 
    277       # TODO:
    278       #  check $out doesn't depend on llvm: builder failures are ignored
    279       #  for some reason grep -qv '${llvmPackages.llvm}' -R "$out";
    280       postFixup = optionalString (galliumDrivers != []) ''
    281         # add RPATH so the drivers can find the moved libgallium and libdricore9
    282         # moved here to avoid problems with stripping patchelfed files
    283         for lib in $drivers/lib/*.so* $drivers/lib/*/*.so*; do
    284           if [[ ! -L "$lib" ]]; then
    285             patchelf --set-rpath "$(patchelf --print-rpath $lib):$drivers/lib" "$lib"
    286           fi
    287         done
    288       '';
    289 
    290       passthru = {
    291         inherit libdrm version;
    292         inherit (libglvnd) driverLink;
    293 
    294         # Use stub libraries from libglvnd and headers from Mesa.
    295         stubs = stdenv.mkDerivation {
    296           name = "libGL-${libglvnd.version}";
    297           outputs = [ "out" "dev" ];
    298 
    299           # On macOS, libglvnd is not supported, so we just use what mesa
    300           # build. We need to also include OpenGL.framework, and some
    301           # extra tricks to go along with. We add mesa’s libGLX to support
    302           # the X extensions to OpenGL.
    303           buildCommand = if stdenv.hostPlatform.isDarwin then ''
    304             mkdir -p $out/nix-support $dev
    305             echo ${OpenGL} >> $out/nix-support/propagated-build-inputs
    306             ln -s ${self.out}/lib $out/lib
    307 
    308             mkdir -p $dev/lib/pkgconfig $dev/nix-support
    309             echo "$out" > $dev/nix-support/propagated-build-inputs
    310             ln -s ${self.dev}/include $dev/include
    311 
    312             cat <<EOF >$dev/lib/pkgconfig/gl.pc
    313           Name: gl
    314           Description: gl library
    315           Version: ${self.version}
    316           Libs: -L${self.out}/lib -lGL
    317           Cflags: -I${self.dev}/include
    318           EOF
    319 
    320             cat <<EOF >$dev/lib/pkgconfig/glesv1_cm.pc
    321           Name: glesv1_cm
    322           Description: glesv1_cm library
    323           Version: ${self.version}
    324           Libs: -L${self.out}/lib -lGLESv1_CM
    325           Cflags: -I${self.dev}/include
    326           EOF
    327 
    328             cat <<EOF >$dev/lib/pkgconfig/glesv2.pc
    329           Name: glesv2
    330           Description: glesv2 library
    331           Version: ${self.version}
    332           Libs: -L${self.out}/lib -lGLESv2
    333           Cflags: -I${self.dev}/include
    334           EOF
    335           ''
    336 
    337           # Otherwise, setup gl stubs to use libglvnd.
    338           else ''
    339             mkdir -p $out/nix-support
    340             ln -s ${libglvnd.out}/lib $out/lib
    341 
    342             mkdir -p $dev/{,lib/pkgconfig,nix-support}
    343             echo "$out" > $dev/nix-support/propagated-build-inputs
    344             ln -s ${self.dev}/include $dev/include
    345 
    346             genPkgConfig() {
    347               local name="$1"
    348               local lib="$2"
    349 
    350               cat <<EOF >$dev/lib/pkgconfig/$name.pc
    351             Name: $name
    352             Description: $lib library
    353             Version: ${self.version}
    354             Libs: -L${libglvnd.out}/lib -l$lib
    355             Cflags: -I${self.dev}/include
    356             EOF
    357             }
    358 
    359             genPkgConfig gl GL
    360             genPkgConfig egl EGL
    361             genPkgConfig glesv1_cm GLESv1_CM
    362             genPkgConfig glesv2 GLESv2
    363           '';
    364         };
    365       };
    366 
    367       meta = {
    368         description = "An open source implementation of OpenGL";
    369         homepage = https://www.mesa3d.org/;
    370         license = licenses.mit; # X11 variant, in most files
    371         platforms = platforms.mesaPlatforms;
    372         maintainers = with maintainers; [ vcunat ];
    373       };
    374     };
    375 in self