1{ stdenv
2, lib
3, fetchurl
4, buildPythonPackage
5, isPy3k, pythonOlder
6, astor
7, gast
8, google-pasta
9, wrapt
10, numpy
11, six
12, termcolor
13, protobuf
14, absl-py
15, grpcio
16, mock
17, backports_weakref
18, tensorflow-estimator
19, tensorflow-tensorboard
20, cudaSupport ? false
21, cudatoolkit ? null
22, cudnn ? null
23, nvidia_x11 ? null
24, zlib
25, python
26, symlinkJoin
27, keras-applications
28, keras-preprocessing
29, addOpenGLRunpath
30}:
31
32# We keep this binary build for two reasons:
33# - the source build doesn't work on Darwin.
34# - the source build is currently brittle and not easy to maintain
35
36assert cudaSupport -> cudatoolkit != null
37 && cudnn != null
38 && nvidia_x11 != null;
39
40# unsupported combination
41assert ! (stdenv.isDarwin && cudaSupport);
42
43let
44 packages = import ./binary-hashes.nix;
45
46 variant = if cudaSupport then "-gpu" else "";
47 pname = "tensorflow${variant}";
48
49in buildPythonPackage {
50 inherit pname;
51 inherit (packages) version;
52 format = "wheel";
53
54 src = let
55 pyVerNoDot = lib.strings.stringAsChars (x: if x == "." then "" else x) python.pythonVersion;
56 pyver = if stdenv.isDarwin then builtins.substring 0 1 pyVerNoDot else pyVerNoDot;
57 platform = if stdenv.isDarwin then "mac" else "linux";
58 unit = if cudaSupport then "gpu" else "cpu";
59 key = "${platform}_py_${pyver}_${unit}";
60 in fetchurl packages.${key};
61
62 propagatedBuildInputs = [
63 protobuf
64 numpy
65 termcolor
66 grpcio
67 six
68 astor
69 absl-py
70 gast
71 google-pasta
72 wrapt
73 tensorflow-estimator
74 tensorflow-tensorboard
75 keras-applications
76 keras-preprocessing
77 ] ++ lib.optional (!isPy3k) mock
78 ++ lib.optionals (pythonOlder "3.4") [ backports_weakref ];
79
80 nativeBuildInputs = lib.optional cudaSupport addOpenGLRunpath;
81
82 # Upstream has a pip hack that results in bin/tensorboard being in both tensorflow
83 # and the propageted input tensorflow-tensorboard which causes environment collisions.
84 # another possibility would be to have tensorboard only in the buildInputs
85 # https://github.com/tensorflow/tensorflow/blob/v1.7.1/tensorflow/tools/pip_package/setup.py#L79
86 postInstall = ''
87 rm $out/bin/tensorboard
88 '';
89
90 # Note that we need to run *after* the fixup phase because the
91 # libraries are loaded at runtime. If we run in preFixup then
92 # patchelf --shrink-rpath will remove the cuda libraries.
93 postFixup = let
94 rpath = stdenv.lib.makeLibraryPath
95 ([ stdenv.cc.cc.lib zlib ] ++ lib.optionals cudaSupport [ cudatoolkit.out cudatoolkit.lib cudnn nvidia_x11 ]);
96 in
97 lib.optionalString stdenv.isLinux ''
98 rrPath="$out/${python.sitePackages}/tensorflow/:$out/${python.sitePackages}/tensorflow/contrib/tensor_forest/:${rpath}"
99 internalLibPath="$out/${python.sitePackages}/tensorflow/python/_pywrap_tensorflow_internal.so"
100 find $out -type f \( -name '*.so' -or -name '*.so.*' \) | while read lib; do
101 patchelf --set-rpath "$rrPath" "$lib"
102 ${lib.optionalString cudaSupport ''
103 addOpenGLRunpath "$lib"
104 ''}
105 done
106 '';
107
108
109 meta = with stdenv.lib; {
110 description = "Computation using data flow graphs for scalable machine learning";
111 homepage = http://tensorflow.org;
112 license = licenses.asl20;
113 maintainers = with maintainers; [ jyp abbradar ];
114 platforms = [ "x86_64-linux" "x86_64-darwin" ];
115 # Python 2.7 build uses different string encoding.
116 # See https://github.com/NixOS/nixpkgs/pull/37044#issuecomment-373452253
117 broken = stdenv.isDarwin && !isPy3k;
118 };
119}