1{
2 stdenv,
3 lib,
4 buildPythonPackage,
5 pythonOlder,
6 isPyPy,
7 fetchPypi,
8 libpq,
9 postgresql,
10 postgresqlTestHook,
11 openssl,
12 sphinxHook,
13 sphinx-better-theme,
14 buildPackages,
15}:
16
17buildPythonPackage rec {
18 pname = "psycopg2";
19 version = "2.9.10";
20 format = "setuptools";
21
22 # Extension modules don't work well with PyPy. Use psycopg2cffi instead.
23 # c.f. https://github.com/NixOS/nixpkgs/pull/104151#issuecomment-729750892
24 disabled = pythonOlder "3.6" || isPyPy;
25
26 outputs = [
27 "out"
28 "doc"
29 ];
30
31 src = fetchPypi {
32 inherit pname version;
33 hash = "sha256-EuwLQLAnP5UpYjPodQRBM5KY5qVy9wOdpbJg48i2DhE=";
34 };
35
36 postPatch = ''
37 # Preferably upstream would not depend on pg_config because config scripts are incompatible with cross-compilation, however postgresql's pc file is lacking information.
38 # some linker flags are added but the linker ignores them because they're incompatible
39 # https://github.com/psycopg/psycopg2/blob/89005ac5b849c6428c05660b23c5a266c96e677d/setup.py
40 substituteInPlace setup.py \
41 --replace-fail "self.pg_config_exe = self.build_ext.pg_config" 'self.pg_config_exe = "${libpq.pg_config}/bin/pg_config"'
42 '';
43
44 nativeBuildInputs = [
45 sphinxHook
46 sphinx-better-theme
47 ];
48
49 buildInputs = [ libpq ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ openssl ];
50
51 sphinxRoot = "doc/src";
52
53 # test suite breaks at some point with:
54 # current transaction is aborted, commands ignored until end of transaction block
55 doCheck = false;
56
57 nativeCheckInputs = [
58 postgresql
59 postgresqlTestHook
60 ];
61
62 env = {
63 PGDATABASE = "psycopg2_test";
64 };
65
66 pythonImportsCheck = [ "psycopg2" ];
67
68 disallowedReferences = lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
69 buildPackages.libpq
70 ];
71
72 meta = with lib; {
73 description = "PostgreSQL database adapter for the Python programming language";
74 homepage = "https://www.psycopg.org";
75 license = with licenses; [
76 lgpl3Plus
77 zpl20
78 ];
79 maintainers = [ ];
80 };
81}