2024-04-21 15:54:59 +00:00
|
|
|
let
|
|
|
|
|
|
|
|
generic =
|
|
|
|
# dependencies
|
2024-07-31 10:19:44 +00:00
|
|
|
{ stdenv, lib, fetchurl, fetchpatch, makeWrapper
|
2024-09-19 14:19:46 +00:00
|
|
|
, glibc, zlib, readline, openssl, icu, lz4, zstd, systemdLibs, libossp_uuid
|
2024-04-21 15:54:59 +00:00
|
|
|
, pkg-config, libxml2, tzdata, libkrb5, substituteAll, darwin
|
|
|
|
, linux-pam
|
2024-10-11 05:15:48 +00:00
|
|
|
|
|
|
|
, removeReferencesTo, writeShellApplication
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
# This is important to obtain a version of `libpq` that does not depend on systemd.
|
2024-09-19 14:19:46 +00:00
|
|
|
, systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemdLibs && !stdenv.hostPlatform.isStatic
|
2024-04-21 15:54:59 +00:00
|
|
|
, enableSystemd ? null
|
|
|
|
, gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic
|
|
|
|
|
|
|
|
# for postgresql.pkgs
|
|
|
|
, self, newScope, buildEnv
|
|
|
|
|
|
|
|
# source specification
|
2024-05-15 15:35:15 +00:00
|
|
|
, version, hash, muslPatches ? {}
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
# for tests
|
2024-09-19 14:19:46 +00:00
|
|
|
, testers
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
# JIT
|
|
|
|
, jitSupport
|
2024-09-19 14:19:46 +00:00
|
|
|
, nukeReferences, llvmPackages, overrideCC
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
# PL/Python
|
|
|
|
, pythonSupport ? false
|
|
|
|
, python3
|
|
|
|
|
|
|
|
# detection of crypt fails when using llvm stdenv, so we add it manually
|
|
|
|
# for <13 (where it got removed: https://github.com/postgres/postgres/commit/c45643d618e35ec2fe91438df15abd4f3c0d85ca)
|
|
|
|
, libxcrypt
|
|
|
|
} @args:
|
|
|
|
let
|
|
|
|
atLeast = lib.versionAtLeast version;
|
|
|
|
olderThan = lib.versionOlder version;
|
|
|
|
lz4Enabled = atLeast "14";
|
|
|
|
zstdEnabled = atLeast "15";
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
dlSuffix = if olderThan "16" then ".so" else stdenv.hostPlatform.extensions.sharedLibrary;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
systemdSupport' = if enableSystemd == null then systemdSupport else (lib.warn "postgresql: argument enableSystemd is deprecated, please use systemdSupport instead." enableSystemd);
|
|
|
|
|
|
|
|
pname = "postgresql";
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
stdenv' =
|
2024-09-26 11:04:55 +00:00
|
|
|
if jitSupport && !stdenv.cc.isClang then
|
2024-09-19 14:19:46 +00:00
|
|
|
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;
|
2024-10-11 05:15:48 +00:00
|
|
|
|
|
|
|
pg_config = writeShellApplication {
|
|
|
|
name = "pg_config";
|
|
|
|
text = builtins.readFile ./pg_config.sh;
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
in stdenv'.mkDerivation (finalAttrs: {
|
2024-05-15 15:35:15 +00:00
|
|
|
inherit version;
|
|
|
|
pname = pname + lib.optionalString jitSupport "-jit";
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
src = fetchurl {
|
|
|
|
url = "mirror://postgresql/source/v${version}/${pname}-${version}.tar.bz2";
|
|
|
|
inherit hash;
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
__structuredAttrs = true;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
hardeningEnable = lib.optionals (!stdenv'.cc.isClang) [ "pie" ];
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
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
|
|
|
|
];
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
buildInputs = [
|
|
|
|
zlib
|
|
|
|
readline
|
|
|
|
openssl
|
2024-09-19 14:19:46 +00:00
|
|
|
(libxml2.override {enableHttp = true;})
|
2024-04-21 15:54:59 +00:00
|
|
|
icu
|
|
|
|
]
|
|
|
|
++ lib.optionals (olderThan "13") [ libxcrypt ]
|
|
|
|
++ lib.optionals jitSupport [ llvmPackages.llvm ]
|
|
|
|
++ lib.optionals lz4Enabled [ lz4 ]
|
|
|
|
++ lib.optionals zstdEnabled [ zstd ]
|
2024-09-19 14:19:46 +00:00
|
|
|
++ lib.optionals systemdSupport' [ systemdLibs ]
|
2024-04-21 15:54:59 +00:00
|
|
|
++ lib.optionals pythonSupport [ python3 ]
|
|
|
|
++ lib.optionals gssSupport [ libkrb5 ]
|
2024-09-26 11:04:55 +00:00
|
|
|
++ lib.optionals stdenv'.hostPlatform.isLinux [ linux-pam ]
|
|
|
|
++ lib.optionals (!stdenv'.hostPlatform.isDarwin) [ libossp_uuid ];
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
nativeBuildInputs = [
|
|
|
|
makeWrapper
|
|
|
|
pkg-config
|
2024-09-19 14:19:46 +00:00
|
|
|
removeReferencesTo
|
2024-04-21 15:54:59 +00:00
|
|
|
]
|
2024-09-19 14:19:46 +00:00
|
|
|
++ lib.optionals jitSupport [ llvmPackages.llvm.dev nukeReferences ];
|
2024-04-21 15:54:59 +00:00
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
enableParallelBuilding = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
separateDebugInfo = true;
|
|
|
|
|
|
|
|
buildFlags = [ "world" ];
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
# 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")
|
|
|
|
# Makes cross-compiling work when xml2-config can't be executed on the host.
|
|
|
|
# Fixed upstream in https://github.com/postgres/postgres/commit/0bc8cebdb889368abdf224aeac8bc197fe4c9ae6
|
|
|
|
+ lib.optionalString (olderThan "13") " -I${libxml2.dev}/include/libxml2";
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
configureFlags = [
|
|
|
|
"--with-openssl"
|
|
|
|
"--with-libxml"
|
|
|
|
"--with-icu"
|
|
|
|
"--sysconfdir=/etc"
|
|
|
|
"--with-system-tzdata=${tzdata}/share/zoneinfo"
|
|
|
|
"--enable-debug"
|
|
|
|
(lib.optionalString systemdSupport' "--with-systemd")
|
2024-09-26 11:04:55 +00:00
|
|
|
(if stdenv'.hostPlatform.isDarwin then "--with-uuid=e2fs" else "--with-ossp-uuid")
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ 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" ]
|
2024-09-26 11:04:55 +00:00
|
|
|
++ lib.optionals stdenv'.hostPlatform.isLinux [ "--with-pam" ]
|
2024-09-19 14:19:46 +00:00
|
|
|
# This could be removed once the upstream issue is resolved:
|
|
|
|
# https://postgr.es/m/flat/427c7c25-e8e1-4fc5-a1fb-01ceff185e5b%40technowledgy.de
|
2024-09-26 11:04:55 +00:00
|
|
|
++ lib.optionals (stdenv'.hostPlatform.isDarwin && atLeast "16") [ "LDFLAGS_EX_BE=-Wl,-export_dynamic" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
patches = [
|
2024-05-15 15:35:15 +00:00
|
|
|
(if atLeast "16" then ./patches/relative-to-symlinks-16+.patch else ./patches/relative-to-symlinks.patch)
|
2024-09-19 14:19:46 +00:00
|
|
|
(if atLeast "15" then ./patches/empty-pg-config-view-15+.patch else ./patches/empty-pg-config-view.patch)
|
2024-04-21 15:54:59 +00:00
|
|
|
./patches/less-is-more.patch
|
2024-05-15 15:35:15 +00:00
|
|
|
./patches/paths-for-split-outputs.patch
|
|
|
|
./patches/paths-with-postgresql-suffix.patch
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
(substituteAll {
|
|
|
|
src = ./patches/locale-binary-path.patch;
|
2024-09-26 11:04:55 +00:00
|
|
|
locale = "${if stdenv.hostPlatform.isDarwin then darwin.adv_cmds else lib.getBin stdenv.cc.libc}/bin/locale";
|
2024-04-21 15:54:59 +00:00
|
|
|
})
|
2024-10-11 05:15:48 +00:00
|
|
|
|
|
|
|
# TODO: Remove this with the next set of minor releases
|
|
|
|
(fetchpatch (
|
|
|
|
if atLeast "14" then {
|
|
|
|
url = "https://github.com/postgres/postgres/commit/b27622c90869aab63cfe22159a459c57768b0fa4.patch";
|
|
|
|
hash = "sha256-7G+BkJULhyx6nlMEjClcr2PJg6awgymZHr2JgGhXanA=";
|
|
|
|
excludes = [ "doc/*" ];
|
|
|
|
} else if atLeast "13" then {
|
|
|
|
url = "https://github.com/postgres/postgres/commit/b28b9b19bbe3410da4a805ef775e0383a66af314.patch";
|
|
|
|
hash = "sha256-meFFskNWlcc/rv4BWo6fNR/tTFgQRgXGqTkJkoX7lHU=";
|
|
|
|
excludes = [ "doc/*" ];
|
|
|
|
} else {
|
|
|
|
url = "https://github.com/postgres/postgres/commit/205813da4c264d80db3c3215db199cc119e18369.patch";
|
|
|
|
hash = "sha256-L8/ns/fxTh2ayfDQXtBIKaArFhMd+v86UxVFWQdmzUw=";
|
|
|
|
excludes = [ "doc/*" ];
|
|
|
|
})
|
|
|
|
)
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ lib.optionals stdenv'.hostPlatform.isMusl (
|
|
|
|
# Using fetchurl instead of fetchpatch on purpose: https://github.com/NixOS/nixpkgs/issues/240141
|
|
|
|
map fetchurl (lib.attrValues muslPatches)
|
2024-09-26 11:04:55 +00:00
|
|
|
) ++ lib.optionals stdenv'.hostPlatform.isLinux [
|
2024-05-15 15:35:15 +00:00
|
|
|
(if atLeast "13" then ./patches/socketdir-in-run-13+.patch else ./patches/socketdir-in-run.patch)
|
2024-09-26 11:04:55 +00:00
|
|
|
] ++ lib.optionals (stdenv'.hostPlatform.isDarwin && olderThan "16") [
|
2024-09-19 14:19:46 +00:00
|
|
|
./patches/export-dynamic-darwin-15-.patch
|
2024-04-21 15:54:59 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
installTargets = [ "install-world" ];
|
|
|
|
|
|
|
|
postPatch = ''
|
2024-09-19 14:19:46 +00:00
|
|
|
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
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
postInstall =
|
|
|
|
''
|
2024-09-19 14:19:46 +00:00
|
|
|
moveToOutput "bin/ecpg" "$dev"
|
|
|
|
moveToOutput "lib/pgxs" "$dev"
|
|
|
|
|
|
|
|
# Pretend pg_config is located in $out/bin to return correct paths, but
|
2024-10-11 05:15:48 +00:00
|
|
|
# actually have it in -dev to avoid pulling in all other outputs. See the
|
|
|
|
# pg_config.sh script's comments for details.
|
2024-09-19 14:19:46 +00:00
|
|
|
moveToOutput "bin/pg_config" "$dev"
|
2024-10-11 05:15:48 +00:00
|
|
|
install -c -m 755 "${pg_config}"/bin/pg_config "$out/bin/pg_config"
|
2024-09-19 14:19:46 +00:00
|
|
|
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"
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
if [ -z "''${dontDisableStatic:-}" ]; then
|
|
|
|
# Remove static libraries in case dynamic are available.
|
2024-09-19 14:19:46 +00:00
|
|
|
for i in $lib/lib/*.a; do
|
2024-04-21 15:54:59 +00:00
|
|
|
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
|
2024-09-19 14:19:46 +00:00
|
|
|
# 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"
|
2024-04-21 15:54:59 +00:00
|
|
|
'' + lib.optionalString jitSupport ''
|
2024-09-19 14:19:46 +00:00
|
|
|
# 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}"
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
postFixup = lib.optionalString stdenv'.hostPlatform.isGnu
|
2024-04-21 15:54:59 +00:00
|
|
|
''
|
|
|
|
# initdb needs access to "locale" command from glibc.
|
|
|
|
wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
|
|
|
|
'';
|
|
|
|
|
2024-09-26 11:04:55 +00:00
|
|
|
doCheck = !stdenv'.hostPlatform.isDarwin;
|
2024-04-21 15:54:59 +00:00
|
|
|
# autodetection doesn't seem to able to find this, but it's there.
|
|
|
|
checkTarget = "check";
|
|
|
|
|
|
|
|
passthru = let
|
|
|
|
this = self.callPackage generic args;
|
|
|
|
jitToggle = this.override {
|
|
|
|
jitSupport = !jitSupport;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2024-09-19 14:19:46 +00:00
|
|
|
inherit dlSuffix;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
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';
|
|
|
|
};
|
|
|
|
newSelf = self // scope;
|
|
|
|
newSuper = { callPackage = newScope (scope // this.pkgs); };
|
|
|
|
in import ./ext newSelf newSuper;
|
|
|
|
|
|
|
|
withPackages = postgresqlWithPackages {
|
2024-09-19 14:19:46 +00:00
|
|
|
inherit buildEnv;
|
2024-04-21 15:54:59 +00:00
|
|
|
postgresql = this;
|
|
|
|
}
|
|
|
|
this.pkgs;
|
|
|
|
|
|
|
|
tests = {
|
2024-05-15 15:35:15 +00:00
|
|
|
postgresql-wal-receiver = import ../../../../nixos/tests/postgresql-wal-receiver.nix {
|
|
|
|
inherit (stdenv) system;
|
|
|
|
pkgs = self;
|
|
|
|
package = this;
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
|
|
|
|
} // lib.optionalAttrs jitSupport {
|
2024-05-15 15:35:15 +00:00
|
|
|
postgresql-jit = import ../../../../nixos/tests/postgresql-jit.nix {
|
|
|
|
inherit (stdenv) system;
|
|
|
|
pkgs = self;
|
|
|
|
package = this;
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = with lib; {
|
|
|
|
homepage = "https://www.postgresql.org";
|
2024-06-20 14:57:18 +00:00
|
|
|
description = "Powerful, open source object-relational database system";
|
2024-04-21 15:54:59 +00:00
|
|
|
license = licenses.postgresql;
|
|
|
|
changelog = "https://www.postgresql.org/docs/release/${finalAttrs.version}/";
|
2024-06-20 14:57:18 +00:00
|
|
|
maintainers = with maintainers; [ thoughtpolice danbst globin ivan ma27 wolfgangwalther ];
|
2024-04-21 15:54:59 +00:00
|
|
|
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`.
|
2024-09-19 14:19:46 +00:00
|
|
|
broken = jitSupport && !stdenv.hostPlatform.canExecute stdenv.buildPlatform;
|
2024-04-21 15:54:59 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
postgresqlWithPackages = { postgresql, buildEnv }: pkgs: f: buildEnv {
|
2024-04-21 15:54:59 +00:00
|
|
|
name = "postgresql-and-plugins-${postgresql.version}";
|
|
|
|
paths = f pkgs ++ [
|
|
|
|
postgresql
|
|
|
|
postgresql.man # in case user installs this into environment
|
|
|
|
];
|
2024-09-19 14:19:46 +00:00
|
|
|
|
|
|
|
pathsToLink = ["/"];
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
passthru.version = postgresql.version;
|
|
|
|
passthru.psqlSchema = postgresql.psqlSchema;
|
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
# passed by <major>.nix
|
|
|
|
versionArgs:
|
|
|
|
# passed by default.nix
|
|
|
|
{ self, ... } @defaultArgs:
|
|
|
|
self.callPackage generic (defaultArgs // versionArgs)
|