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