1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 fetchFromGitHub,
6 rustPlatform,
7 pytest,
8 runCommand,
9 boringssl,
10 libiconv,
11 gcc-unwrapped,
12 python,
13 fetchpatch,
14}:
15
16let
17 boringsslPatched = boringssl.overrideAttrs (oa: {
18 # boringssl source obtained from https://github.com/0x676e67/boring2/tree/1a0f1cd24e728aac100df68027c820f858199224/boring-sys/deps
19 src = fetchFromGitHub {
20 owner = "google";
21 repo = "boringssl";
22 rev = "44b3df6f03d85c901767250329c571db405122d5";
23 hash = "sha256-REELo7X9aFy2OHjubYLO1UQXLTgekD4QFd2vyFthIrg=";
24 };
25 modRoot = "./src";
26 patches = [
27 # A patch required to build boringssl compatible with `boring-sys2`.
28 # See https://github.com/0x676e67/boring2/blob/refs/tags/v4.15.11/boring-sys/build/main.rs#L486-L489
29 (fetchpatch {
30 name = "boringssl-44b3df6f03d85c901767250329c571db405122d5.patch";
31 url = "https://raw.githubusercontent.com/0x676e67/boring2/refs/tags/v4.15.11/boring-sys/patches/boringssl-44b3df6f03d85c901767250329c571db405122d5.patch";
32 hash = "sha256-JRRATcCXo0HBQTzgCAuLpxC3NEGrTw1cEmC0VHOgO2M=";
33 })
34 ];
35
36 # Remove bazel specific build file to make way for build directory
37 # This is a problem on Darwin because of case-insensitive filesystem
38 preBuild =
39 (lib.optionalString stdenv.hostPlatform.isDarwin ''
40 rm ../BUILD
41 '')
42 + oa.preBuild;
43
44 env.NIX_CFLAGS_COMPILE =
45 oa.env.NIX_CFLAGS_COMPILE
46 + " "
47 + toString (
48 lib.optionals stdenv.cc.isClang [
49 "-Wno-error=reorder-ctor"
50 ]
51 ++ lib.optionals stdenv.cc.isGNU [
52 "-Wno-error=reorder"
53 "-Wno-error=ignored-attributes"
54 ]
55 );
56
57 vendorHash = "sha256-06MkjXl0DKFzIH/H+uT9kXsQdPq7qdZh2dlLW/YhJuk=";
58
59 installPhase = ''
60 runHook preInstall
61
62 mkdir -p $bin/bin $dev $out/lib
63
64 install -Dm755 tool/bssl -t $bin/bin
65 install -Dm644 ssl/libssl.a -t $out/lib
66 install -Dm644 crypto/libcrypto.a -t $out/lib
67 install -Dm644 decrepit/libdecrepit.a -t $out/lib
68
69 cp -r ../include $dev
70
71 runHook postInstall
72 '';
73 });
74 # boring-sys expects the static libraries in build/ instead of lib/
75 boringssl-wrapper = runCommand "boringssl-wrapper" { } ''
76 mkdir $out
77 ln -s ${boringsslPatched.out}/lib $out/build
78 ln -s ${boringsslPatched.dev}/include $out/include
79 '';
80in
81buildPythonPackage rec {
82 pname = "primp";
83 version = "0.15.0";
84 pyproject = true;
85
86 src = fetchFromGitHub {
87 owner = "deedy5";
88 repo = "primp";
89 tag = "v${version}";
90 hash = "sha256-13o0Ni0dvZaoKgYy2cFQhebwKAJGm5Z2s+gVAddxYxU=";
91 };
92
93 cargoDeps = rustPlatform.fetchCargoVendor {
94 inherit pname version src;
95 hash = "sha256-UBpf9f3wJgbizHERsm83cuKHiMixj/8JX/IGvteySIo=";
96 };
97
98 nativeBuildInputs = [
99 rustPlatform.bindgenHook
100 rustPlatform.cargoSetupHook
101 rustPlatform.maturinBuildHook
102 ];
103
104 # TODO: Can we improve this?
105 postInstall = lib.optionalString stdenv.hostPlatform.isLinux ''
106 patchelf --add-rpath ${lib.getLib gcc-unwrapped.lib} --add-needed libstdc++.so.6 $out/${python.sitePackages}/primp/primp.abi3.so
107 '';
108
109 buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [
110 libiconv
111 ];
112
113 env.BORING_BSSL_PATH = boringssl-wrapper;
114 env.BORING_BSSL_ASSUME_PATCHED = true;
115
116 optional-dependencies = {
117 dev = [ pytest ];
118 };
119
120 # Test use network
121 doCheck = false;
122
123 pythonImportsCheck = [ "primp" ];
124
125 meta = {
126 changelog = "https://github.com/deedy5/primp/releases/tag/${version}";
127 description = "Python Requests IMPersonate, the fastest Python HTTP client that can impersonate web browsers";
128 homepage = "https://github.com/deedy5/primp";
129 license = lib.licenses.mit;
130 maintainers = with lib.maintainers; [ ];
131 };
132}