-
Notifications
You must be signed in to change notification settings - Fork 0
/
sage.nix
354 lines (322 loc) · 8.71 KB
/
sage.nix
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{ pkgs
, stdenv
, lib
, makeWrapper
, fetchpatch
, sage-src
, sagelib
, sagedoc
, pygments
, ipython
, traitlets
, cysignals
, pexpect
, ptyprocess
, openblasCompat
, openblas-blas-pc
, openblas-cblas-pc
, openblas-lapack-pc
, pkg-config
, psutil
, future
, singular
, sympy
, fpylll
, libgap
, gap
, matplotlib
, scipy
, pyparsing
, networkx
, conway_polynomials
, graphs
, ecl
, twisted
, service-identity
, cvxopt
, ipykernel
, ipywidgets
, rpy2
, sphinx
, sagenb
, docutils
, typing
, pytz
, tornado
, imagesize
, requests
, gcc
, palp
, r
, giac
, polytopes_db
, combinatorial_designs
, three
, tachyon
, jmol
, elliptic_curves
, maxima-ecl
, cddlib
, glpk
, pari
, pari_data
, gmp
, sympow
, gfan
, sqlite
, python3
, python2
, rubiks
, nauty
, less
, ppl
, flintqs
, mathjax
, lcalc
, eclib
, gsl
, ntl
, ecm
, zlib
, gfortran
, flint
, pillow
, pynac
, buildDoc ? true
}:
let
pythonRuntimeDeps = [
sagelib
sagenb
service-identity
pygments
ipython
traitlets
pexpect
ptyprocess
psutil
future
sympy
fpylll
matplotlib
scipy
pyparsing
networkx
twisted
cvxopt
ipykernel
ipywidgets
rpy2
sphinx
docutils
typing
pillow
];
buildInputs = [
# !order is important! (python2 should show up first in PATH)
pythonEnv
python3
pynac # FIXME why necessary?
makeWrapper
openblasCompat
openblas-blas-pc
openblas-cblas-pc
openblas-lapack-pc
singular
libgap
gap
pkg-config
conway_polynomials
graphs
ecl
palp
r
giac
three
tachyon
jmol
cddlib
glpk
pari
gmp
sympow
gfan
sqlite
maxima-ecl
rubiks
nauty
less
flintqs
lcalc
eclib
gsl
ntl
ecm
zlib
gfortran
ppl
flint
# listed explicitly in addition to being propagated by sagelib so that it shows up in CFLAGS
# FIXME set cflags explicitly
pynac
];
pythonEnv = python2.buildEnv.override {
extraLibs = pythonRuntimeDeps;
ignoreCollisions = true;
};
# remove python prefix, replace "-" in the name by "_", apply patch_names
# python2.7-some-pkg-1.0 -> some_pkg-1.0
pkg_to_spkg_name = pkg: patch_names: let
parts = lib.splitString "-" pkg.name;
# remove python2.7-
stripped_parts = if (builtins.head parts) == python2.libPrefix then builtins.tail parts else parts;
version = lib.last stripped_parts;
orig_pkgname = lib.init stripped_parts;
pkgname = patch_names (lib.concatStringsSep "_" orig_pkgname);
in pkgname + "-" + version;
# return the names of all dependencies in the transitive closure
transitiveClosure = dep:
if isNull dep then
# propagatedBuildInputs might contain null
# (although that might be considered a programming error in the derivation)
[]
else
[ dep ] ++ (
if builtins.hasAttr "propagatedBuildInputs" dep then
lib.unique (builtins.concatLists (map transitiveClosure dep.propagatedBuildInputs))
else
[]
);
allInputs = lib.remove null (buildInputs ++ pythonRuntimeDeps);
transitiveDeps = lib.unique (builtins.concatLists (map transitiveClosure allInputs ));
# fix differences between spkg and sage names
# (could patch sage instead, but this is more lightweight and also works for packages depending on sage)
patch_names = builtins.replaceStrings [
"zope.interface"
"node_three"
] [
"zope_interface"
"threejs"
];
# spkg names (this_is_a_package-version) of all transitive deps
input_names = map (dep: pkg_to_spkg_name dep patch_names) transitiveDeps;
in
stdenv.mkDerivation rec {
version = "8.1"; # TODO
name = "sage-${version}";
inherit buildInputs pythonEnv; # FIXME
src = sage-src;
nativeBuildInputs = buildInputs; # TODO
configurePhase = ''
# NOOP
'';
# environment variables for the build
SAGE_ROOT = src; # TODO
SAGE_LOCAL = placeholder "out";
SAGE_SHARE = sagelib + "/share";
SAGE_DOC = if buildDoc then (sagedoc + "/share/doc/sage") else (SAGE_SHARE + "/doc/sage");
SAGE_DOC_SRC = if buildDoc then (sagedoc + "/docsrc") else (src + "/src/doc");
installPhase = ''
#NOOP
'';
autoreconfPhase = ''
#NOOP
''; # TODO
buildPhase = ''
mkdir -p $out/var/lib/sage/installed
for pkg in ${lib.concatStringsSep " " input_names}; do
touch "$out/var/lib/sage/installed/$pkg"
done
cp -r src/bin $out/bin
cp -r build/bin $out/build-bin
# TODO ugly hack to have python2 and python3 mix
# Better to use python.withPackages instead
makeWrapper "${python3}/bin/python3" "$out/bin/python3" --unset PYTHONHOME
mv $out/bin/sage-env{,-orig}
touch $out/bin/sage-arch-env
echo """
export PKG_CONFIG_PATH='$PKG_CONFIG_PATH' # TODO needed for tests, truly needed at runtime?
export SAGE_ROOT='${SAGE_ROOT}'
export SAGE_LOCAL='${SAGE_LOCAL}'
export SAGE_SHARE='${SAGE_SHARE}'
export SAGE_SCRIPTS_DIR='${placeholder "out"}/bin'
export PATH='$out/bin:$out/build-bin:$PATH' # TODO prefix
. "$out/bin/sage-env-orig"
export PATH='$out/bin:$out/build-bin:$PATH' # reset path changed in sage-env-orig
export SAGE_LOGS=\"\''${TMP:-/tmp}/sage-logs\"
export SAGE_DOC='${SAGE_DOC}'
export SAGE_DOC_SRC='${SAGE_DOC_SRC}'
export JUPYTER_PATH="\$DOT_SAGE/jupyter"
mkdir -p "\$JUPYTER_PATH"
export GP_DATA_DIR="${pari_data}/share/pari"
export PARI_DATA_DIR="${pari_data}" # TODO
export GPHELP="${pari}/bin/gphelp"
export GPDOCDIR="${pari}/share/pari/doc"
export SINGULARPATH='${singular}/share/singular'
export SINGULAR_SO='${singular}/lib/libSingular.so'
export SINGULAR_EXECUTABLE='${singular}/bin/Singular'
export MAXIMA_FAS='${maxima-ecl}/lib/maxima/${maxima-ecl.version}/binary-ecl/maxima.fas'
export MAXIMA_PREFIX="${maxima-ecl}"
export CONWAY_POLYNOMIALS_DATA_DIR='${conway_polynomials}/share/conway_polynomials'
export GRAPHS_DATA_DIR='${graphs}/share/graphs'
export ELLCURVE_DATA_DIR='${elliptic_curves}/share/ellcurves' # TODO unify
export POLYTOPE_DATA_DIR='${polytopes_db}/share/reflexive_polytopes'
export GAP_ROOT_DIR='${gap}/share/gap/build-dir'
export THEBE_DIR='$\{thebe}/share/thebe'
export ECLDIR='${ecl}/lib/ecl-${ecl.version}/'
# needed for cython
export CC='${gcc}/bin/gcc'
export LDFLAGS='$NIX_TARGET_LDFLAGS -L${sagelib}/lib -L${sagelib}/lib -Wl,-rpath,${sagelib}/lib' # TODO sage paths needed?
export CFLAGS='$NIX_CFLAGS_COMPILE'
export SAGE_LIB='${sagelib}/lib/python2.7/site-packages'
export SAGE_EXTCODE='${src}/src/ext'
# own extensions
export COMBINATORIAL_DESIGN_DIR="${combinatorial_designs}/share"
export COEXTER_DIR="$\{coexter}"
export CPLEX_DIR="$\{cplex}"
export CSDP_DIR="$\{csdp}"
export CUNNINGHAM_TABLES_DIR="$\{cunningham_tables}"
export D3JS_DIR="$\{d3js}"
export DATABASE_MUTATION_CLASS_DIR="$\{database_mutation_class}"
export ELLIPTIC_CURVES_DIR="${elliptic_curves}" # TODO
export CREMONA_ELLCURVE_DIR="" # TODO optional
export GUROBI_DIR="$\{gubori}"
export JMOL_DIR="${jmol}"
export JONES_DIR="$\{jones}"
export JSMOL_DIR="${jmol}" # TODO duplicate
export KHOEL_DIR="$\{khoel}"
export LIE_DIR="$\{lie}"
export M4RI_DIR="$\{m4ri}"
export MATHJAX_DIR="${mathjax}/lib/node_modules/mathjax"
export MATRIX_GF2E_DENSE_DIR="$\{matrix_gf2e_dense}"
export MATRIX_MOD2_DENSE_DIR="$\{matrix_mod2_dense}"
export ODLYZKO_DIR="$\{odlyzko}"
export PBORI_DIR="$\{pbori}"
export SLOANE_DIR="$\{sloane}"
export STEIN_WATKINS_DIR="$\{stein_watkins}"
export SYMBOLIC_DATA_DIR="$\{symbolic_data}"
export THREEJS_DIR="${three}/lib/node_modules/three"
export CYSIGNALS_INCLUDE="${cysignals}/lib/python2.7/site-packages"
# for find_library
export DYLD_LIBRARY_PATH="${lib.makeLibraryPath [stdenv.cc.libc singular]}:\$DYLD_LIBRARY_PATH"
""" >> $out/bin/sage-env
substituteInPlace $out/bin/sage-env-orig \
--replace '[ ! -f "$SAGE_SCRIPTS_DIR/sage-env-config" ]' 'false' \
--replace '. "$SAGE_SCRIPTS_DIR/sage-env-config"' '# Nothing'
patchShebangs "$out" # done here in addition to fixup for the tests
'';
doCheck = true;
checkPhase = ''
sagehome="$TMP/sage-home"
mkdir -p "$sagehome"
# `env -i` because otherwise gcc gets overwhelmed by long LDFLAG lists etc.
env -i \
HOME="$sagehome" \
SHELL="${stdenv.shell}" \
TMP="$TMP" \
"$out/bin/sage" -t --nthreads "$NIX_BUILD_CORES" --timeout 0 --exitfirst --all
# TODO
#"$out/bin/sage" -t --nthreads "$NIX_BUILD_CORES" --timeout 0 --only-errors --exitfirst --long --all
'';
# TODO optionally enable sagedoc tests
}