From 6dd858dc300d0f185ca916d90d862927c7bd07cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD=20=D0=93=D0=B5=D0=BE?=
 =?UTF-8?q?=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8?=
 <gdamjan@gmail.com>
Date: Mon, 7 Sep 2020 11:59:17 +0200
Subject: [PATCH 1/4] uwsgi: set UWSGICONFIG_PHPPATH to the php.dev store

uwsgi needs to run php-config to get the include path. set the
UWSGICONFIG_PHPPATH to the php-config in the php.dev store.

Patch common.h to directly include php_session.h (since it's a separate package on
nixos).

NIX_CFLAGS_LINK was removed since it doesn't seem to be used at all

https://github.com/unbit/uwsgi/blob/master/plugins/php/common.h
---
 ...o-ext-session-php_session.h-on-NixOS.patch | 29 +++++++++++++++++++
 pkgs/servers/uwsgi/default.nix                | 12 +++++---
 2 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 pkgs/servers/uwsgi/0001-no-ext-session-php_session.h-on-NixOS.patch

diff --git a/pkgs/servers/uwsgi/0001-no-ext-session-php_session.h-on-NixOS.patch b/pkgs/servers/uwsgi/0001-no-ext-session-php_session.h-on-NixOS.patch
new file mode 100644
index 00000000000..bde8f4fda1d
--- /dev/null
+++ b/pkgs/servers/uwsgi/0001-no-ext-session-php_session.h-on-NixOS.patch
@@ -0,0 +1,29 @@
+From 6b9b3559d8ce59eda6c5cd6f04224cebaaa5d0ea Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD=20=D0=93=D0=B5=D0=BE?=
+ =?UTF-8?q?=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8?=
+ <gdamjan@gmail.com>
+Date: Tue, 8 Sep 2020 17:11:39 +0200
+Subject: [PATCH] no ext/session/php_session.h on NixOS
+
+on NixOS php_session.h is in its own package, and is not installed in
+ext/session/
+---
+ plugins/php/common.h | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/plugins/php/common.h b/plugins/php/common.h
+index 9bf1c069..be93f519 100644
+--- a/plugins/php/common.h
++++ b/plugins/php/common.h
+@@ -10,7 +10,7 @@
+ #endif
+ #include "ext/standard/info.h"
+ 
+-#include "ext/session/php_session.h"
++#include "php_session.h"
+ 
+ #include <uwsgi.h>
+ 
+-- 
+2.28.0
+
diff --git a/pkgs/servers/uwsgi/default.nix b/pkgs/servers/uwsgi/default.nix
index 39a3113eb41..bb2742ecba8 100644
--- a/pkgs/servers/uwsgi/default.nix
+++ b/pkgs/servers/uwsgi/default.nix
@@ -38,8 +38,12 @@ let php-embed = php.override {
                   (lib.nameValuePair "php" {
                     # usage: https://uwsgi-docs.readthedocs.io/en/latest/PHP.html#running-php-apps-with-nginx
                     path = "plugins/php";
-                    inputs = [ php-embed ] ++ php-embed.buildInputs;
-                    NIX_CFLAGS_LINK = [ "-L${libmysqlclient}/lib/mysql" ];
+                    inputs = [
+                        php-embed
+                        php-embed.extensions.session
+                        php-embed.extensions.session.dev
+                        php-embed.unwrapped.dev
+                    ] ++ php-embed.unwrapped.buildInputs;
                   })
                 ];
 
@@ -61,6 +65,8 @@ stdenv.mkDerivation rec {
     sha256 = "0256v72b7zr6ds4srpaawk1px3bp0djdwm239w3wrxpw7dzk1gjn";
   };
 
+  patches = [ ./0001-no-ext-session-php_session.h-on-NixOS.patch ];
+
   nativeBuildInputs = [ python3 pkgconfig ];
 
   buildInputs =  [ jansson pcre ]
@@ -94,8 +100,6 @@ stdenv.mkDerivation rec {
     ${lib.concatMapStringsSep "\n" (x: x.install or "") needed}
   '';
 
-  NIX_CFLAGS_LINK = toString (lib.optional withSystemd "-lsystemd" ++ lib.concatMap (x: x.NIX_CFLAGS_LINK or []) needed);
-
   meta = with stdenv.lib; {
     homepage = "https://uwsgi-docs.readthedocs.org/en/latest/";
     description = "A fast, self-healing and developer/sysadmin-friendly application container server coded in pure C";

From 87bed6c86aa9abb434b18d4e5e4aa47133eac666 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD=20=D0=93=D0=B5=D0=BE?=
 =?UTF-8?q?=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8?=
 <gdamjan@gmail.com>
Date: Wed, 9 Sep 2020 12:51:40 +0200
Subject: [PATCH 2/4] uwsgi: the php plugin needs to link with session.so

On NixOS the session.so php extension is a separate package (and not builtin
in libphp.so). But since the uwsgi php plugin uses the session
mechanisms, we need to link the plugin to that library too.

With this change uwsgipluginpy is hacked to take an additional UWSGICONFIG_PHP_LDFLAGS
environment variable and add it to its LDFLAGS, and then in the nix
expression the UWSGICONFIG_PHP_LDFLAGS is set to point to php.extensions.session
---
 pkgs/servers/uwsgi/additional-php-ldflags.patch   | 13 +++++++++++++
 pkgs/servers/uwsgi/default.nix                    | 15 ++++++++++++++-
 ...> no-ext-session-php_session.h-on-NixOS.patch} |  0
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/servers/uwsgi/additional-php-ldflags.patch
 rename pkgs/servers/uwsgi/{0001-no-ext-session-php_session.h-on-NixOS.patch => no-ext-session-php_session.h-on-NixOS.patch} (100%)

diff --git a/pkgs/servers/uwsgi/additional-php-ldflags.patch b/pkgs/servers/uwsgi/additional-php-ldflags.patch
new file mode 100644
index 00000000000..8549d5c2d03
--- /dev/null
+++ b/pkgs/servers/uwsgi/additional-php-ldflags.patch
@@ -0,0 +1,13 @@
+diff --git a/plugins/php/uwsgiplugin.py b/plugins/php/uwsgiplugin.py
+index d930c44e..2fcbc22a 100644
+--- a/plugins/php/uwsgiplugin.py
++++ b/plugins/php/uwsgiplugin.py
+@@ -17,6 +17,8 @@ php_version = os.popen(PHPPATH + ' --version').read().rstrip().split('.')[0]
+ CFLAGS = [os.popen(PHPPATH + ' --includes').read().rstrip(), '-Wno-sign-compare']
+ LDFLAGS = os.popen(PHPPATH + ' --ldflags').read().rstrip().split()
+ 
++LDFLAGS.append(os.environ.get('UWSGICONFIG_PHP_LDFLAGS', []))
++
+ if ld_run_path:
+     LDFLAGS.append('-L%s' % ld_run_path)
+     os.environ['LD_RUN_PATH'] = ld_run_path
diff --git a/pkgs/servers/uwsgi/default.nix b/pkgs/servers/uwsgi/default.nix
index bb2742ecba8..17d430a82ef 100644
--- a/pkgs/servers/uwsgi/default.nix
+++ b/pkgs/servers/uwsgi/default.nix
@@ -65,7 +65,10 @@ stdenv.mkDerivation rec {
     sha256 = "0256v72b7zr6ds4srpaawk1px3bp0djdwm239w3wrxpw7dzk1gjn";
   };
 
-  patches = [ ./0001-no-ext-session-php_session.h-on-NixOS.patch ];
+  patches = [
+        ./no-ext-session-php_session.h-on-NixOS.patch
+        ./additional-php-ldflags.patch
+  ];
 
   nativeBuildInputs = [ python3 pkgconfig ];
 
@@ -89,6 +92,16 @@ stdenv.mkDerivation rec {
     substituteAll ${./nixos.ini} buildconf/nixos.ini
   '';
 
+  # this is a hack to make the php plugin link with session.so (which on nixos is a separate package)
+  # the hack works in coordination with ./additional-php-ldflags.patch
+  UWSGICONFIG_PHP_LDFLAGS = lib.optionalString (builtins.any (x: x.name == "php") needed)
+        (lib.concatStringsSep "," [
+            "-Wl"
+            "-rpath=${php-embed.extensions.session}/lib/php/extensions/"
+            "--library-path=${php-embed.extensions.session}/lib/php/extensions/"
+            "-l:session.so"
+        ]);
+
   buildPhase = ''
     mkdir -p $pluginDir
     python3 uwsgiconfig.py --build nixos
diff --git a/pkgs/servers/uwsgi/0001-no-ext-session-php_session.h-on-NixOS.patch b/pkgs/servers/uwsgi/no-ext-session-php_session.h-on-NixOS.patch
similarity index 100%
rename from pkgs/servers/uwsgi/0001-no-ext-session-php_session.h-on-NixOS.patch
rename to pkgs/servers/uwsgi/no-ext-session-php_session.h-on-NixOS.patch

From 06749ba321ee3f284df74ab848985106eea52c13 Mon Sep 17 00:00:00 2001
From: rnhmjoj <rnhmjoj@inventati.org>
Date: Mon, 21 Dec 2020 00:22:29 +0100
Subject: [PATCH 3/4] nixos/tests/uwsgi: test PHP plugin

---
 nixos/tests/uwsgi.nix | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/nixos/tests/uwsgi.nix b/nixos/tests/uwsgi.nix
index 7f4945a8803..5c0b294e2d2 100644
--- a/nixos/tests/uwsgi.nix
+++ b/nixos/tests/uwsgi.nix
@@ -4,12 +4,13 @@ import ./make-test-python.nix ({ pkgs, ... }:
   meta = with pkgs.stdenv.lib.maintainers; {
     maintainers = [ lnl7 ];
   };
+
   machine = { pkgs, ... }: {
     services.uwsgi.enable = true;
-    services.uwsgi.plugins = [ "python3" ];
+    services.uwsgi.plugins = [ "python3" "php" ];
     services.uwsgi.instance = {
       type = "emperor";
-      vassals.hello = {
+      vassals.python = {
         type = "normal";
         master = true;
         workers = 2;
@@ -25,6 +26,17 @@ import ./make-test-python.nix ({ pkgs, ... }:
         '';
         pythonPackages = self: with self; [ flask ];
       };
+      vassals.php = {
+        type = "normal";
+        master = true;
+        workers = 2;
+        http-socket = ":8001";
+        http-socket-modifier1 = 14;
+        php-index = "index.php";
+        php-docroot = pkgs.writeTextDir "index.php" ''
+          <?php echo "Hello World\n"; ?>
+        '';
+      };
     };
   };
 
@@ -33,6 +45,8 @@ import ./make-test-python.nix ({ pkgs, ... }:
       machine.wait_for_unit("multi-user.target")
       machine.wait_for_unit("uwsgi.service")
       machine.wait_for_open_port(8000)
+      machine.wait_for_open_port(8001)
       assert "Hello World" in machine.succeed("curl -fv 127.0.0.1:8000")
+      assert "Hello World" in machine.succeed("curl -fv 127.0.0.1:8001")
     '';
 })

From 41af371de27257746af9089b1254277a1599bd36 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD=20=D0=93=D0=B5=D0=BE?=
 =?UTF-8?q?=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8?=
 <gdamjan@gmail.com>
Date: Mon, 21 Dec 2020 17:52:04 +0100
Subject: [PATCH 4/4] uwsgi: add passthru.tests.uwsgi to derivation

---
 pkgs/servers/uwsgi/default.nix | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/pkgs/servers/uwsgi/default.nix b/pkgs/servers/uwsgi/default.nix
index 17d430a82ef..d9ed810cdf4 100644
--- a/pkgs/servers/uwsgi/default.nix
+++ b/pkgs/servers/uwsgi/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchurl, pkgconfig, jansson, pcre
+{ stdenv, nixosTests, lib, fetchurl, pkgconfig, jansson, pcre
 # plugins: list of strings, eg. [ "python2" "python3" ]
 , plugins ? []
 , pam, withPAM ? stdenv.isLinux
@@ -120,4 +120,7 @@ stdenv.mkDerivation rec {
     maintainers = with maintainers; [ abbradar schneefux globin ];
     platforms = platforms.unix;
   };
+
+  passthru.tests.uwsgi = nixosTests.uwsgi;
+
 }