1{ stdenv
2, lib
3, buildPythonPackage
4, cargo
5, fetchFromGitHub
6, fetchpatch
7, h5py
8, numpy
9, pythonOlder
10, pytestCheckHook
11, rustc
12, rustPlatform
13, setuptools-rust
14, torch
15, libiconv
16}:
17
18buildPythonPackage rec {
19 pname = "safetensors";
20 version = "0.3.0";
21 format = "pyproject";
22
23 disabled = pythonOlder "3.7";
24
25 src = fetchFromGitHub {
26 owner = "huggingface";
27 repo = pname;
28 rev = "refs/tags/v${version}";
29 hash = "sha256-Qpb5lTw1WEME9tWEGfxC8l8dK9mGMH2rz+O+xGCrUxw";
30 };
31
32 patches = [
33 # remove after next release
34 (fetchpatch {
35 name = "commit-cargo-lockfile";
36 relative = "bindings/python";
37 url = "https://github.com/huggingface/safetensors/commit/a7061b4235b59312010b2dd6f9597381428ee9a2.patch";
38 hash = "sha256-iH4vQOL2LU93kd0dSS8/JJxKGb+kDstqnExjYSSwi78";
39 })
40 ];
41
42 cargoDeps = rustPlatform.fetchCargoTarball {
43 inherit src patches;
44 sourceRoot = "source/bindings/python";
45 hash = "sha256-tC0XawmKWNGCaByHQfJEfmHM3m/qgTuIpcRaEFJC6dM";
46 };
47
48 sourceRoot = "source/bindings/python";
49
50 nativeBuildInputs = [
51 setuptools-rust
52 cargo
53 rustc
54 rustPlatform.cargoSetupHook
55 ];
56
57 buildInputs = lib.optionals stdenv.isDarwin [ libiconv ];
58
59 nativeCheckInputs = [
60 h5py numpy pytestCheckHook torch
61 ];
62 pytestFlagsArray = [ "tests" ];
63 # don't require PaddlePaddle (not in Nixpkgs), Flax, or Tensorflow (onerous) to run tests:
64 disabledTestPaths = [
65 "tests/test_flax_comparison.py"
66 "tests/test_paddle_comparison.py"
67 "tests/test_tf_comparison.py"
68 ];
69
70 pythonImportsCheck = [
71 "safetensors"
72 ];
73
74 meta = with lib; {
75 homepage = "https://github.com/huggingface/safetensors";
76 description = "Fast (zero-copy) and safe (unlike pickle) format for storing tensors";
77 changelog = "https://github.com/huggingface/safetensors/releases/tag/v${version}";
78 license = licenses.asl20;
79 maintainers = with maintainers; [ bcdarwin ];
80 };
81}