-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Expand file tree
/
Copy pathgeneric.nix
More file actions
468 lines (421 loc) · 15 KB
/
generic.nix
File metadata and controls
468 lines (421 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
let
generic =
# dependencies
{
stdenv,
fetchFromGitHub,
fetchpatch,
fetchurl,
lib,
makeWrapper,
glibc,
zlib,
readline,
openssl,
icu,
lz4,
zstd,
systemdLibs,
libuuid,
pkg-config,
libxml2,
tzdata,
libkrb5,
substituteAll,
darwin,
linux-pam,
bison,
flex,
perl,
docbook_xml_dtd_45,
docbook-xsl-nons,
libxslt,
removeReferencesTo,
writeShellScriptBin,
systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemdLibs,
gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic,
# for postgresql.pkgs
self,
newScope,
buildEnv,
stdenvNoCC,
postgresqlTestHook,
# source specification
version,
hash,
muslPatches ? { },
rev,
# for tests
testers,
nixosTests,
# JIT
jitSupport,
nukeReferences,
llvmPackages,
overrideCC,
# PL/Python
pythonSupport ? false,
python3,
}@args:
let
atLeast = lib.versionAtLeast version;
olderThan = lib.versionOlder version;
lz4Enabled = atLeast "14";
zstdEnabled = atLeast "15";
dlSuffix = if olderThan "16" then ".so" else stdenv.hostPlatform.extensions.sharedLibrary;
pname = "postgresql";
stdenv' =
if jitSupport && !stdenv.cc.isClang then
overrideCC llvmPackages.stdenv (
llvmPackages.stdenv.cc.override {
# LLVM bintools are not used by default, but are needed to make -flto work below.
bintools = llvmPackages.bintools;
}
)
else
stdenv;
pg_config = writeShellScriptBin "pg_config" (builtins.readFile ./pg_config.sh);
in
stdenv'.mkDerivation (finalAttrs: {
inherit version;
pname = pname + lib.optionalString jitSupport "-jit";
src = fetchFromGitHub {
owner = "postgres";
repo = "postgres";
# rev, not tag, on purpose: allows updating when new versions
# are "stamped" a few days before release (tag).
inherit hash rev;
};
__structuredAttrs = true;
hardeningEnable = lib.optionals (!stdenv'.cc.isClang) [ "pie" ];
outputs = [
"out"
"dev"
"doc"
"lib"
"man"
];
outputChecks.out = {
disallowedReferences = [
"dev"
"doc"
"man"
];
disallowedRequisites =
[
stdenv'.cc
]
++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs))
++ lib.optionals jitSupport [
llvmPackages.llvm.out
];
};
outputChecks.lib = {
disallowedReferences = [
"out"
"dev"
"doc"
"man"
];
disallowedRequisites =
[
stdenv'.cc
]
++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs))
++ lib.optionals jitSupport [
llvmPackages.llvm.out
];
};
buildInputs =
[
zlib
readline
openssl
(libxml2.override { enableHttp = true; })
icu
libuuid
]
++ lib.optionals jitSupport [ llvmPackages.llvm ]
++ lib.optionals lz4Enabled [ lz4 ]
++ lib.optionals zstdEnabled [ zstd ]
++ lib.optionals systemdSupport [ systemdLibs ]
++ lib.optionals pythonSupport [ python3 ]
++ lib.optionals gssSupport [ libkrb5 ]
++ lib.optionals stdenv'.hostPlatform.isLinux [ linux-pam ];
nativeBuildInputs =
[
bison
docbook-xsl-nons
docbook_xml_dtd_45
flex
libxml2
libxslt
makeWrapper
perl
pkg-config
removeReferencesTo
]
++ lib.optionals jitSupport [
llvmPackages.llvm.dev
nukeReferences
];
enableParallelBuilding = true;
separateDebugInfo = true;
buildFlags = [ "world" ];
# libpgcommon.a and libpgport.a contain all paths returned by pg_config and are linked
# into all binaries. However, almost no binaries actually use those paths. The following
# flags will remove unused sections from all shared libraries and binaries - including
# those paths. This avoids a lot of circular dependency problems with different outputs,
# and allows splitting them cleanly.
env = {
CFLAGS =
"-fdata-sections -ffunction-sections"
+ (if stdenv'.cc.isClang then " -flto" else " -fmerge-constants -Wl,--gc-sections");
} // lib.optionalAttrs pythonSupport { PYTHON = "${python3}/bin/python"; };
configureFlags =
[
"--with-openssl"
"--with-libxml"
"--with-icu"
"--sysconfdir=/etc"
"--with-system-tzdata=${tzdata}/share/zoneinfo"
"--enable-debug"
(lib.optionalString systemdSupport "--with-systemd")
"--with-uuid=e2fs"
]
++ lib.optionals lz4Enabled [ "--with-lz4" ]
++ lib.optionals zstdEnabled [ "--with-zstd" ]
++ lib.optionals gssSupport [ "--with-gssapi" ]
++ lib.optionals pythonSupport [ "--with-python" ]
++ lib.optionals jitSupport [ "--with-llvm" ]
++ lib.optionals stdenv'.hostPlatform.isLinux [ "--with-pam" ]
# This could be removed once the upstream issue is resolved:
# https://postgr.es/m/flat/427c7c25-e8e1-4fc5-a1fb-01ceff185e5b%40technowledgy.de
++ lib.optionals (stdenv'.hostPlatform.isDarwin && atLeast "16") [
"LDFLAGS_EX_BE=-Wl,-export_dynamic"
]
++ lib.optionals (atLeast "17") [ "--without-perl" ];
patches =
[
(
if atLeast "16" then
./patches/relative-to-symlinks-16+.patch
else
./patches/relative-to-symlinks.patch
)
(
if atLeast "15" then
./patches/empty-pg-config-view-15+.patch
else
./patches/empty-pg-config-view.patch
)
./patches/less-is-more.patch
./patches/paths-for-split-outputs.patch
./patches/paths-with-postgresql-suffix.patch
(substituteAll {
src = ./patches/locale-binary-path.patch;
locale = "${
if stdenv.hostPlatform.isDarwin then darwin.adv_cmds else lib.getBin stdenv.cc.libc
}/bin/locale";
})
]
++ lib.optionals stdenv'.hostPlatform.isMusl (
# Using fetchurl instead of fetchpatch on purpose: https://github.com/NixOS/nixpkgs/issues/240141
map fetchurl (lib.attrValues muslPatches)
)
++ lib.optionals stdenv'.hostPlatform.isLinux [
./patches/socketdir-in-run-13+.patch
]
++ lib.optionals (stdenv'.hostPlatform.isDarwin && olderThan "16") [
./patches/export-dynamic-darwin-15-.patch
];
installTargets = [ "install-world" ];
postPatch = ''
substituteInPlace "src/Makefile.global.in" --subst-var out
# Hardcode the path to pgxs so pg_config returns the path in $dev
substituteInPlace "src/common/config_info.c" --subst-var dev
'';
postInstall =
''
moveToOutput "bin/ecpg" "$dev"
moveToOutput "lib/pgxs" "$dev"
# Pretend pg_config is located in $out/bin to return correct paths, but
# actually have it in -dev to avoid pulling in all other outputs. See the
# pg_config.sh script's comments for details.
moveToOutput "bin/pg_config" "$dev"
install -c -m 755 "${pg_config}"/bin/pg_config "$out/bin/pg_config"
wrapProgram "$dev/bin/pg_config" --argv0 "$out/bin/pg_config"
# postgres exposes external symbols get_pkginclude_path and similar. Those
# can't be stripped away by --gc-sections/LTO, because they could theoretically
# be used by dynamically loaded modules / extensions. To avoid circular dependencies,
# references to -dev, -doc and -man are removed here. References to -lib must be kept,
# because there is a realistic use-case for extensions to locate the /lib directory to
# load other shared modules.
remove-references-to -t "$dev" -t "$doc" -t "$man" "$out/bin/postgres"
if [ -z "''${dontDisableStatic:-}" ]; then
# Remove static libraries in case dynamic are available.
for i in $lib/lib/*.a; do
name="$(basename "$i")"
ext="${stdenv'.hostPlatform.extensions.sharedLibrary}"
if [ -e "$lib/lib/''${name%.a}$ext" ] || [ -e "''${i%.a}$ext" ]; then
rm "$i"
fi
done
fi
# The remaining static libraries are libpgcommon.a, libpgport.a and related.
# Those are only used when building e.g. extensions, so go to $dev.
moveToOutput "lib/*.a" "$dev"
''
+ lib.optionalString jitSupport ''
# In the case of JIT support, prevent useless dependencies on header files
find "$out/lib" -iname '*.bc' -type f -exec nuke-refs '{}' +
# Stop lib depending on the -dev output of llvm
remove-references-to -t ${llvmPackages.llvm.dev} "$out/lib/llvmjit${dlSuffix}"
'';
postFixup =
lib.optionalString stdenv'.hostPlatform.isGnu ''
# initdb needs access to "locale" command from glibc.
wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
''
+ lib.optionalString pythonSupport ''
wrapProgram "$out/bin/postgres" --set PYTHONPATH "${python3}/${python3.sitePackages}"
'';
doCheck = !stdenv'.hostPlatform.isDarwin;
# autodetection doesn't seem to able to find this, but it's there.
checkTarget = "check-world";
passthru =
let
this = self.callPackage generic args;
jitToggle = this.override {
jitSupport = !jitSupport;
};
in
{
inherit dlSuffix;
psqlSchema = lib.versions.major version;
withJIT = if jitSupport then this else jitToggle;
withoutJIT = if jitSupport then jitToggle else this;
pkgs =
let
scope = {
inherit jitSupport;
inherit (llvmPackages) llvm;
postgresql = this;
stdenv = stdenv';
postgresqlTestExtension =
{
finalPackage,
withPackages ? [ ],
...
}@extraArgs:
stdenvNoCC.mkDerivation (
{
name = "${finalPackage.name}-test-extension";
dontUnpack = true;
doCheck = true;
nativeCheckInputs = [
postgresqlTestHook
(this.withPackages (ps: [ finalPackage ] ++ (map (p: ps."${p}") withPackages)))
];
failureHook = "postgresqlStop";
postgresqlTestUserOptions = "LOGIN SUPERUSER";
passAsFile = [ "sql" ];
checkPhase = ''
runHook preCheck
psql -a -v ON_ERROR_STOP=1 -f "$sqlPath"
runHook postCheck
'';
installPhase = "touch $out";
}
// extraArgs
);
buildPostgresqlExtension = newSuper.callPackage ./buildPostgresqlExtension.nix { };
};
newSelf = self // scope;
newSuper = {
callPackage = newScope (scope // this.pkgs);
};
in
import ./ext newSelf newSuper;
withPackages = postgresqlWithPackages {
inherit buildEnv;
postgresql = this;
};
tests =
{
postgresql = nixosTests.postgresql.postgresql.passthru.override finalAttrs.finalPackage;
postgresql-tls-client-cert = nixosTests.postgresql.postgresql-tls-client-cert.passthru.override finalAttrs.finalPackage;
postgresql-wal-receiver = nixosTests.postgresql.postgresql-wal-receiver.passthru.override finalAttrs.finalPackage;
pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
}
// lib.optionalAttrs jitSupport {
postgresql-jit = nixosTests.postgresql.postgresql-jit.passthru.override finalAttrs.finalPackage;
};
};
meta = with lib; {
homepage = "https://www.postgresql.org";
description = "Powerful, open source object-relational database system";
license = licenses.postgresql;
changelog = "https://www.postgresql.org/docs/release/${finalAttrs.version}/";
maintainers =
with maintainers;
[
globin
ivan
]
++ teams.postgres.members;
pkgConfigModules = [
"libecpg"
"libecpg_compat"
"libpgtypes"
"libpq"
];
platforms = platforms.unix;
# JIT support doesn't work with cross-compilation. It is attempted to build LLVM-bytecode
# (`%.bc` is the corresponding `make(1)`-rule) for each sub-directory in `backend/` for
# the JIT apparently, but with a $(CLANG) that can produce binaries for the build, not the
# host-platform.
#
# I managed to get a cross-build with JIT support working with
# `depsBuildBuild = [ llvmPackages.clang ] ++ buildInputs`, but considering that the
# resulting LLVM IR isn't platform-independent this doesn't give you much.
# In fact, I tried to test the result in a VM-test, but as soon as JIT was used to optimize
# a query, postgres would coredump with `Illegal instruction`.
broken = jitSupport && !stdenv.hostPlatform.canExecute stdenv.buildPlatform;
};
});
postgresqlWithPackages =
{ postgresql, buildEnv }:
f:
let
installedExtensions = f postgresql.pkgs;
in
buildEnv {
name = "${postgresql.pname}-and-plugins-${postgresql.version}";
paths = installedExtensions ++ [
postgresql
postgresql.man # in case user installs this into environment
];
pathsToLink = [ "/" ];
passthru = {
inherit installedExtensions;
inherit (postgresql)
psqlSchema
version
;
withJIT = postgresqlWithPackages {
inherit buildEnv;
postgresql = postgresql.withJIT;
} f;
withoutJIT = postgresqlWithPackages {
inherit buildEnv;
postgresql = postgresql.withoutJIT;
} f;
};
};
in
# passed by <major>.nix
versionArgs:
# passed by default.nix
{ self, ... }@defaultArgs:
self.callPackage generic (defaultArgs // versionArgs)