postgresql.withPackages.pg_config: fix paths

Previously, pg_config would report the paths of the underlying
postgresql derivation and not the paths of the buildEnv that
postgresql.withPackages creates.

That's a problem when users of pg_config use it to find PostgreSQL's
sharedir, in which they'd like to find the extensions added via
withPackages. Those are only linked into the created buildEnv, but not
available in the postgresql derivation.

By providing our own nix-support/pg_config.env file, we can swap out
those paths. We also do the same for the -man output, because this
output is linked into buildEnv as well. Other paths, which are not
available in the buildEnv environment, will still link to the original
postgresql derivation. Win-win!
This commit is contained in:
Wolfgang Walther 2025-05-17 14:19:35 +02:00
parent 8ad467f6d5
commit 9a146d1244
No known key found for this signature in database
GPG Key ID: B39893FA5F65CAE1
4 changed files with 73 additions and 51 deletions

View File

@ -12,6 +12,7 @@
postgresqlBuildExtension,
postgresqlTestExtension,
python3,
stdenv,
unstableGitUpdater,
}:
@ -29,6 +30,16 @@ postgresqlBuildExtension (finalAttrs: {
hash = "sha256-LKsH+aeLg7v2RfK80D3mgXdPB8jMIv5uFdf+3c5Z0vA=";
};
# This matches postInstall of PostgreSQL's generic.nix, which does this for the PGXS Makefile.
# Since omnigres uses a CMake file, which tries to replicate the things that PGXS does, we need
# to apply the same fix for darwin.
# The reason we need to do this is, because PG_BINARY will point at the postgres wrapper of
# postgresql.withPackages, which does not contain the same symbols as the original file, ofc.
postPatch = lib.optionalString stdenv.hostPlatform.isDarwin ''
substituteInPlace "cmake/PostgreSQLExtension.cmake" \
--replace-fail '-bundle_loader ''${PG_BINARY}' "-bundle_loader ${postgresql}/bin/postgres"
'';
strictDeps = true;
nativeBuildInputs = [
@ -48,8 +59,6 @@ postgresqlBuildExtension (finalAttrs: {
cmakeFlags = [
"-DOPENSSL_CONFIGURED=1"
"-DPG_CONFIG=${pgWithExtensions.pg_config}/bin/pg_config"
"-DPostgreSQL_EXTENSION_DIR=${pgWithExtensions}/share/postgresql/extension/"
"-DPostgreSQL_PACKAGE_LIBRARY_DIR=${pgWithExtensions}/lib/"
"-DPostgreSQL_TARGET_EXTENSION_DIR=${builtins.placeholder "out"}/share/postgresql/extension/"
"-DPostgreSQL_TARGET_PACKAGE_LIBRARY_DIR=${builtins.placeholder "out"}/lib/"
];

View File

@ -610,66 +610,78 @@ let
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
];
finalPackage = buildEnv {
name = "${postgresql.pname}-and-plugins-${postgresql.version}";
paths = installedExtensions ++ [
# consider keeping in-sync with `postBuild` below
postgresql
postgresql.man # in case user installs this into environment
];
pathsToLink = [
"/"
"/bin"
];
pathsToLink = [
"/"
"/bin"
"/share/postgresql/extension"
# Unbreaks Omnigres' build system
"/share/postgresql/timezonesets"
"/share/postgresql/tsearch_data"
];
nativeBuildInputs = [ makeBinaryWrapper ];
postBuild =
let
args = lib.concatMap (ext: ext.wrapperArgs or [ ]) installedExtensions;
in
''
wrapProgram "$out/bin/postgres" ${lib.concatStringsSep " " args}
'';
nativeBuildInputs = [ makeBinaryWrapper ];
postBuild =
let
args = lib.concatMap (ext: ext.wrapperArgs or [ ]) installedExtensions;
in
''
wrapProgram "$out/bin/postgres" ${lib.concatStringsSep " " args}
passthru = {
inherit installedExtensions;
inherit (postgresql)
pg_config
pkgs
psqlSchema
version
;
mkdir -p "$out/nix-support"
substitute "${lib.getDev postgresql}/nix-support/pg_config.env" "$out/nix-support/pg_config.env" \
--replace-fail "${postgresql}" "$out" \
--replace-fail "${postgresql.man}" "$out"
'';
withJIT = postgresqlWithPackages {
inherit
buildEnv
lib
makeBinaryWrapper
postgresql
passthru = {
inherit installedExtensions;
inherit (postgresql)
pkgs
psqlSchema
version
;
} (_: installedExtensions ++ [ postgresql.jit ]);
withoutJIT = postgresqlWithPackages {
inherit
buildEnv
lib
makeBinaryWrapper
postgresql
;
} (_: lib.remove postgresql.jit installedExtensions);
withPackages =
f':
postgresqlWithPackages {
pg_config = postgresql.pg_config.override { inherit finalPackage; };
withJIT = postgresqlWithPackages {
inherit
buildEnv
lib
makeBinaryWrapper
postgresql
;
} (ps: installedExtensions ++ f' ps);
} (_: installedExtensions ++ [ postgresql.jit ]);
withoutJIT = postgresqlWithPackages {
inherit
buildEnv
lib
makeBinaryWrapper
postgresql
;
} (_: lib.remove postgresql.jit installedExtensions);
withPackages =
f':
postgresqlWithPackages {
inherit
buildEnv
lib
makeBinaryWrapper
postgresql
;
} (ps: installedExtensions ++ f' ps);
};
};
};
in
finalPackage;
in
# passed by <major>.nix

View File

@ -9,5 +9,6 @@ pg_config.env: $(top_builddir)/src/port/pg_config_paths.h | $(top_builddir)/src/
echo "LDFLAGS_EX=\"$(LDFLAGS_EX)\"" >>$@
echo "LDFLAGS_SL=\"$(LDFLAGS_SL)\"" >>$@
echo "LIBS=\"$(LIBS)\"" >>$@
echo "PGXS=\"$(dev)/lib/pgxs/src/makefiles/pgxs.mk\"" >>$@
cat $(top_builddir)/src/port/pg_config_paths.h $(top_builddir)/src/include/pg_config.h \
| sed -nE 's/^#define ([^ ]+) ("?)(.*)\2$$/\1="\3"/p' >>$@

View File

@ -70,7 +70,7 @@ for opt; do
--mandir) show+=("$MANDIR") ;;
--sharedir) show+=("$PGSHAREDIR") ;;
--sysconfdir) show+=("$SYSCONFDIR") ;;
--pgxs) show+=("@postgresql-dev@/lib/pgxs/src/makefiles/pgxs.mk") ;;
--pgxs) show+=("$PGXS") ;;
--configure) show+=("$CONFIGURE_ARGS") ;;
--cc) show+=("$CC") ;;
--cppflags) show+=("$CPPFLAGS") ;;
@ -108,7 +108,7 @@ LOCALEDIR = $LOCALEDIR
MANDIR = $MANDIR
SHAREDIR = $PGSHAREDIR
SYSCONFDIR = $SYSCONFDIR
PGXS = @postgresql-dev@/lib/pgxs/src/makefiles/pgxs.mk
PGXS = $PGXS
CONFIGURE = $CONFIGURE_ARGS
CC = $CC
CPPFLAGS = $CPPFLAGS