1let
2
3 generic =
4 # dependencies
5 { stdenv, lib, fetchurl, makeWrapper
6 , glibc, zlib, readline, openssl, icu, lz4, systemd, libossp_uuid
7 , pkg-config, libxml2, tzdata
8
9 # This is important to obtain a version of `libpq` that does not depend on systemd.
10 , enableSystemd ? (lib.versionAtLeast version "9.6" && !stdenv.isDarwin)
11 , gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic, libkrb5
12
13
14 # for postgreql.pkgs
15 , this, self, newScope, buildEnv
16
17 # source specification
18 , version, sha256, psqlSchema,
19
20 # for tests
21 nixosTests, thisAttr
22 }:
23 let
24 atLeast = lib.versionAtLeast version;
25 icuEnabled = atLeast "10";
26 lz4Enabled = atLeast "14";
27
28 in stdenv.mkDerivation rec {
29 pname = "postgresql";
30 inherit version;
31
32 src = fetchurl {
33 url = "mirror://postgresql/source/v${version}/${pname}-${version}.tar.bz2";
34 inherit sha256;
35 };
36
37 hardeningEnable = lib.optionals (!stdenv.cc.isClang) [ "pie" ];
38
39 outputs = [ "out" "lib" "doc" "man" ];
40 setOutputFlags = false; # $out retains configureFlags :-/
41
42 buildInputs =
43 [ zlib readline openssl libxml2 ]
44 ++ lib.optionals icuEnabled [ icu ]
45 ++ lib.optionals lz4Enabled [ lz4 ]
46 ++ lib.optionals enableSystemd [ systemd ]
47 ++ lib.optionals gssSupport [ libkrb5 ]
48 ++ lib.optionals (!stdenv.isDarwin) [ libossp_uuid ];
49
50 nativeBuildInputs = [ makeWrapper ] ++ lib.optionals icuEnabled [ pkg-config ];
51
52 enableParallelBuilding = !stdenv.isDarwin;
53
54 separateDebugInfo = true;
55
56 buildFlags = [ "world" ];
57
58 NIX_CFLAGS_COMPILE = "-I${libxml2.dev}/include/libxml2";
59
60 # Otherwise it retains a reference to compiler and fails; see #44767. TODO: better.
61 preConfigure = "CC=${stdenv.cc.targetPrefix}cc";
62
63 configureFlags = [
64 "--with-openssl"
65 "--with-libxml"
66 "--sysconfdir=/etc"
67 "--libdir=$(lib)/lib"
68 "--with-system-tzdata=${tzdata}/share/zoneinfo"
69 "--enable-debug"
70 (lib.optionalString enableSystemd "--with-systemd")
71 (if stdenv.isDarwin then "--with-uuid=e2fs" else "--with-ossp-uuid")
72 ] ++ lib.optionals icuEnabled [ "--with-icu" ]
73 ++ lib.optionals lz4Enabled [ "--with-lz4" ]
74 ++ lib.optionals gssSupport [ "--with-gssapi" ];
75
76 patches =
77 [ (if atLeast "9.4" then ./patches/disable-resolve_symlinks-94.patch else ./patches/disable-resolve_symlinks.patch)
78 (if atLeast "9.6" then ./patches/less-is-more-96.patch else ./patches/less-is-more.patch)
79 (if atLeast "9.6" then ./patches/hardcode-pgxs-path-96.patch else ./patches/hardcode-pgxs-path.patch)
80 ./patches/specify_pkglibdir_at_runtime.patch
81 ./patches/findstring.patch
82 ]
83 ++ lib.optional stdenv.isLinux (if atLeast "13" then ./patches/socketdir-in-run-13.patch else ./patches/socketdir-in-run.patch);
84
85 installTargets = [ "install-world" ];
86
87 LC_ALL = "C";
88
89 postConfigure =
90 let path = if atLeast "9.6" then "src/common/config_info.c" else "src/bin/pg_config/pg_config.c"; in
91 ''
92 # Hardcode the path to pgxs so pg_config returns the path in $out
93 substituteInPlace "${path}" --replace HARDCODED_PGXS_PATH $out/lib
94 '';
95
96 postInstall =
97 ''
98 moveToOutput "lib/pgxs" "$out" # looks strange, but not deleting it
99 moveToOutput "lib/libpgcommon*.a" "$out"
100 moveToOutput "lib/libpgport*.a" "$out"
101 moveToOutput "lib/libecpg*" "$out"
102
103 # Prevent a retained dependency on gcc-wrapper.
104 substituteInPlace "$out/lib/pgxs/src/Makefile.global" --replace ${stdenv.cc}/bin/ld ld
105
106 if [ -z "''${dontDisableStatic:-}" ]; then
107 # Remove static libraries in case dynamic are available.
108 for i in $out/lib/*.a $lib/lib/*.a; do
109 name="$(basename "$i")"
110 ext="${stdenv.hostPlatform.extensions.sharedLibrary}"
111 if [ -e "$lib/lib/''${name%.a}$ext" ] || [ -e "''${i%.a}$ext" ]; then
112 rm "$i"
113 fi
114 done
115 fi
116 '';
117
118 postFixup = lib.optionalString (!stdenv.isDarwin && stdenv.hostPlatform.libc == "glibc")
119 ''
120 # initdb needs access to "locale" command from glibc.
121 wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
122 '';
123
124 doCheck = !stdenv.isDarwin;
125 # autodetection doesn't seem to able to find this, but it's there.
126 checkTarget = "check";
127
128 preCheck =
129 # On musl, comment skip the following tests, because they break due to
130 # ! ERROR: could not load library "/build/postgresql-11.5/tmp_install/nix/store/...-postgresql-11.5-lib/lib/libpqwalreceiver.so": Error loading shared library libpq.so.5: No such file or directory (needed by /build/postgresql-11.5/tmp_install/nix/store/...-postgresql-11.5-lib/lib/libpqwalreceiver.so)
131 # See also here:
132 # https://git.alpinelinux.org/aports/tree/main/postgresql/disable-broken-tests.patch?id=6d7d32c12e073a57a9e5946e55f4c1fbb68bd442
133 if stdenv.hostPlatform.isMusl then ''
134 substituteInPlace src/test/regress/parallel_schedule \
135 --replace "subscription" "" \
136 --replace "object_address" ""
137 '' else null;
138
139 doInstallCheck = false; # needs a running daemon?
140
141 disallowedReferences = [ stdenv.cc ];
142
143 passthru = {
144 inherit readline psqlSchema;
145
146 pkgs = let
147 scope = { postgresql = this; };
148 newSelf = self // scope;
149 newSuper = { callPackage = newScope (scope // this.pkgs); };
150 in import ./packages.nix newSelf newSuper;
151
152 withPackages = postgresqlWithPackages {
153 inherit makeWrapper buildEnv;
154 postgresql = this;
155 }
156 this.pkgs;
157
158 tests.postgresql = nixosTests.postgresql-wal-receiver.${thisAttr};
159 };
160
161 meta = with lib; {
162 homepage = "https://www.postgresql.org";
163 description = "A powerful, open source object-relational database system";
164 license = licenses.postgresql;
165 maintainers = with maintainers; [ thoughtpolice danbst globin marsam ivan ];
166 platforms = platforms.unix;
167 knownVulnerabilities = optional (!atLeast "9.4")
168 "PostgreSQL versions older than 9.4 are not maintained anymore!";
169 };
170 };
171
172 postgresqlWithPackages = { postgresql, makeWrapper, buildEnv }: pkgs: f: buildEnv {
173 name = "postgresql-and-plugins-${postgresql.version}";
174 paths = f pkgs ++ [
175 postgresql
176 postgresql.lib
177 postgresql.man # in case user installs this into environment
178 ];
179 buildInputs = [ makeWrapper ];
180
181
182 # We include /bin to ensure the $out/bin directory is created, which is
183 # needed because we'll be removing the files from that directory in postBuild
184 # below. See #22653
185 pathsToLink = ["/" "/bin"];
186
187 # Note: the duplication of executables is about 4MB size.
188 # So a nicer solution was patching postgresql to allow setting the
189 # libdir explicitely.
190 postBuild = ''
191 mkdir -p $out/bin
192 rm $out/bin/{pg_config,postgres,pg_ctl}
193 cp --target-directory=$out/bin ${postgresql}/bin/{postgres,pg_config,pg_ctl}
194 wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
195 '';
196
197 passthru.version = postgresql.version;
198 passthru.psqlSchema = postgresql.psqlSchema;
199 };
200
201in self: {
202
203 postgresql_9_6 = self.callPackage generic {
204 version = "9.6.23";
205 psqlSchema = "9.6";
206 sha256 = "1fa735lrmv2vrfiixg73nh024gxlagcbrssklvgwdf0s82cgfjd8";
207 this = self.postgresql_9_6;
208 thisAttr = "postgresql_9_6";
209 inherit self;
210 };
211
212 postgresql_10 = self.callPackage generic {
213 version = "10.18";
214 psqlSchema = "10.0"; # should be 10, but changing it is invasive
215 sha256 = "009qpb02bq0rx0aaw5ck70gk07xwparhfxvlfimgihw2vhp7qisp";
216 this = self.postgresql_10;
217 thisAttr = "postgresql_10";
218 inherit self;
219 icu = self.icu67;
220 };
221
222 postgresql_11 = self.callPackage generic {
223 version = "11.13";
224 psqlSchema = "11.1"; # should be 11, but changing it is invasive
225 sha256 = "0j5wnscnxa3sx8d39s55654df8aikmvkihfb0a02hrgmyygnihx0";
226 this = self.postgresql_11;
227 thisAttr = "postgresql_11";
228 inherit self;
229 };
230
231 postgresql_12 = self.callPackage generic {
232 version = "12.8";
233 psqlSchema = "12";
234 sha256 = "0an6v5bsp26d276wbdx76lsq6cq86hgi2fmkzwawnk63j3h02r72";
235 this = self.postgresql_12;
236 thisAttr = "postgresql_12";
237 inherit self;
238 };
239
240 postgresql_13 = self.callPackage generic {
241 version = "13.4";
242 psqlSchema = "13";
243 sha256 = "1kf0gcsrl5n25rjlvkh87aywmn28kbwvakm5c7j1qpr4j01y34za";
244 this = self.postgresql_13;
245 thisAttr = "postgresql_13";
246 inherit self;
247 };
248
249 postgresql_14 = self.callPackage generic {
250 version = "14.0";
251 psqlSchema = "14";
252 sha256 = "08m14zcrcvc2i0xl10p0wgzycsmfmk27gny40a8mwdx74s8xfapf";
253 this = self.postgresql_14;
254 thisAttr = "postgresql_14";
255 inherit self;
256 };
257}