2024-04-21 15:54:59 +00:00
let
generic =
# dependencies
{ stdenv , lib , fetchurl , makeWrapper , fetchpatch
, glibc , zlib , readline , openssl , icu , lz4 , zstd , systemd , libossp_uuid
, pkg-config , libxml2 , tzdata , libkrb5 , substituteAll , darwin
, linux-pam
# This is important to obtain a version of `libpq` that does not depend on systemd.
, systemdSupport ? lib . meta . availableOn stdenv . hostPlatform systemd && ! stdenv . hostPlatform . isStatic
, 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-05-15 15:35:15 +00:00
, testers , nixosTests
2024-04-21 15:54:59 +00:00
# JIT
, jitSupport
, nukeReferences , patchelf , llvmPackages
# 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 " 1 4 " ;
zstdEnabled = atLeast " 1 5 " ;
systemdSupport' = if enableSystemd == null then systemdSupport else ( lib . warn " p o s t g r e s q l : a r g u m e n t e n a b l e S y s t e m d i s d e p r e c a t e d , p l e a s e u s e s y s t e m d S u p p o r t i n s t e a d . " enableSystemd ) ;
pname = " p o s t g r e s q l " ;
stdenv' = if jitSupport then llvmPackages . stdenv else stdenv ;
in stdenv' . mkDerivation ( finalAttrs : {
2024-05-15 15:35:15 +00:00
inherit version ;
pname = pname + lib . optionalString jitSupport " - j i t " ;
2024-04-21 15:54:59 +00:00
src = fetchurl {
url = " m i r r o r : / / p o s t g r e s q l / s o u r c e / v ${ version } / ${ pname } - ${ version } . t a r . b z 2 " ;
inherit hash ;
} ;
hardeningEnable = lib . optionals ( ! stdenv' . cc . isClang ) [ " p i e " ] ;
outputs = [ " o u t " " l i b " " d o c " " m a n " ] ;
setOutputFlags = false ; # $out retains configureFlags :-/
buildInputs = [
zlib
readline
openssl
libxml2
icu
]
++ lib . optionals ( olderThan " 1 3 " ) [ libxcrypt ]
++ lib . optionals jitSupport [ llvmPackages . llvm ]
++ lib . optionals lz4Enabled [ lz4 ]
++ lib . optionals zstdEnabled [ zstd ]
++ lib . optionals systemdSupport' [ systemd ]
++ lib . optionals pythonSupport [ python3 ]
++ lib . optionals gssSupport [ libkrb5 ]
++ lib . optionals stdenv' . isLinux [ linux-pam ]
++ lib . optionals ( ! stdenv' . isDarwin ) [ libossp_uuid ] ;
nativeBuildInputs = [
makeWrapper
pkg-config
]
++ lib . optionals jitSupport [ llvmPackages . llvm . dev nukeReferences patchelf ] ;
2024-05-15 15:35:15 +00:00
enableParallelBuilding = true ;
2024-04-21 15:54:59 +00:00
separateDebugInfo = true ;
buildFlags = [ " w o r l d " ] ;
2024-05-15 15:35:15 +00:00
# Makes cross-compiling work when xml2-config can't be executed on the host.
# Fixed upstream in https://github.com/postgres/postgres/commit/0bc8cebdb889368abdf224aeac8bc197fe4c9ae6
env . NIX_CFLAGS_COMPILE = lib . optionalString ( olderThan " 1 3 " ) " - I ${ libxml2 . dev } / i n c l u d e / l i b x m l 2 " ;
2024-04-21 15:54:59 +00:00
configureFlags = [
" - - w i t h - o p e n s s l "
" - - w i t h - l i b x m l "
" - - w i t h - i c u "
" - - s y s c o n f d i r = / e t c "
" - - l i b d i r = $ ( l i b ) / l i b "
" - - w i t h - s y s t e m - t z d a t a = ${ tzdata } / s h a r e / z o n e i n f o "
" - - e n a b l e - d e b u g "
( lib . optionalString systemdSupport' " - - w i t h - s y s t e m d " )
( if stdenv' . isDarwin then " - - w i t h - u u i d = e 2 f s " else " - - w i t h - o s s p - u u i d " )
] ++ lib . optionals lz4Enabled [ " - - w i t h - l z 4 " ]
++ lib . optionals zstdEnabled [ " - - w i t h - z s t d " ]
++ lib . optionals gssSupport [ " - - w i t h - g s s a p i " ]
++ lib . optionals pythonSupport [ " - - w i t h - p y t h o n " ]
++ lib . optionals jitSupport [ " - - w i t h - l l v m " ]
++ lib . optionals stdenv' . isLinux [ " - - w i t h - p a m " ] ;
patches = [
2024-05-15 15:35:15 +00:00
( if atLeast " 1 6 " then ./patches/relative-to-symlinks-16+.patch else ./patches/relative-to-symlinks.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
2024-04-21 15:54:59 +00:00
./patches/specify_pkglibdir_at_runtime.patch
2024-05-15 15:35:15 +00:00
./patches/paths-with-postgresql-suffix.patch
2024-04-21 15:54:59 +00:00
( substituteAll {
src = ./patches/locale-binary-path.patch ;
locale = " ${ if stdenv . isDarwin then darwin . adv_cmds else lib . getBin stdenv . cc . libc } / b i n / l o c a l e " ;
} )
] ++ 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' . isLinux [
2024-05-15 15:35:15 +00:00
( if atLeast " 1 3 " then ./patches/socketdir-in-run-13+.patch else ./patches/socketdir-in-run.patch )
2024-04-21 15:54:59 +00:00
] ;
installTargets = [ " i n s t a l l - w o r l d " ] ;
postPatch = ''
# Hardcode the path to pgxs so pg_config returns the path in $out
2024-05-15 15:35:15 +00:00
substituteInPlace " s r c / c o m m o n / c o n f i g _ i n f o . c " - - subst-var out
2024-04-21 15:54:59 +00:00
'' + l i b . o p t i o n a l S t r i n g j i t S u p p o r t ''
# Force lookup of jit stuff in $out instead of $lib
substituteInPlace src/backend/jit/jit.c - - replace pkglib_path \ " $ o u t / l i b \"
substituteInPlace src/backend/jit/llvm/llvmjit.c - - replace pkglib_path \ " $ o u t / l i b \"
substituteInPlace src/backend/jit/llvm/llvmjit_inline.cpp - - replace pkglib_path \ " $ o u t / l i b \"
'' ;
postInstall =
''
moveToOutput " l i b / p g x s " " $ o u t " # looks strange, but not deleting it
moveToOutput " l i b / l i b p g c o m m o n * . a " " $ o u t "
moveToOutput " l i b / l i b p g p o r t * . a " " $ o u t "
moveToOutput " l i b / l i b e c p g * " " $ o u t "
# Prevent a retained dependency on gcc-wrapper.
substituteInPlace " $ o u t / l i b / p g x s / s r c / M a k e f i l e . g l o b a l " - - replace $ { stdenv' . cc } /bin/ld ld
if [ - z " ' ' ${ dontDisableStatic:- } " ] ; then
# Remove static libraries in case dynamic are available.
for i in $ out/lib /* . a $ l i b / l i b / * . a ; d o
name = " $ ( b a s e n a m e " $ i " ) "
ext = " ${ stdenv' . hostPlatform . extensions . sharedLibrary } "
if [ - e " $ l i b / l i b / ' ' ${ name % . a } $ e x t " ] || [ - e " ' ' ${ i % . a } $ e x t " ] ; then
rm " $ i "
fi
done
fi
'' + l i b . o p t i o n a l S t r i n g j i t S u p p o r t ''
# Move the bitcode and libllvmjit.so library out of $lib; otherwise, every client that
# depends on libpq.so will also have libLLVM.so in its closure too, bloating it
moveToOutput " l i b / b i t c o d e " " $ o u t "
moveToOutput " l i b / l l v m j i t * " " $ o u t "
# In the case of JIT support, prevent a retained dependency on clang-wrapper
substituteInPlace " $ o u t / l i b / p g x s / s r c / M a k e f i l e . g l o b a l " - - replace $ { stdenv' . cc } /bin/clang clang
nuke-refs $ out/lib/llvmjit_types.bc $ ( find $ out/lib/bitcode - type f )
# Stop out depending on the default output of llvm
substituteInPlace $ out/lib/pgxs/src/Makefile.global \
- - replace $ { llvmPackages . llvm . out } /bin " " \
- - replace ' $ ( LLVM_BINPATH ) / ' " "
# Stop out depending on the -dev output of llvm
substituteInPlace $ out/lib/pgxs/src/Makefile.global \
- - replace $ { llvmPackages . llvm . dev } /bin/llvm-config llvm-config \
- - replace - I $ { llvmPackages . llvm . dev } /include " "
$ { lib . optionalString ( ! stdenv' . isDarwin ) ''
# Stop lib depending on the -dev output of llvm
rpath = $ ( patchelf - - print-rpath $ out/lib/llvmjit.so )
nuke-refs - e $ out $ out/lib/llvmjit.so
# Restore the correct rpath
patchelf $ out/lib/llvmjit.so - - set-rpath " $ r p a t h "
'' }
'' ;
postFixup = lib . optionalString ( ! stdenv' . isDarwin && stdenv' . hostPlatform . libc == " g l i b c " )
''
# initdb needs access to "locale" command from glibc.
wrapProgram $ out/bin/initdb - - prefix PATH " : " $ { glibc . bin } /bin
'' ;
doCheck = ! stdenv' . isDarwin ;
# autodetection doesn't seem to able to find this, but it's there.
checkTarget = " c h e c k " ;
2024-05-15 15:35:15 +00:00
# TODO: Remove after the next set of minor releases on May 9th 2024
2024-04-21 15:54:59 +00:00
preCheck =
# On musl, comment skip the following tests, because they break due to
# ! 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)
# See also here:
# https://git.alpinelinux.org/aports/tree/main/postgresql/disable-broken-tests.patch?id=6d7d32c12e073a57a9e5946e55f4c1fbb68bd442
if stdenv' . hostPlatform . isMusl then ''
substituteInPlace src/test/regress/parallel_schedule \
- - replace " s u b s c r i p t i o n " " " \
- - replace " o b j e c t _ a d d r e s s " " "
'' e l s e n u l l ;
disallowedReferences = [ stdenv' . cc ] ;
passthru = let
this = self . callPackage generic args ;
jitToggle = this . override {
jitSupport = ! jitSupport ;
} ;
in
{
psqlSchema = lib . versions . major version ;
withJIT = if jitSupport then this else jitToggle ;
withoutJIT = if jitSupport then jitToggle else this ;
dlSuffix = if olderThan " 1 6 " then " . s o " else stdenv . hostPlatform . extensions . sharedLibrary ;
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 {
inherit makeWrapper buildEnv ;
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 = " h t t p s : / / w w w . p o s t g r e s q l . o r g " ;
description = " A p o w e r f u l , o p e n s o u r c e o b j e c t - r e l a t i o n a l d a t a b a s e s y s t e m " ;
license = licenses . postgresql ;
changelog = " h t t p s : / / w w w . p o s t g r e s q l . o r g / d o c s / r e l e a s e / ${ finalAttrs . version } / " ;
2024-05-15 15:35:15 +00:00
maintainers = with maintainers ; [ thoughtpolice danbst globin ivan ma27 ] ;
2024-04-21 15:54:59 +00:00
pkgConfigModules = [ " l i b e c p g " " l i b e c p g _ c o m p a t " " l i b p g t y p e s " " l i b p q " ] ;
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-05-15 15:35:15 +00:00
broken = ( jitSupport && stdenv . hostPlatform != stdenv . buildPlatform )
# Allmost all tests fail FATAL errors for v12 and v13
|| ( jitSupport && stdenv . hostPlatform . isMusl && olderThan " 1 4 " ) ;
2024-04-21 15:54:59 +00:00
} ;
} ) ;
postgresqlWithPackages = { postgresql , makeWrapper , buildEnv }: pkgs : f : buildEnv {
name = " p o s t g r e s q l - a n d - p l u g i n s - ${ postgresql . version } " ;
paths = f pkgs ++ [
postgresql
postgresql . lib
postgresql . man # in case user installs this into environment
] ;
nativeBuildInputs = [ makeWrapper ] ;
# We include /bin to ensure the $out/bin directory is created, which is
# needed because we'll be removing the files from that directory in postBuild
# below. See #22653
pathsToLink = [ " / " " / b i n " ] ;
# Note: the duplication of executables is about 4MB size.
# So a nicer solution was patching postgresql to allow setting the
# libdir explicitly.
postBuild = ''
mkdir - p $ out/bin
rm $ out/bin / { pg_config , postgres , pg_ctl }
cp - - target-directory = $ out/bin $ { postgresql } /bin / { postgres , pg_config , pg_ctl }
wrapProgram $ out/bin/postgres - - set NIX_PGLIBDIR $ out/lib
'' ;
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 )