1{ lib
2, stdenv
3, fetchFromGitHub
4, fetchpatch
5, fetchurl
6, writeText
7, python
8, pythonOlder
9, buildPythonPackage
10, cython
11, gfortran
12, meson-python
13, pkg-config
14, pythran
15, wheel
16, nose
17, pytest
18, pytest-xdist
19, numpy
20, pybind11
21, pooch
22, libxcrypt
23, xsimd
24, blas
25, lapack
26}:
27
28let
29 pname = "scipy";
30 # DON'T UPDATE THESE ATTRIBUTES MANUALLY - USE:
31 #
32 # nix-shell maintainers/scripts/update.nix --argstr package python3.pkgs.scipy
33 #
34 # The update script uses sed regexes to replace them with the updated hashes.
35 version = "1.11.2";
36 srcHash = "sha256-7FE740/yKUXtujVX60fQB/xvCZFfV69FRihvSi6+UWo=";
37 datasetsHashes = {
38 ascent = "1qjp35ncrniq9rhzb14icwwykqg2208hcssznn3hz27w39615kh3";
39 ecg = "1bwbjp43b7znnwha5hv6wiz3g0bhwrpqpi75s12zidxrbwvd62pj";
40 face = "11i8x29h80y7hhyqhil1fg8mxag5f827g33lhnsf44qk116hp2wx";
41 };
42 datasets = lib.mapAttrs (
43 d: hash: fetchurl {
44 url = "https://raw.githubusercontent.com/scipy/dataset-${d}/main/${d}.dat";
45 sha256 = hash;
46 }
47 ) datasetsHashes;
48 # Additional cross compilation related properties that scipy reads in scipy/meson.build
49 crossFileScipy = writeText "cross-file-scipy.conf" ''
50 [properties]
51 numpy-include-dir = '${numpy}/${python.sitePackages}/numpy/core/include'
52 pythran-include-dir = '${pythran}/${python.sitePackages}/pythran'
53 host-python-path = '${python.interpreter}'
54 host-python-version = '${python.pythonVersion}'
55 '';
56in buildPythonPackage {
57 inherit pname version;
58 format = "pyproject";
59
60 src = fetchFromGitHub {
61 owner = "scipy";
62 repo = pname;
63 rev = "v${version}";
64 hash = srcHash;
65 fetchSubmodules = true;
66 };
67
68 patches = [
69 # Helps with cross compilation, see https://github.com/scipy/scipy/pull/18167
70 (fetchpatch {
71 url = "https://github.com/scipy/scipy/commit/dd50ac9d98dbb70625333a23e3a90e493228e3be.patch";
72 hash = "sha256-Vf6/hhwu6X5s8KWhq8bUZKtSkdVu/GtEpGtj8Olxe7s=";
73 excludes = [
74 "doc/source/dev/contributor/meson_advanced.rst"
75 ];
76 })
77 ];
78
79 # Relax deps a bit
80 postPatch = ''
81 substituteInPlace pyproject.toml \
82 --replace 'numpy==' 'numpy>=' \
83 --replace "pybind11>=2.10.4,<2.11.0" "pybind11>=2.10.4,<2.12.0" \
84 --replace 'wheel<0.41.0' 'wheel'
85 '';
86
87 nativeBuildInputs = [
88 cython
89 gfortran
90 meson-python
91 pythran
92 pkg-config
93 wheel
94 ];
95
96 buildInputs = [
97 blas
98 lapack
99 pybind11
100 pooch
101 xsimd
102 ] ++ lib.optionals (pythonOlder "3.9") [
103 libxcrypt
104 ];
105
106 propagatedBuildInputs = [ numpy ];
107
108 __darwinAllowLocalNetworking = true;
109
110 nativeCheckInputs = [ nose pytest pytest-xdist ];
111
112 doCheck = !(stdenv.isx86_64 && stdenv.isDarwin);
113
114 preConfigure = ''
115 # Helps parallelization a bit
116 export NPY_NUM_BUILD_JOBS=$NIX_BUILD_CORES
117 # We download manually the datasets and this variable tells the pooch
118 # library where these files are cached. See also:
119 # https://github.com/scipy/scipy/pull/18518#issuecomment-1562350648 And at:
120 # https://github.com/scipy/scipy/pull/17965#issuecomment-1560759962
121 export XDG_CACHE_HOME=$PWD; export HOME=$(mktemp -d); mkdir scipy-data
122 '' + (lib.concatStringsSep "\n" (lib.mapAttrsToList (d: dpath:
123 # Actually copy the datasets
124 "cp ${dpath} scipy-data/${d}.dat"
125 ) datasets));
126
127 mesonFlags = [
128 "-Dblas=${blas.pname}"
129 "-Dlapack=${lapack.pname}"
130 # We always run what's necessary for cross compilation, which is passing to
131 # meson the proper cross compilation related arguments. See also:
132 # https://docs.scipy.org/doc/scipy/building/cross_compilation.html
133 "--cross-file=${crossFileScipy}"
134 ];
135
136 # disable stackprotector on aarch64-darwin for now
137 #
138 # build error:
139 #
140 # /private/tmp/nix-build-python3.9-scipy-1.6.3.drv-0/ccDEsw5U.s:109:15: error: index must be an integer in range [-256, 255].
141 #
142 # ldr x0, [x0, ___stack_chk_guard];momd
143 #
144 hardeningDisable = lib.optionals (stdenv.isAarch64 && stdenv.isDarwin) [ "stackprotector" ];
145
146 checkPhase = ''
147 runHook preCheck
148 pushd "$out"
149 export OMP_NUM_THREADS=$(( $NIX_BUILD_CORES / 4 ))
150 ${python.interpreter} -c "import scipy, sys; sys.exit(scipy.test('fast', verbose=10, parallel=$NIX_BUILD_CORES) != True)"
151 popd
152 runHook postCheck
153 '';
154
155 requiredSystemFeatures = [ "big-parallel" ]; # the tests need lots of CPU time
156
157 passthru = {
158 inherit blas;
159 updateScript = [
160 ./update.sh
161 # Pass it this file name as argument
162 (builtins.unsafeGetAttrPos "pname" python.pkgs.scipy).file
163 ]
164 # Pass it the names of the datasets to update their hashes
165 ++ (builtins.attrNames datasetsHashes)
166 ;
167 };
168
169 SCIPY_USE_G77_ABI_WRAPPER = 1;
170
171 meta = with lib; {
172 description = "SciPy (pronounced 'Sigh Pie') is open-source software for mathematics, science, and engineering";
173 homepage = "https://www.scipy.org/";
174 license = licenses.bsd3;
175 maintainers = with maintainers; [ fridh doronbehar ];
176 };
177}