From fbdb09b2cc74fde22755ae23eadd4c2f8ec49ab7 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Fri, 28 Feb 2025 15:46:17 +0100 Subject: [PATCH] postgresql: provide plpython3 extension as package This allows us to always build the extension, but still have the user explicitly enable it without causing rebuilds. --- doc/release-notes/rl-2505.section.md | 2 + pkgs/servers/sql/postgresql/ext.nix | 3 ++ pkgs/servers/sql/postgresql/ext/plpython3.nix | 45 ++++++++++++++++ pkgs/servers/sql/postgresql/generic.nix | 53 +++++++++++++------ 4 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 pkgs/servers/sql/postgresql/ext/plpython3.nix diff --git a/doc/release-notes/rl-2505.section.md b/doc/release-notes/rl-2505.section.md index e386ead66797..8f7a2e2babfb 100644 --- a/doc/release-notes/rl-2505.section.md +++ b/doc/release-notes/rl-2505.section.md @@ -25,6 +25,8 @@ The `nixLog` function, which logs unconditionally, was also re-introduced and modified to prefix messages with the function name of the caller. For more information, [see this PR](https://github.com/NixOS/nixpkgs/pull/370742). +- `postgresql`'s `pythonSupport` argument has been changed. It is now enabled by default, but to use PL/Python the extension needs to be added explicitly with `postgresql.withPackages`. If you were using `postgresql.override { pythonSupport = true; }` before, change it to `postgresql.withPackages (ps: [ ps.plpython3 ])`. + - The `rustPlatform.fetchCargoTarball` function is deprecated, because it relied on `cargo vendor` not changing its output format to keep fixed-output derivation hashes the same, which is a Nix invariant, and Cargo 1.84.0 changed `cargo vendor`'s output format. It should generally be replaced with `rustPlatform.fetchCargoVendor`, but `rustPlatform.importCargoLock` may also be appropriate in some circumstances. `rustPlatform.buildRustPackage` users must set `useFetchCargoVendor` to `true` and regenerate the `cargoHash`. diff --git a/pkgs/servers/sql/postgresql/ext.nix b/pkgs/servers/sql/postgresql/ext.nix index 5095275a423d..9ec29ea3eb10 100644 --- a/pkgs/servers/sql/postgresql/ext.nix +++ b/pkgs/servers/sql/postgresql/ext.nix @@ -9,6 +9,9 @@ in // { timescaledb-apache = super.callPackage ./ext/timescaledb.nix { enableUnfree = false; }; } +// lib.optionalAttrs (!self.pythonSupport) { + plpython3 = throw "PostgreSQL extension `plpython3` is not available, because `postgresql` was built without Python support. Override with `pythonSupport = true` to enable the extension."; +} // lib.optionalAttrs config.allowAliases { pg_embedding = throw "PostgreSQL extension `pg_embedding` has been removed since the project has been abandoned. Upstream's recommendation is to use pgvector instead (https://neon.tech/docs/extensions/pg_embedding#migrate-from-pg_embedding-to-pgvector)"; } diff --git a/pkgs/servers/sql/postgresql/ext/plpython3.nix b/pkgs/servers/sql/postgresql/ext/plpython3.nix new file mode 100644 index 000000000000..32f2ffd3c89a --- /dev/null +++ b/pkgs/servers/sql/postgresql/ext/plpython3.nix @@ -0,0 +1,45 @@ +{ + buildEnv, + postgresql, + postgresqlTestExtension, + python3, +}: + +let + withPackages = + f: + let + python = python3.withPackages f; + finalPackage = buildEnv { + name = "${postgresql.pname}-plpython3-${postgresql.version}"; + paths = [ postgresql.plpython3 ]; + passthru = { + inherit withPackages; + wrapperArgs = [ + ''--set PYTHONPATH "${python}/${python.sitePackages}"'' + ]; + tests.extension = postgresqlTestExtension { + finalPackage = finalPackage.withPackages (ps: [ ps.base58 ]); + sql = '' + CREATE EXTENSION plpython3u; + DO LANGUAGE plpython3u $$ + import base58 + $$; + ''; + }; + }; + meta = { + inherit (postgresql.meta) + homepage + license + changelog + maintainers + platforms + ; + description = "PL/Python - Python Procedural Language"; + }; + }; + in + finalPackage; +in +withPackages (_: [ ]) diff --git a/pkgs/servers/sql/postgresql/generic.nix b/pkgs/servers/sql/postgresql/generic.nix index 8b7accabccd8..91c952215857 100644 --- a/pkgs/servers/sql/postgresql/generic.nix +++ b/pkgs/servers/sql/postgresql/generic.nix @@ -93,7 +93,13 @@ let perl, # PL/Python - pythonSupport ? false, + pythonSupport ? + lib.meta.availableOn stdenv.hostPlatform python3 + # Building with python in pkgsStatic gives this error: + # checking how to link an embedded Python application... configure: error: could not find shared library for Python + && !stdenv.hostPlatform.isStatic + # configure tries to call the python executable + && stdenv.buildPlatform.canExecute stdenv.hostPlatform, python3, # PL/Tcl @@ -153,7 +159,7 @@ let "doc" "lib" "man" - ]; + ] ++ lib.optionals pythonSupport [ "plpython3" ]; outputChecks = { out = { disallowedReferences = [ @@ -399,17 +405,17 @@ let # to their own output for installation, will then fail to find "postgres" during linking. substituteInPlace "$dev/lib/pgxs/src/Makefile.port" \ --replace-fail '-bundle_loader $(bindir)/postgres' "-bundle_loader $out/bin/postgres" - ''; - - 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}" + moveToOutput "lib/*plpython3*" "$plpython3" + moveToOutput "share/postgresql/extension/*plpython3*" "$plpython3" ''; + postFixup = lib.optionalString stdenv'.hostPlatform.isGnu '' + # initdb needs access to "locale" command from glibc. + wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin + ''; + # Running tests as "install check" to work around SIP issue on macOS: # https://www.postgresql.org/message-id/flat/4D8E1BC5-BBCF-4B19-8226-359201EA8305%40gmail.com # Also see /doc/stdenv/platform-notes.chapter.md @@ -443,7 +449,7 @@ let pkgs = let scope = { - inherit jitSupport; + inherit jitSupport pythonSupport; inherit (llvmPackages) llvm; postgresql = this; stdenv = stdenv'; @@ -458,7 +464,7 @@ let import ./ext.nix newSelf newSuper; withPackages = postgresqlWithPackages { - inherit buildEnv; + inherit buildEnv lib makeBinaryWrapper; postgresql = this; }; @@ -506,7 +512,12 @@ let }); postgresqlWithPackages = - { postgresql, buildEnv }: + { + postgresql, + buildEnv, + lib, + makeBinaryWrapper, + }: f: let installedExtensions = f postgresql.pkgs; @@ -518,7 +529,19 @@ let postgresql.man # in case user installs this into environment ]; - pathsToLink = [ "/" ]; + pathsToLink = [ + "/" + "/bin" + ]; + + nativeBuildInputs = [ makeBinaryWrapper ]; + postBuild = + let + args = lib.concatMap (ext: ext.wrapperArgs or [ ]) installedExtensions; + in + '' + wrapProgram "$out/bin/postgres" ${lib.concatStringsSep " " args} + ''; passthru = { inherit installedExtensions; @@ -528,11 +551,11 @@ let ; withJIT = postgresqlWithPackages { - inherit buildEnv; + inherit buildEnv lib makeBinaryWrapper; postgresql = postgresql.withJIT; } f; withoutJIT = postgresqlWithPackages { - inherit buildEnv; + inherit buildEnv lib makeBinaryWrapper; postgresql = postgresql.withoutJIT; } f; };