From cd4167381321b305182b8d66a33640ca5461d047 Mon Sep 17 00:00:00 2001
From: Michael Weiss <dev.primeos@gmail.com>
Date: Sat, 6 Jun 2020 16:27:09 +0200
Subject: [PATCH] mesa: Move the DriConf defaults from $drivers to $out

The effective change is that "drirc.d/00-mesa-defaults.conf" will now be
installed to $out instead of $drivers and not appear under
"/run/opengl-driver/share/" anymore.

This is done to fix #89421, i.e. so that Mesa will find and load the
included DriConf defaults. The other files in "$drivers/share" will not
be moved to $out as some Nixpkgs logic might depend on them being
available under "/run/opengl-driver/share/" (e.g. addOpenGLRunpath).
---
 pkgs/development/libraries/mesa/default.nix | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/libraries/mesa/default.nix b/pkgs/development/libraries/mesa/default.nix
index e46f3fb1488..ac107981dcf 100644
--- a/pkgs/development/libraries/mesa/default.nix
+++ b/pkgs/development/libraries/mesa/default.nix
@@ -79,6 +79,12 @@ stdenv.mkDerivation {
     substituteInPlace meson.build --replace \
       "find_program('pkg-config')" \
       "find_program('${buildPackages.pkg-config.targetPrefix}pkg-config')"
+
+    # The drirc.d directory cannot be installed to $drivers as that would cause a cyclic dependency:
+    substituteInPlace src/util/xmlconfig.c --replace \
+      'DATADIR "/drirc.d"' '"${placeholder "out"}/drirc.d"'
+    substituteInPlace src/util/meson.build --replace \
+      "get_option('datadir')" "'${placeholder "out"}'"
   '';
 
   outputs = [ "out" "dev" "drivers" ] ++ lib.optional enableOSMesa "osmesa";
@@ -86,6 +92,7 @@ stdenv.mkDerivation {
   # TODO: Figure out how to enable opencl without having a runtime dependency on clang
   mesonFlags = [
     "--sysconfdir=/etc"
+    "--datadir=${placeholder "drivers"}/share" # Vendor files
 
     # Don't build in debug mode
     # https://gitlab.freedesktop.org/mesa/mesa/blob/master/docs/meson.html#L327
@@ -156,9 +163,6 @@ stdenv.mkDerivation {
       mv $out/lib/lib*_mesa* $drivers/lib
     fi
 
-    # move vendor files
-    mv $out/share/ $drivers/
-
     # Update search path used by glvnd
     for js in $drivers/share/glvnd/egl_vendor.d/*.json; do
       substituteInPlace "$js" --replace '"libEGL_' '"'"$drivers/lib/libEGL_"