1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 fetchFromGitHub,
6
7 # build-system
8 hatchling,
9
10 # preBuild
11 wgpu-native,
12
13 # dependencies
14 cffi,
15 rubicon-objc,
16 sniffio,
17
18 # optional dependency
19 glfw,
20
21 # docs
22 sphinx-rtd-theme,
23 sphinxHook,
24
25 # tests
26 anyio,
27 imageio,
28 numpy,
29 psutil,
30 pytest,
31 ruff,
32 trio,
33
34 # passthru
35 wgpu-py,
36 testers,
37}:
38buildPythonPackage rec {
39 pname = "wgpu-py";
40 version = "0.21.1";
41 pyproject = true;
42
43 src = fetchFromGitHub {
44 owner = "pygfx";
45 repo = "wgpu-py";
46 tag = "v${version}";
47 hash = "sha256-XlV0ovIF3w/u6f65+3c4zAfisCoQDbzMiINJJTR/I6o=";
48 };
49
50 # `requests` is only used to fetch a copy of `wgpu-native` via `tools/hatch_build.py`.
51 # As we retrieve `wgpu-native` from nixpkgs instead, none of this is needed, and
52 # remove an extra dependency.
53 postPatch = ''
54 substituteInPlace pyproject.toml \
55 --replace-fail 'requires = ["requests", "hatchling"]' 'requires = ["hatchling"]' \
56 --replace-fail '[tool.hatch.build.targets.wheel.hooks.custom]' "" \
57 --replace-fail 'path = "tools/hatch_build.py"' ""
58 '';
59
60 # wgpu-py expects to have an appropriately named wgpu-native library in wgpu/resources
61 preBuild = ''
62 ln -s ${wgpu-native}/lib/libwgpu_native${stdenv.hostPlatform.extensions.library} \
63 wgpu/resources/libwgpu_native-release${stdenv.hostPlatform.extensions.library}
64 '';
65
66 build-system = [ hatchling ];
67
68 dependencies =
69 # Runtime dependencies
70 [
71 cffi
72 sniffio
73 ]
74 # Required only on darwin
75 ++ lib.optionals stdenv.hostPlatform.isDarwin [
76 rubicon-objc
77 ];
78
79 optional-dependencies = {
80 # jupyter = [ jupyter_rfb ] not in nixpkgs
81 glfw = [ glfw ];
82 # imgui = ["imgui-bundle>=1.2.1"] not in nixpkgs
83
84 docs = [
85 sphinxHook
86 sphinx-rtd-theme
87 ];
88 };
89
90 pythonRemoveDeps = [ "requests" ];
91
92 pythonImportsCheck = [ "wgpu" ];
93
94 nativeCheckInputs = [
95 anyio
96 imageio
97 numpy
98 psutil
99 pytest
100 ruff
101 trio
102 ];
103
104 # Tests break due to sandboxing on everything except darwin
105 # Prefer to run them in passthru
106 doCheck = false;
107
108 passthru = {
109 tests =
110 {
111 version = testers.testVersion {
112 package = wgpu-py;
113 command = "python3 -c 'import wgpu; print(wgpu.__version__)'";
114 };
115 }
116 // lib.optionalAttrs stdenv.buildPlatform.isDarwin {
117 tests = testers.runCommand {
118 name = "tests";
119 script = ''
120 WGPU_LIB_PATH=${wgpu-native}/lib/libwgpu_native${stdenv.hostPlatform.extensions.library} \
121 pytest -v ${wgpu-py.src}/tests
122 '';
123 nativeBuildInputs = [ wgpu-py ] ++ nativeCheckInputs;
124 };
125
126 examples = testers.runCommand {
127 name = "examples";
128 script = ''
129 WGPU_LIB_PATH=${wgpu-native}/lib/libwgpu_native${stdenv.hostPlatform.extensions.library} \
130 pytest -v ${wgpu-py.src}/examples
131 '';
132 nativeBuildInputs = [ wgpu-py ] ++ nativeCheckInputs;
133 };
134
135 codegen = testers.runCommand {
136 name = "codegen";
137 script = ''
138 WGPU_LIB_PATH=${wgpu-native}/lib/libwgpu_native${stdenv.hostPlatform.extensions.library} \
139 pytest -v ${wgpu-py.src}/codegen
140 '';
141 nativeBuildInputs = [ wgpu-py ] ++ nativeCheckInputs;
142 };
143
144 tests_mem = testers.runCommand {
145 name = "tests_mem";
146 script = ''
147 WGPU_LIB_PATH=${wgpu-native}/lib/libwgpu_native${stdenv.hostPlatform.extensions.library} \
148 pytest -v ${wgpu-py.src}/tests_mem
149 '';
150 nativeBuildInputs = [ wgpu-py ] ++ nativeCheckInputs;
151 };
152 };
153 };
154
155 meta = {
156 description = "WebGPU for Python";
157 homepage = "https://github.com/pygfx/wgpu-py";
158 changelog = "https://github.com/pygfx/wgpu-py/blob/v${version}/CHANGELOG.md";
159
160 platforms = lib.platforms.all;
161 license = lib.licenses.bsd2;
162 maintainers = [ lib.maintainers.bengsparks ];
163 };
164}