Files
nixpkgs/pkgs/development/python-modules/psycopg2/default.nix
Wolfgang Walther 88dfade94b postgresql: replace pg_config with custom script
By replacing upstream's pg_config binary with a shell script, we:
- gain the ability to run pg_config easily when cross-compiling,
- can remove the fake pg_config in the default output,
- can remove the pg_config wrapper script dealing with special cases.

Some 20 years ago, pg_config *was* a shell script upstream, too. It was
changed to a binary, when it was made "relocatable", so it would return
paths depending on the location of the "postgres" binary. However, this
is exactly the thing that just hurts us in nixpkgs - we don't want those
paths to change, we want them to always point at the right outputs. By
writing the script ourselves, this becomes a lot less painful.

This approach means more lines of codes, but all of them are dead simple
and we have a lot less complexity overall.

Additionally, pg_config is now made a separate derivation, only exposed
as "postgresql.pg_config". This has the nice side-effect, that all users
of postgresql and libpq in nixpkgs must be very *explicit* about their
dependency on pg_config. This gives a lot more visibility into the state
of affairs regarding pkg-config support for libpq, which ultimately is
the much better solution.
2025-03-21 18:05:38 +01:00

82 lines
2.0 KiB
Nix

{
stdenv,
lib,
buildPythonPackage,
pythonOlder,
isPyPy,
fetchPypi,
libpq,
postgresql,
postgresqlTestHook,
openssl,
sphinxHook,
sphinx-better-theme,
buildPackages,
}:
buildPythonPackage rec {
pname = "psycopg2";
version = "2.9.10";
format = "setuptools";
# Extension modules don't work well with PyPy. Use psycopg2cffi instead.
# c.f. https://github.com/NixOS/nixpkgs/pull/104151#issuecomment-729750892
disabled = pythonOlder "3.6" || isPyPy;
outputs = [
"out"
"doc"
];
src = fetchPypi {
inherit pname version;
hash = "sha256-EuwLQLAnP5UpYjPodQRBM5KY5qVy9wOdpbJg48i2DhE=";
};
postPatch = ''
# Preferably upstream would not depend on pg_config because config scripts are incompatible with cross-compilation, however postgresql's pc file is lacking information.
# some linker flags are added but the linker ignores them because they're incompatible
# https://github.com/psycopg/psycopg2/blob/89005ac5b849c6428c05660b23c5a266c96e677d/setup.py
substituteInPlace setup.py \
--replace-fail "self.pg_config_exe = self.build_ext.pg_config" 'self.pg_config_exe = "${libpq.pg_config}/bin/pg_config"'
'';
nativeBuildInputs = [
sphinxHook
sphinx-better-theme
];
buildInputs = [ libpq ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ openssl ];
sphinxRoot = "doc/src";
# test suite breaks at some point with:
# current transaction is aborted, commands ignored until end of transaction block
doCheck = false;
nativeCheckInputs = [
postgresql
postgresqlTestHook
];
env = {
PGDATABASE = "psycopg2_test";
};
pythonImportsCheck = [ "psycopg2" ];
disallowedReferences = lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
buildPackages.libpq
];
meta = with lib; {
description = "PostgreSQL database adapter for the Python programming language";
homepage = "https://www.psycopg.org";
license = with licenses; [
lgpl3Plus
zpl20
];
maintainers = [ ];
};
}