From 7c338b7df8e35fab4c08a494bff6018f8fb1be72 Mon Sep 17 00:00:00 2001
From: Thomas Tuegel <ttuegel@mailbox.org>
Date: Sat, 13 May 2017 09:16:13 -0500
Subject: [PATCH] qtbase: search platform plugin paths before other plugin
 paths

---
 .../libraries/qt-5/5.6/qtbase/default.nix     |  2 +
 .../5.6/qtbase/qpa-platform-plugin-path.patch | 43 +++++++++++++++++++
 .../libraries/qt-5/5.6/qtbase/series          |  1 +
 .../libraries/qt-5/5.8/qtbase/default.nix     |  2 +-
 .../qtbase/nix-profiles-library-paths.patch   |  4 +-
 .../5.8/qtbase/qpa-platform-plugin-path.patch | 43 +++++++++++++++++++
 .../qt-5/5.8/qtbase/qpa-plugin-path.patch     | 15 -------
 .../libraries/qt-5/5.8/qtbase/series          |  2 +-
 8 files changed, 92 insertions(+), 20 deletions(-)
 create mode 100644 pkgs/development/libraries/qt-5/5.6/qtbase/qpa-platform-plugin-path.patch
 create mode 100644 pkgs/development/libraries/qt-5/5.8/qtbase/qpa-platform-plugin-path.patch
 delete mode 100644 pkgs/development/libraries/qt-5/5.8/qtbase/qpa-plugin-path.patch

diff --git a/pkgs/development/libraries/qt-5/5.6/qtbase/default.nix b/pkgs/development/libraries/qt-5/5.6/qtbase/default.nix
index b26fc7faf9d7..289a1bbac6c7 100644
--- a/pkgs/development/libraries/qt-5/5.6/qtbase/default.nix
+++ b/pkgs/development/libraries/qt-5/5.6/qtbase/default.nix
@@ -104,6 +104,8 @@ stdenv.mkDerivation {
         -importdir $out/lib/qt5/imports \
         -qmldir $out/lib/qt5/qml \
         -docdir $out/share/doc/qt5"
+
+    NIX_CFLAGS_COMPILE+=" -DNIXPKGS_QPA_PLATFORM_PLUGIN_PATH=\"''${!outputLib}/lib/qt5/plugins/platforms\""
   '';
 
   prefixKey = "-prefix ";
diff --git a/pkgs/development/libraries/qt-5/5.6/qtbase/qpa-platform-plugin-path.patch b/pkgs/development/libraries/qt-5/5.6/qtbase/qpa-platform-plugin-path.patch
new file mode 100644
index 000000000000..32d9dcad622d
--- /dev/null
+++ b/pkgs/development/libraries/qt-5/5.6/qtbase/qpa-platform-plugin-path.patch
@@ -0,0 +1,43 @@
+Index: qtbase-opensource-src-5.6.2/src/gui/kernel/qplatformintegrationfactory.cpp
+===================================================================
+--- qtbase-opensource-src-5.6.2.orig/src/gui/kernel/qplatformintegrationfactory.cpp
++++ qtbase-opensource-src-5.6.2/src/gui/kernel/qplatformintegrationfactory.cpp
+@@ -67,9 +67,10 @@ QPlatformIntegration *QPlatformIntegrati
+     // Try loading the plugin from platformPluginPath first:
+     if (!platformPluginPath.isEmpty()) {
+         QCoreApplication::addLibraryPath(platformPluginPath);
+-        if (QPlatformIntegration *ret = loadIntegration(directLoader(), platform, paramList, argc, argv))
+-            return ret;
+     }
++    QCoreApplication::addLibraryPath(QStringLiteral(NIXPKGS_QPA_PLATFORM_PLUGIN_PATH));
++    if (QPlatformIntegration *ret = loadIntegration(directLoader(), platform, paramList, argc, argv))
++        return ret;
+     if (QPlatformIntegration *ret = loadIntegration(loader(), platform, paramList, argc, argv))
+         return ret;
+ #else
+@@ -95,15 +96,16 @@ QStringList QPlatformIntegrationFactory:
+     QStringList list;
+     if (!platformPluginPath.isEmpty()) {
+         QCoreApplication::addLibraryPath(platformPluginPath);
+-        list = directLoader()->keyMap().values();
+-        if (!list.isEmpty()) {
+-            const QString postFix = QStringLiteral(" (from ")
+-                                    + QDir::toNativeSeparators(platformPluginPath)
+-                                    + QLatin1Char(')');
+-            const QStringList::iterator end = list.end();
+-            for (QStringList::iterator it = list.begin(); it != end; ++it)
+-                (*it).append(postFix);
+-        }
++    }
++    QCoreApplication::addLibraryPath(QStringLiteral(NIXPKGS_QPA_PLATFORM_PLUGIN_PATH));
++    list = directLoader()->keyMap().values();
++    if (!list.isEmpty()) {
++        const QString postFix = QStringLiteral(" (from ")
++            + QDir::toNativeSeparators(platformPluginPath)
++            + QLatin1Char(')');
++        const QStringList::iterator end = list.end();
++        for (QStringList::iterator it = list.begin(); it != end; ++it)
++            (*it).append(postFix);
+     }
+     list.append(loader()->keyMap().values());
+     return list;
diff --git a/pkgs/development/libraries/qt-5/5.6/qtbase/series b/pkgs/development/libraries/qt-5/5.6/qtbase/series
index 2196d8383752..3527a91c84de 100644
--- a/pkgs/development/libraries/qt-5/5.6/qtbase/series
+++ b/pkgs/development/libraries/qt-5/5.6/qtbase/series
@@ -7,3 +7,4 @@ xdg-config-dirs.patch
 nix-profiles-library-paths.patch
 compose-search-path.patch
 libressl.patch
+qpa-platform-plugin-path.patch
diff --git a/pkgs/development/libraries/qt-5/5.8/qtbase/default.nix b/pkgs/development/libraries/qt-5/5.8/qtbase/default.nix
index e11d9dd1f29b..403c7f2636bb 100644
--- a/pkgs/development/libraries/qt-5/5.8/qtbase/default.nix
+++ b/pkgs/development/libraries/qt-5/5.8/qtbase/default.nix
@@ -128,7 +128,7 @@ stdenv.mkDerivation {
         -qmldir $out/lib/qt5/qml \
         -docdir $out/share/doc/qt5"
 
-    NIX_CFLAGS_COMPILE+=" -DNIXPKGS_QPA_PLATFORM_PLUGIN_PATH=\"''${!outputLib}/lib/qt5/plugins\""
+    NIX_CFLAGS_COMPILE+=" -DNIXPKGS_QPA_PLATFORM_PLUGIN_PATH=\"''${!outputLib}/lib/qt5/plugins/platforms\""
   '';
 
 
diff --git a/pkgs/development/libraries/qt-5/5.8/qtbase/nix-profiles-library-paths.patch b/pkgs/development/libraries/qt-5/5.8/qtbase/nix-profiles-library-paths.patch
index c5d91066d8f6..553c71d0ace3 100644
--- a/pkgs/development/libraries/qt-5/5.8/qtbase/nix-profiles-library-paths.patch
+++ b/pkgs/development/libraries/qt-5/5.8/qtbase/nix-profiles-library-paths.patch
@@ -2,7 +2,7 @@ Index: qtbase-opensource-src-5.8.0/src/corelib/kernel/qcoreapplication.cpp
 ===================================================================
 --- qtbase-opensource-src-5.8.0.orig/src/corelib/kernel/qcoreapplication.cpp
 +++ qtbase-opensource-src-5.8.0/src/corelib/kernel/qcoreapplication.cpp
-@@ -2476,7 +2476,17 @@ QStringList QCoreApplication::libraryPat
+@@ -2476,6 +2476,15 @@ QStringList QCoreApplication::libraryPat
          QStringList *app_libpaths = new QStringList;
          coreappdata()->app_libpaths.reset(app_libpaths);
  
@@ -16,7 +16,5 @@ Index: qtbase-opensource-src-5.8.0/src/corelib/kernel/qcoreapplication.cpp
 +        }
 +
          const QByteArray libPathEnv = qgetenv("QT_PLUGIN_PATH");
-+        qunsetenv("QT_PLUGIN_PATH"); // do not propagate to child processes
          if (!libPathEnv.isEmpty()) {
              QStringList paths = QFile::decodeName(libPathEnv).split(QDir::listSeparator(), QString::SkipEmptyParts);
-             for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
diff --git a/pkgs/development/libraries/qt-5/5.8/qtbase/qpa-platform-plugin-path.patch b/pkgs/development/libraries/qt-5/5.8/qtbase/qpa-platform-plugin-path.patch
new file mode 100644
index 000000000000..270116e1978d
--- /dev/null
+++ b/pkgs/development/libraries/qt-5/5.8/qtbase/qpa-platform-plugin-path.patch
@@ -0,0 +1,43 @@
+Index: qtbase-opensource-src-5.8.0/src/gui/kernel/qplatformintegrationfactory.cpp
+===================================================================
+--- qtbase-opensource-src-5.8.0.orig/src/gui/kernel/qplatformintegrationfactory.cpp
++++ qtbase-opensource-src-5.8.0/src/gui/kernel/qplatformintegrationfactory.cpp
+@@ -62,9 +62,10 @@ QPlatformIntegration *QPlatformIntegrati
+     // Try loading the plugin from platformPluginPath first:
+     if (!platformPluginPath.isEmpty()) {
+         QCoreApplication::addLibraryPath(platformPluginPath);
+-        if (QPlatformIntegration *ret = qLoadPlugin<QPlatformIntegration, QPlatformIntegrationPlugin>(directLoader(), platform, paramList, argc, argv))
+-            return ret;
+     }
++    QCoreApplication::addLibraryPath(QStringLiteral(NIXPKGS_QPA_PLATFORM_PLUGIN_PATH));
++    if (QPlatformIntegration *ret = qLoadPlugin<QPlatformIntegration, QPlatformIntegrationPlugin>(directLoader(), platform, paramList, argc, argv))
++        return ret;
+ #else
+     Q_UNUSED(platformPluginPath);
+ #endif
+@@ -84,15 +85,16 @@ QStringList QPlatformIntegrationFactory:
+ #ifndef QT_NO_LIBRARY
+     if (!platformPluginPath.isEmpty()) {
+         QCoreApplication::addLibraryPath(platformPluginPath);
+-        list = directLoader()->keyMap().values();
+-        if (!list.isEmpty()) {
+-            const QString postFix = QLatin1String(" (from ")
+-                                    + QDir::toNativeSeparators(platformPluginPath)
+-                                    + QLatin1Char(')');
+-            const QStringList::iterator end = list.end();
+-            for (QStringList::iterator it = list.begin(); it != end; ++it)
+-                (*it).append(postFix);
+-        }
++    }
++    QCoreApplication::addLibraryPath(QStringLiteral(NIXPKGS_QPA_PLATFORM_PLUGIN_PATH));
++    list = directLoader()->keyMap().values();
++    if (!list.isEmpty()) {
++        const QString postFix = QLatin1String(" (from ")
++            + QDir::toNativeSeparators(platformPluginPath)
++            + QLatin1Char(')');
++        const QStringList::iterator end = list.end();
++        for (QStringList::iterator it = list.begin(); it != end; ++it)
++            (*it).append(postFix);
+     }
+ #else
+     Q_UNUSED(platformPluginPath);
diff --git a/pkgs/development/libraries/qt-5/5.8/qtbase/qpa-plugin-path.patch b/pkgs/development/libraries/qt-5/5.8/qtbase/qpa-plugin-path.patch
deleted file mode 100644
index 6d40ed19c00a..000000000000
--- a/pkgs/development/libraries/qt-5/5.8/qtbase/qpa-plugin-path.patch
+++ /dev/null
@@ -1,15 +0,0 @@
-Index: qtbase-opensource-src-5.8.0/src/gui/kernel/qguiapplication.cpp
-===================================================================
---- qtbase-opensource-src-5.8.0.orig/src/gui/kernel/qguiapplication.cpp
-+++ qtbase-opensource-src-5.8.0/src/gui/kernel/qguiapplication.cpp
-@@ -1217,6 +1217,10 @@ void QGuiApplicationPrivate::createPlatf
- 
-     // Load the platform integration
-     QString platformPluginPath = QString::fromLocal8Bit(qgetenv("QT_QPA_PLATFORM_PLUGIN_PATH"));
-+    if (!platformPluginPath.isEmpty()) {
-+        platformPluginPath.append(QStringLiteral(":"));
-+    }
-+    platformPluginPath.append(QStringLiteral(NIXPKGS_QPA_PLATFORM_PLUGIN_PATH));
- 
- 
-     QByteArray platformName;
diff --git a/pkgs/development/libraries/qt-5/5.8/qtbase/series b/pkgs/development/libraries/qt-5/5.8/qtbase/series
index 6f6942b44443..47400cf7aa30 100644
--- a/pkgs/development/libraries/qt-5/5.8/qtbase/series
+++ b/pkgs/development/libraries/qt-5/5.8/qtbase/series
@@ -4,7 +4,7 @@ dlopen-libXcursor.patch
 xdg-config-dirs.patch
 nix-profiles-library-paths.patch
 libressl.patch
-qpa-plugin-path.patch
+qpa-platform-plugin-path.patch
 dlopen-gl.patch
 compose-search-path.patch
 cmake-paths.patch