1{ buildPackages
2, buildPythonPackage
3, fetchpatch
4, isPyPy
5, lib
6, protobuf
7, pytestCheckHook
8, pythonAtLeast
9, tzdata
10}:
11
12assert lib.versionAtLeast protobuf.version "3.21" -> throw "Protobuf 3.20 or older required";
13
14buildPythonPackage {
15 inherit (protobuf) pname src;
16
17 version = protobuf.version;
18
19 sourceRoot = "${protobuf.src.name}/python";
20
21 patches = lib.optionals (pythonAtLeast "3.11") [
22 (fetchpatch {
23 name = "support-python311.patch";
24 url = "https://github.com/protocolbuffers/protobuf/commit/2206b63c4649cf2e8a06b66c9191c8ef862ca519.diff";
25 stripLen = 1; # because sourceRoot above
26 hash = "sha256-3GaoEyZIhS3QONq8LEvJCH5TdO9PKnOgcQF0GlEiwFo=";
27 })
28 ];
29
30 prePatch = ''
31 if [[ "$(<../version.json)" != *'"python": "'"$version"'"'* ]]; then
32 echo "Python library version mismatch. Derivation version: $version, actual: $(<../version.json)"
33 exit 1
34 fi
35 '';
36
37 # Remove the line in setup.py that forces compiling with C++14. Upstream's
38 # CMake build has been updated to support compiling with other versions of
39 # C++, but the Python build has not. Without this, we observe compile-time
40 # errors using GCC.
41 #
42 # Fedora appears to do the same, per this comment:
43 #
44 # https://github.com/protocolbuffers/protobuf/issues/12104#issuecomment-1542543967
45 #
46 postPatch = ''
47 sed -i "/extra_compile_args.append('-std=c++14')/d" setup.py
48 '';
49
50 nativeBuildInputs = lib.optional isPyPy tzdata;
51
52 buildInputs = [ protobuf ];
53
54 propagatedNativeBuildInputs = [
55 # For protoc of the same version.
56 buildPackages."protobuf${lib.versions.major protobuf.version}_${lib.versions.minor protobuf.version}"
57 ];
58
59 setupPyGlobalFlags = [ "--cpp_implementation" ];
60
61 nativeCheckInputs = [
62 pytestCheckHook
63 ];
64
65 disabledTests = lib.optionals isPyPy [
66 # error message differs
67 "testInvalidTimestamp"
68 # requires tracemalloc which pypy does not implement
69 # https://foss.heptapod.net/pypy/pypy/-/issues/3048
70 "testUnknownFieldsNoMemoryLeak"
71 # assertion is not raised for some reason
72 "testStrictUtf8Check"
73 ];
74
75 pythonImportsCheck = [
76 "google.protobuf"
77 "google.protobuf.internal._api_implementation" # Verify that --cpp_implementation worked
78 ];
79
80 passthru = {
81 inherit protobuf;
82 };
83
84 meta = with lib; {
85 description = "Protocol Buffers are Google's data interchange format";
86 homepage = "https://developers.google.com/protocol-buffers/";
87 license = licenses.bsd3;
88 maintainers = with maintainers; [ knedlsepp ];
89 };
90}