1let
2
3 generic =
4 # utils
5 {
6 stdenv,
7 fetchFromGitHub,
8 fetchurl,
9 lib,
10 replaceVars,
11 writeShellScriptBin,
12
13 # source specification
14 hash,
15 muslPatches ? { },
16 rev,
17 version,
18
19 # runtime dependencies
20 darwin,
21 freebsd,
22 glibc,
23 libuuid,
24 libxml2,
25 lz4,
26 openssl,
27 readline,
28 tzdata,
29 zlib,
30 zstd,
31
32 # build dependencies
33 bison,
34 docbook-xsl-nons,
35 docbook_xml_dtd_45,
36 flex,
37 libxslt,
38 makeBinaryWrapper,
39 pkg-config,
40 removeReferencesTo,
41
42 # passthru
43 buildEnv,
44 buildPackages,
45 newScope,
46 nixosTests,
47 postgresqlTestHook,
48 self,
49 stdenvNoCC,
50 testers,
51
52 # Block size
53 # Changing the block size will break on-disk database compatibility. See:
54 # https://www.postgresql.org/docs/current/install-make.html#CONFIGURE-OPTION-WITH-BLOCKSIZE
55 withBlocksize ? null,
56 withWalBlocksize ? null,
57
58 # bonjour
59 bonjourSupport ? false,
60
61 # Curl
62 curlSupport ?
63 lib.versionAtLeast version "18"
64 && lib.meta.availableOn stdenv.hostPlatform curl
65 # Building statically fails with:
66 # configure: error: library 'curl' does not provide curl_multi_init
67 # https://www.postgresql.org/message-id/487dacec-6d8d-46c0-a36f-d5b8c81a56f1%40technowledgy.de
68 && !stdenv.hostPlatform.isStatic,
69 curl,
70
71 # GSSAPI
72 gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic,
73 libkrb5,
74
75 # icu
76 # Building with icu in pkgsStatic gives tons of "undefined reference" errors like this:
77 # /nix/store/452lkaak37d3mzzn3p9ak7aa3wzhdqaj-icu4c-74.2-x86_64-unknown-linux-musl/lib/libicuuc.a(chariter.ao):
78 # (.data.rel.ro._ZTIN6icu_7417CharacterIteratorE[_ZTIN6icu_7417CharacterIteratorE]+0x0):
79 # undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
80 icuSupport ? !stdenv.hostPlatform.isStatic,
81 icu,
82
83 # JIT
84 jitSupport ?
85 stdenv.hostPlatform.canExecute stdenv.buildPlatform
86 # Building with JIT in pkgsStatic fails like this:
87 # fatal error: 'stdio.h' file not found
88 && !stdenv.hostPlatform.isStatic,
89 llvmPackages_20,
90 nukeReferences,
91 overrideCC,
92
93 # LDAP
94 ldapSupport ? false,
95 openldap,
96
97 # NLS
98 nlsSupport ? false,
99 gettext,
100
101 # NUMA
102 numaSupport ? lib.versionAtLeast version "18" && lib.meta.availableOn stdenv.hostPlatform numactl,
103 numactl,
104
105 # PAM
106 pamSupport ?
107 lib.meta.availableOn stdenv.hostPlatform linux-pam
108 # Building with linux-pam in pkgsStatic gives a few "undefined reference" errors like this:
109 # /nix/store/3s55icpsbc36sgn7sa8q3qq4z6al6rlr-linux-pam-static-x86_64-unknown-linux-musl-1.6.1/lib/libpam.a(pam_audit.o):
110 # in function `pam_modutil_audit_write':(.text+0x571):
111 # undefined reference to `audit_close'
112 && !stdenv.hostPlatform.isStatic,
113 linux-pam,
114
115 # PL/Perl
116 perlSupport ?
117 lib.meta.availableOn stdenv.hostPlatform perl
118 # Building with perl in pkgsStatic gives this error:
119 # configure: error: cannot build PL/Perl because libperl is not a shared library
120 && !stdenv.hostPlatform.isStatic
121 # configure tries to call the perl executable for the version
122 && stdenv.buildPlatform.canExecute stdenv.hostPlatform,
123 perl,
124
125 # PL/Python
126 pythonSupport ?
127 lib.meta.availableOn stdenv.hostPlatform python3
128 # Building with python in pkgsStatic gives this error:
129 # checking how to link an embedded Python application... configure: error: could not find shared library for Python
130 && !stdenv.hostPlatform.isStatic
131 # configure tries to call the python executable
132 && stdenv.buildPlatform.canExecute stdenv.hostPlatform,
133 python3,
134
135 # PL/Tcl
136 tclSupport ?
137 lib.meta.availableOn stdenv.hostPlatform tcl
138 # tcl is broken in pkgsStatic
139 && !stdenv.hostPlatform.isStatic
140 # configure fails with:
141 # configure: error: file 'tclConfig.sh' is required for Tcl
142 && stdenv.buildPlatform.canExecute stdenv.hostPlatform,
143 tcl,
144
145 # SELinux
146 selinuxSupport ? false,
147 libselinux,
148
149 # Systemd
150 systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemdLibs,
151 systemdLibs,
152
153 # Uring
154 uringSupport ? lib.versionAtLeast version "18" && lib.meta.availableOn stdenv.hostPlatform liburing,
155 liburing,
156 }@args:
157 let
158 atLeast = lib.versionAtLeast version;
159 olderThan = lib.versionOlder version;
160 lz4Enabled = atLeast "14";
161 zstdEnabled = atLeast "15";
162
163 dlSuffix = if olderThan "16" then ".so" else stdenv.hostPlatform.extensions.sharedLibrary;
164
165 # Pin LLVM 20 until upstream has resolved:
166 # https://www.postgresql.org/message-id/flat/d25e6e4a-d1b4-84d3-2f8a-6c45b975f53d%40applied-asynchrony.com
167 # TODO: Remove with next minor releases
168 llvmPackages = lib.warnIf (
169 version == "17.7"
170 ) "PostgreSQL: Is the pin for LLVM 20 still needed?" llvmPackages_20;
171
172 stdenv' =
173 if !stdenv.cc.isClang then
174 overrideCC llvmPackages.stdenv (
175 llvmPackages.stdenv.cc.override {
176 # LLVM bintools are not used by default, but are needed to make -flto work below.
177 bintools = llvmPackages.bintools;
178 }
179 )
180 else
181 stdenv;
182 in
183 stdenv'.mkDerivation (finalAttrs: {
184 inherit version;
185 pname = "postgresql";
186
187 src = fetchFromGitHub {
188 owner = "postgres";
189 repo = "postgres";
190 # rev, not tag, on purpose: allows updating when new versions
191 # are "stamped" a few days before release (tag).
192 inherit hash rev;
193 };
194
195 __structuredAttrs = true;
196
197 outputs = [
198 "out"
199 "dev"
200 "doc"
201 "lib"
202 "man"
203 ]
204 ++ lib.optionals jitSupport [ "jit" ]
205 ++ lib.optionals perlSupport [ "plperl" ]
206 ++ lib.optionals pythonSupport [ "plpython3" ]
207 ++ lib.optionals tclSupport [ "pltcl" ];
208
209 outputChecks = {
210 out = {
211 disallowedReferences = [
212 "dev"
213 "doc"
214 "man"
215 ]
216 ++ lib.optionals jitSupport [ "jit" ];
217 disallowedRequisites = [
218 stdenv'.cc
219 llvmPackages.llvm.out
220 llvmPackages.llvm.lib
221 ]
222 ++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs));
223 };
224
225 lib = {
226 disallowedReferences = [
227 "out"
228 "dev"
229 "doc"
230 "man"
231 ]
232 ++ lib.optionals jitSupport [ "jit" ];
233 disallowedRequisites = [
234 stdenv'.cc
235 llvmPackages.llvm.out
236 llvmPackages.llvm.lib
237 ]
238 ++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs));
239 };
240
241 doc = {
242 disallowedReferences = [
243 "out"
244 "dev"
245 "man"
246 ]
247 ++ lib.optionals jitSupport [ "jit" ];
248 };
249
250 man = {
251 disallowedReferences = [
252 "out"
253 "dev"
254 "doc"
255 ]
256 ++ lib.optionals jitSupport [ "jit" ];
257 };
258 }
259 // lib.optionalAttrs jitSupport {
260 jit = {
261 disallowedReferences = [
262 "dev"
263 "doc"
264 "man"
265 ];
266 disallowedRequisites = [
267 stdenv'.cc
268 llvmPackages.llvm.out
269 ]
270 ++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs));
271 };
272 };
273
274 strictDeps = true;
275
276 buildInputs = [
277 zlib
278 readline
279 openssl
280 libxml2
281 libuuid
282 ]
283 ++ lib.optionals icuSupport [ icu ]
284 ++ lib.optionals jitSupport [ llvmPackages.llvm ]
285 ++ lib.optionals lz4Enabled [ lz4 ]
286 ++ lib.optionals zstdEnabled [ zstd ]
287 ++ lib.optionals systemdSupport [ systemdLibs ]
288 ++ lib.optionals uringSupport [ liburing ]
289 ++ lib.optionals curlSupport [ curl ]
290 ++ lib.optionals numaSupport [ numactl ]
291 ++ lib.optionals gssSupport [ libkrb5 ]
292 ++ lib.optionals pamSupport [ linux-pam ]
293 ++ lib.optionals perlSupport [ perl ]
294 ++ lib.optionals ldapSupport [ openldap ]
295 ++ lib.optionals selinuxSupport [ libselinux ]
296 ++ lib.optionals nlsSupport [ gettext ];
297
298 nativeBuildInputs = [
299 bison
300 docbook-xsl-nons
301 docbook_xml_dtd_45
302 flex
303 libxml2
304 libxslt
305 makeBinaryWrapper
306 perl
307 pkg-config
308 removeReferencesTo
309 ]
310 ++ lib.optionals jitSupport [
311 llvmPackages.llvm.dev
312 nukeReferences
313 ];
314
315 enableParallelBuilding = true;
316
317 separateDebugInfo = true;
318
319 buildFlags = [ "world" ];
320
321 env = {
322 # libpgcommon.a and libpgport.a contain all paths returned by pg_config and are linked
323 # into all binaries. However, almost no binaries actually use those paths. The following
324 # flags will remove unused sections from all shared libraries and binaries - including
325 # those paths. This avoids a lot of circular dependency problems with different outputs,
326 # and allows splitting them cleanly.
327 CFLAGS = "-fdata-sections -ffunction-sections -flto";
328
329 # This flag was introduced upstream in:
330 # https://github.com/postgres/postgres/commit/b6c7cfac88c47a9194d76f3d074129da3c46545a
331 # It causes errors when linking against libpq.a in pkgsStatic:
332 # undefined reference to `pg_encoding_to_char'
333 # Unsetting the flag fixes it. The upstream reasoning to introduce it is about the risk
334 # to have initdb load a libpq.so from a different major version and how to avoid that.
335 # This doesn't apply to us with Nix.
336 NIX_CFLAGS_COMPILE = "-UUSE_PRIVATE_ENCODING_FUNCS";
337 }
338 // lib.optionalAttrs perlSupport { PERL = lib.getExe perl; }
339 // lib.optionalAttrs pythonSupport { PYTHON = lib.getExe python3; }
340 // lib.optionalAttrs tclSupport { TCLSH = "${lib.getBin tcl}/bin/tclsh"; };
341
342 configureFlags =
343 let
344 inherit (lib) withFeature;
345 in
346 [
347 "--with-openssl"
348 "--with-libxml"
349 (withFeature icuSupport "icu")
350 "--sysconfdir=/etc"
351 "--with-system-tzdata=${tzdata}/share/zoneinfo"
352 "--enable-debug"
353 (lib.optionalString systemdSupport "--with-systemd")
354 (if stdenv.hostPlatform.isFreeBSD then "--with-uuid=bsd" else "--with-uuid=e2fs")
355 (withFeature perlSupport "perl")
356 ]
357 ++ lib.optionals (withBlocksize != null) [ "--with-blocksize=${toString withBlocksize}" ]
358 ++ lib.optionals (withWalBlocksize != null) [ "--with-wal-blocksize=${toString withWalBlocksize}" ]
359 ++ lib.optionals lz4Enabled [ "--with-lz4" ]
360 ++ lib.optionals zstdEnabled [ "--with-zstd" ]
361 ++ lib.optionals uringSupport [ "--with-liburing" ]
362 ++ lib.optionals curlSupport [ "--with-libcurl" ]
363 ++ lib.optionals numaSupport [ "--with-libnuma" ]
364 ++ lib.optionals gssSupport [ "--with-gssapi" ]
365 ++ lib.optionals pythonSupport [ "--with-python" ]
366 ++ lib.optionals jitSupport [ "--with-llvm" ]
367 ++ lib.optionals pamSupport [ "--with-pam" ]
368 # This can be removed once v17 is removed. v18+ ships with it.
369 ++ lib.optionals (stdenv'.hostPlatform.isDarwin && atLeast "16" && olderThan "18") [
370 "LDFLAGS_EX_BE=-Wl,-export_dynamic"
371 ]
372 # some version of this flag is required in all cross configurations
373 # since it cannot be automatically detected
374 ++
375 lib.optionals
376 (
377 (!stdenv'.hostPlatform.isDarwin)
378 && (!(stdenv'.buildPlatform.canExecute stdenv.hostPlatform))
379 && atLeast "16"
380 )
381 [
382 "LDFLAGS_EX_BE=-Wl,--export-dynamic"
383 ]
384 ++ lib.optionals ldapSupport [ "--with-ldap" ]
385 ++ lib.optionals tclSupport [ "--with-tcl" ]
386 ++ lib.optionals selinuxSupport [ "--with-selinux" ]
387 ++ lib.optionals nlsSupport [ "--enable-nls" ]
388 ++ lib.optionals bonjourSupport [ "--with-bonjour" ];
389
390 patches = [
391 (
392 if atLeast "16" then
393 ./patches/relative-to-symlinks-16+.patch
394 else
395 ./patches/relative-to-symlinks.patch
396 )
397 (
398 if atLeast "15" then
399 ./patches/empty-pg-config-view-15+.patch
400 else
401 ./patches/empty-pg-config-view.patch
402 )
403 ./patches/less-is-more.patch
404 ./patches/paths-for-split-outputs.patch
405 ./patches/paths-with-postgresql-suffix.patch
406
407 (replaceVars ./patches/locale-binary-path.patch {
408 locale = "${
409 if stdenv.hostPlatform.isDarwin then
410 darwin.adv_cmds
411 else if stdenv.hostPlatform.isFreeBSD then
412 freebsd.locale
413 else
414 lib.getBin stdenv.cc.libc
415 }/bin/locale";
416 })
417 ]
418 ++ lib.optionals stdenv'.hostPlatform.isMusl (
419 # Using fetchurl instead of fetchpatch on purpose: https://github.com/NixOS/nixpkgs/issues/240141
420 map fetchurl (lib.attrValues muslPatches)
421 )
422 ++ lib.optionals stdenv'.hostPlatform.isLinux [
423 ./patches/socketdir-in-run-13+.patch
424 ]
425 ++ lib.optionals (stdenv'.hostPlatform.isDarwin && olderThan "16") [
426 ./patches/export-dynamic-darwin-15-.patch
427 ];
428
429 installTargets = [ "install-world" ];
430
431 postPatch = ''
432 substituteInPlace "src/Makefile.global.in" --subst-var out
433 substituteInPlace "src/common/config_info.c" --subst-var dev
434 cat ${./pg_config.env.mk} >> src/common/Makefile
435 ''
436 # This test always fails on hardware with >1 NUMA node: the sysfs
437 # dirs providing information about the topology are hidden in the sandbox,
438 # so postgres assumes there's only a single node `0`. However,
439 # the test checks on which NUMA nodes the allocated pages are which is >1
440 # on such hardware. This in turn triggers a safeguard in the view
441 # which breaks the test.
442 # Manual tests confirm that the testcase behaves properly outside of the
443 # Nix sandbox.
444 + lib.optionalString (atLeast "18") ''
445 substituteInPlace src/test/regress/parallel_schedule \
446 --replace-fail numa ""
447 ''
448 # This check was introduced upstream to prevent calls to "exit" inside libpq.
449 # However, this doesn't work reliably with static linking, see this and following:
450 # https://postgr.es/m/flat/20210703001639.GB2374652%40rfd.leadboat.com#52584ca4bd3cb9dac376f3158c419f97
451 # Thus, disable the check entirely, as it would currently fail with this:
452 # > libpq.so.5.17: U atexit
453 # > libpq.so.5.17: U pthread_exit
454 # > libpq must not be calling any function which invokes exit
455 # Don't mind the fact that this checks libpq.**so** in pkgsStatic - that's correct, since PostgreSQL
456 # still needs a shared library internally.
457 + lib.optionalString (atLeast "15" && stdenv'.hostPlatform.isStatic) ''
458 substituteInPlace src/interfaces/libpq/Makefile \
459 --replace-fail "echo 'libpq must not be calling any function which invokes exit'; exit 1;" "echo;"
460 '';
461
462 postInstall = ''
463 moveToOutput "bin/ecpg" "$dev"
464 moveToOutput "lib/pgxs" "$dev"
465 ''
466 + lib.optionalString (stdenv'.buildPlatform.canExecute stdenv'.hostPlatform) ''
467 mkdir -p "$dev/nix-support"
468 "$out/bin/pg_config" > "$dev/nix-support/pg_config.expected"
469 ''
470 + ''
471 rm "$out/bin/pg_config"
472 make -C src/common pg_config.env
473 substituteInPlace src/common/pg_config.env \
474 --replace-fail "$out" "@out@" \
475 --replace-fail "$man" "@man@"
476 install -D src/common/pg_config.env "$dev/nix-support/pg_config.env"
477
478 # postgres exposes external symbols get_pkginclude_path and similar. Those
479 # can't be stripped away by --gc-sections/LTO, because they could theoretically
480 # be used by dynamically loaded modules / extensions. To avoid circular dependencies,
481 # references to -dev, -doc and -man are removed here. References to -lib must be kept,
482 # because there is a realistic use-case for extensions to locate the /lib directory to
483 # load other shared modules.
484 remove-references-to -t "$dev" -t "$doc" -t "$man" "$out/bin/postgres"
485 ''
486 + lib.optionalString (!stdenv'.hostPlatform.isStatic) ''
487 if [ -z "''${dontDisableStatic:-}" ]; then
488 # Remove static libraries in case dynamic are available.
489 for i in $lib/lib/*.a; do
490 name="$(basename "$i")"
491 ext="${stdenv'.hostPlatform.extensions.sharedLibrary}"
492 if [ -e "$lib/lib/''${name%.a}$ext" ] || [ -e "''${i%.a}$ext" ]; then
493 rm "$i"
494 fi
495 done
496 fi
497 ''
498 + ''
499 # The remaining static libraries are libpgcommon.a, libpgport.a and related.
500 # Those are only used when building e.g. extensions, so go to $dev.
501 moveToOutput "lib/*.a" "$dev"
502 ''
503 + lib.optionalString jitSupport ''
504 # In the case of JIT support, prevent useless dependencies on header files
505 find "$out/lib" -iname '*.bc' -type f -exec nuke-refs '{}' +
506
507 # Stop lib depending on the -dev output of llvm
508 remove-references-to -t ${llvmPackages.llvm.dev} "$out/lib/llvmjit${dlSuffix}"
509
510 moveToOutput "lib/bitcode" "$jit"
511 moveToOutput "lib/llvmjit*" "$jit"
512 ''
513 + lib.optionalString stdenv'.hostPlatform.isDarwin ''
514 # The darwin specific Makefile for PGXS contains a reference to the postgres
515 # binary. Some extensions (here: postgis), which are able to set bindir correctly
516 # to their own output for installation, will then fail to find "postgres" during linking.
517 substituteInPlace "$dev/lib/pgxs/src/Makefile.port" \
518 --replace-fail '-bundle_loader $(bindir)/postgres' "-bundle_loader $out/bin/postgres"
519 ''
520 + lib.optionalString perlSupport ''
521 moveToOutput "lib/*plperl*" "$plperl"
522 moveToOutput "share/postgresql/extension/*plperl*" "$plperl"
523 ''
524 + lib.optionalString pythonSupport ''
525 moveToOutput "lib/*plpython3*" "$plpython3"
526 moveToOutput "share/postgresql/extension/*plpython3*" "$plpython3"
527 ''
528 + lib.optionalString tclSupport ''
529 moveToOutput "lib/*pltcl*" "$pltcl"
530 moveToOutput "share/postgresql/extension/*pltcl*" "$pltcl"
531 '';
532
533 postFixup = lib.optionalString stdenv'.hostPlatform.isGnu ''
534 # initdb needs access to "locale" command from glibc.
535 wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
536 '';
537
538 # Running tests as "install check" to work around SIP issue on macOS:
539 # https://www.postgresql.org/message-id/flat/4D8E1BC5-BBCF-4B19-8226-359201EA8305%40gmail.com
540 # Also see <nixpkgs>/doc/stdenv/platform-notes.chapter.md
541 doCheck = false;
542 doInstallCheck =
543 !(stdenv'.hostPlatform.isStatic)
544 &&
545 # Tests currently can't be run on darwin, because of a Nix bug:
546 # https://github.com/NixOS/nix/issues/12548
547 # https://git.lix.systems/lix-project/lix/issues/691
548 # The error appears as this in the initdb logs:
549 # FATAL: could not create shared memory segment: Cannot allocate memory
550 # Don't let yourself be fooled when trying to remove this condition: Running
551 # the tests works fine most of the time. But once the tests (or any package using
552 # postgresqlTestHook) fails on the same machine for a few times, enough IPC objects
553 # will be stuck around, and any future builds with the tests enabled *will* fail.
554 !(stdenv'.hostPlatform.isDarwin)
555 &&
556 # Regression tests currently fail in pkgsMusl because of a difference in EXPLAIN output.
557 !(stdenv'.hostPlatform.isMusl)
558 &&
559 # Modifying block sizes is expected to break regression tests.
560 # https://www.postgresql.org/message-id/E1TJOeZ-000717-Lg%40wrigleys.postgresql.org
561 (withBlocksize == null && withWalBlocksize == null);
562 installCheckTarget = "check-world";
563
564 passthru =
565 let
566 this = self.callPackage generic args;
567 in
568 {
569 inherit dlSuffix;
570
571 psqlSchema = lib.versions.major version;
572
573 withJIT = this.withPackages (_: [ this.jit ]);
574 withoutJIT = this;
575
576 pkgs =
577 let
578 scope = {
579 inherit
580 jitSupport
581 pythonSupport
582 perlSupport
583 tclSupport
584 ;
585 inherit (llvmPackages) llvm;
586 postgresql = this;
587 stdenv = stdenv';
588 postgresqlTestExtension = newSuper.callPackage ./postgresqlTestExtension.nix { };
589 postgresqlBuildExtension = newSuper.callPackage ./postgresqlBuildExtension.nix { };
590 };
591 newSelf = self // scope;
592 newSuper = {
593 callPackage = newScope (scope // this.pkgs);
594 };
595 in
596 import ./ext.nix newSelf newSuper;
597
598 withPackages = postgresqlWithPackages {
599 inherit buildEnv lib makeBinaryWrapper;
600 postgresql = this;
601 };
602
603 pg_config = buildPackages.callPackage ./pg_config.nix {
604 inherit (finalAttrs) finalPackage;
605 outputs = {
606 out = lib.getOutput "out" finalAttrs.finalPackage;
607 man = lib.getOutput "man" finalAttrs.finalPackage;
608 };
609 };
610
611 tests = {
612 postgresql = nixosTests.postgresql.postgresql.passthru.override finalAttrs.finalPackage;
613 postgresql-tls-client-cert = nixosTests.postgresql.postgresql-tls-client-cert.passthru.override finalAttrs.finalPackage;
614 postgresql-wal-receiver = nixosTests.postgresql.postgresql-wal-receiver.passthru.override finalAttrs.finalPackage;
615 pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
616 }
617 // lib.optionalAttrs jitSupport {
618 postgresql-jit = nixosTests.postgresql.postgresql-jit.passthru.override finalAttrs.finalPackage;
619 };
620 };
621
622 meta = with lib; {
623 homepage = "https://www.postgresql.org";
624 description = "Powerful, open source object-relational database system";
625 license = licenses.postgresql;
626 changelog = "https://www.postgresql.org/docs/release/${finalAttrs.version}/";
627 teams = [ teams.postgres ];
628 pkgConfigModules = [
629 "libecpg"
630 "libecpg_compat"
631 "libpgtypes"
632 "libpq"
633 ];
634 platforms = platforms.unix;
635
636 # JIT support doesn't work with cross-compilation. It is attempted to build LLVM-bytecode
637 # (`%.bc` is the corresponding `make(1)`-rule) for each sub-directory in `backend/` for
638 # the JIT apparently, but with a $(CLANG) that can produce binaries for the build, not the
639 # host-platform.
640 #
641 # I managed to get a cross-build with JIT support working with
642 # `depsBuildBuild = [ llvmPackages.clang ] ++ buildInputs`, but considering that the
643 # resulting LLVM IR isn't platform-independent this doesn't give you much.
644 # In fact, I tried to test the result in a VM-test, but as soon as JIT was used to optimize
645 # a query, postgres would coredump with `Illegal instruction`.
646 #
647 # Note: This is "host canExecute build" on purpose, since this is about the LLVM that is called
648 # to do JIT at **runtime**.
649 broken = jitSupport && !stdenv.hostPlatform.canExecute stdenv.buildPlatform;
650 };
651 });
652
653 postgresqlWithPackages =
654 {
655 postgresql,
656 buildEnv,
657 lib,
658 makeBinaryWrapper,
659 }:
660 f:
661 let
662 installedExtensions = f postgresql.pkgs;
663 finalPackage = buildEnv {
664 name = "${postgresql.pname}-and-plugins-${postgresql.version}";
665 paths = installedExtensions ++ [
666 # consider keeping in-sync with `postBuild` below
667 postgresql
668 postgresql.man # in case user installs this into environment
669 ];
670
671 pathsToLink = [
672 "/"
673 "/bin"
674 "/share/postgresql/extension"
675 # Unbreaks Omnigres' build system
676 "/share/postgresql/timezonesets"
677 "/share/postgresql/tsearch_data"
678 ];
679
680 nativeBuildInputs = [ makeBinaryWrapper ];
681 postBuild =
682 let
683 args = lib.concatMap (ext: ext.wrapperArgs or [ ]) installedExtensions;
684 in
685 ''
686 wrapProgram "$out/bin/postgres" ${lib.concatStringsSep " " args}
687 '';
688
689 passthru = {
690 inherit installedExtensions;
691 inherit (postgresql)
692 pkgs
693 psqlSchema
694 version
695 ;
696
697 pg_config = postgresql.pg_config.override {
698 outputs = {
699 out = finalPackage;
700 man = finalPackage;
701 };
702 };
703
704 withJIT = postgresqlWithPackages {
705 inherit
706 buildEnv
707 lib
708 makeBinaryWrapper
709 postgresql
710 ;
711 } (_: installedExtensions ++ [ postgresql.jit ]);
712 withoutJIT = postgresqlWithPackages {
713 inherit
714 buildEnv
715 lib
716 makeBinaryWrapper
717 postgresql
718 ;
719 } (_: lib.remove postgresql.jit installedExtensions);
720
721 withPackages =
722 f':
723 postgresqlWithPackages {
724 inherit
725 buildEnv
726 lib
727 makeBinaryWrapper
728 postgresql
729 ;
730 } (ps: installedExtensions ++ f' ps);
731 };
732 };
733 in
734 finalPackage;
735
736in
737# passed by <major>.nix
738versionArgs:
739# passed by default.nix
740{ self, ... }@defaultArgs:
741self.callPackage generic (defaultArgs // versionArgs)