1{
2 lib,
3 stdenv,
4
5 # bazel wheel
6 buildBazelPackage,
7 fetchFromGitHub,
8
9 # nativeBuildInputs
10 python,
11 setuptools,
12 wheel,
13 absl-py,
14
15 bazel_6,
16 cctools,
17
18 # python package
19 buildPythonPackage,
20
21 # dependencies
22 cloudpickle,
23 decorator,
24 dm-tree,
25 gast,
26 keras,
27 numpy,
28 six,
29 tensorflow,
30
31 # tests
32 hypothesis,
33 matplotlib,
34 mock,
35 mpmath,
36 pandas,
37 pytest,
38 scipy,
39}:
40
41let
42 version = "0.25.0";
43 pname = "tensorflow-probability";
44
45 # first build all binaries and generate setup.py using bazel
46 bazel-wheel = buildBazelPackage {
47 name = "tensorflow_probability-${version}-py2.py3-none-any.whl";
48 src = fetchFromGitHub {
49 owner = "tensorflow";
50 repo = "probability";
51 rev = "refs/tags/v${version}";
52 hash = "sha256-LXQfGFgnM7WYUQjJ2Y3jskdeJ/dEKz+Afg+UOQjv5kc=";
53 };
54 nativeBuildInputs = [
55 absl-py
56 # needed to create the output wheel in installPhase
57 python
58 setuptools
59 tensorflow
60 wheel
61 ];
62
63 bazel = bazel_6;
64
65 bazelTargets = [ ":pip_pkg" ];
66 LIBTOOL = lib.optionalString stdenv.hostPlatform.isDarwin "${cctools}/bin/libtool";
67
68 fetchAttrs = {
69 sha256 = "sha256-TbWcWYidyXuAMgBnO2/k0NKCzc4wThf2uUeC3QxdBJY=";
70 };
71
72 buildAttrs = {
73 preBuild = ''
74 patchShebangs .
75 '';
76
77 installPhase = ''
78 # work around timestamp issues
79 # https://github.com/NixOS/nixpkgs/issues/270#issuecomment-467583872
80 export SOURCE_DATE_EPOCH=315532800
81
82 # First build, then move. Otherwise pip_pkg would create the dir $out
83 # and then put the wheel in that directory. However we want $out to
84 # point directly to the wheel file.
85 ./bazel-bin/pip_pkg . --release
86 mv *.whl "$out"
87 '';
88 };
89 };
90in
91buildPythonPackage {
92 inherit version pname;
93 format = "wheel";
94
95 src = bazel-wheel;
96
97 dependencies = [
98 cloudpickle
99 decorator
100 dm-tree
101 gast
102 keras
103 numpy
104 six
105 tensorflow
106 ];
107
108 # Listed here:
109 # https://github.com/tensorflow/probability/blob/f3777158691787d3658b5e80883fe1a933d48989/testing/dependency_install_lib.sh#L83
110 nativeCheckInputs = [
111 hypothesis
112 matplotlib
113 mock
114 mpmath
115 pandas
116 pytest
117 scipy
118 ];
119
120 # Ideally, we run unit tests with pytest, but in checkPhase, only the Bazel-build wheel is available.
121 # But it seems not guaranteed that running the tests with pytest will even work, see
122 # https://github.com/tensorflow/probability/blob/c2a10877feb2c4c06a4dc58281e69c37a11315b9/CONTRIBUTING.md?plain=1#L69
123 # Ideally, tests would be run using Bazel. For now, lets's do a...
124
125 # sanity check
126 pythonImportsCheck = [ "tensorflow_probability" ];
127
128 meta = {
129 description = "Library for probabilistic reasoning and statistical analysis";
130 homepage = "https://www.tensorflow.org/probability/";
131 changelog = "https://github.com/tensorflow/probability/releases/tag/v${version}";
132 license = lib.licenses.asl20;
133 maintainers = with lib.maintainers; [ GaetanLepage ];
134 };
135}