1{
2 autoconf,
3 automake,
4 cunit,
5 docbook5,
6 fetchFromGitHub,
7 gdalMinimal,
8 geos,
9 jitSupport,
10 json_c,
11 lib,
12 libiconv,
13 libtool,
14 libxml2,
15 libxslt,
16 llvm,
17 pcre2,
18 perl,
19 pkg-config,
20 postgresql,
21 postgresqlBuildExtension,
22 postgresqlTestExtension,
23 postgresqlTestHook,
24 proj,
25 protobufc,
26 stdenv,
27 which,
28
29 withSfcgal ? false,
30 sfcgal,
31}:
32
33let
34 gdal = gdalMinimal;
35in
36postgresqlBuildExtension (finalAttrs: {
37 pname = "postgis";
38 version = "3.6.0";
39
40 outputs = [
41 "out"
42 "doc"
43 ];
44
45 src = fetchFromGitHub {
46 owner = "postgis";
47 repo = "postgis";
48 tag = finalAttrs.version;
49 hash = "sha256-L8k3yk1Dn4Dk7UyHse+8RJsjYsYMebdsiZp6fS7cC0Y=";
50 };
51
52 buildInputs = [
53 geos
54 proj
55 gdal
56 json_c
57 protobufc
58 pcre2.dev
59 ]
60 ++ lib.optional stdenv.hostPlatform.isDarwin libiconv
61 ++ lib.optional withSfcgal sfcgal;
62
63 nativeBuildInputs = [
64 autoconf
65 automake
66 libtool
67 libxml2
68 perl
69 pkg-config
70 protobufc
71 which
72 ]
73 ++ lib.optional jitSupport llvm;
74
75 dontDisableStatic = true;
76
77 checkInputs = [
78 cunit
79 ];
80
81 nativeCheckInputs = [
82 postgresql
83 postgresqlTestHook
84 libxslt
85 ];
86
87 postgresqlTestUserOptions = "LOGIN SUPERUSER";
88
89 # postgis config directory assumes /include /lib from the same root for json-c library
90 env.NIX_LDFLAGS = "-L${lib.getLib json_c}/lib";
91
92 setOutputFlags = false;
93 preConfigure = ''
94 ./autogen.sh
95 '';
96
97 configureFlags =
98 let
99 isCross = stdenv.hostPlatform.config != stdenv.buildPlatform.config;
100 in
101 [
102 (lib.enableFeature false "extension-upgrades-install")
103 (lib.withFeatureAs true "pgconfig" "${postgresql.pg_config}/bin/pg_config")
104 (lib.withFeatureAs true "gdalconfig" "${gdal}/bin/gdal-config")
105 (lib.withFeatureAs true "jsondir" (lib.getDev json_c))
106 (lib.withFeatureAs true "xml2config" (lib.getExe' (lib.getDev libxml2) "xml2-config"))
107 (lib.withFeatureAs withSfcgal "sfcgal" "${sfcgal}/bin/sfcgal-config")
108 (lib.withFeature (!isCross) "json") # configure: error: cannot check for file existence when cross compiling
109 ];
110
111 makeFlags = [
112 "PERL=${perl}/bin/perl"
113 ];
114
115 doCheck = stdenv.hostPlatform.isLinux;
116
117 preCheck = ''
118 substituteInPlace doc/postgis-out.xml --replace-fail "http://docbook.org/xml/5.0/dtd/docbook.dtd" "${docbook5}/xml/dtd/docbook/docbookx.dtd"
119 # The test suite hardcodes it to use /tmp.
120 export PGIS_REG_TMPDIR="$TMPDIR/pgis_reg"
121 '';
122
123 # create aliases for all commands adding version information
124 postInstall = ''
125 for prog in $out/bin/*; do # */
126 ln -s $prog $prog-${finalAttrs.version}
127 done
128
129 mkdir -p $doc/share/doc/postgis
130 mv doc/* $doc/share/doc/postgis/
131 '';
132
133 passthru.tests.extension = postgresqlTestExtension {
134 inherit (finalAttrs) finalPackage;
135 sql = ''
136 CREATE EXTENSION postgis;
137 CREATE EXTENSION postgis_raster;
138 CREATE EXTENSION postgis_topology;
139 -- st_makepoint goes through c code
140 select st_makepoint(1, 1);
141 ''
142 + lib.optionalString withSfcgal ''
143 CREATE EXTENSION postgis_sfcgal;
144 CREATE TABLE geometries (
145 name varchar,
146 geom geometry(PolygonZ) NOT NULL
147 );
148
149 INSERT INTO geometries(name, geom) VALUES
150 ('planar geom', 'PolygonZ((1 1 0, 1 2 0, 2 2 0, 2 1 0, 1 1 0))'),
151 ('nonplanar geom', 'PolygonZ((1 1 1, 1 2 -1, 2 2 2, 2 1 0, 1 1 1))');
152
153 SELECT name from geometries where cg_isplanar(geom);
154 '';
155 asserts = [
156 {
157 query = "postgis_version()";
158 expected = "'${lib.versions.major finalAttrs.version}.${lib.versions.minor finalAttrs.version} USE_GEOS=1 USE_PROJ=1 USE_STATS=1'";
159 description = "postgis_version() returns correct values.";
160 }
161 ]
162 ++ lib.optional withSfcgal {
163 query = "postgis_sfcgal_version()";
164 expected = "'${sfcgal.version}'";
165 description = "postgis_sfcgal_version() returns correct value.";
166 };
167 };
168
169 meta = {
170 description = "Geographic Objects for PostgreSQL";
171 homepage = "https://postgis.net/";
172 changelog = "https://git.osgeo.org/gitea/postgis/postgis/raw/tag/${finalAttrs.version}/NEWS";
173 license = lib.licenses.gpl2Plus;
174 maintainers = with lib.maintainers; [ marcweber ];
175 teams = [ lib.teams.geospatial ];
176 inherit (postgresql.meta) platforms;
177 };
178})