From edaecb41db05b512614ca0be10782790ea86a149 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebasti=C3=A1n=20Bernardo=20Galkin?= <paraseba@gmail.com>
Date: Sat, 14 May 2016 17:37:10 -0700
Subject: [PATCH] postgis: fix build after output splits

Fixes #15236

Two changes were needed:

- pg_config from postgresql package wasn't reporting the correct location for
the pgxs extension system, after the output split
- json_c is now split in dev and out outputs, postgis configure doesn't find the
library location properly

Closes #15470
---
 pkgs/development/libraries/postgis/default.nix  |  4 ++++
 pkgs/servers/sql/postgresql/default.nix         |  7 +++++++
 .../sql/postgresql/hardcode-pgxs-path.patch     | 17 +++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 pkgs/servers/sql/postgresql/hardcode-pgxs-path.patch

diff --git a/pkgs/development/libraries/postgis/default.nix b/pkgs/development/libraries/postgis/default.nix
index 5f41eb61c29b..f3e2312f4845 100644
--- a/pkgs/development/libraries/postgis/default.nix
+++ b/pkgs/development/libraries/postgis/default.nix
@@ -89,6 +89,10 @@ in rec {
     sha256 = "02gsi1cm63kf0r7881444lrkzdjqhhpz9a5zav3al0q24nq01r8g";
     sql_srcs = ["postgis.sql" "spatial_ref_sys.sql"];
     builtInputs = [gdal json_c pkgconfig];
+
+    # postgis config directory assumes /include /lib from the same root for json-c library
+    NIX_LDFLAGS = "-L${stdenv.lib.getLib json_c}/lib";
+
     dontDisableStatic = true;
     preConfigure = ''
       sed -i 's@/usr/bin/file@${file}/bin/file@' configure
diff --git a/pkgs/servers/sql/postgresql/default.nix b/pkgs/servers/sql/postgresql/default.nix
index 5fec92a5bf8a..bd7b8fd3dea0 100644
--- a/pkgs/servers/sql/postgresql/default.nix
+++ b/pkgs/servers/sql/postgresql/default.nix
@@ -32,12 +32,19 @@ let
     patches =
       [ (if lib.versionAtLeast version "9.4" then ./disable-resolve_symlinks-94.patch else ./disable-resolve_symlinks.patch)
         ./less-is-more.patch
+        ./hardcode-pgxs-path.patch
       ];
 
     installTargets = [ "install-world" ];
 
     LC_ALL = "C";
 
+    postConfigure =
+      ''
+        # Hardcode the path to pgxs so pg_config returns the path in $out
+        substituteInPlace "src/bin/pg_config/pg_config.c" --replace HARDCODED_PGXS_PATH $out/lib
+      '';
+
     postInstall =
       ''
         moveToOutput "lib/pgxs" "$out" # looks strange, but not deleting it
diff --git a/pkgs/servers/sql/postgresql/hardcode-pgxs-path.patch b/pkgs/servers/sql/postgresql/hardcode-pgxs-path.patch
new file mode 100644
index 000000000000..355813ffe20e
--- /dev/null
+++ b/pkgs/servers/sql/postgresql/hardcode-pgxs-path.patch
@@ -0,0 +1,17 @@
+--- a/src/bin/pg_config/pg_config.c
++++ b/src/bin/pg_config/pg_config.c
+@@ -220,11 +220,13 @@ show_sysconfdir(bool all)
+ static void
+ show_pgxs(bool all)
+ {
+-	char		path[MAXPGPATH];
++	char		path[MAXPGPATH] = "HARDCODED_PGXS_PATH";
+ 
+ 	if (all)
+ 		printf("PGXS = ");
++  /* commented out to be able to point to nix $out path
+ 	get_pkglib_path(mypath, path);
++  */
+ 	strlcat(path, "/pgxs/src/makefiles/pgxs.mk", sizeof(path));
+ 	cleanup_path(path);
+ 	printf("%s\n", path);