summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/wgpu-py/default.nix
blob: 72d937d01fc3fecc2e6ea028e4b34356712846e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
  lib,
  stdenv,
  buildPythonPackage,
  fetchFromGitHub,

  # build-system
  hatchling,

  # preBuild
  wgpu-native,

  # dependencies
  cffi,
  rubicon-objc,
  sniffio,

  # optional dependency
  glfw,

  # docs
  sphinx-rtd-theme,
  sphinxHook,

  # tests
  anyio,
  imageio,
  numpy,
  psutil,
  pypng,
  pytest,
  rendercanvas,
  ruff,
  trio,

  # passthru
  testers,
  wgpu-py,
}:
buildPythonPackage rec {
  pname = "wgpu-py";
  version = "0.29.0";
  pyproject = true;

  src = fetchFromGitHub {
    owner = "pygfx";
    repo = "wgpu-py";
    tag = "v${version}";
    hash = "sha256-drXO3NHIuK34tbOZjxOCz1lnlcrfx6mADZ2WlEc9vDU=";
  };

  postPatch =
    # `requests` is only used to fetch a copy of `wgpu-native` via `tools/hatch_build.py`.
    # As we retrieve `wgpu-native` from nixpkgs instead, none of this is needed, and
    # remove an extra dependency.
    ''
      substituteInPlace pyproject.toml \
        --replace-fail 'requires = ["requests", "hatchling"]' 'requires = ["hatchling"]' \
        --replace-fail '[tool.hatch.build.targets.wheel.hooks.custom]' "" \
        --replace-fail 'path = "tools/hatch_build.py"' ""
    ''
    # Skip the compute_textures example during testing, as it uses `imageio` to
    # retrieve an image of an astronaut, which touches the network.
    # Additionally skip the imgui_backend_sea and imgui_basic_example examples during testing,
    # as they depend on imgui_bundle which has not been packaged for nixpkgs.
    + ''
      substituteInPlace examples/{compute_textures.py,imgui_backend_sea.py,imgui_basic_example.py} \
        --replace-fail 'import wgpu' 'import wgpu # run_example = false'
    '';

  # wgpu-py expects to have an appropriately named wgpu-native library in wgpu/resources
  preBuild = ''
    ln -s ${wgpu-native}/lib/libwgpu_native${stdenv.hostPlatform.extensions.library} \
      wgpu/resources/libwgpu_native-release${stdenv.hostPlatform.extensions.library}
  '';

  build-system = [ hatchling ];

  dependencies =
    # Runtime dependencies
    [
      cffi
      sniffio
      # https://github.com/pygfx/wgpu-py/blob/ff8db1772f7f94bf2a3e82989f5d296d2ddbb923/pyproject.toml#L16
      (rendercanvas.overrideAttrs { doInstallCheck = false; })
    ]
    # Required only on darwin
    ++ lib.optionals stdenv.hostPlatform.isDarwin [
      rubicon-objc
    ];

  optional-dependencies = {
    # jupyter = [ jupyter_rfb ] not in nixpkgs
    glfw = [ glfw ];
    # imgui = ["imgui-bundle>=1.2.1"] not in nixpkgs

    docs = [
      sphinxHook
      sphinx-rtd-theme
    ];
  };

  pythonRemoveDeps = [ "requests" ];

  pythonImportsCheck = [ "wgpu" ];

  nativeCheckInputs = [
    anyio
    imageio
    numpy
    psutil
    pypng
    pytest
    # break circular dependency cycle
    (rendercanvas.overrideAttrs { doInstallCheck = false; })
    ruff
    trio
  ];

  # Tests break in Linux CI due to wgpu being unable to find any adapters.
  # Ordinarily, this would be fixed in an approach similar to `pkgs/by-name/wg/wgpu-native/examples.nix`'s
  # usage of `runtimeInputs` and `makeWrapperArgs`.
  # Unfortunately, as this is a Python module without a `mainProgram`, `makeWrapperArgs` will not apply here,
  # as there is no "script" to wrap.
  doCheck = stdenv.hostPlatform.isDarwin;

  installCheckPhase = ''
    runHook preInstallCheck

    pytest tests -k "not test_render_timestamps_inside_encoder"
    pytest examples
    pytest tests_mem

    runHook postInstallCheck
  '';

  passthru.tests.version = testers.testVersion {
    package = wgpu-py;
    command = "python3 -c 'import wgpu; print(wgpu.__version__)'";
  };

  meta = {
    description = "WebGPU for Python";
    homepage = "https://github.com/pygfx/wgpu-py";
    changelog = "https://github.com/pygfx/wgpu-py/blob/${src.tag}/CHANGELOG.md";

    platforms = lib.platforms.all;
    license = lib.licenses.bsd2;
    maintainers = [ lib.maintainers.bengsparks ];
  };
}