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