summary refs log tree commit diff
path: root/pkgs/applications/science
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/applications/science')
-rw-r--r--pkgs/applications/science/astronomy/celestia/default.nix3
-rw-r--r--pkgs/applications/science/astronomy/gildas/clang.patch22
-rw-r--r--pkgs/applications/science/astronomy/gildas/default.nix68
-rw-r--r--pkgs/applications/science/astronomy/gildas/return-error-code.patch13
-rw-r--r--pkgs/applications/science/astronomy/gildas/wrapper.patch19
-rw-r--r--pkgs/applications/science/astronomy/gravit/default.nix5
-rw-r--r--pkgs/applications/science/astronomy/stellarium/default.nix23
-rw-r--r--pkgs/applications/science/biology/diamond/default.nix2
-rw-r--r--pkgs/applications/science/biology/freebayes/default.nix2
-rw-r--r--pkgs/applications/science/biology/hmmer/default.nix28
-rw-r--r--pkgs/applications/science/biology/igv/default.nix6
-rw-r--r--pkgs/applications/science/biology/iv/default.nix2
-rw-r--r--pkgs/applications/science/biology/mosdepth/default.nix42
-rw-r--r--pkgs/applications/science/biology/neuron/default.nix2
-rw-r--r--pkgs/applications/science/biology/picard-tools/default.nix4
-rw-r--r--pkgs/applications/science/biology/raxml/default.nix5
-rwxr-xr-xpkgs/applications/science/biology/vcftools/default.nix2
-rw-r--r--pkgs/applications/science/chemistry/jmol/default.nix11
-rw-r--r--pkgs/applications/science/chemistry/octopus/default.nix10
-rw-r--r--pkgs/applications/science/electronics/adms/default.nix2
-rw-r--r--pkgs/applications/science/electronics/bitscope/common.nix6
-rw-r--r--pkgs/applications/science/electronics/bitscope/packages.nix9
-rw-r--r--pkgs/applications/science/electronics/gtkwave/default.nix4
-rw-r--r--pkgs/applications/science/electronics/kicad/default.nix2
-rw-r--r--pkgs/applications/science/electronics/kicad/unstable.nix6
-rw-r--r--pkgs/applications/science/electronics/librepcb/default.nix16
-rw-r--r--pkgs/applications/science/electronics/librepcb/fix-2017-12.patch29
-rw-r--r--pkgs/applications/science/electronics/verilator/default.nix8
-rw-r--r--pkgs/applications/science/logic/abc/default.nix21
-rw-r--r--pkgs/applications/science/logic/acgtk/default.nix3
-rw-r--r--pkgs/applications/science/logic/aspino/default.nix2
-rw-r--r--pkgs/applications/science/logic/celf/default.nix2
-rw-r--r--pkgs/applications/science/logic/coq/8.3.nix2
-rw-r--r--pkgs/applications/science/logic/coq/8.4.nix2
-rw-r--r--pkgs/applications/science/logic/coq/default.nix1
-rw-r--r--pkgs/applications/science/logic/coq2html/default.nix2
-rw-r--r--pkgs/applications/science/logic/cvc4/default.nix14
-rw-r--r--pkgs/applications/science/logic/elan/default.nix2
-rw-r--r--pkgs/applications/science/logic/eprover/default.nix10
-rw-r--r--pkgs/applications/science/logic/hol_light/default.nix2
-rw-r--r--pkgs/applications/science/logic/isabelle/default.nix2
-rw-r--r--pkgs/applications/science/logic/lean2/default.nix2
-rw-r--r--pkgs/applications/science/logic/leo2/default.nix2
-rw-r--r--pkgs/applications/science/logic/mcrl2/default.nix7
-rw-r--r--pkgs/applications/science/logic/monosat/default.nix67
-rw-r--r--pkgs/applications/science/logic/poly/default.nix23
-rw-r--r--pkgs/applications/science/logic/potassco/clingo.nix4
-rw-r--r--pkgs/applications/science/logic/prooftree/default.nix2
-rw-r--r--pkgs/applications/science/logic/prover9/default.nix2
-rw-r--r--pkgs/applications/science/logic/satallax/default.nix2
-rw-r--r--pkgs/applications/science/logic/saw-tools/default.nix2
-rw-r--r--pkgs/applications/science/logic/tamarin-prover/default.nix2
-rw-r--r--pkgs/applications/science/logic/tlaplus/tlaps.nix6
-rw-r--r--pkgs/applications/science/logic/tlaplus/toolbox.nix4
-rw-r--r--pkgs/applications/science/logic/vampire/default.nix59
-rw-r--r--pkgs/applications/science/logic/why3/default.nix8
-rw-r--r--pkgs/applications/science/logic/workcraft/default.nix4
-rw-r--r--pkgs/applications/science/logic/yices/default.nix10
-rw-r--r--pkgs/applications/science/logic/z3/default.nix12
-rw-r--r--pkgs/applications/science/machine-learning/torch/torch-distro.nix6
-rw-r--r--pkgs/applications/science/math/R/default.nix11
-rw-r--r--pkgs/applications/science/math/bliss/default.nix37
-rw-r--r--pkgs/applications/science/math/cbc/default.nix2
-rw-r--r--pkgs/applications/science/math/clp/default.nix32
-rw-r--r--pkgs/applications/science/math/cntk/default.nix2
-rw-r--r--pkgs/applications/science/math/colpack/default.nix2
-rw-r--r--pkgs/applications/science/math/csdp/default.nix2
-rw-r--r--pkgs/applications/science/math/fricas/default.nix4
-rw-r--r--pkgs/applications/science/math/glsurf/default.nix2
-rw-r--r--pkgs/applications/science/math/gurobi/default.nix48
-rw-r--r--pkgs/applications/science/math/hmetis/default.nix42
-rw-r--r--pkgs/applications/science/math/mathematica/10.nix2
-rw-r--r--pkgs/applications/science/math/mathematica/9.nix2
-rw-r--r--pkgs/applications/science/math/mathematica/default.nix7
-rw-r--r--pkgs/applications/science/math/nasc/default.nix34
-rw-r--r--pkgs/applications/science/math/palp/default.nix19
-rw-r--r--pkgs/applications/science/math/pari/default.nix2
-rw-r--r--pkgs/applications/science/math/pari/gp2c.nix2
-rw-r--r--pkgs/applications/science/math/pynac/default.nix4
-rw-r--r--pkgs/applications/science/math/qalculate-gtk/default.nix12
-rw-r--r--pkgs/applications/science/math/sage/README.md117
-rw-r--r--pkgs/applications/science/math/sage/default.nix426
-rw-r--r--pkgs/applications/science/math/sage/env-locations.nix45
-rw-r--r--pkgs/applications/science/math/sage/env.patch22
-rwxr-xr-xpkgs/applications/science/math/sage/fetch-mirrors.sh11
-rw-r--r--pkgs/applications/science/math/sage/flask-oldsessions.nix36
-rw-r--r--pkgs/applications/science/math/sage/flask-openid.nix28
-rw-r--r--pkgs/applications/science/math/sage/openblas-pc.nix17
-rw-r--r--pkgs/applications/science/math/sage/patchSageShebangs.sh51
-rw-r--r--pkgs/applications/science/math/sage/patches/arb-2.13.0.patch427
-rw-r--r--pkgs/applications/science/math/sage/patches/dochtml-optional.patch127
-rw-r--r--pkgs/applications/science/math/sage/patches/eclib-20180710.patch40
-rw-r--r--pkgs/applications/science/math/sage/patches/eclib-regulator-precision.patch98
-rw-r--r--pkgs/applications/science/math/sage/patches/matplotlib-normed-deprecated.patch12
-rw-r--r--pkgs/applications/science/math/sage/patches/maxima-5.41.0-doctests.patch48
-rw-r--r--pkgs/applications/science/math/sage/patches/numpy-1.14.3.patch812
-rw-r--r--pkgs/applications/science/math/sage/patches/pari-stackwarn.patch20
-rw-r--r--pkgs/applications/science/math/sage/patches/pynac-0.7.22.patch479
-rw-r--r--pkgs/applications/science/math/sage/patches/python-5755-hotpatch.patch28
-rw-r--r--pkgs/applications/science/math/sage/patches/python3-syntax-without-write.patch40
-rw-r--r--pkgs/applications/science/math/sage/patches/sagenb-sphinx-1.7.patch31
-rw-r--r--pkgs/applications/science/math/sage/patches/sphinx-1.7.patch62
-rw-r--r--pkgs/applications/science/math/sage/patches/test-in-tmpdir.patch31
-rw-r--r--pkgs/applications/science/math/sage/patches/zn_poly_version.patch13
-rw-r--r--pkgs/applications/science/math/sage/pybrial.nix29
-rw-r--r--pkgs/applications/science/math/sage/python-openid.nix42
-rw-r--r--pkgs/applications/science/math/sage/sage-env.nix162
-rw-r--r--pkgs/applications/science/math/sage/sage-src.nix212
-rw-r--r--pkgs/applications/science/math/sage/sage-with-env.nix126
-rw-r--r--pkgs/applications/science/math/sage/sage-wrapper.nix41
-rw-r--r--pkgs/applications/science/math/sage/sage.nix32
-rw-r--r--pkgs/applications/science/math/sage/sagedoc.nix95
-rw-r--r--pkgs/applications/science/math/sage/sagelib.nix138
-rw-r--r--pkgs/applications/science/math/sage/sagenb.nix57
-rw-r--r--pkgs/applications/science/math/sage/shebangs.patch36
-rw-r--r--pkgs/applications/science/math/sage/spkg-giac.patch20
-rw-r--r--pkgs/applications/science/math/sage/spkg-git.patch18
-rw-r--r--pkgs/applications/science/math/sage/spkg-python.patch13
-rw-r--r--pkgs/applications/science/math/sage/spkg-singular.patch18
-rw-r--r--pkgs/applications/science/math/scilab-bin/default.nix2
-rw-r--r--pkgs/applications/science/math/scilab/default.nix4
-rw-r--r--pkgs/applications/science/math/scotch/default.nix2
-rw-r--r--pkgs/applications/science/math/singular/default.nix40
-rw-r--r--pkgs/applications/science/math/symmetrica/default.nix23
-rw-r--r--pkgs/applications/science/misc/gephi/default.nix2
-rw-r--r--pkgs/applications/science/misc/golly/beta.nix12
-rw-r--r--pkgs/applications/science/misc/golly/default.nix4
-rw-r--r--pkgs/applications/science/misc/openmvg/default.nix6
-rw-r--r--pkgs/applications/science/misc/openmvs/default.nix20
-rw-r--r--pkgs/applications/science/misc/root/default.nix2
-rw-r--r--pkgs/applications/science/molecular-dynamics/gromacs/default.nix6
-rw-r--r--pkgs/applications/science/physics/sacrifice/default.nix2
-rw-r--r--pkgs/applications/science/physics/sherpa/default.nix2
-rw-r--r--pkgs/applications/science/programming/scyther/default.nix6
-rw-r--r--pkgs/applications/science/robotics/gazebo/default.nix9
-rw-r--r--pkgs/applications/science/robotics/qgroundcontrol/default.nix6
-rw-r--r--pkgs/applications/science/robotics/yarp/default.nix2
-rw-r--r--pkgs/applications/science/spyder/default.nix4
138 files changed, 4412 insertions, 710 deletions
diff --git a/pkgs/applications/science/astronomy/celestia/default.nix b/pkgs/applications/science/astronomy/celestia/default.nix
index 589625cc18f9..e3ceffd34c19 100644
--- a/pkgs/applications/science/astronomy/celestia/default.nix
+++ b/pkgs/applications/science/astronomy/celestia/default.nix
@@ -1,6 +1,5 @@
 { stdenv, fetchurl, freeglut, gtk2, gtkglext, libjpeg_turbo, libtheora, libXmu
-, lua, libGLU_combined, pkgconfig, perl, autoreconfHook, glib, cairo
-, pango, gdk_pixbuf, atk
+, lua, libGLU_combined, pkgconfig, perl, autoreconfHook
 }:
 
 let
diff --git a/pkgs/applications/science/astronomy/gildas/clang.patch b/pkgs/applications/science/astronomy/gildas/clang.patch
new file mode 100644
index 000000000000..4d6654a99aee
--- /dev/null
+++ b/pkgs/applications/science/astronomy/gildas/clang.patch
@@ -0,0 +1,22 @@
+diff -ruN gildas-src-jun18a/admin/define-system.sh gildas-src-jun18a.clang/admin/define-system.sh
+--- gildas-src-jun18a/admin/define-system.sh	2018-03-12 11:07:57.000000000 +0100
++++ gildas-src-jun18a.clang/admin/define-system.sh	2018-06-12 14:56:14.000000000 +0200
+@@ -218,13 +218,13 @@
+         	else
+         	    GAG_MACHINE=pc
+         	fi
+-                if which gcc > /dev/null 2>&1; then
+-                    DEFAULT_CCOMPILER=gcc
++                if which clang > /dev/null 2>&1; then
++                    DEFAULT_CCOMPILER=clang
+                 fi
+-                if which g++ > /dev/null 2>&1; then
+-                    DEFAULT_CXXCOMPILER=g++
+-                elif which clang++ > /dev/null 2>&1; then
++                if which clang++ > /dev/null 2>&1; then
+                     DEFAULT_CXXCOMPILER=clang++
++                elif which g++ > /dev/null 2>&1; then
++                    DEFAULT_CXXCOMPILER=g++
+                 fi
+         	if which ifort > /dev/null 2>&1; then
+         	    DEFAULT_FCOMPILER=ifort
diff --git a/pkgs/applications/science/astronomy/gildas/default.nix b/pkgs/applications/science/astronomy/gildas/default.nix
new file mode 100644
index 000000000000..bd4587bf2276
--- /dev/null
+++ b/pkgs/applications/science/astronomy/gildas/default.nix
@@ -0,0 +1,68 @@
+{ stdenv, fetchurl, gtk2-x11 , pkgconfig , python27 , gfortran , lesstif
+, cfitsio , getopt , perl , groff , which
+}:
+
+let
+  python27Env = python27.withPackages(ps: with ps; [ numpy ]);
+in
+
+stdenv.mkDerivation rec {
+  srcVersion = "jun18a";
+  version = "20180601_a";
+  name = "gildas-${version}";
+
+  src = fetchurl {
+    url = "http://www.iram.fr/~gildas/dist/gildas-src-${srcVersion}.tar.gz";
+    sha256 = "0k4x0g69fphb1759cwcw6bbs8imwmq0qwj6zqixxk60skk4n4jvb";
+  };
+
+  enableParallelBuilding = true;
+
+  nativeBuildInputs = [ pkgconfig groff perl getopt gfortran which ];
+
+  buildInputs = [ gtk2-x11 lesstif cfitsio python27Env ];
+
+  patches = [ ./wrapper.patch ./return-error-code.patch ./clang.patch ];
+
+  configurePhase=''
+    substituteInPlace admin/wrapper.sh --replace '%%OUT%%' $out
+    substituteInPlace admin/wrapper.sh --replace '%%PYTHONHOME%%' ${python27Env}
+    source admin/gildas-env.sh -c gfortran -o openmp
+    echo "gag_doc:        $out/share/doc/" >> kernel/etc/gag.dico.lcl
+  '';
+
+  buildPhase=''
+    make depend
+    make
+  '';
+
+  postInstall=''
+    mkdir -p $out/bin
+    cp -a ../gildas-exe-${srcVersion}/* $out
+    mv $out/$GAG_EXEC_SYSTEM $out/libexec
+    cp admin/wrapper.sh $out/bin/gildas-wrapper.sh
+    chmod 755 $out/bin/gildas-wrapper.sh
+    for i in $out/libexec/bin/* ; do
+      ln -s $out/bin/gildas-wrapper.sh $out/bin/$(basename "$i")
+    done
+  '';
+
+  meta = {
+    description = "Radioastronomy data analysis software";
+    longDescription = ''
+      GILDAS is a collection of state-of-the-art software
+      oriented toward (sub-)millimeter radioastronomical
+      applications (either single-dish or interferometer).
+      It is daily used to reduce all data acquired with the
+      IRAM 30M telescope and Plateau de Bure Interferometer
+      PDBI (except VLBI observations). GILDAS is easily
+      extensible. GILDAS is written in Fortran-90, with a
+      few parts in C/C++ (mainly keyboard interaction,
+      plotting, widgets).'';
+    homepage = http://www.iram.fr/IRAMFR/GILDAS/gildas.html;
+    license = stdenv.lib.licenses.free;
+    maintainers = [ stdenv.lib.maintainers.bzizou stdenv.lib.maintainers.smaret ];
+    platforms = stdenv.lib.platforms.all;
+  };
+
+}
diff --git a/pkgs/applications/science/astronomy/gildas/return-error-code.patch b/pkgs/applications/science/astronomy/gildas/return-error-code.patch
new file mode 100644
index 000000000000..f0bd7da9354e
--- /dev/null
+++ b/pkgs/applications/science/astronomy/gildas/return-error-code.patch
@@ -0,0 +1,13 @@
+diff --new-file -r -u gildas-src-dec17a.orig/admin/gildas-env.sh gildas-src-dec17a/admin/gildas-env.sh
+--- gildas-src-dec17a.orig/admin/gildas-env.sh	2017-10-24 11:39:18.000000000 +0200
++++ gildas-src-dec17a/admin/gildas-env.sh	2017-12-01 11:17:32.051953670 +0100
+@@ -1010,6 +1010,9 @@
+     gagenv_message "$gagenv_errors error(s) and $gagenv_warnings warning(s) detected"
+     if [ $gagenv_errors -ne 0 ]; then
+         gagenv_message "GILDAS will not compile"
++	gagenv_clean
++        echo
++        \return 1
+     elif [ $gagenv_warnings -ne 0 ]; then
+         gagenv_message "GILDAS will compile with some optional features disabled"
+     else
diff --git a/pkgs/applications/science/astronomy/gildas/wrapper.patch b/pkgs/applications/science/astronomy/gildas/wrapper.patch
new file mode 100644
index 000000000000..2e470a1e9cdc
--- /dev/null
+++ b/pkgs/applications/science/astronomy/gildas/wrapper.patch
@@ -0,0 +1,19 @@
+diff --new-file -r -u gildas-src-feb17d.orig/admin/wrapper.sh gildas-src-feb17d/admin/wrapper.sh
+--- gildas-src-feb17d.orig/admin/wrapper.sh	1970-01-01 01:00:00.000000000 +0100
++++ gildas-src-feb17d/admin/wrapper.sh	2017-05-18 21:00:01.660778782 +0200
+@@ -0,0 +1,15 @@
++#!/bin/sh -e
++
++export GAG_ROOT_DIR="%%OUT%%"
++export GAG_PATH="${GAG_ROOT_DIR}/etc"
++export GAG_EXEC_SYSTEM="libexec"
++export PYTHONHOME="%%PYTHONHOME%%"
++if [ -z "\$PYTHONPATH" ]; then
++  PYTHONPATH="${GAG_ROOT_DIR}/${GAG_EXEC_SYSTEM}/python"
++else
++  PYTHONPATH="${GAG_ROOT_DIR}/${GAG_EXEC_SYSTEM}/python:${PYTHONPATH}"
++fi
++export PYTHONPATH
++export LD_LIBRARY_PATH=${GAG_ROOT_DIR}/${GAG_EXEC_SYSTEM}/lib/
++me=`basename $0`
++exec ${GAG_ROOT_DIR}/${GAG_EXEC_SYSTEM}/bin/${me} ${*}
diff --git a/pkgs/applications/science/astronomy/gravit/default.nix b/pkgs/applications/science/astronomy/gravit/default.nix
index d572dd55b2b2..03d0018f237b 100644
--- a/pkgs/applications/science/astronomy/gravit/default.nix
+++ b/pkgs/applications/science/astronomy/gravit/default.nix
@@ -4,7 +4,7 @@ stdenv.mkDerivation rec {
   name = "gravit-0.5.1";
 
   src = fetchurl {
-    url = "http://gravit.slowchop.com/media/downloads/${name}.tgz";
+    url = "https://gravit.slowchop.com/media/downloads/${name}.tgz";
     sha256 = "14vf7zj2bgrl96wsl3f1knsggc8h9624354ajzd72l46y09x5ky7";
   };
 
@@ -22,7 +22,7 @@ stdenv.mkDerivation rec {
   enableParallelBuilding = true;
 
   meta = {
-    homepage = http://gravit.slowchop.com;
+    homepage = https://gravit.slowchop.com;
     description = "Beautiful OpenGL-based gravity simulator";
     license = stdenv.lib.licenses.gpl2;
 
@@ -37,5 +37,6 @@ stdenv.mkDerivation rec {
     '';
 
     platforms = stdenv.lib.platforms.mesaPlatforms;
+    hydraPlatforms = stdenv.lib.platforms.linux; # darwin times out
   };
 }
diff --git a/pkgs/applications/science/astronomy/stellarium/default.nix b/pkgs/applications/science/astronomy/stellarium/default.nix
index 3c7af809b884..24d6246e90cd 100644
--- a/pkgs/applications/science/astronomy/stellarium/default.nix
+++ b/pkgs/applications/science/astronomy/stellarium/default.nix
@@ -1,25 +1,32 @@
-{ mkDerivation, lib, fetchurl
-, cmake, freetype, libpng, libGLU_combined, gettext, openssl, perl, libiconv
+{ mkDerivation, lib, fetchFromGitHub
+, cmake, freetype, libpng, libGLU_combined, openssl, perl, libiconv
 , qtscript, qtserialport, qttools
-, qtmultimedia, qtlocation
+, qtmultimedia, qtlocation, makeWrapper, qtbase
 }:
 
 mkDerivation rec {
   name = "stellarium-${version}";
-  version = "0.16.1";
+  version = "0.18.1";
 
-  src = fetchurl {
-    url = "mirror://sourceforge/stellarium/${name}.tar.gz";
-    sha256 = "087x6mbcn2yj8d3qi382vfkzgdwmanxzqi5l1x3iranxmx9c40dh";
+  src = fetchFromGitHub {
+    owner = "Stellarium";
+    repo = "stellarium";
+    rev = "v${version}";
+    sha256 = "0srwi08azzzayf50dr4dr1zcdcc8hwribzv7xvb7hbp6xp51c813";
   };
 
   nativeBuildInputs = [ cmake perl ];
 
   buildInputs = [
     freetype libpng libGLU_combined openssl libiconv qtscript qtserialport qttools
-    qtmultimedia qtlocation
+    qtmultimedia qtlocation qtbase makeWrapper
   ];
 
+  postInstall = ''
+    wrapProgram $out/bin/stellarium \
+      --prefix QT_PLUGIN_PATH : "${qtbase}/lib/qt-5.${lib.versions.minor qtbase.version}/plugins"
+  '';
+
   meta = with lib; {
     description = "Free open-source planetarium";
     homepage = http://stellarium.org/;
diff --git a/pkgs/applications/science/biology/diamond/default.nix b/pkgs/applications/science/biology/diamond/default.nix
index a45e5df2f528..854e91b2fcbb 100644
--- a/pkgs/applications/science/biology/diamond/default.nix
+++ b/pkgs/applications/science/biology/diamond/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, cmake, gcc, zlib }:
+{ stdenv, fetchurl, cmake, zlib }:
 
 stdenv.mkDerivation rec {
   name = "diamond-0.8.36";
diff --git a/pkgs/applications/science/biology/freebayes/default.nix b/pkgs/applications/science/biology/freebayes/default.nix
index 99d2dfa55ddd..1a15dcf6197d 100644
--- a/pkgs/applications/science/biology/freebayes/default.nix
+++ b/pkgs/applications/science/biology/freebayes/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, cmake, gcc, zlib, bzip2, lzma }:
+{ stdenv, fetchFromGitHub, zlib, bzip2, lzma }:
 
 stdenv.mkDerivation rec {
   name    = "freebayes-${version}";
diff --git a/pkgs/applications/science/biology/hmmer/default.nix b/pkgs/applications/science/biology/hmmer/default.nix
new file mode 100644
index 000000000000..8552aa34c433
--- /dev/null
+++ b/pkgs/applications/science/biology/hmmer/default.nix
@@ -0,0 +1,28 @@
+{ stdenv, fetchurl }:
+
+stdenv.mkDerivation rec {
+  version = "3.1b2";
+  name = "hmmer-${version}";
+
+  src = fetchurl {
+    url = "http://eddylab.org/software/hmmer3/${version}/${name}.tar.gz";
+    sha256 = "0djmgc0pfli0jilfx8hql1axhwhqxqb8rxg2r5rg07aw73sfs5nx";
+  };
+
+  meta = with stdenv.lib; {
+    description = "Biosequence analysis using profile hidden Markov models";
+    longDescription = ''
+HMMER is used for searching sequence databases for sequence homologs, and for making sequence alignments. It implements methods using probabilistic models called profile hidden Markov models (profile HMMs).
+
+HMMER is often used together with a profile database, such as Pfam or many of the databases that participate in Interpro. But HMMER can also work with query sequences, not just profiles, just like BLAST. For example, you can search a protein query sequence against a database with phmmer, or do an iterative search with jackhmmer.
+
+HMMER is designed to detect remote homologs as sensitively as possible, relying on the strength of its underlying probability models. In the past, this strength came at significant computational expense, but as of the new HMMER3 project, HMMER is now essentially as fast as BLAST.
+
+HMMER can be downloaded and installed as a command line tool on your own hardware, and now it is also more widely accessible to the scientific community via new search servers at the European Bioinformatics Institute.
+    '';
+    homepage = http://hmmer.org/;
+    license = licenses.gpl3;
+    maintainers = [ maintainers.iimog ];
+    platforms = platforms.linux;
+  };
+}
diff --git a/pkgs/applications/science/biology/igv/default.nix b/pkgs/applications/science/biology/igv/default.nix
index bf963958406d..2df8b188f785 100644
--- a/pkgs/applications/science/biology/igv/default.nix
+++ b/pkgs/applications/science/biology/igv/default.nix
@@ -2,11 +2,11 @@
 
 stdenv.mkDerivation rec {
   name = "igv-${version}";
-  version = "2.4.10";
+  version = "2.4.13";
 
   src = fetchurl {
-    url = "http://data.broadinstitute.org/igv/projects/downloads/2.4/IGV_${version}.zip";
-    sha256 = "1wyv3ny06m6ipb83hi3pzcc50v49zms69c5714iixh0nqpd826f2";
+    url = "https://data.broadinstitute.org/igv/projects/downloads/2.4/IGV_${version}.zip";
+    sha256 = "0cl6fs1v9rqkzjq4syx0m1pr71hbhvidwa3ym57pvz8ardfs3qa1";
   };
 
   buildInputs = [ unzip jre ];
diff --git a/pkgs/applications/science/biology/iv/default.nix b/pkgs/applications/science/biology/iv/default.nix
index 2258482bd4b2..f204d9bbca96 100644
--- a/pkgs/applications/science/biology/iv/default.nix
+++ b/pkgs/applications/science/biology/iv/default.nix
@@ -5,7 +5,7 @@
 stdenv.mkDerivation rec
   { name = "iv-19";
     src = fetchurl
-      { url = "http://www.neuron.yale.edu/ftp/neuron/versions/v${neuron-version}/${name}.tar.gz";
+      { url = "https://www.neuron.yale.edu/ftp/neuron/versions/v${neuron-version}/${name}.tar.gz";
         sha256 = "07a3g8zzay4h0bls7fh89dd0phn7s34c2g15pij6dsnwpmjg06yx";
       };
     nativeBuildInputs = [ patchelf ];
diff --git a/pkgs/applications/science/biology/mosdepth/default.nix b/pkgs/applications/science/biology/mosdepth/default.nix
new file mode 100644
index 000000000000..4b4920a6ca3a
--- /dev/null
+++ b/pkgs/applications/science/biology/mosdepth/default.nix
@@ -0,0 +1,42 @@
+{stdenv, fetchFromGitHub, nim, htslib, pcre}:
+
+let
+  hts-nim = fetchFromGitHub {
+    owner = "brentp";
+    repo = "hts-nim";
+    rev = "9cd83e30522ab64cd71eb8209be4154aa5579ce1";
+    sha256 = "10g408idy14667varq1syf06rrbpk63i3ib7i5dh1md4ib19av6f";
+  };
+
+  docopt = fetchFromGitHub {
+    owner = "docopt";
+    repo = "docopt.nim";
+    rev = "v0.6.5";
+    sha256 = "0yx79m4jkdcazwlky55nwf39zj5kdhymrrdrjq29mahiwx83x5zr";
+  };
+
+in stdenv.mkDerivation rec {
+  name = "mosdepth-${version}";
+  version = "0.2.3";
+
+  src = fetchFromGitHub {
+    owner = "brentp";
+    repo = "mosdepth";
+    rev = "v${version}";
+    sha256 = "1b9frrwhcvay3alhn0d02jccc2qlbij1732hzq9nhwnr4kvsvxx7";
+  };
+
+  buildInputs = [ nim ];
+
+  buildPhase = "nim -p:${hts-nim}/src -p:${docopt}/src c -d:release mosdepth.nim";
+  installPhase = "install -Dt $out/bin mosdepth";
+  fixupPhase = "patchelf --set-rpath ${stdenv.lib.makeLibraryPath [ stdenv.cc.cc htslib pcre ]} $out/bin/mosdepth";
+
+  meta = with stdenv.lib; {
+    description = "fast BAM/CRAM depth calculation for WGS, exome, or targeted sequencing.";
+    license = licenses.mit;
+    homepage = https://github.com/brentp/mosdepth;
+    maintainers = with maintainers; [ jbedo ];
+    platforms = platforms.linux;
+  };
+}
diff --git a/pkgs/applications/science/biology/neuron/default.nix b/pkgs/applications/science/biology/neuron/default.nix
index 7fa229c42b10..35710fd1fe1f 100644
--- a/pkgs/applications/science/biology/neuron/default.nix
+++ b/pkgs/applications/science/biology/neuron/default.nix
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
   buildInputs = [ ncurses readline python mpi iv ];
 
   src = fetchurl {
-    url = "http://www.neuron.yale.edu/ftp/neuron/versions/v${version}/nrn-${version}.tar.gz";
+    url = "https://www.neuron.yale.edu/ftp/neuron/versions/v${version}/nrn-${version}.tar.gz";
     sha256 = "0f26v3qvzblcdjg7isq0m9j2q8q7x3vhmkfllv8lsr3gyj44lljf";
   };
 
diff --git a/pkgs/applications/science/biology/picard-tools/default.nix b/pkgs/applications/science/biology/picard-tools/default.nix
index 555542b76318..a08a54c94ecd 100644
--- a/pkgs/applications/science/biology/picard-tools/default.nix
+++ b/pkgs/applications/science/biology/picard-tools/default.nix
@@ -2,11 +2,11 @@
 
 stdenv.mkDerivation rec {
   name = "picard-tools-${version}";
-  version = "2.18.5";
+  version = "2.18.10";
 
   src = fetchurl {
     url = "https://github.com/broadinstitute/picard/releases/download/${version}/picard.jar";
-    sha256 = "1lk1qnwc2gwjh8js3rx4qayan5157an6yzxnhija6kqgrxv58f11";
+    sha256 = "1px5ar6iizm4czpc66hyyafandw6g9x9s1abj8klc78alvihciw4";
   };
 
   buildInputs = [ jre makeWrapper ];
diff --git a/pkgs/applications/science/biology/raxml/default.nix b/pkgs/applications/science/biology/raxml/default.nix
index 0bac6c778049..140f70323194 100644
--- a/pkgs/applications/science/biology/raxml/default.nix
+++ b/pkgs/applications/science/biology/raxml/default.nix
@@ -1,20 +1,19 @@
 { stdenv
 , fetchFromGitHub
-, zlib
 , pkgs
 , mpi ? false
 }:
 
 stdenv.mkDerivation rec {
   pname = "RAxML";
-  version = "8.2.11";
+  version = "8.2.12";
   name = "${pname}-${version}";
 
   src = fetchFromGitHub {
     owner = "stamatak";
     repo = "standard-${pname}";
     rev = "v${version}";
-    sha256 = "08fmqrr7y5a2fmmrgfz2p0hmn4mn71l5yspxfcwwsqbw6vmdfkhg";
+    sha256 = "1jqjzhch0rips0vp04prvb8vmc20c5pdmsqn8knadcf91yy859fh";
   };
 
   buildInputs = if mpi then [ pkgs.openmpi ] else [];
diff --git a/pkgs/applications/science/biology/vcftools/default.nix b/pkgs/applications/science/biology/vcftools/default.nix
index ab3050955df7..394973692dca 100755
--- a/pkgs/applications/science/biology/vcftools/default.nix
+++ b/pkgs/applications/science/biology/vcftools/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, htslib, zlib, autoreconfHook, pkgconfig, perl }:
+{ stdenv, fetchFromGitHub, zlib, autoreconfHook, pkgconfig, perl }:
 
 stdenv.mkDerivation rec {
   name = "${pname}-${version}";
diff --git a/pkgs/applications/science/chemistry/jmol/default.nix b/pkgs/applications/science/chemistry/jmol/default.nix
index 4c4278c30faa..d5dae364cc3d 100644
--- a/pkgs/applications/science/chemistry/jmol/default.nix
+++ b/pkgs/applications/science/chemistry/jmol/default.nix
@@ -1,4 +1,5 @@
 { stdenv
+, lib
 , fetchurl
 , unzip
 , makeDesktopItem
@@ -16,15 +17,15 @@ let
   };
 in
 stdenv.mkDerivation rec {
-  version = "${baseVersion}.${patchVersion}";
-  baseVersion = "14.29";
-  patchVersion = "12";
+  version = "14.29.17";
   pname = "jmol";
   name = "${pname}-${version}";
 
-  src = fetchurl {
+  src = let 
+    baseVersion = "${lib.versions.major version}.${lib.versions.minor version}";
+  in fetchurl {
     url = "mirror://sourceforge/jmol/Jmol/Version%20${baseVersion}/Jmol%20${version}/Jmol-${version}-binary.tar.gz";
-    sha256 = "1ndq9am75janshrnk26334z1nmyh3k4bp20napvf2zv0lfp8k3bv";
+    sha256 = "1dnxbvi8ha9z2ldymkjpxydd216afv6k7fdp3j70sql10zgy0isk";
   };
 
   patchPhase = ''
diff --git a/pkgs/applications/science/chemistry/octopus/default.nix b/pkgs/applications/science/chemistry/octopus/default.nix
index bf8ccee4a635..0d8ab0d0e191 100644
--- a/pkgs/applications/science/chemistry/octopus/default.nix
+++ b/pkgs/applications/science/chemistry/octopus/default.nix
@@ -1,9 +1,9 @@
 { stdenv, fetchurl, symlinkJoin, gfortran, perl, procps
-, libyaml, libxc, fftw, openblas, gsl
+, libyaml, libxc, fftw, openblas, gsl, netcdf, arpack
 }:
 
 let
-  version = "7.3";
+  version = "8.1";
   fftwAll = symlinkJoin { name ="ftw-dev-out"; paths = [ fftw.dev fftw.out ]; };
 
 in stdenv.mkDerivation {
@@ -11,11 +11,11 @@ in stdenv.mkDerivation {
 
   src = fetchurl {
     url = "http://www.tddft.org/programs/octopus/down.php?file=${version}/octopus-${version}.tar.gz";
-    sha256 = "0hnpqjjxdxh2ggf6ckrsy4hs9iglnazscb4siczddvmysi4kv15d";
+    sha256 = "0rxwvcp22364nnhwhqlr38w4rwv1yl60snxi2f8nqdflx1143n10";
   };
 
   nativeBuildInputs = [ perl procps fftw.dev ];
-  buildInputs = [ libyaml gfortran libxc openblas gsl fftw.out ];
+  buildInputs = [ libyaml gfortran libxc openblas gsl fftw.out netcdf arpack ];
 
   configureFlags = ''
     --with-yaml-prefix=${libyaml}
@@ -37,6 +37,8 @@ in stdenv.mkDerivation {
     patchShebangs testsuite/oct-run_testsuite.sh
   '';
 
+  enableParallelBuilding = true;
+
   meta = with stdenv.lib; {
     description = "Real-space time dependent density-functional theory code";
     homepage = http://octopus-code.org;
diff --git a/pkgs/applications/science/electronics/adms/default.nix b/pkgs/applications/science/electronics/adms/default.nix
index 99b7ef79594f..236225959f7a 100644
--- a/pkgs/applications/science/electronics/adms/default.nix
+++ b/pkgs/applications/science/electronics/adms/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, libtool, autoreconfHook, flex, bison, gperf,
+{ stdenv, fetchFromGitHub, autoreconfHook, flex, bison, gperf,
   libxml2, perl, perlPackages, gd }:
 
 stdenv.mkDerivation rec {
diff --git a/pkgs/applications/science/electronics/bitscope/common.nix b/pkgs/applications/science/electronics/bitscope/common.nix
index e1db7131a65b..64777b5a0ac8 100644
--- a/pkgs/applications/science/electronics/bitscope/common.nix
+++ b/pkgs/applications/science/electronics/bitscope/common.nix
@@ -2,14 +2,12 @@
 , buildFHSUserEnv
 , cairo
 , dpkg
-, fetchurl
 , gdk_pixbuf
 , glib
 , gtk2-x11
 , makeWrapper
 , pango
 , stdenv
-, writeTextFile
 , xorg
 }:
 
@@ -60,10 +58,6 @@ let
       ${(wrapBinary libs) attrs.toolName}
     '';
   });
-  fhs = target: buildFHSUserEnv {
-    inherit (pkg) name;
-    runScript = target;
-  };
 in buildFHSUserEnv {
   name = "${attrs.toolName}-${attrs.version}";
   runScript = "${pkg.outPath}/bin/${attrs.toolName}";
diff --git a/pkgs/applications/science/electronics/bitscope/packages.nix b/pkgs/applications/science/electronics/bitscope/packages.nix
index c10e9de851a4..58e01f4f9f49 100644
--- a/pkgs/applications/science/electronics/bitscope/packages.nix
+++ b/pkgs/applications/science/electronics/bitscope/packages.nix
@@ -1,15 +1,8 @@
-{ buildFHSUserEnv
-, callPackage
+{ callPackage
 , fetchurl
-, makeWrapper
-, stdenv
 }:
 
 let
-  wrapBinary = libPaths: binaryName: ''
-    wrapProgram "$out/bin/${binaryName}" \
-      --prefix LD_LIBRARY_PATH : "${stdenv.lib.makeLibraryPath libPaths}"
-  '';
   mkBitscope = callPackage (import ./common.nix) { };
 in {
   chart = let
diff --git a/pkgs/applications/science/electronics/gtkwave/default.nix b/pkgs/applications/science/electronics/gtkwave/default.nix
index 3583c3dbd19c..d4cf7f5ea907 100644
--- a/pkgs/applications/science/electronics/gtkwave/default.nix
+++ b/pkgs/applications/science/electronics/gtkwave/default.nix
@@ -2,11 +2,11 @@
 
 stdenv.mkDerivation rec {
   name = "gtkwave-${version}";
-  version = "3.3.91";
+  version = "3.3.92";
 
   src = fetchurl {
     url    = "mirror://sourceforge/gtkwave/${name}.tar.gz";
-    sha256 = "1vp9qj3wyfwm36jk3pajvi09xvc1m1crf3d4gphfbs6nkyx2z942";
+    sha256 = "08916gv5vl7dgvlsq098mxljbciby9miyh8jqwn1q8sfdhzvmxak";
   };
 
   nativeBuildInputs = [ pkgconfig ];
diff --git a/pkgs/applications/science/electronics/kicad/default.nix b/pkgs/applications/science/electronics/kicad/default.nix
index 8e98bad55c3d..e3244ec339ea 100644
--- a/pkgs/applications/science/electronics/kicad/default.nix
+++ b/pkgs/applications/science/electronics/kicad/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, fetchbzr, cmake, libGLU_combined, wxGTK, zlib, libX11, gettext, glew, cairo, curl, openssl, boost, pkgconfig, doxygen }:
+{ stdenv, fetchurl, cmake, libGLU_combined, wxGTK, zlib, libX11, gettext, glew, cairo, curl, openssl, boost, pkgconfig, doxygen }:
 
 stdenv.mkDerivation rec {
   name = "kicad-${version}";
diff --git a/pkgs/applications/science/electronics/kicad/unstable.nix b/pkgs/applications/science/electronics/kicad/unstable.nix
index eca3ddd32e8b..e43fd59e0431 100644
--- a/pkgs/applications/science/electronics/kicad/unstable.nix
+++ b/pkgs/applications/science/electronics/kicad/unstable.nix
@@ -2,7 +2,7 @@
 , libX11, gettext, glew, glm, cairo, curl, openssl, boost, pkgconfig
 , doxygen, pcre, libpthreadstubs, libXdmcp
 
-, oceSupport ? true, opencascade_oce
+, oceSupport ? true, opencascade
 , ngspiceSupport ? true, libngspice
 , scriptingSupport ? true, swig, python, wxPython
 }:
@@ -27,7 +27,7 @@ stdenv.mkDerivation rec {
   '';
 
   cmakeFlags =
-    optionals (oceSupport) [ "-DKICAD_USE_OCE=ON" "-DOCE_DIR=${opencascade_oce}" ]
+    optionals (oceSupport) [ "-DKICAD_USE_OCE=ON" "-DOCE_DIR=${opencascade}" ]
     ++ optional (ngspiceSupport) "-DKICAD_SPICE=ON"
     ++ optionals (scriptingSupport) [
       "-DKICAD_SCRIPTING=ON"
@@ -43,7 +43,7 @@ stdenv.mkDerivation rec {
   buildInputs = [
     libGLU_combined zlib libX11 wxGTK pcre libXdmcp gettext glew glm libpthreadstubs
     cairo curl openssl boost
-  ] ++ optional (oceSupport) opencascade_oce
+  ] ++ optional (oceSupport) opencascade
     ++ optional (ngspiceSupport) libngspice
     ++ optionals (scriptingSupport) [ swig python wxPython ];
 
diff --git a/pkgs/applications/science/electronics/librepcb/default.nix b/pkgs/applications/science/electronics/librepcb/default.nix
index ea0ebd52b3eb..33eb52c18ee7 100644
--- a/pkgs/applications/science/electronics/librepcb/default.nix
+++ b/pkgs/applications/science/electronics/librepcb/default.nix
@@ -1,15 +1,15 @@
-{ stdenv, fetchFromGitHub, qtbase, qttools, qmake, libGLU_combined, openssl, zlib }:
+{ stdenv, fetchFromGitHub, qtbase, qttools, qmake }:
 
 stdenv.mkDerivation rec {
   name = "librepcb-${version}";
-  version = "20171229";
+  version = "20180628";
 
   src = fetchFromGitHub {
     owner = "LibrePCB";
     repo = "LibrePCB";
     fetchSubmodules = true;
-    rev = "4efb06fa42755abc5e606da4669cc17e8de2f8c6";
-    sha256 = "0r33fm1djqpy0dzvnf5gv2dfh5nj2acaxb7w4cn8yxdgrazjf7ak";
+    rev = "68577ecf8f39299ef4d81ff964b01c3908d1f10b";
+    sha256 = "1ca4q8b8fhp19vq5yi55sq6xlsz14ihw3i0h7rq5fw0kigpjldmz";
   };
 
   enableParallelBuilding = true;
@@ -18,11 +18,13 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ qtbase ];
 
-	# LibrePCB still supports QT below 5.9. But some code lines break the build, so they are removed by this patch so that the software builds.
-  patches = [ ./fix-2017-12.patch ];
-
   qmakeFlags = ["-r"];
 
+  postInstall = ''
+      mkdir -p $out/share/librepcb/fontobene
+      cp share/librepcb/fontobene/newstroke.bene $out/share/librepcb/fontobene/
+    '';
+
   meta = with stdenv.lib; {
     description = "A free EDA software to develop printed circuit boards";
     homepage = http://librepcb.org/;
diff --git a/pkgs/applications/science/electronics/librepcb/fix-2017-12.patch b/pkgs/applications/science/electronics/librepcb/fix-2017-12.patch
deleted file mode 100644
index 75fc590ad7fe..000000000000
--- a/pkgs/applications/science/electronics/librepcb/fix-2017-12.patch
+++ /dev/null
@@ -1,29 +0,0 @@
---- a/libs/librepcb/common/fileio/serializableobjectlist.h
-+++ b/libs/librepcb/common/fileio/serializableobjectlist.h
-@@ -374,26 +374,6 @@
- } // namespace librepcb
-
- /*****************************************************************************************
-- * Prevent from using SerializableObjectList in a foreach loop because it always would
-- * create a deep copy of the list! You should use C++11 range based for loops instead.
-- ****************************************************************************************/
--
--#if (QT_VERSION > QT_VERSION_CHECK(5, 9, 0))
--#define QFOREACHCONTAINER_TEMPLATE QtPrivate::QForeachContainer
--#else
--#define QFOREACHCONTAINER_TEMPLATE QForeachContainer
--#endif
--
--template <typename T, typename P>
--class QFOREACHCONTAINER_TEMPLATE<librepcb::SerializableObjectList<T, P>> { public:
--    ~QForeachContainer() = delete;
--};
--template <typename T, typename P>
--class QFOREACHCONTAINER_TEMPLATE<const librepcb::SerializableObjectList<T, P>> { public:
--    ~QForeachContainer() = delete;
--};
--
--/*****************************************************************************************
-  *  End of File
-  ****************************************************************************************/
-
diff --git a/pkgs/applications/science/electronics/verilator/default.nix b/pkgs/applications/science/electronics/verilator/default.nix
index 75e99dcf6594..6ee3f3266f8d 100644
--- a/pkgs/applications/science/electronics/verilator/default.nix
+++ b/pkgs/applications/science/electronics/verilator/default.nix
@@ -2,11 +2,11 @@
 
 stdenv.mkDerivation rec {
   name    = "verilator-${version}";
-  version = "3.922";
+  version = "3.924";
 
   src = fetchurl {
-    url    = "http://www.veripool.org/ftp/${name}.tgz";
-    sha256 = "1srv8d1w3mwblfydznl3frswg98i3dkylx8x18c4807wsjk8vflg";
+    url    = "https://www.veripool.org/ftp/${name}.tgz";
+    sha256 = "0xarifraycmdzjxs0lscpwvrwr59z84p8g8dkyjssc463dqikjvx";
   };
 
   enableParallelBuilding = true;
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
     description = "Fast and robust (System)Verilog simulator/compiler";
     homepage    = "https://www.veripool.org/wiki/verilator";
     license     = stdenv.lib.licenses.lgpl3;
-    platforms   = stdenv.lib.platforms.linux;
+    platforms   = stdenv.lib.platforms.unix;
     maintainers = with stdenv.lib.maintainers; [ thoughtpolice ];
   };
 }
diff --git a/pkgs/applications/science/logic/abc/default.nix b/pkgs/applications/science/logic/abc/default.nix
index bab9b302d7d1..6e7a3cfc88ac 100644
--- a/pkgs/applications/science/logic/abc/default.nix
+++ b/pkgs/applications/science/logic/abc/default.nix
@@ -1,20 +1,21 @@
-{ fetchhg, stdenv, readline }:
+{ fetchFromGitHub, stdenv, readline, cmake }:
 
 stdenv.mkDerivation rec {
   name = "abc-verifier-${version}";
-  version = "20160818";
+  version = "2018-07-08";
 
-  src = fetchhg {
-    url    = "https://bitbucket.org/alanmi/abc";
-    rev    = "a2e5bc66a68a72ccd267949e5c9973dd18f8932a";
-    sha256 = "09yvhj53af91nc54gmy7cbp7yljfcyj68a87494r5xvdfnsj11gy";
+  src = fetchFromGitHub {
+    owner = "berkeley-abc";
+    repo = "abc";
+    rev    = "24407e13db4b8ca16c3996049b2d33ec3722de39";
+    sha256 = "1rckji7nk81n6v1yajz7daqwipxacv7zlafknvmbiwji30j47sq5";
   };
 
+  nativeBuildInputs = [ cmake ];
   buildInputs = [ readline ];
-  preBuild = ''
-    export buildFlags="CC=$CC CXX=$CXX LD=$CXX"
-  '';
+
   enableParallelBuilding = true;
+
   installPhase = ''
     mkdir -p $out/bin
     mv abc $out/bin
@@ -22,7 +23,7 @@ stdenv.mkDerivation rec {
 
   meta = {
     description = "A tool for squential logic synthesis and formal verification";
-    homepage    = "https://people.eecs.berkeley.edu/~alanmi/abc/abc.htm";
+    homepage    = https://people.eecs.berkeley.edu/~alanmi/abc;
     license     = stdenv.lib.licenses.mit;
     platforms   = stdenv.lib.platforms.unix;
     maintainers = [ stdenv.lib.maintainers.thoughtpolice ];
diff --git a/pkgs/applications/science/logic/acgtk/default.nix b/pkgs/applications/science/logic/acgtk/default.nix
index 9e6243c54980..b787a4e7a01c 100644
--- a/pkgs/applications/science/logic/acgtk/default.nix
+++ b/pkgs/applications/science/logic/acgtk/default.nix
@@ -4,8 +4,7 @@
   installExamples ? true,
   installEmacsMode ? true }:
 
-let inherit (stdenv.lib) versionAtLeast
-                         optionals optionalString; in
+let inherit (stdenv.lib) versionAtLeast optionalString; in
 
 let inherit (ocamlPackages) ocaml camlp4; in
 
diff --git a/pkgs/applications/science/logic/aspino/default.nix b/pkgs/applications/science/logic/aspino/default.nix
index 5e1a448a7cf9..d7a5e7b4c83f 100644
--- a/pkgs/applications/science/logic/aspino/default.nix
+++ b/pkgs/applications/science/logic/aspino/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, fetchFromGitHub, zlib, boost, glucose }:
+{ stdenv, fetchurl, fetchFromGitHub, zlib, boost }:
 
 let
   glucose' = fetchurl {
diff --git a/pkgs/applications/science/logic/celf/default.nix b/pkgs/applications/science/logic/celf/default.nix
index 669cd46cd2ec..708270f772fd 100644
--- a/pkgs/applications/science/logic/celf/default.nix
+++ b/pkgs/applications/science/logic/celf/default.nix
@@ -28,7 +28,7 @@ stdenv.mkDerivation rec {
 
   meta = with stdenv.lib; {
     description = "Linear logic programming system";
-    homepage = http://github.com/clf/celf;
+    homepage = https://github.com/clf/celf;
     license = licenses.gpl3;
     maintainers = with maintainers; [ bcdarwin ];
     platforms = platforms.unix;
diff --git a/pkgs/applications/science/logic/coq/8.3.nix b/pkgs/applications/science/logic/coq/8.3.nix
index 87b847087e34..341267b2cebe 100644
--- a/pkgs/applications/science/logic/coq/8.3.nix
+++ b/pkgs/applications/science/logic/coq/8.3.nix
@@ -27,7 +27,7 @@ stdenv.mkDerivation {
   name = "coq-${version}";
 
   src = fetchurl {
-    url = "http://coq.inria.fr/V${version}/files/coq-${version}.tar.gz";
+    url = "https://coq.inria.fr/V${version}/files/coq-${version}.tar.gz";
     sha256 = "17d3lmchmqir1rawnr52g78srg4wkd7clzpzfsivxc4y1zp6rwkr";
   };
 
diff --git a/pkgs/applications/science/logic/coq/8.4.nix b/pkgs/applications/science/logic/coq/8.4.nix
index 64b0f85aed2a..c3da1205ab0c 100644
--- a/pkgs/applications/science/logic/coq/8.4.nix
+++ b/pkgs/applications/science/logic/coq/8.4.nix
@@ -23,7 +23,7 @@ stdenv.mkDerivation {
   inherit ocaml camlp5;
 
   src = fetchurl {
-    url = "http://coq.inria.fr/distrib/V${version}/files/coq-${version}.tar.gz";
+    url = "https://coq.inria.fr/distrib/V${version}/files/coq-${version}.tar.gz";
     sha256 = "1mpbj4yf36kpjg2v2sln12i8dzqn8rag6fd07hslj2lpm4qs4h55";
   };
 
diff --git a/pkgs/applications/science/logic/coq/default.nix b/pkgs/applications/science/logic/coq/default.nix
index 2b35c91d10c4..dbfec66789a3 100644
--- a/pkgs/applications/science/logic/coq/default.nix
+++ b/pkgs/applications/science/logic/coq/default.nix
@@ -23,6 +23,7 @@ let
    "8.7.1"     = "0gjn59jkbxwrihk8fx9d823wjyjh5m9gvj9l31nv6z6bcqhgdqi8";
    "8.7.2"     = "0a0657xby8wdq4aqb2xsxp3n7pmc2w4yxjmrb2l4kccs1aqvaj4w";
    "8.8.0" = "13a4fka22hdxsjk11mgjb9ffzplfxyxp1sg5v1c8nk1grxlscgw8";
+   "8.8.1" = "1hlf58gwazywbmfa48219amid38vqdl94yz21i11b4map6jfwhbk";
   }."${version}";
   coq-version = builtins.substring 0 3 version;
   camlp5 = ocamlPackages.camlp5_strict;
diff --git a/pkgs/applications/science/logic/coq2html/default.nix b/pkgs/applications/science/logic/coq2html/default.nix
index 9687ac66ead1..a987bf1ba57e 100644
--- a/pkgs/applications/science/logic/coq2html/default.nix
+++ b/pkgs/applications/science/logic/coq2html/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, make, fetchgit, ocaml }:
+{ stdenv, make, fetchgit, ocaml }:
 
 let 
   version = "20170720";
diff --git a/pkgs/applications/science/logic/cvc4/default.nix b/pkgs/applications/science/logic/cvc4/default.nix
index 403eff216f53..cddcbef7a035 100644
--- a/pkgs/applications/science/logic/cvc4/default.nix
+++ b/pkgs/applications/science/logic/cvc4/default.nix
@@ -1,19 +1,19 @@
 { stdenv, fetchurl, cln, gmp, swig, pkgconfig
 , readline, libantlr3c, boost, jdk, autoreconfHook
-, python2, antlr3_4
+, python3, antlr3_4
 }:
 
 stdenv.mkDerivation rec {
   name = "cvc4-${version}";
-  version = "1.5";
+  version = "1.6";
 
   src = fetchurl {
-    url = "http://cvc4.cs.stanford.edu/downloads/builds/src/cvc4-${version}.tar.gz";
-    sha256 = "0yxxawgc9vd2cz883swjlm76rbdkj48n7a8dfppsami530y2rvhi";
+    url = "https://cvc4.cs.stanford.edu/downloads/builds/src/cvc4-${version}.tar.gz";
+    sha256 = "1iw793zsi48q91lxpf8xl8lnvv0jsj4whdad79rakywkm1gbs62w";
   };
 
   nativeBuildInputs = [ autoreconfHook pkgconfig ];
-  buildInputs = [ gmp cln readline swig libantlr3c antlr3_4 boost jdk python2 ];
+  buildInputs = [ gmp cln readline swig libantlr3c antlr3_4 boost jdk python3 ];
   configureFlags = [
     "--enable-language-bindings=c,c++,java"
     "--enable-gpl"
@@ -35,9 +35,9 @@ stdenv.mkDerivation rec {
 
   meta = with stdenv.lib; {
     description = "A high-performance theorem prover and SMT solver";
-    homepage    = http://cvc4.cs.nyu.edu/web/;
+    homepage    = http://cvc4.cs.stanford.edu/web/;
     license     = licenses.gpl3;
     platforms   = platforms.unix;
-    maintainers = with maintainers; [ vbgl thoughtpolice ];
+    maintainers = with maintainers; [ vbgl thoughtpolice gebner ];
   };
 }
diff --git a/pkgs/applications/science/logic/elan/default.nix b/pkgs/applications/science/logic/elan/default.nix
index ff737de0cc4f..2b7cb9c30fae 100644
--- a/pkgs/applications/science/logic/elan/default.nix
+++ b/pkgs/applications/science/logic/elan/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, pkgconfig, curl, openssl, zlib, fetchFromGitHub, rustPlatform }:
+{ stdenv, pkgconfig, curl, openssl, zlib, fetchFromGitHub, rustPlatform }:
 
 rustPlatform.buildRustPackage rec {
   name = "elan-${version}";
diff --git a/pkgs/applications/science/logic/eprover/default.nix b/pkgs/applications/science/logic/eprover/default.nix
index 44328eeb6bbb..95f5385fac3d 100644
--- a/pkgs/applications/science/logic/eprover/default.nix
+++ b/pkgs/applications/science/logic/eprover/default.nix
@@ -2,11 +2,11 @@
 
 stdenv.mkDerivation rec {
   name = "eprover-${version}";
-  version = "2.0";
+  version = "2.1";
 
   src = fetchurl {
-    url = "http://wwwlehre.dhbw-stuttgart.de/~sschulz/WORK/E_DOWNLOAD/V_${version}/E.tgz";
-    sha256 = "1xmwr32pd8lv3f6yh720mdqhi3na505y3zbgcsgh2hwb7b5i3ngb";
+    url = "https://wwwlehre.dhbw-stuttgart.de/~sschulz/WORK/E_DOWNLOAD/V_${version}/E.tgz";
+    sha256 = "1gh99ajmza33f54idhqkdqxp5zh2k06jsf45drihnrzydlqv1n7l";
   };
 
   buildInputs = [ which ];
@@ -16,10 +16,6 @@ stdenv.mkDerivation rec {
   '';
   configureFlags = "--exec-prefix=$(out) --man-prefix=$(out)/share/man";
 
-  postInstall = ''
-    sed -e s,EXECPATH=.\*,EXECPATH=$out/bin, -i $out/bin/eproof{,_ram}
-  '';
-
   meta = with stdenv.lib; {
     description = "Automated theorem prover for full first-order logic with equality";
     homepage = http://www.eprover.org/;
diff --git a/pkgs/applications/science/logic/hol_light/default.nix b/pkgs/applications/science/logic/hol_light/default.nix
index 8d7c11ee91b7..a1779baf5f82 100644
--- a/pkgs/applications/science/logic/hol_light/default.nix
+++ b/pkgs/applications/science/logic/hol_light/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, fetchpatch, writeScript, ocaml, camlp5 }:
+{ stdenv, fetchFromGitHub, fetchpatch, ocaml, camlp5 }:
 
 let
   start_script = ''
diff --git a/pkgs/applications/science/logic/isabelle/default.nix b/pkgs/applications/science/logic/isabelle/default.nix
index af2bb3ed0c4e..ae04c11d9590 100644
--- a/pkgs/applications/science/logic/isabelle/default.nix
+++ b/pkgs/applications/science/logic/isabelle/default.nix
@@ -15,7 +15,7 @@ stdenv.mkDerivation {
       sha256 = "1awgg39i72pivwfijdwffvil3glnpimjz2x04qbl5la2j6la48nb";
     }
     else fetchurl {
-      url = "http://isabelle.in.tum.de/website-${dirname}/dist/${dirname}_linux.tar.gz";
+      url = "https://isabelle.in.tum.de/website-${dirname}/dist/${dirname}_linux.tar.gz";
       sha256 = "01v1zrajyfamjq5b8v18qr3ffivjckifsvvx2vs13di6wsnmm9gw";
     };
 
diff --git a/pkgs/applications/science/logic/lean2/default.nix b/pkgs/applications/science/logic/lean2/default.nix
index 2b861b6d06d9..4e1415d3961d 100644
--- a/pkgs/applications/science/logic/lean2/default.nix
+++ b/pkgs/applications/science/logic/lean2/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, cmake, gmp, mpfr, boost, python
+{ stdenv, fetchFromGitHub, cmake, gmp, mpfr, python
 , gperftools, ninja, makeWrapper }:
 
 stdenv.mkDerivation rec {
diff --git a/pkgs/applications/science/logic/leo2/default.nix b/pkgs/applications/science/logic/leo2/default.nix
index e30895346258..be337a1c258e 100644
--- a/pkgs/applications/science/logic/leo2/default.nix
+++ b/pkgs/applications/science/logic/leo2/default.nix
@@ -5,7 +5,7 @@ stdenv.mkDerivation rec {
   version = "1.6.2";
 
   src = fetchurl {
-    url = "http://page.mi.fu-berlin.de/cbenzmueller/leo/leo2_v${version}.tgz";
+    url = "https://page.mi.fu-berlin.de/cbenzmueller/leo/leo2_v${version}.tgz";
     sha256 = "1wjpmizb181iygnd18lx7p77fwaci2clgzs5ix5j51cc8f3pazmv";
   };
 
diff --git a/pkgs/applications/science/logic/mcrl2/default.nix b/pkgs/applications/science/logic/mcrl2/default.nix
index 6e8797fcc40b..dc32e84279a8 100644
--- a/pkgs/applications/science/logic/mcrl2/default.nix
+++ b/pkgs/applications/science/logic/mcrl2/default.nix
@@ -1,5 +1,4 @@
-{stdenv, fetchurl, cmake, subversion, libGLU_combined, qt5, boost,
- python27, python27Packages}:
+{stdenv, fetchurl, cmake, libGLU_combined, qt5, boost}:
 
 stdenv.mkDerivation rec {
   version = "201707";
@@ -7,7 +6,7 @@ stdenv.mkDerivation rec {
   name = "mcrl2-${version}";
 
   src = fetchurl {
-    url = "http://www.mcrl2.org/download/release/mcrl2-${version}.${build_nr}.tar.gz";
+    url = "https://www.mcrl2.org/download/release/mcrl2-${version}.${build_nr}.tar.gz";
     sha256 = "1c8h94ja7271ph61zrcgnjgblxppld6v22f7f900prjgzbcfy14m";
   };
 
@@ -22,7 +21,7 @@ stdenv.mkDerivation rec {
       that can be used for modelling, validation and verification of
       concurrent systems and protocols
     '';
-    homepage = http://www.mcrl2.org/;
+    homepage = https://www.mcrl2.org/;
     license = licenses.boost;
     maintainers = with maintainers; [ moretea ];
     platforms = platforms.unix;
diff --git a/pkgs/applications/science/logic/monosat/default.nix b/pkgs/applications/science/logic/monosat/default.nix
new file mode 100644
index 000000000000..54bedc7c5e97
--- /dev/null
+++ b/pkgs/applications/science/logic/monosat/default.nix
@@ -0,0 +1,67 @@
+{ stdenv, fetchFromGitHub, cmake, zlib, gmp, jdk8,
+  # The JDK we use on Darwin currenly makes extensive use of rpaths which are
+  # annoying and break the python library, so let's not bother for now
+  includeJava ? !stdenv.hostPlatform.isDarwin, includeGplCode ? true }:
+
+with stdenv.lib;
+
+let
+  boolToCmake = x: if x then "ON" else "OFF";
+
+  rev    = "2deeadeff214e975c9f7508bc8a24fa05a1a0c32";
+  sha256 = "09yhym2lxmn3xbhw5fcxawnmvms5jd9fw9m7x2wzil7yvy4vwdjn";
+
+  pname   = "monosat";
+  version = substring 0 7 sha256;
+
+  src = fetchFromGitHub {
+    owner = "sambayless";
+    repo  = pname;
+    inherit rev sha256;
+  };
+
+  core = stdenv.mkDerivation rec {
+    name = "${pname}-${version}";
+    inherit src;
+    buildInputs = [ cmake zlib gmp jdk8 ];
+
+    cmakeFlags = [ "-DJAVA=${boolToCmake includeJava}" "-DGPL=${boolToCmake includeGplCode}" ];
+
+    postInstall = optionalString includeJava ''
+      mkdir -p $out/share/java
+      cp monosat.jar $out/share/java
+    '';
+
+    passthru = { inherit python; };
+
+    meta = {
+      description = "SMT solver for Monotonic Theories";
+      platforms   = platforms.unix;
+      license     = if includeGplCode then licenses.gpl2 else licenses.mit;
+      homepage    = https://github.com/sambayless/monosat;
+    };
+  };
+
+  python = { buildPythonPackage, cython }: buildPythonPackage {
+    inherit pname version src;
+
+    # The top-level "source" is what fetchFromGitHub gives us. The rest is inside the repo
+    sourceRoot = "source/src/monosat/api/python/";
+
+    propagatedBuildInputs = [ core cython ];
+
+    # This tells setup.py to use cython
+    MONOSAT_CYTHON = true;
+
+    # The relative paths here don't make sense for our Nix build
+    # Also, let's use cython since it should produce faster bindings
+    # TODO: do we want to just reference the core monosat library rather than copying the
+    # shared lib? The current setup.py copies the .dylib/.so...
+    postPatch = ''
+
+      substituteInPlace setup.py \
+        --replace '../../../../libmonosat.dylib' '${core}/lib/libmonosat.dylib' \
+        --replace '../../../../libmonosat.so'  '${core}/lib/libmonosat.so'
+    '';
+  };
+in core
\ No newline at end of file
diff --git a/pkgs/applications/science/logic/poly/default.nix b/pkgs/applications/science/logic/poly/default.nix
index 1b401c927b6e..2f765572f9ad 100644
--- a/pkgs/applications/science/logic/poly/default.nix
+++ b/pkgs/applications/science/logic/poly/default.nix
@@ -1,17 +1,20 @@
-{stdenv, fetchurl, gmp, cmake, python}:
+{stdenv, fetchFromGitHub, gmp, cmake, python}:
 
-let version = "0.1.4";
-in
+stdenv.mkDerivation rec {
+  name = "${pname}-${version}";
+  pname = "libpoly";
+  version = "0.1.7";
 
-stdenv.mkDerivation {
-  name = "libpoly-${version}";
-
-  src = fetchurl {
-    url = "https://github.com/SRI-CSL/libpoly/archive/v${version}.tar.gz";
-    sha256 = "16x1pk2a3pcb5a0dzyw28ccjwkhmbsck4hy80ss7kx0dd7qgpi7j";
+  src = fetchFromGitHub {
+    owner = "SRI-CSL";
+    repo = "libpoly";
+    rev = "v${version}";
+    sha256 = "0i5ar4lhs88glk0rvkmag656ii434i6i1q5dspx6d0kyg78fii64";
   };
 
-  buildInputs = [ cmake gmp python ];
+  nativeBuildInputs = [ cmake ];
+
+  buildInputs = [ gmp python ];
 
   meta = with stdenv.lib; {
     homepage = https://github.com/SRI-CSL/libpoly;
diff --git a/pkgs/applications/science/logic/potassco/clingo.nix b/pkgs/applications/science/logic/potassco/clingo.nix
index 6bf19a2a2f4d..4abfdf9162c0 100644
--- a/pkgs/applications/science/logic/potassco/clingo.nix
+++ b/pkgs/applications/science/logic/potassco/clingo.nix
@@ -3,11 +3,11 @@
 stdenv.mkDerivation rec {
   name = "${pname}-${version}";
   pname = "clingo";
-  version = "5.2.2";
+  version = "5.3.0";
 
   src = fetchzip {
     url = "https://github.com/potassco/clingo/archive/v${version}.tar.gz";
-    sha256 = "04rjwpna37gzm8vxr09z3z6ay8y8cxbjd8lga7xvqfpn2l178zjm";
+    sha256 = "01czx26p8gv81ahrh650x208hjhd8bx1kb688fmk1m4pw4yg5bfv";
   };
 
   buildInputs = [];
diff --git a/pkgs/applications/science/logic/prooftree/default.nix b/pkgs/applications/science/logic/prooftree/default.nix
index b143fd982505..01dfc35f6e0d 100644
--- a/pkgs/applications/science/logic/prooftree/default.nix
+++ b/pkgs/applications/science/logic/prooftree/default.nix
@@ -5,7 +5,7 @@ stdenv.mkDerivation (rec {
   version = "0.12";
 
   src = fetchurl {
-    url = "http://askra.de/software/prooftree/releases/prooftree-${version}.tar.gz";
+    url = "https://askra.de/software/prooftree/releases/prooftree-${version}.tar.gz";
     sha256 = "08yp66j05pdkdpv9xkfqymqy82mir5xbwfh9mkzhh219xkps4b4m";
   };
 
diff --git a/pkgs/applications/science/logic/prover9/default.nix b/pkgs/applications/science/logic/prover9/default.nix
index 74e31d062f48..f96532b7038b 100644
--- a/pkgs/applications/science/logic/prover9/default.nix
+++ b/pkgs/applications/science/logic/prover9/default.nix
@@ -4,7 +4,7 @@ stdenv.mkDerivation {
   name = "prover9-2009-11a";
 
   src = fetchurl {
-    url = http://www.cs.unm.edu/~mccune/mace4/download/LADR-2009-11A.tar.gz;
+    url = https://www.cs.unm.edu/~mccune/mace4/download/LADR-2009-11A.tar.gz;
     sha256 = "1l2i3d3h5z7nnbzilb6z92r0rbx0kh6yaxn2c5qhn3000xcfsay3";
   };
 
diff --git a/pkgs/applications/science/logic/satallax/default.nix b/pkgs/applications/science/logic/satallax/default.nix
index c22bff9cdee2..b9dd90f601f5 100644
--- a/pkgs/applications/science/logic/satallax/default.nix
+++ b/pkgs/applications/science/logic/satallax/default.nix
@@ -5,7 +5,7 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ocaml zlib which eprover makeWrapper coq];
   src = fetchurl {
-    url = "http://www.ps.uni-saarland.de/~cebrown/satallax/downloads/${name}.tar.gz";
+    url = "https://www.ps.uni-saarland.de/~cebrown/satallax/downloads/${name}.tar.gz";
     sha256 = "1kvxn8mc35igk4vigi5cp7w3wpxk2z3bgwllfm4n3h2jfs0vkpib";
   };
 
diff --git a/pkgs/applications/science/logic/saw-tools/default.nix b/pkgs/applications/science/logic/saw-tools/default.nix
index 949b34420b79..65d2b4d91c30 100644
--- a/pkgs/applications/science/logic/saw-tools/default.nix
+++ b/pkgs/applications/science/logic/saw-tools/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, gmp4, ncurses, zlib, makeWrapper, clang_35 }:
+{ stdenv, fetchurl, gmp4, ncurses, zlib, clang_35 }:
 
 let
   libPath = stdenv.lib.makeLibraryPath
diff --git a/pkgs/applications/science/logic/tamarin-prover/default.nix b/pkgs/applications/science/logic/tamarin-prover/default.nix
index fb37554b65fe..4efc384ed227 100644
--- a/pkgs/applications/science/logic/tamarin-prover/default.nix
+++ b/pkgs/applications/science/logic/tamarin-prover/default.nix
@@ -1,4 +1,4 @@
-{ haskell, haskellPackages, mkDerivation, fetchFromGitHub, lib
+{ haskellPackages, mkDerivation, fetchFromGitHub, lib
 # the following are non-haskell dependencies
 , makeWrapper, which, maude, graphviz, sapic
 }:
diff --git a/pkgs/applications/science/logic/tlaplus/tlaps.nix b/pkgs/applications/science/logic/tlaplus/tlaps.nix
index 9a78c1df8b17..7d35f5f91df3 100644
--- a/pkgs/applications/science/logic/tlaplus/tlaps.nix
+++ b/pkgs/applications/science/logic/tlaplus/tlaps.nix
@@ -1,8 +1,6 @@
-{ lib
-, fetchurl
-, makeWrapper
+{ fetchurl
 , stdenv
-, ocaml, gawk, isabelle, cvc3, perl, wget, which
+, ocaml, isabelle, cvc3, perl, wget, which
 }:
 
 stdenv.mkDerivation rec {
diff --git a/pkgs/applications/science/logic/tlaplus/toolbox.nix b/pkgs/applications/science/logic/tlaplus/toolbox.nix
index 5ce359dea0ec..342d91cf5825 100644
--- a/pkgs/applications/science/logic/tlaplus/toolbox.nix
+++ b/pkgs/applications/science/logic/tlaplus/toolbox.nix
@@ -3,7 +3,7 @@
 }:
 
 let
-  version = "1.5.6";
+  version = "1.5.7";
   arch = "x86_64";
 
   desktopItem = makeDesktopItem rec {
@@ -24,7 +24,7 @@ in stdenv.mkDerivation {
   name = "tla-toolbox-${version}";
   src = fetchzip {
     url = "https://tla.msr-inria.inria.fr/tlatoolbox/products/TLAToolbox-${version}-linux.gtk.${arch}.zip";
-    sha256 = "1h63mcbrkf4jcg6qncpqffdi0x665z0wlfdq43d67p411xcqmbw9";
+    sha256 = "0lg9sizpw5mkcnwwvmgqigkizjyz2lf1wrg48h7mg7wcv3macy4q";
   };
 
   buildInputs = [ makeWrapper  ];
diff --git a/pkgs/applications/science/logic/vampire/default.nix b/pkgs/applications/science/logic/vampire/default.nix
new file mode 100644
index 000000000000..e05839b2a143
--- /dev/null
+++ b/pkgs/applications/science/logic/vampire/default.nix
@@ -0,0 +1,59 @@
+{ stdenv, fetchFromGitHub, fetchpatch, z3, zlib, git }:
+
+stdenv.mkDerivation rec {
+  version = "4.2.2";
+  name = "vampire-${version}";
+
+  src = fetchFromGitHub {
+    owner = "vprover";
+    repo = "vampire";
+    rev = version;
+    sha256 = "080zwgmyhn0b2c6hqlhcgaw7n3frz02sh894v5kk68kzxbqr29w2";
+    fetchSubmodules = true;
+    leaveDotGit = true;
+  };
+
+  nativeBuildInputs = [ git ];
+  buildInputs = [ z3 zlib ];
+
+  makeFlags = [ "vampire_z3_rel" "CC:=$(CC)" "CXX:=$(CXX)" ];
+
+  patches = [
+    # https://github.com/vprover/vampire/pull/54
+    (fetchpatch {
+      name = "fix-apple-cygwin-defines.patch";
+      url = https://github.com/vprover/vampire/pull/54.patch;
+      sha256 = "0i6nrc50wlg1dqxq38lkpx4rmfb3lf7s8f95l4jkvqp0nxa20cza";
+    })
+    # https://github.com/vprover/vampire/pull/55
+    (fetchpatch {
+      name = "fix-wait-any.patch";
+      url = https://github.com/vprover/vampire/pull/55.patch;
+      sha256 = "1pwfpwpl23bqsgkmmvw6bnniyvp5j9v8l3z9s9pllfabnfcrcz9l";
+    })
+    # https://github.com/vprover/vampire/pull/56
+    (fetchpatch {
+      name = "fenv.patch";
+      url = https://github.com/vprover/vampire/pull/56.patch;
+      sha256 = "0xl3jcyqmk146mg3qj5hdd0pbja6wbq3250zmfhbxqrjh40mm40g";
+    })
+  ];
+
+  enableParallelBuilding = true;
+
+  fixupPhase = ''
+    rm -rf z3
+  '';
+
+  installPhase = ''
+    install -m0755 -D vampire_z3_rel* $out/bin/vampire
+  '';
+
+  meta = with stdenv.lib; {
+    homepage = "https://vprover.github.io/";
+    description = "The Vampire Theorem Prover";
+    platforms = platforms.unix;
+    license = licenses.unfree;
+    maintainers = with maintainers; [ gebner ];
+  };
+}
diff --git a/pkgs/applications/science/logic/why3/default.nix b/pkgs/applications/science/logic/why3/default.nix
index 202d52f0cc0e..1ccc365498ce 100644
--- a/pkgs/applications/science/logic/why3/default.nix
+++ b/pkgs/applications/science/logic/why3/default.nix
@@ -2,15 +2,15 @@
 
 stdenv.mkDerivation rec {
   name    = "why3-${version}";
-  version = "0.88.3";
+  version = "1.0.0";
 
   src = fetchurl {
-    url    = https://gforge.inria.fr/frs/download.php/file/37313/why3-0.88.3.tar.gz;
-    sha256 = "0limdqy9l5bjzwhdalcfdyh0b6laxgiphhvr4bby9p0030agssiy";
+    url    = https://gforge.inria.fr/frs/download.php/file/37604/why3-1.0.0.tar.gz;
+    sha256 = "18h00diw1c051v7ya0lv09ns5630qi9savwffx0652mcc4b4qpxn";
   };
 
   buildInputs = (with ocamlPackages; [
-      ocaml findlib lablgtk ocamlgraph zarith menhir ]) ++
+      ocaml findlib num lablgtk ocamlgraph zarith menhir ]) ++
     stdenv.lib.optionals (ocamlPackages.ocaml == coq.ocaml ) [
       coq coq.camlp5
     ];
diff --git a/pkgs/applications/science/logic/workcraft/default.nix b/pkgs/applications/science/logic/workcraft/default.nix
index 9ae5d75c4577..313fe5a9f1dd 100644
--- a/pkgs/applications/science/logic/workcraft/default.nix
+++ b/pkgs/applications/science/logic/workcraft/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, pkgs, fetchurl, jre, makeWrapper }:
+{ stdenv, fetchurl, jre, makeWrapper }:
 
 stdenv.mkDerivation rec {
   name = "workcraft-${version}";
@@ -23,7 +23,7 @@ stdenv.mkDerivation rec {
   '';
 
   meta = {
-    homepage = http://workcraft.org/;
+    homepage = https://workcraft.org/;
     description = "Framework for interpreted graph modeling, verification and synthesis";
     platforms = stdenv.lib.platforms.linux;
     license = stdenv.lib.licenses.mit;
diff --git a/pkgs/applications/science/logic/yices/default.nix b/pkgs/applications/science/logic/yices/default.nix
index a607f1066395..3121a83e5b98 100644
--- a/pkgs/applications/science/logic/yices/default.nix
+++ b/pkgs/applications/science/logic/yices/default.nix
@@ -2,12 +2,12 @@
 
 stdenv.mkDerivation rec {
   name    = "yices-${version}";
-  version = "2.5.4";
+  version = "2.6.0";
 
   src = fetchurl {
     url = "https://github.com/SRI-CSL/yices2/archive/Yices-${version}.tar.gz";
     name = "${name}-src.tar.gz";
-    sha256 = "1k8wmlddi3zv5kgg6xbch3a0s0xqsmsfc7y6z8zrgcyhswl36h7p";
+    sha256 = "10ikq7ib8jhx7hlxfm6mp5qg6r8dflqs8242q5zaicn80qixpm12";
   };
 
   nativeBuildInputs = [ autoreconfHook ];
@@ -26,9 +26,11 @@ stdenv.mkDerivation rec {
 
   # Includes a fix for the embedded soname being libyices.so.2.5, but
   # only installing the libyices.so.2.5.x file.
-  installPhase = ''
+  installPhase = let
+    ver_XdotY = builtins.concatStringsSep "." (stdenv.lib.take 2 (stdenv.lib.splitString "." version));
+  in ''
       make install LDCONFIG=true
-      (cd $out/lib && ln -s -f libyices.so.${version} libyices.so.2.5)
+      ln -sfr $out/lib/libyices.so.{${version},${ver_XdotY}}
   '';
 
   meta = with stdenv.lib; {
diff --git a/pkgs/applications/science/logic/z3/default.nix b/pkgs/applications/science/logic/z3/default.nix
index 54c9799a01b8..1cbe914779e6 100644
--- a/pkgs/applications/science/logic/z3/default.nix
+++ b/pkgs/applications/science/logic/z3/default.nix
@@ -20,6 +20,18 @@ stdenv.mkDerivation rec {
     cd build
   '';
 
+  postInstall = ''
+    mkdir -p $dev $lib $python/lib
+
+    mv $out/lib/python*  $python/lib/
+    mv $out/lib          $lib/lib
+    mv $out/include      $dev/include
+
+    ln -sf $lib/lib/libz3${stdenv.hostPlatform.extensions.sharedLibrary} $python/${python.sitePackages}/z3/lib/libz3${stdenv.hostPlatform.extensions.sharedLibrary}
+  '';
+
+  outputs = [ "out" "lib" "dev" "python" ];
+
   meta = {
     description = "A high-performance theorem prover and SMT solver";
     homepage    = "https://github.com/Z3Prover/z3";
diff --git a/pkgs/applications/science/machine-learning/torch/torch-distro.nix b/pkgs/applications/science/machine-learning/torch/torch-distro.nix
index 85a4aa02e441..9c4f24021430 100644
--- a/pkgs/applications/science/machine-learning/torch/torch-distro.nix
+++ b/pkgs/applications/science/machine-learning/torch/torch-distro.nix
@@ -1,6 +1,6 @@
-{ luarocks, lib , stdenv,  writeText , readline,  makeWrapper,
-  less, ncurses, cmake, openblas, coreutils, fetchgit, libuuid, czmq, openssl,
-  gnuplot, fetchurl, lua, src, libjpeg, libpng
+{ luarocks, lib , stdenv,  readline,  makeWrapper,
+  less, ncurses, cmake, coreutils, fetchgit, libuuid, czmq, openssl,
+  gnuplot, lua, src, libjpeg, libpng
 } :
 
 let
diff --git a/pkgs/applications/science/math/R/default.nix b/pkgs/applications/science/math/R/default.nix
index 953f4e62c22b..913fac6ac128 100644
--- a/pkgs/applications/science/math/R/default.nix
+++ b/pkgs/applications/science/math/R/default.nix
@@ -1,17 +1,17 @@
 { stdenv, fetchurl, bzip2, gfortran, libX11, libXmu, libXt, libjpeg, libpng
 , libtiff, ncurses, pango, pcre, perl, readline, tcl, texLive, tk, xz, zlib
 , less, texinfo, graphviz, icu, pkgconfig, bison, imake, which, jdk, openblas
-, curl, Cocoa, Foundation, cf-private, libobjc, libcxx, tzdata, fetchpatch
+, curl, Cocoa, Foundation, libobjc, libcxx, tzdata
 , withRecommendedPackages ? true
 , enableStrictBarrier ? false
 }:
 
 stdenv.mkDerivation rec {
-  name = "R-3.5.0";
+  name = "R-3.5.1";
 
   src = fetchurl {
-    url = "http://cran.r-project.org/src/base/R-3/${name}.tar.gz";
-    sha256 = "0w38865laqg28cdhikxdxhx4rfp0kgcn72gakwypsy91br9ja5zx";
+    url = "https://cran.r-project.org/src/base/R-3/${name}.tar.gz";
+    sha256 = "0463bff5eea0f3d93fa071f79c18d0993878fd4f2e18ae6cf22c1639d11457ed";
   };
 
   buildInputs = [
@@ -65,8 +65,7 @@ stdenv.mkDerivation rec {
 
   installTargets = [ "install" "install-info" "install-pdf" ];
 
-  doCheck = withRecommendedPackages;  # R 3.5.0 fails the test suite if the
-                                      # recommended packages are not built
+  doCheck = true;
   preCheck = "export TZ=CET; bin/Rscript -e 'sessionInfo()'";
 
   enableParallelBuilding = true;
diff --git a/pkgs/applications/science/math/bliss/default.nix b/pkgs/applications/science/math/bliss/default.nix
new file mode 100644
index 000000000000..1b7a97360079
--- /dev/null
+++ b/pkgs/applications/science/math/bliss/default.nix
@@ -0,0 +1,37 @@
+{ stdenv, fetchurl, unzip, doxygen }:
+
+stdenv.mkDerivation rec {
+  name = "bliss-${version}";
+  version = "0.73";
+
+  src = fetchurl {
+    url = "http://www.tcs.hut.fi/Software/bliss/${name}.zip";
+    sha256 = "f57bf32804140cad58b1240b804e0dbd68f7e6bf67eba8e0c0fa3a62fd7f0f84";
+  };
+
+  patches = fetchurl {
+    url = "http://scip.zib.de/download/bugfixes/scip-5.0.1/bliss-0.73.patch";
+    sha256 = "815868d6586bcd49ff3c28e14ccb536d38b2661151088fe08187c13909c5dab0";
+  };
+
+  nativeBuildInputs = [ unzip doxygen ];
+
+  preBuild = ''
+    doxygen Doxyfile
+  '';
+
+  installPhase = ''
+    mkdir -p $out/bin $out/share/doc/bliss $out/lib $out/include/bliss
+    mv bliss $out/bin 
+    mv html/* COPYING* $out/share/doc/bliss
+    mv *.a $out/lib
+    mv *.h *.hh $out/include/bliss
+  '';
+
+  meta = with stdenv.lib; {
+    description = "bliss is an open source tool for computing automorphism groups and canonical forms of graphs. It has both a command line user interface as well as C++ and C programming language APIs.";
+    homepage = http://www.tcs.hut.fi/Software/bliss/;
+    license = licenses.lgpl3;
+    platforms = [ "i686-linux" "x86_64-linux" ];
+  };
+}
diff --git a/pkgs/applications/science/math/cbc/default.nix b/pkgs/applications/science/math/cbc/default.nix
index 6de77d81be6f..689261abc661 100644
--- a/pkgs/applications/science/math/cbc/default.nix
+++ b/pkgs/applications/science/math/cbc/default.nix
@@ -4,7 +4,7 @@ stdenv.mkDerivation {
   name = "cbc-2.9.9";
 
   src = fetchurl {
-    url = "http://www.coin-or.org/download/source/Cbc/Cbc-2.9.9.tgz";
+    url = "https://www.coin-or.org/download/source/Cbc/Cbc-2.9.9.tgz";
     sha256 = "1w8axdzm05xf5y13c31w7rc5z6ywxqxiwafnxcq3p195kgj0915a";
   };
 
diff --git a/pkgs/applications/science/math/clp/default.nix b/pkgs/applications/science/math/clp/default.nix
index daa9a53c59a1..4899ebe42d77 100644
--- a/pkgs/applications/science/math/clp/default.nix
+++ b/pkgs/applications/science/math/clp/default.nix
@@ -1,24 +1,22 @@
 { stdenv, fetchurl, zlib }:
 
 stdenv.mkDerivation rec {
-	version = "1.16.11";
-	name = "clp-${version}";
-	src = fetchurl {
-		url = "https://www.coin-or.org/download/source/Clp/Clp-${version}.tgz";
-		sha256 = "0fazlqpp845186nmixa9f1xfxqqkdr1xj4va7q29m8594ca4a9dm";
-	};
+  version = "1.16.11";
+  name = "clp-${version}";
+  src = fetchurl {
+    url = "https://www.coin-or.org/download/source/Clp/Clp-${version}.tgz";
+    sha256 = "0fazlqpp845186nmixa9f1xfxqqkdr1xj4va7q29m8594ca4a9dm";
+  };
 
-	propagatedBuildInputs = [ zlib ];
+  propagatedBuildInputs = [ zlib ];
 
-	doCheck = true;
+  doCheck = true;
 
-	checkTarget = "test";
-
-	meta = {
-		license = stdenv.lib.licenses.epl10;
-		homepage = https://projects.coin-or.org/Clp;
-		description = "An open-source linear programming solver written in C++";
-		platforms = stdenv.lib.platforms.all;
-		maintainers = [ stdenv.lib.maintainers.vbgl ];
-	};
+  meta = {
+    license = stdenv.lib.licenses.epl10;
+    homepage = https://projects.coin-or.org/Clp;
+    description = "An open-source linear programming solver written in C++";
+    platforms = stdenv.lib.platforms.all;
+    maintainers = [ stdenv.lib.maintainers.vbgl ];
+  };
 }
diff --git a/pkgs/applications/science/math/cntk/default.nix b/pkgs/applications/science/math/cntk/default.nix
index 07740f4c8aea..fe5c77be8b22 100644
--- a/pkgs/applications/science/math/cntk/default.nix
+++ b/pkgs/applications/science/math/cntk/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchgit, fetchFromGitHub, fetchpatch, cmake
+{ lib, stdenv, fetchgit, fetchFromGitHub, cmake
 , openblas, opencv3, libzip, boost, protobuf, openmpi
 , onebitSGDSupport ? false
 , cudaSupport ? false, cudatoolkit, nvidia_x11
diff --git a/pkgs/applications/science/math/colpack/default.nix b/pkgs/applications/science/math/colpack/default.nix
index 29785363b048..e62df7f11cc7 100644
--- a/pkgs/applications/science/math/colpack/default.nix
+++ b/pkgs/applications/science/math/colpack/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchFromGitHub, autoconf, automake, libtool, gettext }:
+{ stdenv, fetchFromGitHub, autoconf, automake, libtool, gettext }:
 
 stdenv.mkDerivation rec {
 
diff --git a/pkgs/applications/science/math/csdp/default.nix b/pkgs/applications/science/math/csdp/default.nix
index 796e8b7264fb..9b402fe0ac94 100644
--- a/pkgs/applications/science/math/csdp/default.nix
+++ b/pkgs/applications/science/math/csdp/default.nix
@@ -4,7 +4,7 @@ stdenv.mkDerivation {
   name = "csdp-6.1.1";
 
   src = fetchurl {
-    url = "http://www.coin-or.org/download/source/Csdp/Csdp-6.1.1.tgz";
+    url = "https://www.coin-or.org/download/source/Csdp/Csdp-6.1.1.tgz";
     sha256 = "1f9ql6cjy2gwiyc51ylfan24v1ca9sjajxkbhszlds1lqmma8n05";
   };
 
diff --git a/pkgs/applications/science/math/fricas/default.nix b/pkgs/applications/science/math/fricas/default.nix
index 553905c21957..2e48d334a603 100644
--- a/pkgs/applications/science/math/fricas/default.nix
+++ b/pkgs/applications/science/math/fricas/default.nix
@@ -1,7 +1,7 @@
 { stdenv, fetchurl, sbcl, libX11, libXpm, libICE, libSM, libXt, libXau, libXdmcp }:
 
 let
-  version = "1.3.3";
+  version = "1.3.4";
   name = "fricas-" + version;
 in
 stdenv.mkDerivation {
@@ -9,7 +9,7 @@ stdenv.mkDerivation {
 
   src = fetchurl {
     url    = "http://sourceforge.net/projects/fricas/files/fricas/${version}/${name}-full.tar.bz2";
-    sha256 = "1avp9mbl5yn192c7kz5c2d18k33hay9lwii363b0v5hj3qgq2hhl";
+    sha256 = "156k9az1623y5808j845c56z2nvvdrm48dzg1v0ivpplyl7vp57x";
   };
 
   buildInputs = [ sbcl libX11 libXpm libICE libSM libXt libXau libXdmcp ];
diff --git a/pkgs/applications/science/math/glsurf/default.nix b/pkgs/applications/science/math/glsurf/default.nix
index 798141b6b438..a53829347053 100644
--- a/pkgs/applications/science/math/glsurf/default.nix
+++ b/pkgs/applications/science/math/glsurf/default.nix
@@ -6,7 +6,7 @@ stdenv.mkDerivation {
   name = "glsurf-3.3.1";
 
   src = fetchurl {
-    url = "http://lama.univ-savoie.fr/~raffalli/glsurf/glsurf-3.3.1.tar.gz";
+    url = "https://lama.univ-savoie.fr/~raffalli/glsurf/glsurf-3.3.1.tar.gz";
     sha256 = "0w8xxfnw2snflz8wdr2ca9f5g91w5vbyp1hwlx1v7vg83d4bwqs7";
   };
 
diff --git a/pkgs/applications/science/math/gurobi/default.nix b/pkgs/applications/science/math/gurobi/default.nix
new file mode 100644
index 000000000000..06d448f6252e
--- /dev/null
+++ b/pkgs/applications/science/math/gurobi/default.nix
@@ -0,0 +1,48 @@
+{ stdenv, fetchurl, autoPatchelfHook, python }:
+
+stdenv.mkDerivation rec {
+  name = "gurobi-${version}";
+  version = "8.0.1";
+
+  src = with stdenv.lib; fetchurl {
+    url = "http://packages.gurobi.com/${versions.majorMinor version}/gurobi${version}_linux64.tar.gz";
+    sha256 = "0y3lb0mngnyn7ql4s2n8qxnr1d2xcjdpdhpdjdxc4sc8f2w2ih18";
+  };
+
+  sourceRoot = "gurobi${builtins.replaceStrings ["."] [""] version}/linux64";
+
+  nativeBuildInputs = [ autoPatchelfHook ];
+  buildInputs = [ (python.withPackages (ps: [ ps.gurobipy ])) ];
+
+  buildPhase = ''
+    cd src/build
+    make
+    cd ../..
+  '';
+
+  installPhase = ''
+    mkdir -p $out/bin
+    cp bin/* $out/bin/
+    rm $out/bin/gurobi.env
+    rm $out/bin/gurobi.sh
+    rm $out/bin/python2.7
+
+    cp lib/gurobi.py $out/bin/gurobi.sh
+
+    mkdir -p $out/include
+    cp include/gurobi*.h $out/include/
+
+    mkdir -p $out/lib
+    cp lib/libgurobi*.so* $out/lib/
+    cp lib/libgurobi*.a $out/lib/
+    cp src/build/*.a $out/lib/
+  '';
+
+  meta = with stdenv.lib; {
+    description = "Optimization solver for mathematical programming";
+    homepage = https://www.gurobi.com;
+    license = licenses.unfree;
+    platforms = [ "x86_64-linux" ];
+    maintainers = with maintainers; [ jfrankenau ];
+  };
+}
diff --git a/pkgs/applications/science/math/hmetis/default.nix b/pkgs/applications/science/math/hmetis/default.nix
new file mode 100644
index 000000000000..9eab9ca39992
--- /dev/null
+++ b/pkgs/applications/science/math/hmetis/default.nix
@@ -0,0 +1,42 @@
+{ stdenv, fetchurl, ghostscript }:
+
+stdenv.mkDerivation rec {
+  name = "hmetis-${version}";
+  version = "1.5";
+
+  src = fetchurl {
+    url = "http://glaros.dtc.umn.edu/gkhome/fetch/sw/hmetis/hmetis-${version}-linux.tar.gz";
+    sha256 = "e835a098c046e9c26cecb8addfea4d18ff25214e49585ffd87038e72819be7e1";
+  };
+
+  nativeBuildInputs = [ ghostscript ];
+
+  binaryFiles = "hmetis khmetis shmetis";
+
+  patchPhase = ''
+    for binaryfile in $binaryFiles; do
+      patchelf \
+        --set-interpreter ${stdenv.glibc}/lib/ld-linux.so.2 \
+        --set-rpath ${stdenv.glibc}/lib \
+        $binaryfile
+    done
+  '';
+
+  buildPhase = ''
+    gs -sOutputFile=manual.pdf -sDEVICE=pdfwrite -SNOPAUSE -dBATCH manual.ps
+  '';
+
+  installPhase = ''
+    mkdir -p $out/bin $out/share/doc/hmetis $out/lib
+    mv $binaryFiles $out/bin
+    mv manual.pdf $out/share/doc/hmetis
+    mv libhmetis.a $out/lib
+  '';
+
+  meta = with stdenv.lib; {
+    description = "hMETIS is a set of programs for partitioning hypergraphs";
+    homepage = http://glaros.dtc.umn.edu/gkhome/metis/hmetis/overview;
+    license = licenses.unfree;
+    platforms = [ "i686-linux" "x86_64-linux" ];
+  };
+}
diff --git a/pkgs/applications/science/math/mathematica/10.nix b/pkgs/applications/science/math/mathematica/10.nix
index 6a49976c86b8..4884ee1f93ab 100644
--- a/pkgs/applications/science/math/mathematica/10.nix
+++ b/pkgs/applications/science/math/mathematica/10.nix
@@ -7,13 +7,11 @@
 , freetype
 , gcc
 , glib
-, libpng
 , ncurses
 , opencv
 , openssl
 , unixODBC
 , xorg
-, zlib
 , libxml2
 , libuuid
 }:
diff --git a/pkgs/applications/science/math/mathematica/9.nix b/pkgs/applications/science/math/mathematica/9.nix
index c4fd0c384c14..d60feb54695d 100644
--- a/pkgs/applications/science/math/mathematica/9.nix
+++ b/pkgs/applications/science/math/mathematica/9.nix
@@ -7,13 +7,11 @@
 , freetype
 , gcc
 , glib
-, libpng
 , ncurses
 , opencv
 , openssl
 , unixODBC
 , xorg
-, zlib
 }:
 
 let
diff --git a/pkgs/applications/science/math/mathematica/default.nix b/pkgs/applications/science/math/mathematica/default.nix
index 97781a69ce64..fa4ff8b00687 100644
--- a/pkgs/applications/science/math/mathematica/default.nix
+++ b/pkgs/applications/science/math/mathematica/default.nix
@@ -8,7 +8,6 @@
 , freetype
 , gcc
 , glib
-, libpng
 , ncurses
 , opencv
 , openssl
@@ -24,12 +23,6 @@
 }:
 
 let
-  platform =
-    if stdenv.system == "i686-linux" || stdenv.system == "x86_64-linux" then
-      "Linux"
-    else
-      throw "Mathematica requires i686-linux or x86_64 linux";
-
   l10n =
     with stdenv.lib;
     with callPackage ./l10ns.nix {};
diff --git a/pkgs/applications/science/math/nasc/default.nix b/pkgs/applications/science/math/nasc/default.nix
index 8efe8d9da941..cac403b131b8 100644
--- a/pkgs/applications/science/math/nasc/default.nix
+++ b/pkgs/applications/science/math/nasc/default.nix
@@ -1,8 +1,6 @@
 { stdenv
-, bash
-, gnused
 , fetchFromGitHub
-, gettext
+, fetchpatch
 , pkgconfig
 , gtk3
 , granite
@@ -11,23 +9,30 @@
 , ninja
 , vala
 , libqalculate
-, elementary-cmake-modules
+, gobjectIntrospection
 , wrapGAppsHook }:
 
 stdenv.mkDerivation rec {
   name = "nasc-${version}";
-  version = "0.4.6";
+  version = "0.4.7";
 
   src = fetchFromGitHub {
     owner = "parnold-x";
     repo = "nasc";
     rev = version;
-    sha256 = "01n4ldj5phrsv97vb04qvs9c1ip6v8wygx9llj704hly1il9fb54";
+    sha256 = "0p74953pdgsijvqj3msssqiwm6sc1hzp68dlmjamqrqirwgqv5aa";
   };
 
-  XDG_DATA_DIRS = stdenv.lib.concatStringsSep ":" [
-    "${granite}/share"
-    "${gnome3.libgee}/share"
+  patches = [
+    # Install libqalculatenasc.so
+    (fetchpatch {
+      url = https://github.com/parnold-x/nasc/commit/93a799f9afb3e32f3f1a54e056b59570aae2e437.patch;
+      sha256 = "1m32w2zaswzxnzbr7p3lf8s6fac4mjvfhm8v9k59b4jyzmvrl631";
+    })
+    (fetchpatch {
+      url = https://github.com/parnold-x/nasc/commit/570b49169326de154af2cf43c5f12268fff1dc6d.patch;
+      sha256 = "1y3w6rxn0453iscx2xg427wy1bd5kv4z1c41hhbjmg614ycp6bka";
+    })
   ];
 
   nativeBuildInputs = [
@@ -35,7 +40,8 @@ stdenv.mkDerivation rec {
     wrapGAppsHook
     vala
     cmake
-    gettext
+    ninja
+    gobjectIntrospection # for setup-hook
   ];
   buildInputs = [
     libqalculate
@@ -46,14 +52,6 @@ stdenv.mkDerivation rec {
     gnome3.gtksourceview
   ];
 
-  prePatch = ''
-    substituteInPlace ./libqalculatenasc/libtool \
-      --replace "/bin/bash" "${bash}/bin/bash" \
-      --replace "/bin/sed" "${gnused}/bin/sed"
-    substituteInPlace ./libqalculatenasc/configure.inc \
-      --replace 'ac_default_path="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin"' 'ac_default_path=$PATH'
-  '';
-
   meta = with stdenv.lib; {
     description = "Do maths like a normal person";
     longDescription = ''
diff --git a/pkgs/applications/science/math/palp/default.nix b/pkgs/applications/science/math/palp/default.nix
index 3484c369db3b..4c7df4412e4f 100644
--- a/pkgs/applications/science/math/palp/default.nix
+++ b/pkgs/applications/science/math/palp/default.nix
@@ -14,13 +14,28 @@ stdenv.mkDerivation rec {
     sha256 = "1s7s2lc5f0ig1yy7ygsh3sddm3sbq4mxwybqsj8lp9wjdxs7qfrs";
   };
 
-  hardeningDisable = [ "format" ];
+  hardeningDisable = [
+    "format"
+    "strictoverflow" # causes runtime failure (tested in checkPhase)
+  ];
 
   preBuild = ''
       echo Building PALP optimized for ${dim} dimensions
       sed -i "s/^#define[^a-zA-Z]*POLY_Dmax.*/#define POLY_Dmax ${dim}/" Global.h
   '';
 
+  # palp has no tests of its own. This test is an adapted sage test that failed
+  # when #28029 was merged.
+  doCheck = true;
+  checkPhase = ''
+    ./nef.x -f -N << EOF | grep -q 'np='
+      3 6
+      1  0  0 -1  0  0
+      0  1  0  0 -1  0
+      0  0  1  0  0 -1
+    EOF
+  '';
+
   installPhase = ''
     mkdir -p "$out/bin"
     for file in poly class cws nef mori; do
@@ -62,6 +77,6 @@ stdenv.mkDerivation rec {
     # the right license.
     license = licenses.gpl2;
     maintainers = with maintainers; [ timokau ];
-    platforms = platforms.all;
+    platforms = platforms.linux;
   };
 }
diff --git a/pkgs/applications/science/math/pari/default.nix b/pkgs/applications/science/math/pari/default.nix
index 07c6af0926d6..a811c9da8c27 100644
--- a/pkgs/applications/science/math/pari/default.nix
+++ b/pkgs/applications/science/math/pari/default.nix
@@ -11,7 +11,7 @@ stdenv.mkDerivation rec {
   version = "2.9.5";
 
   src = fetchurl {
-    url = "http://pari.math.u-bordeaux.fr/pub/pari/unix/${name}.tar.gz";
+    url = "https://pari.math.u-bordeaux.fr/pub/pari/unix/${name}.tar.gz";
     sha256 = "05z6y5iwdzcdggbrkic9cy9vy9wmk5qxc21cb4lqnbqxnhjihibb";
   };
 
diff --git a/pkgs/applications/science/math/pari/gp2c.nix b/pkgs/applications/science/math/pari/gp2c.nix
index b3e36b6bf206..10b0d0cba36d 100644
--- a/pkgs/applications/science/math/pari/gp2c.nix
+++ b/pkgs/applications/science/math/pari/gp2c.nix
@@ -7,7 +7,7 @@ stdenv.mkDerivation rec {
   version = "0.0.10pl1";
 
   src = fetchurl {
-    url = "http://pari.math.u-bordeaux.fr/pub/pari/GP2C/${name}.tar.gz";
+    url = "https://pari.math.u-bordeaux.fr/pub/pari/GP2C/${name}.tar.gz";
     sha256 = "16hgmdvzxbmv63x1f72q1xgfyh0qhx7kaf9nbaamy0gdawxjxcav";
   };
 
diff --git a/pkgs/applications/science/math/pynac/default.nix b/pkgs/applications/science/math/pynac/default.nix
index a3ff0d6d0f1e..1a059aeb1670 100644
--- a/pkgs/applications/science/math/pynac/default.nix
+++ b/pkgs/applications/science/math/pynac/default.nix
@@ -9,14 +9,14 @@
 }:
 
 stdenv.mkDerivation rec {
-  version = "0.7.21";
+  version = "0.7.22";
   name = "pynac-${version}";
 
   src = fetchFromGitHub {
     owner = "pynac";
     repo = "pynac";
     rev = "pynac-${version}";
-    sha256 = "072mdziyx52y0knxjf2hs6nab36bckkanp7g6ff6qiy4fm0v8q04";
+    sha256 = "1ribm5vpbgsja4hbca1ckw4ln9kjkv608aaqsvxxvbs4z76ys6yi";
   };
 
   buildInputs = [
diff --git a/pkgs/applications/science/math/qalculate-gtk/default.nix b/pkgs/applications/science/math/qalculate-gtk/default.nix
index 11ae3852f067..7990088ffbe9 100644
--- a/pkgs/applications/science/math/qalculate-gtk/default.nix
+++ b/pkgs/applications/science/math/qalculate-gtk/default.nix
@@ -1,12 +1,14 @@
-{ stdenv, fetchurl, intltool, autoreconfHook, pkgconfig, libqalculate, gtk3, wrapGAppsHook }:
+{ stdenv, fetchFromGitHub, intltool, autoreconfHook, pkgconfig, libqalculate, gtk3, wrapGAppsHook }:
 
 stdenv.mkDerivation rec {
   name = "qalculate-gtk-${version}";
-  version = "2.5.0";
+  version = "2.6.1";
 
-  src = fetchurl {
-    url = "https://github.com/Qalculate/qalculate-gtk/archive/v${version}.tar.gz";
-    sha256 = "1hwwsdk3mlzvg9fsnv0hpj0s1lfkhycwv3sx2yrjwffzphhmxs7a";
+  src = fetchFromGitHub {
+    owner = "qalculate";
+    repo = "qalculate-gtk";
+    rev = "v${version}";
+    sha256 = "19jw1w29x0r1qq5r8gmqrqr00ml2pfi2w433723vjzxpfg2pp70r";
   };
 
   patchPhase = ''
diff --git a/pkgs/applications/science/math/sage/README.md b/pkgs/applications/science/math/sage/README.md
new file mode 100644
index 000000000000..46496664f28e
--- /dev/null
+++ b/pkgs/applications/science/math/sage/README.md
@@ -0,0 +1,117 @@
+# Sage on nixos
+
+Sage is a pretty complex package that depends on many other complex packages and patches some of those. As a result, the sage nix package is also quite complex.
+
+Don't feel discouraged to fix, simplify or improve things though. Here's a quick overview over the functions of the individual files:
+
+- `sage-src.nix`  
+  Downloads the source code and applies patches. This makes sure that all the other files work with the same sage source. If you want to apply a patch to sage or update sage to a new version, this is the place to do it.
+
+- `env-locations.nix`  
+  Creates a bash file that sets a bunch of environment variables telling sage where to find various packages and files. The definitions of those environment variables can be found in the sage source in the `src/env.py` file. This bash file needs to be sourced before sage is started (done in `sage-env.nix` and `sagedoc.nix`).
+
+- `sage-env.nix`  
+  Sets all environment variables sage needs to run. This includes the package locations defined in `env-locations.nix` as well as the location of sage itself and its various subdirectories.
+
+- `sagelib.nix`  
+  Defines the main sage package (without setting the necessary environments or running any tests).
+
+- `sage-with-env.nix`  
+  Wraps sage in the necessary environment.
+
+- `sage.nix`  
+  Runs sages doctests.
+
+- `sage-wrapper.nix`  
+  Optionally tells sage where do find the docs.
+
+- `sagedoc.nix`  
+  Builds and tests the sage html documentation. Can be used for offline documentation viewing as well as the sage `browse_sage_doc` and `search_doc` functions.
+
+- `sagenb.nix`  
+  The (semi deprecated) sage notebook.
+
+- `default.nix`  
+  Introduces necessary overrides, defines new packages and ties everything together (returning the `sage` package).
+
+- `flask-oldsessions.nix`, `flask-openid.nix`, `python-openid.nix`
+  These are python packages that were rejected from the main nixpkgs tree because they appear unmaintained. They are needed for the (semi-deprecated) sage notebook. Since that notebook is still needed to run the sage doctests, these packages are included but not exposed to the rest of nixpkgs.
+
+- `pybrial.nix`  
+  pybrial is a dependency of sage. However, pybrial itself also has sage as a dependency. Because of that circular dependency, pybrial is hidden from the rest of nixpkgs (just as the flask packages and python-openid.
+
+- `openblas-pc.nix`  
+  This creates a `.pc` file to be read by `pkg-config` that allows openblas to take on different roles, like `cblas` or `lapack`.
+
+## The sage build is broken
+
+First you should find out which change to nixpkgs is at fault (if you don't already know). You can use `git-bisect` for that (see the manpage).
+
+If the build broke as a result of a package update, try those solutions in order:
+
+- search the [sage trac](https://trac.sagemath.org/) for keywords like "Upgrade <package>". Maybe somebody has already proposed a patch that fixes the issue. You can then add a `fetchpatch` to `sage-src.nix`.
+
+- check if [gentoo](https://github.com/cschwan/sage-on-gentoo/tree/master/sci-mathematics/sage), [debian](https://salsa.debian.org/science-team/sagemath/tree/master/debian) or [arch linux](https://git.archlinux.org/svntogit/community.git/tree/trunk?h=packages/sagemath) already solved the problem. You can then again add a `fetchpatch` to `sage-src.nix`. If applicable you should also [propose the patch upstream](#proposing-a-sage-patch).
+
+- fix the problem yourself. First clone the sagemath source and then check out the sage version you want to patch:
+
+```
+[user@localhost ~]$ git clone git://github.com/sagemath/sage.git
+[user@localhost ~]$ cd sage
+[user@localhost sage]$ git checkout 8.2 # substitute the relevant version here
+```
+
+Then make the needed changes and generate a patch with `git diff`:
+
+```
+[user@localhost ~]$ <make changes>
+[user@localhost ~]$ git diff -u > /path/to/nixpkgs/pkgs/applications/science/math/sage/patches/name-of-patch.patch
+```
+
+Now just add the patch to `sage-src.nix` and test your changes. If they fix the problem, [propose them upstream](#proposing-a-sage-patch) and add a link to the trac ticket.
+
+- pin the package version in `default.nix` and add a note that explains why that is necessary.
+
+
+## Proposing a sage patch
+
+You can [login the sage trac using GitHub](https://trac.sagemath.org/login). Your username will then be `gh-<your-github-name>`. The only other way is to request a trac account via email. After that refer to [git the hard way](http://doc.sagemath.org/html/en/developer/manual_git.html#chapter-manual-git) in the sage documentation. The "easy way" requires a non-GitHub account (requested via email) and a special tool. The "hard way" is really not all that hard if you're a bit familiar with git.
+
+Here's the gist, assuming you want to use ssh key authentication. First, [add your public ssh key](https://trac.sagemath.org/prefs/sshkeys). Then:
+
+```
+[user@localhost ~]$ git clone git://github.com/sagemath/sage.git
+[user@localhost ~]$ cd sage
+[user@localhost sage]$ git remote add trac git@trac.sagemath.org:sage.git -t master
+[user@localhost sage]$ git checkout -b u/gh-<your-github-username>/<your-branch-name> develop
+[user@localhost sage]$ <make changes>
+[user@localhost sage]$ git add .
+[user@localhost sage]$ git commit
+[user@localhost sage]$ git show # review your changes
+[user@localhost sage]$ git push --set-upstream trac u/gh-<your-github-username>/<your-branch-name>
+```
+
+You now created a branch on the trac server (you *must* follow the naming scheme as you only have push access to branches with the `u/gh-<your-github-username>/` prefix).
+Now you can [create a new trac ticket](https://trac.sagemath.org/newticket).
+- Write a description of the change
+- set the type and component as appropriate
+- write your real name in the "Authors" field
+- write `u/gh-<your-github-username>/<your-branch-name>` in the "Branch" field
+- click "Create ticket"
+- click "Modify" on the top right of your ticket (for some reason you can only change the ticket status after you have created it)
+- set the ticket status from `new` to `needs_review`
+- click "Save changes"
+
+Refer to sages [Developer's Guide](http://doc.sagemath.org/html/en/developer/index.html) for further details.
+
+## I want to update sage
+
+You'll need to change the `version` field in `sage-src.nix`. Afterwards just try to build and let nix tell you which patches no longer apply (hopefully because they were adopted upstream). Remove those.
+
+Hopefully the build will succeed now. If it doesn't and the problem is obvious, fix it as described in [The sage build is broken](#the-sage-build-is-broken).
+If the problem is not obvious, you can try to first update sage to an intermediate version (remember that you can also set the `version` field to any git revision of sage) and locate the sage commit that introduced the issue. You can even use `git-bisect` for that (it will only be a bit tricky to keep track of which patches to apply). Hopefully after that the issue will be obvious.
+
+## Well, that didn't help!
+
+If you couldn't fix the problem, create a GitHub issue on the nixpkgs repo and ping @timokau (or whoever is listed in the `maintainers` list of the sage package).
+Describe what you did and why it didn't work. Afterwards it would be great if you help the next guy out and improve this documentation!
diff --git a/pkgs/applications/science/math/sage/default.nix b/pkgs/applications/science/math/sage/default.nix
index b74bcc3d6377..63b751b814ee 100644
--- a/pkgs/applications/science/math/sage/default.nix
+++ b/pkgs/applications/science/math/sage/default.nix
@@ -1,230 +1,214 @@
-# TODO
-# - consider writing a script to convert spkgs to nix packages, similar to vim
-#   or cabal2nix. This would allow a more efficient and "cleaner" build, greater
-#   flexibility and the possibility to select which dependencies to add and which
-#   to remove. It would also allow to use system packages for some dependencies
-#   and recompile others (optimized for the system) without recompiling everything.
-# - add optdeps:
-#   - imagemagick
-#   - texlive full for documentation
-#   - ...
-# - further seperate build outputs. Also maybe run `make doc`.
-#   Configure flags like --bindir and --libdir oculd also be used for that, see
-#   ./configure --help`.
-
-# Other resources:
-# - https://wiki.debian.org/DebianScience/Sage
-# - https://github.com/cschwan/sage-on-gentoo
-# - https://git.archlinux.org/svntogit/community.git/tree/trunk?h=packages/sagemath
-
-{ stdenv
-, bash
-, fetchurl
-, perl
-, gfortran6
-, python
-, autoreconfHook
-, gettext
-, which
-, texlive
-, texinfo
-, hevea
-, buildDocs ? false
-, optimize ? false # optimize sage to the current system (obviously impure)
+{ nixpkgs
+, withDoc ? false
 }:
 
-stdenv.mkDerivation rec {
-  version = "8.1";
-  name = "sage-${version}";
-
-  # Modified version of patchShebangs that patches to the sage-internal version if possible
-  # and falls back to the system version if not.
-  patchSageShebangs = ./patchSageShebangs.sh;
-  src = fetchurl {
-    # Note that the source is *not* fetched from github, since that doesn't
-    # the upstream folder with all the source tarballs of the spkgs.
-    # If those are not present they are fetched at build time, which breaks
-    # when building in a sandbox (and probably only works if you install the
-    # latest sage version).
-    urls = [
-      "http://mirrors.mit.edu/sage/src/sage-${version}.tar.gz"
-      "ftp://ftp.fu-berlin.de/unix/misc/sage/src/sage-${version}.tar.gz"
-      "http://sagemath.polytechnic.edu.na/src/sage-${version}.tar.gz"
-      "ftp://ftp.sun.ac.za/pub/mirrors/www.sagemath.org/src/sage-${version}.tar.gz"
-      "http://sagemath.mirror.ac.za/src/sage-${version}.tar.gz"
-      "http://ftp.leg.uct.ac.za/pub/packages/sage/src/sage-${version}.tar.gz"
-      "http://mirror.ufs.ac.za/sagemath/src/sage-${version}.tar.gz"
-      "http://mirrors-usa.go-parts.com/sage/sagemath/src/sage-${version}.tar.gz"
-      "http://www.cecm.sfu.ca/sage/src/sage-${version}.tar.gz"
-      "http://files.sagemath.org/src/sage-${version}.tar.gz"
-      "http://mirrors.xmission.com/sage/src/sage-${version}.tar.gz"
-      "http://sagemath.c3sl.ufpr.br/src/sage-${version}.tar.gz"
-      "http://linorg.usp.br/sage/src/sage-${version}.tar.gz"
-      "http://mirror.hust.edu.cn/sagemath/src/sage-${version}.tar.gz"
-      "http://ftp.iitm.ac.in/sage/src/sage-${version}.tar.gz"
-      "http://ftp.kaist.ac.kr/sage/src/sage-${version}.tar.gz"
-      "http://ftp.riken.jp/sagemath/src/sage-${version}.tar.gz"
-      "http://mirrors.tuna.tsinghua.edu.cn/sagemath/src/sage-${version}.tar.gz"
-      "http://mirrors.ustc.edu.cn/sagemath/src/sage-${version}.tar.gz"
-      "http://ftp.tsukuba.wide.ad.jp/software/sage/src/sage-${version}.tar.gz"
-      "http://ftp.yz.yamagata-u.ac.jp/pub/math/sage/src/sage-${version}.tar.gz"
-      "http://mirror.yandex.ru/mirrors/sage.math.washington.edu/src/sage-${version}.tar.gz"
-      "http://mirror.aarnet.edu.au/pub/sage/src/sage-${version}.tar.gz"
-      "http://sage.mirror.garr.it/mirrors/sage/src/sage-${version}.tar.gz"
-      "http://www.mirrorservice.org/sites/www.sagemath.org/src/sage-${version}.tar.gz"
-      "http://mirror.switch.ch/mirror/sagemath/src/sage-${version}.tar.gz"
-      "https://mirrors.up.pt/pub/sage/src/sage-${version}.tar.gz"
-      "http://www-ftp.lip6.fr/pub/math/sagemath/src/sage-${version}.tar.gz"
-      "http://ftp.ntua.gr/pub/sagemath/src/sage-${version}.tar.gz"
-    ];
-    sha256 = "1cpcs1mr0yii64s152xmxyd450bfzjb22jjj0zh9y3n6g9alzpyq";
+let
+  inherit (nixpkgs) fetchpatch fetchurl symlinkJoin callPackage nodePackages_8_x;
+
+  # https://trac.sagemath.org/ticket/15980 for tracking of python3 support
+  python = nixpkgs.python2.override {
+    packageOverrides = self: super: {
+      cypari2 = super.cypari2.override { inherit pari; };
+
+      cysignals = super.cysignals.override { inherit pari; };
+
+      cvxopt = super.cvxopt.override { inherit glpk; };
+
+      # python packages that appear unmaintained and were not accepted into the nixpkgs
+      # tree because of that. These packages are only dependencies of the more-or-less
+      # deprecated sagenb. However sagenb is still a default dependency and the doctests
+      # depend on it.
+      # See https://github.com/NixOS/nixpkgs/pull/38787 for a discussion.
+      flask-oldsessions = self.callPackage ./flask-oldsessions.nix {};
+      flask-openid = self.callPackage ./flask-openid.nix {};
+      python-openid = self.callPackage ./python-openid.nix {};
+
+      pybrial = self.callPackage ./pybrial.nix {};
+
+      sagelib = self.callPackage ./sagelib.nix {
+        inherit flint ecl pari glpk eclib ntl arb;
+        inherit sage-src openblas-blas-pc openblas-cblas-pc openblas-lapack-pc pynac singular;
+        linbox = nixpkgs.linbox.override { withSage = true; };
+      };
+
+      sagenb = self.callPackage ./sagenb.nix {
+        mathjax = nodePackages_8_x.mathjax;
+      };
+
+      sagedoc = self.callPackage ./sagedoc.nix {
+        inherit sage-src;
+      };
+
+      env-locations = self.callPackage ./env-locations.nix {
+        inherit pari_data ecl pari;
+        inherit singular;
+        three = nodePackages_8_x.three;
+        mathjax = nodePackages_8_x.mathjax;
+      };
+
+      sage-env = self.callPackage ./sage-env.nix {
+        inherit sage-src python rWrapper openblas-cblas-pc glpk ecl singular eclib pari palp flint pynac pythonEnv giac ntl;
+        pkg-config = nixpkgs.pkgconfig; # not to confuse with pythonPackages.pkgconfig
+      };
+
+      sage-with-env = self.callPackage ./sage-with-env.nix {
+        inherit pari eclib pythonEnv ntl;
+        inherit sage-src openblas-blas-pc openblas-cblas-pc openblas-lapack-pc pynac singular giac;
+        pkg-config = nixpkgs.pkgconfig; # not to confuse with pythonPackages.pkgconfig
+        three = nodePackages_8_x.three;
+      };
+
+      sage = self.callPackage ./sage.nix { };
+
+      sage-wrapper = self.callPackage ./sage-wrapper.nix {
+        inherit sage-src withDoc;
+      };
+    };
   };
 
-  postPatch = ''
-    substituteAllInPlace src/bin/sage-env
-    bash=${bash} substituteAllInPlace build/bin/sage-spkg
-  '';
-
-  installPhase = ''
-    # Sage installs during first `make`, `make install` is no-op and just takes time.
-  '';
-
-  outputs = [ "out" ] ++ stdenv.lib.optionals (buildDocs) [ "doc" ];
-
-  buildInputs = [
-    bash # needed for the build
-    perl # needed for the build
-    python # needed for the build
-    gfortran6 # needed to build giac, openblas
-    autoreconfHook # needed to configure sage with prefix
-    gettext # needed to build the singular spkg
-    which # needed in configure of mpir
-    texinfo # needed to build maxima
-  ] ++ stdenv.lib.optionals(buildDocs) [
-    hevea # needed to build the docs of the giac spkg
-    (texlive.combine { inherit (texlive)
-      scheme-basic
-      collection-pstricks # needed by giac
-      times # font needed by giac
-      stmaryrd # needed by giac
-      babel-greek # optional for giac, otherwise throws a bunch of latex command not founds
-      ;
-    })
-  ];
-
-  nativeBuildInputs = [ gfortran6 perl which ];
-
-  patches = [
-    # fix usages of /bin/rm
-    ./spkg-singular.patch
-    # help python find the crypt library
-    # patches python3 and indirectly python2, since those installation files are symlinked
-    ./spkg-python.patch
-    # fix usages of /usr/bin/perl
-    ./spkg-git.patch
-    # fix usages of /bin/cp and add necessary argument to function call
-    ./spkg-giac.patch
-    # environment
-    ./env.patch
-    # adjust wrapper shebang and patch shebangs after each spkg build
-    ./shebangs.patch
+  openblas-blas-pc = callPackage ./openblas-pc.nix { name = "blas"; };
+  openblas-cblas-pc = callPackage ./openblas-pc.nix { name = "cblas"; };
+  openblas-lapack-pc = callPackage ./openblas-pc.nix { name = "lapack"; };
+
+  sage-src = callPackage ./sage-src.nix {};
+
+  pythonRuntimeDeps = with python.pkgs; [
+    sagelib
+    pybrial
+    sagenb
+    cvxopt
+    networkx
+    service-identity
+    psutil
+    sympy
+    fpylll
+    matplotlib
+    scipy
+    ipywidgets
+    rpy2
+    sphinx
+    typing
+    pillow
   ];
 
-  enableParallelBuilding = true;
+  pythonEnv = python.buildEnv.override {
+    extraLibs = pythonRuntimeDeps;
+    ignoreCollisions = true;
+  } // { extraLibs = pythonRuntimeDeps; }; # make the libs accessible
+
+  # needs to be rWrapper, standard "R" doesn't include default packages
+  rWrapper = nixpkgs.rWrapper.override {
+    # https://trac.sagemath.org/ticket/25674
+    R = nixpkgs.R.overrideAttrs (attrs: rec {
+      name = "R-3.4.4";
+      src = fetchurl {
+        url = "http://cran.r-project.org/src/base/R-3/${name}.tar.gz";
+        sha256 = "0dq3jsnwsb5j3fhl0wi3p5ycv8avf8s5j1y4ap3d2mkjmcppvsdk";
+      };
+    });
+  };
 
-  hardeningDisable = [
-    "format" # needed to build palp, for lines like `printf(ctime(&_NFL->TIME))`
-    # TODO could be patched with `sed s|printf(ctime(\(.*\)))|%s... or fixed upstream
-  ];
+  # https://trac.sagemath.org/ticket/25532
+  ntl = nixpkgs.ntl.overrideAttrs (oldAttrs: rec {
+    name = "ntl-10.5.0";
+    sourceRoot = "${name}/src";
+    src = fetchurl {
+      url = "http://www.shoup.net/ntl/${name}.tar.gz";
+      sha256 = "1lmldaldgfr2b2a6585m3np5ds8bq1bis2s1ajycjm49vp4kc2xr";
+    };
+  });
+
+  giac = nixpkgs.giac.override { inherit ntl; };
+  arb = nixpkgs.arb.override { inherit flint; };
+
+  # update causes issues
+  # https://groups.google.com/forum/#!topic/sage-packaging/cS3v05Q0zso
+  # https://trac.sagemath.org/ticket/24735
+  singular = (nixpkgs.singular.override { inherit ntl flint; }).overrideAttrs (oldAttrs: {
+    name = "singular-4.1.0p3";
+    src = fetchurl {
+      url = "http://www.mathematik.uni-kl.de/ftp/pub/Math/Singular/SOURCES/4-1-0/singular-4.1.0p3.tar.gz";
+      sha256 = "105zs3zk46b1cps403ap9423rl48824ap5gyrdgmg8fma34680a4";
+    };
+  });
+
+  # *not* to confuse with the python package "pynac"
+  # https://trac.sagemath.org/ticket/24838 (depends on arb update)
+  pynac = nixpkgs.pynac.override { inherit singular flint; };
+
+  eclib = nixpkgs.eclib.override { inherit pari ntl; };
+
+  # With openblas (64 bit), the tests fail the same way as when sage is build with
+  # openblas instead of openblasCompat. Apparently other packages somehow use flints
+  # blas when it is available. Alternative would be to override flint to use
+  # openblasCompat.
+  flint = nixpkgs.flint.override { withBlas = false; inherit ntl; };
+
+  # Multiple palp dimensions need to be available and sage expects them all to be
+  # in the same folder.
+  palp = symlinkJoin {
+    name = "palp-${nixpkgs.palp.version}";
+    paths = [
+      (nixpkgs.palp.override { dimensions = 4; doSymlink = false; })
+      (nixpkgs.palp.override { dimensions = 5; doSymlink = false; })
+      (nixpkgs.palp.override { dimensions = 6; doSymlink = true; })
+      (nixpkgs.palp.override { dimensions = 11; doSymlink = false; })
+    ];
+  };
 
-  configureFlags = stdenv.lib.optionals(buildDocs) [ "--docdir=$(doc)" ];
-  preConfigure = ''
-    export SAGE_NUM_THREADS="$NIX_BUILD_CORES"
-
-    export HOME=/tmp/sage-home
-    export SAGE_ROOT="$PWD"
-    export SAGE_SRC="$PWD"
-
-    mkdir -p "$HOME"
-    mkdir -p "$out"
-
-    # we need to keep the source around
-    dir="$PWD"
-    cd ..
-    mv "$dir" "$out/sage-root"
-
-    export SAGE_SPKG_INSTALL_DOCS='no'
-    cd "$out/sage-root" # build in target dir, since `make` is also `make install`
-  ''
-  + stdenv.lib.optionalString (buildDocs) ''
-    mkdir -p "$doc"
-    export SAGE_DOC="$doc"
-    export SAGE_DOCBUILD_OPTS="--no-pdf-links -k"
-  ''
-  + stdenv.lib.optionalString (!optimize) ''
-    export SAGE_FAT_BINARY=yes
-  '';
-
-  buildFlags = if (buildDocs) then "doc" else "build";
-
-  # for reference: http://doc.sagemath.org/html/en/installation/source.html
-  preBuild = ''
-    # symlink python to make sure the shebangs are patched to the sage path
-    # while still being able to use python before building it
-    # (this is important because otherwise sage will try to install python
-    # packages globally later on)
-    ln -s "${python}/bin/python2" $out/bin/python2
-    ln -s "$out/bin/python2" $out/bin/python
-    touch $out/bin/python3
-    bash $patchSageShebangs .
-  '';
-
-  postBuild = ''
-    # Clean up
-    rm -r "$out/sage-root/upstream" # don't keep the sources of all the spkgs
-    rm -rf "$out/sage-root/src/build"
-    rm -rf "$out/sage-root/src/autom4te.cache"
-    rm -rf "$out/sage-root/src/config"
-    rm -rf "$out/sage-root/src/m4"
-    rm -rf "$out/sage-root/.git"
-    rm -r "$out/sage-root/logs"
-    rm -r "$out"/lib/python*/test
-    # Fix dependency cycle between out and doc
-    rm -f "$out/sage-root/config.log"
-    rm -f "$out/sage-root/config.status"
-    rm -f "$out/sage-root/build/make/Makefile-auto"
-    rm -f "$out/sage-home/.sage/gap/libgap-workspace-"*
-    # Make sure unnessessary packages don't end up in the build closure
-    find "$out" \
-        -iname 'config.log' \
-        -delete \
-        -or -iname 'Makefile' \
-        -delete
-    rm -f "$out/lib/R/etc/Renviron"
-    # Make sure all shebangs are properly patched
-    bash $patchSageShebangs $out
-  '';
-
-  # TODO there are some doctest failures, which seem harmless.
-  # We should figure out a way to fix the failures or ignore only those tests.
-  doCheck = false;
-
-  checkTarget = "ptestalllong"; # all long tests in parallell
-  preCheck = ''
-    export SAGE_TIMEOUT=0 # no timeout
-    export SAGE_TIMEOUT_LONG=0 # no timeout
-  '';
-
-  meta = {
-    homepage = http://www.sagemath.org;
-    description = "A free open source mathematics software system";
-    # taken from the homepage
-    longDescription = ''
-      SageMath is a free open-source mathematics software system licensed under the GPL. It builds on top of many existing open-source packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R and many more. Access their combined power through a common, Python-based language or directly via interfaces or wrappers.
-      Mission: Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab.
-    '';
-    license = stdenv.lib.licenses.gpl2Plus;
-    platforms = ["x86_64-linux" "i686-linux"];
-    maintainers = with stdenv.lib.maintainers; [ timokau ];
+  # Sage expects those in the same directory.
+  pari_data = symlinkJoin {
+    name = "pari_data";
+    paths = with nixpkgs; [
+      pari-galdata
+      pari-seadata-small
+    ];
   };
-}
+
+  # https://trac.sagemath.org/ticket/22191
+  ecl = nixpkgs.ecl_16_1_2;
+
+  # sage currently uses an unreleased version of pari
+  pari = (nixpkgs.pari.override { withThread = false; }).overrideAttrs (attrs: rec {
+    version = "2.10-1280-g88fb5b3"; # on update remove pari-stackwarn patch from `sage-src.nix`
+    src = fetchurl {
+      url = "mirror://sageupstream/pari/pari-${version}.tar.gz";
+      sha256 = "19gbsm8jqq3hraanbmsvzkbh88iwlqbckzbnga3y76r7k42akn7m";
+    };
+  });
+
+  # https://trac.sagemath.org/ticket/24824
+  glpk = nixpkgs.glpk.overrideAttrs (attrs: rec {
+    version = "4.63";
+    name = "glpk-${version}";
+    src = fetchurl {
+      url = "mirror://gnu/glpk/${name}.tar.gz";
+      sha256 = "1xp7nclmp8inp20968bvvfcwmz3mz03sbm0v3yjz8aqwlpqjfkci";
+    };
+    patches = (attrs.patches or []) ++ [
+      # Alternatively patch sage with debians
+      # https://sources.debian.org/data/main/s/sagemath/8.1-7/debian/patches/t-version-glpk-4.60-extra-hack-fixes.patch
+      # The header of that debian patch contains a good description of the issue. The gist of it:
+      # > If GLPK in Sage causes one error, and this is caught by Sage and recovered from, then
+      # > later (because upstream GLPK does not clear the "error" flag) Sage will append
+      # > all subsequent terminal output of GLPK into the error_message string but not
+      # > actually forward it to the user's terminal. This breaks some doctests.
+      (fetchpatch {
+        name = "error_recovery.patch";
+        url = "https://git.sagemath.org/sage.git/plain/build/pkgs/glpk/patches/error_recovery.patch?id=07d6c37d18811e2b377a9689790a7c5e24da16ba";
+        sha256 = "0z99z9gd31apb6x5n5n26411qzx0ma3s6dnznc4x61x86bhq31qf";
+      })
+
+      # Allow setting a exact verbosity level (OFF|ERR|ON|ALL|DBG)
+      (fetchpatch {
+        name = "exact_verbosity.patch";
+        url = "https://git.sagemath.org/sage.git/plain/build/pkgs/glpk/patches/glp_exact_verbosity.patch?id=07d6c37d18811e2b377a9689790a7c5e24da16ba";
+        sha256 = "15gm5i2alqla3m463i1qq6jx6c0ns6lip7njvbhp37pgxg4s9hx8";
+      })
+    ];
+  });
+in
+  python.pkgs.sage-wrapper // {
+    doc = python.pkgs.sagedoc;
+    lib = python.pkgs.sagelib;
+  }
diff --git a/pkgs/applications/science/math/sage/env-locations.nix b/pkgs/applications/science/math/sage/env-locations.nix
new file mode 100644
index 000000000000..288ffd6e0e1a
--- /dev/null
+++ b/pkgs/applications/science/math/sage/env-locations.nix
@@ -0,0 +1,45 @@
+{ writeTextFile
+, pari_data
+, pari
+, singular
+, maxima-ecl
+, conway_polynomials
+, graphs
+, elliptic_curves
+, polytopes_db
+, gap-libgap-compatible
+, ecl
+, combinatorial_designs
+, jmol
+, mathjax
+, three
+, cysignals
+}:
+
+writeTextFile rec {
+  name = "sage-env-locations";
+  destination = "/${name}";
+  text = ''
+    export GP_DATA_DIR="${pari_data}/share/pari"
+    export PARI_DATA_DIR="${pari_data}"
+    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'
+    export POLYTOPE_DATA_DIR='${polytopes_db}/share/reflexive_polytopes'
+    export GAP_ROOT_DIR='${gap-libgap-compatible}/share/gap/build-dir'
+    export ECLDIR='${ecl}/lib/ecl-${ecl.version}/'
+    export COMBINATORIAL_DESIGN_DATA_DIR="${combinatorial_designs}/share/combinatorial_designs"
+    export CREMONA_MINI_DATA_DIR="${elliptic_curves}/share/cremona"
+    export JMOL_DIR="${jmol}"
+    export MATHJAX_DIR="${mathjax}/lib/node_modules/mathjax"
+    export THREEJS_DIR="${three}/lib/node_modules/three"
+    export SAGE_INCLUDE_DIRECTORIES="${cysignals}/lib/python2.7/site-packages"
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/env.patch b/pkgs/applications/science/math/sage/env.patch
deleted file mode 100644
index a7f8538eac3d..000000000000
--- a/pkgs/applications/science/math/sage/env.patch
+++ /dev/null
@@ -1,22 +0,0 @@
-diff --git a/src/bin/sage-env b/src/bin/sage-env
-index ead308f861..ed8db9f9b7 100644
---- a/src/bin/sage-env
-+++ b/src/bin/sage-env
-@@ -111,6 +111,8 @@ resolvelinks() {
- }
- 
- 
-+SAGE_ROOT="@out@/sage-root"
-+
- # New value for SAGE_ROOT: either SAGE_ROOT (if given)
- # or a guessed value based on pwd.
- if [ -n "$SAGE_ROOT" ]; then
-@@ -185,6 +187,8 @@ fi
- export SAGE_ENV_SOURCED=$SAGE_ENV_VERSION
- 
- export SAGE_ROOT="$NEW_SAGE_ROOT"
-+export SAGE_LOCAL='@out@/'
-+export PYTHONPATH="@out@/lib/python2.7/site-packages:$PYTHONPATH"
- 
- 
- # sage-env must know where the Sage's script files are.
diff --git a/pkgs/applications/science/math/sage/fetch-mirrors.sh b/pkgs/applications/science/math/sage/fetch-mirrors.sh
deleted file mode 100755
index a1103e86beb9..000000000000
--- a/pkgs/applications/science/math/sage/fetch-mirrors.sh
+++ /dev/null
@@ -1,11 +0,0 @@
-#! /usr/bin/env nix-shell
-#! nix-shell -i bash -p curl go-pup
-
-# Fetches a list of all available source mirrors from the sage homepage.
-# Note that the list is sorted by country, but fetchurl doesn't offer an option
-# to customize mirror preference.
-
-curl -s http://www.sagemath.org/download-source.html \
-    | pup 'table#mirror'  \
-    | pup 'a attr{href}' \
-    | sed -e 's/index\.html/sage-${version}.tar.gz/'
diff --git a/pkgs/applications/science/math/sage/flask-oldsessions.nix b/pkgs/applications/science/math/sage/flask-oldsessions.nix
new file mode 100644
index 000000000000..85e35736d030
--- /dev/null
+++ b/pkgs/applications/science/math/sage/flask-oldsessions.nix
@@ -0,0 +1,36 @@
+{ stdenv
+, buildPythonPackage
+, fetchFromGitHub
+, python
+, flask
+}:
+
+buildPythonPackage rec {
+  pname = "Flask-OldSessions";
+  version = "0.10";
+
+  # no artifact on pypi: https://github.com/mitsuhiko/flask-oldsessions/issues/1
+  src = fetchFromGitHub {
+    owner = "mitsuhiko";
+    repo = "flask-oldsessions";
+    rev = "${version}";
+    sha256 = "04b5m8njjiwld9a0zw55iqwvyjgwcpdbhz1cic8nyhgcmypbicqn";
+  };
+
+  propagatedBuildInputs = [
+    flask
+  ];
+
+  # missing module flask.testsuite, probably assumes an old version of flask
+  doCheck = false;
+  checkPhase = ''
+    ${python.interpreter} run-tests.py
+  '';
+
+  meta = with stdenv.lib; {
+    description = "Provides a session class that works like the one in Flask before 0.10.";
+    license = licenses.bsd2;
+    maintainers = with maintainers; [ timokau ];
+    homepage = https://github.com/mitsuhiko/flask-oldsessions;
+  };
+}
diff --git a/pkgs/applications/science/math/sage/flask-openid.nix b/pkgs/applications/science/math/sage/flask-openid.nix
new file mode 100644
index 000000000000..9043fabd9b4a
--- /dev/null
+++ b/pkgs/applications/science/math/sage/flask-openid.nix
@@ -0,0 +1,28 @@
+{ stdenv
+, buildPythonPackage
+, fetchPypi
+, flask
+, python-openid
+}:
+
+buildPythonPackage rec {
+  pname = "Flask-OpenID";
+  version = "1.2.5";
+
+  src = fetchPypi {
+    inherit pname version;
+    sha256 = "1aycwmwi7ilcaa5ab8hm0bp6323zl8z25q9ha0gwrl8aihfgx3ss";
+  };
+
+  propagatedBuildInputs = [
+    flask
+    python-openid
+  ];
+
+  meta = with stdenv.lib; {
+    description = "Adds openid support to flask applications";
+    license = licenses.bsd2;
+    maintainers = with maintainers; [ timokau ];
+    homepage = https://pythonhosted.org/Flask-OpenID/;
+  };
+}
diff --git a/pkgs/applications/science/math/sage/openblas-pc.nix b/pkgs/applications/science/math/sage/openblas-pc.nix
new file mode 100644
index 000000000000..f4669a6557e9
--- /dev/null
+++ b/pkgs/applications/science/math/sage/openblas-pc.nix
@@ -0,0 +1,17 @@
+{ openblasCompat
+, writeTextFile
+, name
+}:
+
+writeTextFile {
+  name = "openblas-${name}-pc-${openblasCompat.version}";
+  destination = "/lib/pkgconfig/${name}.pc";
+  text = ''
+    Name: ${name}
+    Version: ${openblasCompat.version}
+
+    Description: ${name} for SageMath, provided by the OpenBLAS package.
+    Cflags: -I${openblasCompat}/include
+    Libs: -L${openblasCompat}/lib -lopenblas
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/patchSageShebangs.sh b/pkgs/applications/science/math/sage/patchSageShebangs.sh
deleted file mode 100644
index 6ddf93af0114..000000000000
--- a/pkgs/applications/science/math/sage/patchSageShebangs.sh
+++ /dev/null
@@ -1,51 +0,0 @@
-# This is a slightly modified version of nix's default patchShebangs
-
-dir="$1"
-
-echo "patching sage internal script interpreter paths in $( readlink -f "$dir")"
-
-find "$dir" -type f -perm -0100 | while read f; do
-    if [ "$(head -1 "$f" | head -c+2)" != '#!' ]; then
-        # missing shebang => not a script
-        continue
-    fi
-
-    oldInterpreterLine=$(head -1 "$f" | tail -c+3)
-    read -r oldPath arg0 args <<< "$oldInterpreterLine"
-
-    if $(echo "$oldPath" | grep -q "/bin/env$"); then
-        # Check for unsupported 'env' functionality:
-        # - options: something starting with a '-'
-        # - environment variables: foo=bar
-        if $(echo "$arg0" | grep -q -- "^-.*\|.*=.*"); then
-            echo "unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)"
-            exit 1
-        fi
-        executable="$arg0"
-    else
-        if [ "$oldPath" = "" ]; then
-            # If no interpreter is specified linux will use /bin/sh. Set
-            # oldpath="/bin/sh" so that we get /nix/store/.../sh.
-            oldPath="/bin/sh"
-        fi
-        executable="$(basename "$oldPath")"
-        args="$arg0 $args"
-    fi
-
-    newPath="$(echo "$out/bin/$executable $args" | sed 's/[[:space:]]*$//')"
-    if [[ ! -x "$newPath" ]] ; then
-        newPath="$(command -v "$executable" || true)"
-    fi
-
-    # Strip trailing whitespace introduced when no arguments are present
-    newInterpreterLine="$(echo "$newPath $args" | sed 's/[[:space:]]*$//')"
-
-    if [ -n "$oldPath" -a "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then
-        if [ -n "$newPath" -a "$newPath" != "$oldPath" ]; then
-            echo "$f: sage interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
-            # escape the escape chars so that sed doesn't interpret them
-            escapedInterpreterLine=$(echo "$newInterpreterLine" | sed 's|\\|\\\\|g')
-            sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
-        fi
-    fi
-done
diff --git a/pkgs/applications/science/math/sage/patches/arb-2.13.0.patch b/pkgs/applications/science/math/sage/patches/arb-2.13.0.patch
new file mode 100644
index 000000000000..a20f1670a256
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/arb-2.13.0.patch
@@ -0,0 +1,427 @@
+commit c885927e25b29bd23869e02379c2918da430323e
+Author: Timo Kaufmann <timokau@zoho.com>
+Date:   Sat Jun 30 02:26:15 2018 +0200
+
+diff --git a/build/pkgs/arb/checksums.ini b/build/pkgs/arb/checksums.ini
+index 1924ee03c3..9323b97391 100644
+--- a/build/pkgs/arb/checksums.ini
++++ b/build/pkgs/arb/checksums.ini
+@@ -1,4 +1,4 @@
+ tarball=arb-VERSION.tar.gz
+-sha1=27476d0529e48a07d92da90bd0fb80dd18f443e3
+-md5=733285d9705d10b8024e551ffa81952f
+-cksum=2391183744
++sha1=44eda7bf8eaa666c45b1fc2c1b5bd08756d94b58
++md5=fa24de9fffe4394fb6a7a6792e2ecc5f
++cksum=3689220688
+diff --git a/build/pkgs/arb/package-version.txt b/build/pkgs/arb/package-version.txt
+index c8810e9bdb..fb2c0766b7 100644
+--- a/build/pkgs/arb/package-version.txt
++++ b/build/pkgs/arb/package-version.txt
+@@ -1 +1 @@
+-2.12.0.p0
++2.13.0
+diff --git a/build/pkgs/arb/patches/arb-pie-hardening-conflict.patch b/build/pkgs/arb/patches/arb-pie-hardening-conflict.patch
+deleted file mode 100644
+index 3e5c0e708b..0000000000
+--- a/build/pkgs/arb/patches/arb-pie-hardening-conflict.patch
++++ /dev/null
+@@ -1,17 +0,0 @@
+-In newer binutils, ld options -r and -pie conflict.
+-Patch due to Jörg-Volker Peetz
+-(source : https://groups.google.com/d/msg/sage-devel/TduebNoZuBE/sEULolL0BQAJ),
+-packaged by Emmanuel Charpentier
+-
+-diff -ru arb-2.8.1-orig/Makefile.subdirs arb-2.8.1-new/Makefile.subdirs
+---- arb-2.8.1-orig/Makefile.subdirs	2015-12-31 17:30:01.000000000 +0100
+-+++ arb-2.8.1-new/Makefile.subdirs	2016-11-07 18:50:34.540051779 +0100
+-@@ -52,7 +52,7 @@
+- 	$(QUIET_CC) $(CC) $(CFLAGS) $(INCS) -c $< -o $@ -MMD -MP -MF "$(BUILD_DIR)/$(MOD_DIR)_$*.d" -MT "$(BUILD_DIR)/$(MOD_DIR)_$*.d" -MT "$@"
+- 
+- $(MOD_LOBJ): $(LOBJS)
+--	$(QUIET_CC) $(CC) $(ABI_FLAG) -Wl,-r $^ -o $@ -nostdlib
+-+	$(QUIET_CC) $(CC) $(ABI_FLAG) -r $^ -o $@ -nostdlib
+- 
+- -include $(LOBJS:.lo=.d)
+- 
+diff --git a/src/sage/rings/complex_arb.pyx b/src/sage/rings/complex_arb.pyx
+index 70d51e655a..00e7caea2c 100644
+--- a/src/sage/rings/complex_arb.pyx
++++ b/src/sage/rings/complex_arb.pyx
+@@ -857,14 +857,14 @@ class ComplexBallField(UniqueRepresentation, Field):
+             [0.500000000000000 +/- 2.09e-16]
+ 
+             sage: CBF.integral(lambda x, _: x.gamma(), 1 - CBF(i), 1 + CBF(i))
+-            [+/- 3.95e-15] + [1.5723926694981 +/- 4.53e-14]*I
++            [+/- 4...e-15] + [1.5723926694981 +/- 4...e-14]*I
+ 
+             sage: C = ComplexBallField(100)
+             sage: C.integral(lambda x, _: x.cos() * x.sin(), 0, 1)
+             [0.35403670913678559674939205737 +/- 8.89e-30]
+ 
+             sage: CBF.integral(lambda x, _: (x + x.exp()).sin(), 0, 8)
+-            [0.34740017266 +/- 6.36e-12]
++            [0.34740017266 +/- 6...e-12]
+ 
+             sage: C = ComplexBallField(2000)
+             sage: C.integral(lambda x, _: (x + x.exp()).sin(), 0, 8) # long time
+@@ -879,14 +879,14 @@ class ComplexBallField(UniqueRepresentation, Field):
+             ....:     else:
+             ....:         return z.sqrt()
+             sage: CBF.integral(my_sqrt, -1 + CBF(i), -1 - CBF(i))
+-            [+/- 1.14e-14] + [-0.4752076627926 +/- 5.18e-14]*I
++            [+/- 1.14e-14] + [-0.4752076627926 +/- 5...e-14]*I
+ 
+         Note, though, that proper handling of the ``analytic`` flag is required
+         even when the path does not touch the branch cut::
+ 
+             sage: correct = CBF.integral(my_sqrt, 1, 2); correct
+             [1.21895141649746 +/- 3.73e-15]
+-            sage: RBF(integral(sqrt(x), x, 1, 2))
++            sage: RBF(integral(sqrt(x), x, 1, 2))  # long time
+             [1.21895141649746 +/- 1.79e-15]
+             sage: wrong = CBF.integral(lambda z, _: z.sqrt(), 1, 2) # WRONG!
+             sage: correct - wrong
+@@ -915,9 +915,9 @@ class ComplexBallField(UniqueRepresentation, Field):
+         the integrand is unbounded::
+ 
+             sage: CBF.integral(lambda x, _: 1/x, -1, 1)
+-            [+/- inf] + [+/- inf]*I
++            nan + nan*I
+             sage: CBF.integral(lambda x, _: 1/x, 10^-1000, 1)
+-            [+/- inf] + [+/- inf]*I
++            nan + nan*I
+             sage: CBF.integral(lambda x, _: 1/x, 10^-1000, 1, abs_tol=1e-10)
+             [2302.5850930 +/- 1.26e-8]
+ 
+@@ -928,14 +928,15 @@ class ComplexBallField(UniqueRepresentation, Field):
+             sage: CBF.integral(lambda x, _: x.exp(), -1020, -1010, abs_tol=1e-450)
+             [2.304377150950e-439 +/- 9.74e-452]
+             sage: CBF.integral(lambda x, _: x.exp(), -1020, -1010, abs_tol=0)
+-            [2.304377150949e-439 +/- 7.53e-452]
+-            sage: CBF.integral(lambda x, _: x.exp(), -1020, -1010, rel_tol=1e-4, abs_tol=0)
+-            [2.30438e-439 +/- 3.90e-445]
++            [2.304377150950e-439 +/- 7...e-452]
++            sage: CBF.integral(lambda x, _: x.exp(), -1020, -1010, rel_tol=1e-2, abs_tol=0)
++            [2.30438e-439 +/- 5.94e-445]
+ 
+-            sage: CBF.integral(lambda x, _: x*(1/x).sin(), 0, 1)
+-            [+/- 0.644]
+-            sage: CBF.integral(lambda x, _: x*(1/x).sin(), 0, 1, use_heap=True)
+-            [0.3785300 +/- 4.32e-8]
++            sage: epsi = CBF(1e-10)
++            sage: CBF.integral(lambda x, _: x*(1/x).sin(), epsi, 1)
++            [0.38 +/- 8.54e-3]
++            sage: CBF.integral(lambda x, _: x*(1/x).sin(), epsi, 1, use_heap=True)
++            [0.37853002 +/- 8.73e-9]
+ 
+         ALGORITHM:
+ 
+@@ -951,12 +952,12 @@ class ComplexBallField(UniqueRepresentation, Field):
+ 
+             sage: i = QuadraticField(-1).gen()
+             sage: CBF.integral(lambda x, _: (1 + i*x).gamma(), -1, 1)
+-            [1.5723926694981 +/- 4.53e-14] + [+/- 3.95e-15]*I
++            [1.5723926694981 +/- 4...e-14] + [+/- 4...e-15]*I
+ 
+-            sage: ComplexBallField(10000).integral(lambda x, _: x.sin(), 0, 1, rel_tol=1e-400)
+-            [0.459... +/- ...e-4...]
++            sage: ComplexBallField(10000).integral(lambda x, _: x.sin(), 0, 1, rel_tol=1e-300)
++            [0.459... +/- ...e-3...]
+             sage: CBF.integral(lambda x, _: x.sin(), 0, 100, rel_tol=10)
+-            [+/- 7.61]
++            [0.138 +/- 5.53e-4]
+ 
+             sage: ComplexBallField(10000).integral(lambda x, _: x.sin(), 0, 1, abs_tol=1e-400)
+             [0.459697... +/- ...e-4...]
+@@ -2389,9 +2390,9 @@ cdef class ComplexBall(RingElement):
+             sage: ~CBF(i/3)
+             [-3.00000000000000 +/- 9.44e-16]*I
+             sage: ~CBF(0)
+-            [+/- inf]
++            nan
+             sage: ~CBF(RIF(10,11))
+-            [0.1 +/- 9.53e-3]
++            [0.1 +/- 9.10e-3]
+         """
+         cdef ComplexBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
+@@ -2512,9 +2513,9 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(-2, 1)/CBF(1, 1/3)
+             [-1.500000000000000 +/- 8.83e-16] + [1.500000000000000 +/- 5.64e-16]*I
+             sage: CBF(2+I)/CBF(0)
+-            [+/- inf] + [+/- inf]*I
++            nan + nan*I
+             sage: CBF(1)/CBF(0)
+-            [+/- inf]
++            nan
+             sage: CBF(1)/CBF(RBF(0, 1.))
+             nan
+         """
+@@ -2543,9 +2544,9 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(0)^(1/3)
+             0
+             sage: CBF(0)^(-1)
+-            [+/- inf]
++            nan
+             sage: CBF(0)^(-2)
+-            [+/- inf] + [+/- inf]*I
++            nan + nan*I
+ 
+         TESTS::
+ 
+@@ -2656,12 +2657,12 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(1).rising_factorial(5)
+             120.0000000000000
+             sage: CBF(1/3, 1/2).rising_factorial(300)
+-            [-3.87949484514e+612 +/- 5.23e+600] + [-3.52042209763e+612 +/- 5.55e+600]*I
++            [-3.87949484514e+612 +/- 5...e+600] + [-3.52042209763e+612 +/- 5...e+600]*I
+ 
+             sage: CBF(1).rising_factorial(-1)
+             nan
+             sage: CBF(1).rising_factorial(2**64)
+-            [+/- 2.30e+347382171305201370464]
++            [+/- 2.30e+347382171326740403407]
+             sage: ComplexBallField(128)(1).rising_factorial(2**64)
+             [2.343691126796861348e+347382171305201285713 +/- 4.71e+347382171305201285694]
+             sage: CBF(1/2).rising_factorial(CBF(2,3))
+@@ -2700,7 +2701,7 @@ cdef class ComplexBall(RingElement):
+             [1.000000000000000 +/- 2.83e-16] + [-0.441271200305303 +/- 2.82e-16]*I
+ 
+             sage: CBF('inf').log()
+-            nan + nan*I
++            [+/- inf]
+             sage: CBF(2).log(0)
+             nan + nan*I
+         """
+@@ -2808,7 +2809,7 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(pi/2, 1/10).tan()
+             [+/- 2.87e-14] + [10.0333111322540 +/- 2.36e-14]*I
+             sage: CBF(pi/2).tan()
+-            [+/- inf]
++            nan
+         """
+         cdef ComplexBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
+@@ -2825,7 +2826,7 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(pi, 1/10).cot()
+             [+/- 5.74e-14] + [-10.0333111322540 +/- 2.81e-14]*I
+             sage: CBF(pi).cot()
+-            [+/- inf]
++            nan
+         """
+         cdef ComplexBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
+@@ -3211,9 +3212,9 @@ cdef class ComplexBall(RingElement):
+             1.000000000000000*I
+ 
+             sage: CBF(2+3*I).hypergeometric([1/4,1/3],[1/2])
+-            [0.7871684267473 +/- 7.34e-14] + [0.2749254173721 +/- 9.23e-14]*I
++            [0.7871684267473 +/- 7...e-14] + [0.2749254173721 +/- 9...e-14]*I
+             sage: CBF(2+3*I).hypergeometric([1/4,1/3],[1/2],regularized=True)
+-            [0.4441122268685 +/- 3.96e-14] + [0.1551100567338 +/- 5.75e-14]*I
++            [0.4441122268685 +/- 3...e-14] + [0.1551100567338 +/- 5...e-14]*I
+ 
+             sage: CBF(5).hypergeometric([2,3], [-5])
+             nan + nan*I
+@@ -4041,9 +4042,9 @@ cdef class ComplexBall(RingElement):
+ 
+             sage: phi = CBF(1,1)
+             sage: (CBF.pi()/2).elliptic_e_inc(phi)
+-            [1.283840957898 +/- 3.23e-13] + [-0.5317843366915 +/- 7.79e-14]*I
++            [1.283840957898 +/- 3...e-13] + [-0.5317843366915 +/- 7...e-14]*I
+             sage: phi.elliptic_e()
+-            [1.2838409578982 +/- 5.90e-14] + [-0.5317843366915 +/- 3.35e-14]*I
++            [1.2838409578982 +/- 5...e-14] + [-0.5317843366915 +/- 3...e-14]*I
+ 
+             sage: phi = CBF(2, 3/7)
+             sage: (CBF.pi()/2).elliptic_e_inc(phi)
+@@ -4312,8 +4313,7 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(10).laguerre_L(3, 2)
+             [-6.666666666667 +/- 4.15e-13]
+             sage: CBF(5,7).laguerre_L(CBF(2,3), CBF(1,-2))
+-            [5515.315030271 +/- 4.37e-10] + [-12386.942845271 +/- 5.47e-10]*I
+-
++            [5515.315030271 +/- 4...e-10] + [-12386.942845271 +/- 5...e-10]*I
+         """
+         cdef ComplexBall my_n = self._parent.coerce(n)
+         cdef ComplexBall my_m = self._parent.coerce(m)
+@@ -4357,9 +4357,9 @@ cdef class ComplexBall(RingElement):
+         EXAMPLES::
+ 
+             sage: CBF(1/2).legendre_P(5)
+-            [0.08984375000000000 +/- 4.5...e-18]
++            [0.0898437500000000 +/- 7...e-17]
+             sage: CBF(1,2).legendre_P(CBF(2,3), CBF(0,1))
+-            [0.10996180744364 +/- 7.45e-15] + [0.14312767804055 +/- 8.38e-15]*I
++            [0.10996180744364 +/- 7.12e-15] + [0.14312767804055 +/- 8.07e-15]*I
+             sage: CBF(-10).legendre_P(5, 325/100)
+             [-22104403.487377 +/- 6.81e-7] + [53364750.687392 +/- 7.25e-7]*I
+             sage: CBF(-10).legendre_P(5, 325/100, type=3)
+@@ -4393,9 +4393,9 @@ cdef class ComplexBall(RingElement):
+             sage: CBF(1/2).legendre_Q(5)
+             [0.55508089057168 +/- 2.79e-15]
+             sage: CBF(1,2).legendre_Q(CBF(2,3), CBF(0,1))
+-            [0.167678710 +/- 4.60e-10] + [-0.161558598 +/- 7.47e-10]*I
++            [0.167678710 +/- 3.91e-10] + [-0.161558598 +/- 6.77e-10]*I
+             sage: CBF(-10).legendre_Q(5, 325/100)
+-            [-83825154.36008 +/- 4.94e-6] + [-34721515.80396 +/- 5.40e-6]*I
++            [-83825154.36008 +/- 5.02e-6] + [-34721515.80396 +/- 5.42e-6]*I
+             sage: CBF(-10).legendre_Q(5, 325/100, type=3)
+             [-4.797306921692e-6 +/- 6.82e-19] + [-4.797306921692e-6 +/- 6.57e-19]*I
+ 
+diff --git a/src/sage/rings/polynomial/polynomial_complex_arb.pyx b/src/sage/rings/polynomial/polynomial_complex_arb.pyx
+index c436d4705b..ef611a566b 100644
+--- a/src/sage/rings/polynomial/polynomial_complex_arb.pyx
++++ b/src/sage/rings/polynomial/polynomial_complex_arb.pyx
+@@ -543,7 +543,7 @@ cdef class Polynomial_complex_arb(Polynomial):
+             sage: (1 - x/3).inverse_series_trunc(3)
+             ([0.1111111111111111 +/- 5.99e-17])*x^2 + ([0.3333333333333333 +/- 7.04e-17])*x + 1.000000000000000
+             sage: x.inverse_series_trunc(1)
+-            [+/- inf]
++            nan
+             sage: Pol(0).inverse_series_trunc(2)
+             (nan + nan*I)*x + nan + nan*I
+ 
+@@ -671,7 +671,7 @@ cdef class Polynomial_complex_arb(Polynomial):
+             sage: pol._sqrt_series(2)
+             ([+/- 7.51e-3] + [+/- 0.501]*I)*x + [+/- 5.01e-3] + [+/- 1.01]*I
+             sage: x._sqrt_series(2)
+-            ([+/- inf] + [+/- inf]*I)*x
++            (nan + nan*I)*x
+         """
+         cdef Polynomial_complex_arb res = self._new()
+         if n < 0:
+diff --git a/src/sage/rings/real_arb.pyx b/src/sage/rings/real_arb.pyx
+index c9f68e38d7..76e3037a9a 100644
+--- a/src/sage/rings/real_arb.pyx
++++ b/src/sage/rings/real_arb.pyx
+@@ -161,7 +161,7 @@ values and should be preferred::
+ 
+     sage: RBF(NaN) < RBF(infinity)
+     False
+-    sage: 1/RBF(0) <= RBF(infinity)
++    sage: RBF(0).add_error(infinity) <= RBF(infinity)
+     True
+ 
+ TESTS::
+@@ -252,6 +252,8 @@ cdef void mpfi_to_arb(arb_t target, const mpfi_t source, const long precision):
+         (+infinity, +infinity)
+         sage: RBF(RIF(-infinity)).endpoints()
+         (-infinity, -infinity)
++        sage: RBF(RIF(-infinity, infinity)).endpoints()
++        (-infinity, +infinity)
+         sage: RIF(RBF(infinity)).endpoints()
+         (+infinity, +infinity)
+         sage: RIF(RBF(-infinity)).endpoints()
+@@ -266,10 +268,11 @@ cdef void mpfi_to_arb(arb_t target, const mpfi_t source, const long precision):
+     if _do_sig(precision): sig_on()
+     mpfi_get_left(left, source)
+     mpfi_get_right(right, source)
+-    arb_set_interval_mpfr(target, left, right, precision)
+-    # Work around weakness of arb_set_interval_mpfr(tgt, inf, inf)
+-    if mpfr_equal_p(left, right):
+-        mag_zero(arb_radref(target))
++    if mpfr_inf_p(left) and mpfr_inf_p(right) and mpfr_sgn(left) < 0 < mpfr_sgn(right):
++        # Work around a weakness of arb_set_interval_mpfr(tgt, -inf, inf)
++        arb_zero_pm_inf(target)
++    else:
++        arb_set_interval_mpfr(target, left, right, precision)
+     if _do_sig(precision): sig_off()
+ 
+     mpfr_clear(left)
+@@ -649,17 +652,15 @@ class RealBallField(UniqueRepresentation, Field):
+         EXAMPLES::
+ 
+             sage: RBF.some_elements()
+-            [1.000000000000000,
+-            [0.3333333333333333 +/- 7.04e-17],
++            [0, 1.000000000000000, [0.3333333333333333 +/- 7.04e-17],
+             [-4.733045976388941e+363922934236666733021124 +/- 3.46e+363922934236666733021108],
+-            [+/- inf],
+-            [+/- inf],
+-            nan]
++            [+/- inf], [+/- inf], [+/- inf], nan]
+         """
+         import sage.symbolic.constants
+-        return [self(1), self(1)/3,
++        inf = self(sage.rings.infinity.Infinity)
++        return [self(0), self(1), self(1)/3,
+                 -self(2)**(Integer(2)**80),
+-                self(sage.rings.infinity.Infinity), ~self(0),
++                inf, -inf, self.zero().add_error(inf),
+                 self.element_class(self, sage.symbolic.constants.NotANumber())]
+ 
+     def _sum_of_products(self, terms):
+@@ -881,7 +882,7 @@ class RealBallField(UniqueRepresentation, Field):
+             sage: RBF.gamma(5)
+             24.00000000000000
+             sage: RBF.gamma(10**20)
+-            [+/- 5.92e+1956570551809674821757]
++            [+/- 5.50e+1956570552410610660600]
+             sage: RBF.gamma(1/3)
+             [2.678938534707747 +/- 8.99e-16]
+             sage: RBF.gamma(-5)
+@@ -2247,7 +2248,7 @@ cdef class RealBall(RingElement):
+             sage: inf = RBF(+infinity)
+             sage: other_inf = RBF(+infinity, 42.r)
+             sage: neg_inf = RBF(-infinity)
+-            sage: extended_line = 1/RBF(0)
++            sage: extended_line = RBF(0).add_error(infinity)
+             sage: exact_nan = inf - inf
+             sage: exact_nan.mid(), exact_nan.rad()
+             (NaN, 0.00000000)
+@@ -2659,7 +2660,7 @@ cdef class RealBall(RingElement):
+             sage: ~RBF(5)
+             [0.2000000000000000 +/- 4.45e-17]
+             sage: ~RBF(0)
+-            [+/- inf]
++            nan
+             sage: RBF(RIF(-0.1,0.1))
+             [+/- 0.101]
+ 
+@@ -2739,7 +2740,7 @@ cdef class RealBall(RingElement):
+             sage: RBF(pi)/RBF(e)
+             [1.155727349790922 +/- 8.43e-16]
+             sage: RBF(2)/RBF(0)
+-            [+/- inf]
++            nan
+         """
+         cdef RealBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
+@@ -2765,7 +2766,7 @@ cdef class RealBall(RingElement):
+             sage: RBF(-1)^(1/3)
+             nan
+             sage: RBF(0)^(-1)
+-            [+/- inf]
++            nan
+             sage: RBF(-e)**RBF(pi)
+             nan
+ 
+@@ -3129,7 +3130,7 @@ cdef class RealBall(RingElement):
+             sage: RBF(1).tan()
+             [1.557407724654902 +/- 3.26e-16]
+             sage: RBF(pi/2).tan()
+-            [+/- inf]
++            nan
+         """
+         cdef RealBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
+@@ -3146,7 +3147,7 @@ cdef class RealBall(RingElement):
+             sage: RBF(1).cot()
+             [0.642092615934331 +/- 4.79e-16]
+             sage: RBF(pi).cot()
+-            [+/- inf]
++            nan
+         """
+         cdef RealBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
+@@ -3257,7 +3258,7 @@ cdef class RealBall(RingElement):
+             sage: RBF(1).coth()
+             [1.313035285499331 +/- 4.97e-16]
+             sage: RBF(0).coth()
+-            [+/- inf]
++            nan
+         """
+         cdef RealBall res = self._new()
+         if _do_sig(prec(self)): sig_on()
diff --git a/pkgs/applications/science/math/sage/patches/dochtml-optional.patch b/pkgs/applications/science/math/sage/patches/dochtml-optional.patch
new file mode 100644
index 000000000000..a51e30312ed9
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/dochtml-optional.patch
@@ -0,0 +1,127 @@
+diff --git a/src/doc/common/conf.py b/src/doc/common/conf.py
+index 25f94f7b7d..9f6139ea4a 100644
+--- a/src/doc/common/conf.py
++++ b/src/doc/common/conf.py
+@@ -622,9 +622,9 @@ def call_intersphinx(app, env, node, contnode):
+     Check that the link from the thematic tutorials to the reference
+     manual is relative, see :trac:`20118`::
+ 
+-        sage: from sage.env import SAGE_DOC
+-        sage: thematic_index = os.path.join(SAGE_DOC, "html", "en", "thematic_tutorials", "index.html")
+-        sage: for line in open(thematic_index).readlines():
++        sage: from sage.env import SAGE_DOC  # optional - dochtml
++        sage: thematic_index = os.path.join(SAGE_DOC, "html", "en", "thematic_tutorials", "index.html")  # optional - dochtml
++        sage: for line in open(thematic_index).readlines():  # optional - dochtml
+         ....:     if "padics" in line:
+         ....:         sys.stdout.write(line)
+         <li><a class="reference external" href="../reference/padics/sage/rings/padics/tutorial.html#sage-rings-padics-tutorial" title="(in Sage Reference Manual: p-Adics ...)"><span>Introduction to the -adics</span></a></li>
+diff --git a/src/sage/doctest/control.py b/src/sage/doctest/control.py
+index 4236fd05e0..8e499cbaf7 100644
+--- a/src/sage/doctest/control.py
++++ b/src/sage/doctest/control.py
+@@ -87,7 +87,7 @@ class DocTestDefaults(SageObject):
+         self.sagenb = False
+         self.long = False
+         self.warn_long = None
+-        self.optional = set(['sage']) | auto_optional_tags
++        self.optional = set(['sage', 'dochtml']) | auto_optional_tags
+         self.randorder = None
+         self.global_iterations = 1  # sage-runtests default is 0
+         self.file_iterations = 1    # sage-runtests default is 0
+@@ -343,7 +343,8 @@ class DocTestController(SageObject):
+                     if not optionaltag_regex.search(o):
+                         raise ValueError('invalid optional tag {!r}'.format(o))
+ 
+-                options.optional |= auto_optional_tags
++                if "sage" in options.optional:
++                    options.optional |= auto_optional_tags
+ 
+         self.options = options
+         self.files = args
+@@ -741,7 +742,7 @@ class DocTestController(SageObject):
+             sage: DC = DocTestController(DD, [dirname])
+             sage: DC.expand_files_into_sources()
+             sage: sorted(DC.sources[0].options.optional)  # abs tol 1
+-            ['guava', 'magma', 'py3']
++            ['guava', 'magma']
+ 
+         We check that files are skipped appropriately::
+ 
+@@ -968,7 +969,7 @@ class DocTestController(SageObject):
+             sage: from sage.doctest.control import DocTestDefaults, DocTestController
+             sage: DC = DocTestController(DocTestDefaults(), [])
+             sage: DC._optional_tags_string()
+-            'sage'
++            'dochtml,sage'
+             sage: DC = DocTestController(DocTestDefaults(optional="all,and,some,more"), [])
+             sage: DC._optional_tags_string()
+             'all'
+diff --git a/src/sage/misc/sagedoc.py b/src/sage/misc/sagedoc.py
+index 9255aa848f..cc4712d3ec 100644
+--- a/src/sage/misc/sagedoc.py
++++ b/src/sage/misc/sagedoc.py
+@@ -18,9 +18,9 @@ TESTS:
+ Check that argspecs of extension function/methods appear correctly,
+ see :trac:`12849`::
+ 
+-    sage: from sage.env import SAGE_DOC
+-    sage: docfilename = os.path.join(SAGE_DOC, 'html', 'en', 'reference', 'calculus', 'sage', 'symbolic', 'expression.html')
+-    sage: with open(docfilename) as fobj:
++    sage: from sage.env import SAGE_DOC  # optional - dochtml
++    sage: docfilename = os.path.join(SAGE_DOC, 'html', 'en', 'reference', 'calculus', 'sage', 'symbolic', 'expression.html')  # optional - dochtml
++    sage: with open(docfilename) as fobj:  # optional - dochtml
+     ....:     for line in fobj:
+     ....:         if "#sage.symbolic.expression.Expression.numerical_approx" in line:
+     ....:             print(line)
+@@ -790,11 +790,12 @@ def _search_src_or_doc(what, string, extra1='', extra2='', extra3='',
+ 
+     ::
+ 
+-        sage: len(_search_src_or_doc('src', r'matrix\(', 'incidence_structures', 'self', 'combinat', interact=False).splitlines()) > 1
++        sage: from sage.misc.sagedoc import _search_src_or_doc  # optional - dochtml
++        sage: len(_search_src_or_doc('src', r'matrix\(', 'incidence_structures', 'self', 'combinat', interact=False).splitlines()) > 1  # optional - dochtml
+         True
+-        sage: 'abvar/homology' in _search_src_or_doc('doc', 'homology', 'variety', interact=False)  # long time (4s on sage.math, 2012)
++        sage: 'abvar/homology' in _search_src_or_doc('doc', 'homology', 'variety', interact=False)  # optional - dochtml, long time (4s on sage.math, 2012)
+         True
+-        sage: 'divisors' in _search_src_or_doc('src', '^ *def prime', interact=False)
++        sage: 'divisors' in _search_src_or_doc('src', '^ *def prime', interact=False)  # optional - dochtml
+         True
+     """
+     # process keywords
+@@ -1160,9 +1161,9 @@ def search_doc(string, extra1='', extra2='', extra3='', extra4='',
+     counting the length of ``search_doc('tree',
+     interact=False).splitlines()`` gives the number of matches. ::
+ 
+-        sage: len(search_doc('tree', interact=False).splitlines()) > 4000  # long time
++        sage: len(search_doc('tree', interact=False).splitlines()) > 4000  # optional - dochtml, long time
+         True
+-        sage: len(search_doc('tree', whole_word=True, interact=False).splitlines()) < 2000  # long time
++        sage: len(search_doc('tree', whole_word=True, interact=False).splitlines()) < 2000  # optional - dochtml, long time
+         True
+     """
+     return _search_src_or_doc('doc', string, extra1=extra1, extra2=extra2,
+@@ -1332,9 +1333,9 @@ class _sage_doc:
+ 
+     EXAMPLES::
+ 
+-        sage: browse_sage_doc._open("reference", testing=True)[0]  # indirect doctest
++        sage: browse_sage_doc._open("reference", testing=True)[0]  # optional - dochtml, indirect doctest
+         'http://localhost:8000/doc/live/reference/index.html'
+-        sage: browse_sage_doc(identity_matrix, 'rst')[-107:-47]
++        sage: browse_sage_doc(identity_matrix, 'rst')[-107:-47]  # optional - dochtml
+         'Full MatrixSpace of 3 by 3 sparse matrices over Integer Ring'
+     """
+     def __init__(self):
+@@ -1494,9 +1495,9 @@ class _sage_doc:
+ 
+         EXAMPLES::
+ 
+-            sage: browse_sage_doc._open("reference", testing=True)[0]
++            sage: browse_sage_doc._open("reference", testing=True)[0]  # optional - dochtml
+             'http://localhost:8000/doc/live/reference/index.html'
+-            sage: browse_sage_doc._open("tutorial", testing=True)[1]
++            sage: browse_sage_doc._open("tutorial", testing=True)[1]  # optional - dochtml
+             '.../html/en/tutorial/index.html'
+         """
+         url = self._base_url + os.path.join(name, "index.html")
diff --git a/pkgs/applications/science/math/sage/patches/eclib-20180710.patch b/pkgs/applications/science/math/sage/patches/eclib-20180710.patch
new file mode 100644
index 000000000000..d06e1e6cedf2
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/eclib-20180710.patch
@@ -0,0 +1,40 @@
+diff --git a/src/sage/interfaces/mwrank.py b/src/sage/interfaces/mwrank.py
+index 4417b59276..ae57ca2991 100644
+--- a/src/sage/interfaces/mwrank.py
++++ b/src/sage/interfaces/mwrank.py
+@@ -54,8 +54,9 @@ def Mwrank(options="", server=None, server_tmpdir=None):
+         sage: M = Mwrank('-v 0 -l')
+         sage: print(M('0 0 1 -1 0'))
+         Curve [0,0,1,-1,0] :    Rank = 1
+-        Generator 1 is [0:-1:1]; height 0.0511114082399688
+-        Regulator = 0.0511114082399688
++        Generator 1 is [0:-1:1]; height 0.051111408239969
++        Regulator = 0.051111408239969
++
+     """
+     global instances
+     try:
+diff --git a/src/sage/libs/eclib/wrap.cpp b/src/sage/libs/eclib/wrap.cpp
+index 5fd5693b53..d12468faa8 100644
+--- a/src/sage/libs/eclib/wrap.cpp
++++ b/src/sage/libs/eclib/wrap.cpp
+@@ -133,8 +133,8 @@ char* Curvedata_isogeny_class(struct Curvedata* E, int verbose)
+ 
+ 
+ int mw_process(struct Curvedata* curve, struct mw* m,
+-                      const struct bigint* x, const struct bigint* y,
+-                      const struct bigint* z, int sat)
++                      const bigint* x, const bigint* y,
++                      const bigint* z, int sat)
+ {
+   Point P(*curve, *x, *y, *z);
+   if (!P.isvalid())
+@@ -188,7 +188,7 @@ int mw_rank(struct mw* m)
+ }
+ 
+ /* Returns index and unsat long array, which user must deallocate */
+-int mw_saturate(struct mw* m, struct bigint* index, char** unsat,
++int mw_saturate(struct mw* m, bigint* index, char** unsat,
+                        long sat_bd, int odd_primes_only)
+ {
+   vector<long> v;
diff --git a/pkgs/applications/science/math/sage/patches/eclib-regulator-precision.patch b/pkgs/applications/science/math/sage/patches/eclib-regulator-precision.patch
new file mode 100644
index 000000000000..a1464b8fe310
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/eclib-regulator-precision.patch
@@ -0,0 +1,98 @@
+diff --git a/src/sage/libs/eclib/interface.py b/src/sage/libs/eclib/interface.py
+index f77000c478..9d17d412ae 100644
+--- a/src/sage/libs/eclib/interface.py
++++ b/src/sage/libs/eclib/interface.py
+@@ -1014,7 +1014,7 @@ class mwrank_MordellWeil(SageObject):
+             WARNING: saturation at primes p > 2 will not be done;
+             ...
+             Gained index 2
+-            New regulator =  93.857300720636393209
++            New regulator =  93.85730...
+             (False, 2, '[ ]')
+             sage: EQ.points()
+             [[-2, 3, 1], [2707496766203306, 864581029138191, 2969715140223272], [-13422227300, -49322830557, 12167000000]]
+@@ -1025,7 +1025,7 @@ class mwrank_MordellWeil(SageObject):
+             WARNING: saturation at primes p > 3 will not be done;
+             ...
+             Gained index 3
+-            New regulator =  10.4285889689595992455
++            New regulator =  10.42858...
+             (False, 3, '[ ]')
+             sage: EQ.points()
+             [[-2, 3, 1], [-14, 25, 8], [-13422227300, -49322830557, 12167000000]]
+@@ -1036,7 +1036,7 @@ class mwrank_MordellWeil(SageObject):
+             WARNING: saturation at primes p > 5 will not be done;
+             ...
+             Gained index 5
+-            New regulator =  0.417143558758383969818
++            New regulator =  0.41714...
+             (False, 5, '[ ]')
+             sage: EQ.points()
+             [[-2, 3, 1], [-14, 25, 8], [1, -1, 1]]
+@@ -1221,7 +1221,7 @@ class mwrank_MordellWeil(SageObject):
+             WARNING: saturation at primes p > 2 will not be done;
+             ...
+             Gained index 2
+-            New regulator =  93.857300720636393209
++            New regulator =  93.85730...
+             (False, 2, '[ ]')
+             sage: EQ
+             Subgroup of Mordell-Weil group: [[-2:3:1], [2707496766203306:864581029138191:2969715140223272], [-13422227300:-49322830557:12167000000]]
+@@ -1235,7 +1235,7 @@ class mwrank_MordellWeil(SageObject):
+             WARNING: saturation at primes p > 3 will not be done;
+             ...
+             Gained index 3
+-            New regulator =  10.4285889689595992455
++            New regulator =  10.42858...
+             (False, 3, '[ ]')
+             sage: EQ
+             Subgroup of Mordell-Weil group: [[-2:3:1], [-14:25:8], [-13422227300:-49322830557:12167000000]]
+@@ -1249,7 +1249,7 @@ class mwrank_MordellWeil(SageObject):
+             WARNING: saturation at primes p > 5 will not be done;
+             ...
+             Gained index 5
+-            New regulator =  0.417143558758383969818
++            New regulator =  0.41714...
+             (False, 5, '[ ]')
+             sage: EQ
+             Subgroup of Mordell-Weil group: [[-2:3:1], [-14:25:8], [1:-1:1]]
+diff --git a/src/sage/libs/eclib/mwrank.pyx b/src/sage/libs/eclib/mwrank.pyx
+index a4f89e1ca5..f8a22d2f55 100644
+--- a/src/sage/libs/eclib/mwrank.pyx
++++ b/src/sage/libs/eclib/mwrank.pyx
+@@ -1234,9 +1234,9 @@ cdef class _two_descent:
+             sage: D2.saturate()
+             Searching for points (bound = 8)...done:
+               found points which generate a subgroup of rank 3
+-              and regulator 0.417143558758383969817119544618093396749810106098479
++              and regulator 0.41714...
+             Processing points found during 2-descent...done:
+-              now regulator = 0.417143558758383969817119544618093396749810106098479
++              now regulator = 0.41714...
+             No saturation being done
+             sage: D2.getbasis()
+             '[[1:-1:1], [-2:3:1], [-14:25:8]]'
+@@ -1281,9 +1281,9 @@ cdef class _two_descent:
+             sage: D2.saturate()
+             Searching for points (bound = 8)...done:
+               found points which generate a subgroup of rank 3
+-              and regulator 0.417143558758383969817119544618093396749810106098479
++              and regulator 0.41714...
+             Processing points found during 2-descent...done:
+-              now regulator = 0.417143558758383969817119544618093396749810106098479
++              now regulator = 0.41714...
+             No saturation being done
+             sage: D2.getbasis()
+             '[[1:-1:1], [-2:3:1], [-14:25:8]]'
+@@ -1329,9 +1329,9 @@ cdef class _two_descent:
+             sage: D2.saturate()
+             Searching for points (bound = 8)...done:
+               found points which generate a subgroup of rank 3
+-              and regulator 0.417143558758383969817119544618093396749810106098479
++              and regulator 0.41714...
+             Processing points found during 2-descent...done:
+-              now regulator = 0.417143558758383969817119544618093396749810106098479
++              now regulator = 0.41714...
+             No saturation being done
+             sage: D2.getbasis()
+             '[[1:-1:1], [-2:3:1], [-14:25:8]]'
diff --git a/pkgs/applications/science/math/sage/patches/matplotlib-normed-deprecated.patch b/pkgs/applications/science/math/sage/patches/matplotlib-normed-deprecated.patch
new file mode 100644
index 000000000000..41747635cacd
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/matplotlib-normed-deprecated.patch
@@ -0,0 +1,12 @@
+diff --git a/src/sage/all.py b/src/sage/all.py
+index 14cec431f1..25a35a0522 100644
+--- a/src/sage/all.py
++++ b/src/sage/all.py
+@@ -310,6 +310,7 @@ warnings.filters.remove(('ignore', None, DeprecationWarning, None, 0))
+ # Ignore all deprecations from IPython etc.
+ warnings.filterwarnings('ignore',
+     module='.*(IPython|ipykernel|jupyter_client|jupyter_core|nbformat|notebook|ipywidgets|storemagic)')
++warnings.filterwarnings('ignore', "The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.") # matplotlib normed deprecation
+ # However, be sure to keep OUR deprecation warnings
+ warnings.filterwarnings('default',
+     '[\s\S]*See http://trac.sagemath.org/[0-9]* for details.')
diff --git a/pkgs/applications/science/math/sage/patches/maxima-5.41.0-doctests.patch b/pkgs/applications/science/math/sage/patches/maxima-5.41.0-doctests.patch
new file mode 100644
index 000000000000..fad434e52ada
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/maxima-5.41.0-doctests.patch
@@ -0,0 +1,48 @@
+diff --git a/src/sage/interfaces/maxima_abstract.py b/src/sage/interfaces/maxima_abstract.py
+index 961c20aaac..3d601d8939 100644
+--- a/src/sage/interfaces/maxima_abstract.py
++++ b/src/sage/interfaces/maxima_abstract.py
+@@ -1743,7 +1743,7 @@ class MaximaAbstractElement(ExtraTabCompletion, InterfaceElement):
+             sage: y,d = var('y,d')
+             sage: f = function('f')
+             sage: latex(maxima(derivative(f(x*y), x)))
+-            \left(\left.{{{\it \partial}}\over{{\it \partial}\,  {\it t_0}}}\,f\left({\it t_0}\right)  \right|_{{\it t_0}={\it x}\,  {\it y}}\right)\,{\it y}
++            \left(\left.{{{\it \partial}}\over{{\it \partial}\,  {\it t}_{0}}}\,f\left({\it t}_{0}\right)  \right|_{{\it t}_{0}={\it x}\,  {\it y}}\right)\,{\it y}
+             sage: latex(maxima(derivative(f(x,y,d), d,x,x,y)))
+             {{{\it \partial}^4}\over{{\it \partial}\,{\it d}\,  {\it \partial}\,{\it x}^2\,{\it \partial}\,  {\it y}}}\,f\left({\it x} ,  {\it y} , {\it d}\right)
+             sage: latex(maxima(d/(d-2)))
+diff --git a/src/sage/manifolds/differentiable/metric.py b/src/sage/manifolds/differentiable/metric.py
+index 3cd6ad3235..1e18af1a6b 100644
+--- a/src/sage/manifolds/differentiable/metric.py
++++ b/src/sage/manifolds/differentiable/metric.py
+@@ -993,7 +993,7 @@ class PseudoRiemannianMetric(TensorField):
+              2-dimensional differentiable manifold S^2
+             sage: g.riemann()[:]
+             [[[[0, 0], [0, 0]], [[0, sin(th)^2], [-sin(th)^2, 0]]],
+-             [[[0, (cos(th)^2 - 1)/sin(th)^2], [1, 0]], [[0, 0], [0, 0]]]]
++             [[[0, -1], [1, 0]], [[0, 0], [0, 0]]]]
+ 
+         In dimension 2, the Riemann tensor can be expressed entirely in terms of
+         the Ricci scalar `r`:
+diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx
+index dfb8751467..27402e54ab 100644
+--- a/src/sage/symbolic/expression.pyx
++++ b/src/sage/symbolic/expression.pyx
+@@ -7154,7 +7154,7 @@ cdef class Expression(CommutativeRingElement):
+             sage: ex = lcm(sin(x)^2 - 1, sin(x)^2 + sin(x)); ex
+             (sin(x)^2 + sin(x))*(sin(x)^2 - 1)/(sin(x) + 1)
+             sage: ex.simplify_full()
+-            -cos(x)^2*sin(x)
++            sin(x)^3 - sin(x)
+ 
+         TESTS:
+ 
+@@ -10004,7 +10004,7 @@ cdef class Expression(CommutativeRingElement):
+ 
+             sage: f=tan(3*x)
+             sage: f.simplify_trig()
+-            (4*cos(x)^2 - 1)*sin(x)/(4*cos(x)^3 - 3*cos(x))
++            -(4*cos(x)^2 - 1)*sin(x)/(4*cos(x)*sin(x)^2 - cos(x))
+             sage: f.simplify_trig(False)
+             sin(3*x)/cos(3*x)
+ 
diff --git a/pkgs/applications/science/math/sage/patches/numpy-1.14.3.patch b/pkgs/applications/science/math/sage/patches/numpy-1.14.3.patch
new file mode 100644
index 000000000000..5927bc116096
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/numpy-1.14.3.patch
@@ -0,0 +1,812 @@
+diff --git a/src/doc/en/faq/faq-usage.rst b/src/doc/en/faq/faq-usage.rst
+index 79b4205fd3..9a89bd2136 100644
+--- a/src/doc/en/faq/faq-usage.rst
++++ b/src/doc/en/faq/faq-usage.rst
+@@ -338,7 +338,7 @@ ints. For example::
+     sage: RealNumber = float; Integer = int
+     sage: from scipy import stats
+     sage: stats.ttest_ind(list([1,2,3,4,5]),list([2,3,4,5,.6]))
+-    Ttest_indResult(statistic=0.076752955645333687, pvalue=0.94070490247380478)
++    Ttest_indResult(statistic=0.0767529..., pvalue=0.940704...)
+     sage: stats.uniform(0,15).ppf([0.5,0.7])
+     array([  7.5,  10.5])
+ 
+diff --git a/src/doc/en/thematic_tutorials/numerical_sage/cvxopt.rst b/src/doc/en/thematic_tutorials/numerical_sage/cvxopt.rst
+index 314811c42b..e5f54ec4c2 100644
+--- a/src/doc/en/thematic_tutorials/numerical_sage/cvxopt.rst
++++ b/src/doc/en/thematic_tutorials/numerical_sage/cvxopt.rst
+@@ -48,11 +48,13 @@ we could do the following.
+     sage: B = numpy.array([1.0]*5)
+     sage: B.shape=(5,1)
+     sage: print(B)
+-    [[ 1.]
+-     [ 1.]
+-     [ 1.]
+-     [ 1.]
+-     [ 1.]]
++    [[1.]
++     [1.]
++     [1.]
++     [1.]
++     [1.]]
++
++
+     sage: print(A)
+     [ 2.00e+00  3.00e+00     0         0         0    ]
+     [ 3.00e+00     0      4.00e+00     0      6.00e+00]
+diff --git a/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst b/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst
+index 5b89cd75ee..e50b2ea5d4 100644
+--- a/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst
++++ b/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst
+@@ -84,7 +84,7 @@ well as take slices
+     sage: l[3]
+     3.0
+     sage: l[3:6]
+-    array([ 3.,  4.,  5.])
++    array([3., 4., 5.])
+ 
+ You can do basic arithmetic operations
+ 
+@@ -147,11 +147,11 @@ also do matrix vector multiplication, and matrix addition
+     sage: n = numpy.matrix([[1,2],[3,4]],dtype=float)
+     sage: v = numpy.array([[1],[2]],dtype=float)
+     sage: n*v
+-    matrix([[  5.],
+-            [ 11.]])
++    matrix([[ 5.],
++            [11.]])
+     sage: n+n
+-    matrix([[ 2.,  4.],
+-            [ 6.,  8.]])
++    matrix([[2., 4.],
++            [6., 8.]])
+ 
+ If ``n`` was created with :meth:`numpy.array`, then to do matrix vector
+ multiplication, you would use ``numpy.dot(n,v)``.
+@@ -170,11 +170,11 @@ to manipulate
+             22.,  23.,  24.])
+     sage: n.shape=(5,5)
+     sage: n
+-    array([[  0.,   1.,   2.,   3.,   4.],
+-           [  5.,   6.,   7.,   8.,   9.],
+-           [ 10.,  11.,  12.,  13.,  14.],
+-           [ 15.,  16.,  17.,  18.,  19.],
+-           [ 20.,  21.,  22.,  23.,  24.]])
++    array([[ 0.,  1.,  2.,  3.,  4.],
++           [ 5.,  6.,  7.,  8.,  9.],
++           [10., 11., 12., 13., 14.],
++           [15., 16., 17., 18., 19.],
++           [20., 21., 22., 23., 24.]])
+ 
+ This changes the one-dimensional array into a `5\times 5` array.
+ 
+@@ -187,8 +187,8 @@ NumPy arrays can be sliced as well
+     sage: n=numpy.array(range(25),dtype=float)
+     sage: n.shape=(5,5)
+     sage: n[2:4,1:3]
+-    array([[ 11.,  12.],
+-           [ 16.,  17.]])
++    array([[11., 12.],
++           [16., 17.]])
+ 
+ It is important to note that the sliced matrices are references to
+ the original
+@@ -224,8 +224,8 @@ Some particularly useful commands are
+ 
+     sage: x=numpy.arange(0,2,.1,dtype=float)
+     sage: x
+-    array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
+-            1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9])
++    array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
++           1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
+ 
+ You can see that :meth:`numpy.arange` creates an array of floats increasing by 0.1
+ from 0 to 2. There is a useful command :meth:`numpy.r_` that is best explained by example
+@@ -240,10 +240,11 @@ from 0 to 2. There is a useful command :meth:`numpy.r_` that is best explained b
+     sage: Integer=int
+     sage: n=r_[0.0:5.0]
+     sage: n
+-    array([ 0.,  1.,  2.,  3.,  4.])
++    array([0., 1., 2., 3., 4.])
+     sage: n=r_[0.0:5.0, [0.0]*5]
+     sage: n
+-    array([ 0.,  1.,  2.,  3.,  4.,  0.,  0.,  0.,  0.,  0.])
++    array([0., 1., 2., 3., 4., 0., 0., 0., 0., 0.])
++
+ 
+ :meth:`numpy.r_` provides a shorthand for constructing NumPy arrays efficiently.
+ Note in the above ``0.0:5.0`` was shorthand for ``0.0, 1.0, 2.0, 3.0, 4.0``.
+@@ -255,7 +256,7 @@ intervals. We can do this as follows
+ ::
+ 
+     sage: r_[0.0:5.0:11*j]
+-    array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ])
++    array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
+ 
+ The notation ``0.0:5.0:11*j`` expands to a list of 11 equally space
+ points between 0 and 5 including both endpoints. Note that ``j`` is the
+@@ -287,23 +288,23 @@ an equally spaced grid with `\Delta x = \Delta y = .25` for
+     sage: y=numpy.r_[0.0:1.0:5*j]
+     sage: xx,yy= meshgrid(x,y)
+     sage: xx
+-    array([[ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
+-           [ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
+-           [ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
+-           [ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
+-           [ 0.  ,  0.25,  0.5 ,  0.75,  1.  ]])
++    array([[0.  , 0.25, 0.5 , 0.75, 1.  ],
++           [0.  , 0.25, 0.5 , 0.75, 1.  ],
++           [0.  , 0.25, 0.5 , 0.75, 1.  ],
++           [0.  , 0.25, 0.5 , 0.75, 1.  ],
++           [0.  , 0.25, 0.5 , 0.75, 1.  ]])
+     sage: yy
+-    array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
+-           [ 0.25,  0.25,  0.25,  0.25,  0.25],
+-           [ 0.5 ,  0.5 ,  0.5 ,  0.5 ,  0.5 ],
+-           [ 0.75,  0.75,  0.75,  0.75,  0.75],
+-           [ 1.  ,  1.  ,  1.  ,  1.  ,  1.  ]])
++    array([[0.  , 0.  , 0.  , 0.  , 0.  ],
++           [0.25, 0.25, 0.25, 0.25, 0.25],
++           [0.5 , 0.5 , 0.5 , 0.5 , 0.5 ],
++           [0.75, 0.75, 0.75, 0.75, 0.75],
++           [1.  , 1.  , 1.  , 1.  , 1.  ]])
+     sage: f(xx,yy)
+-    array([[ 0.    ,  0.0625,  0.25  ,  0.5625,  1.    ],
+-           [ 0.0625,  0.125 ,  0.3125,  0.625 ,  1.0625],
+-           [ 0.25  ,  0.3125,  0.5   ,  0.8125,  1.25  ],
+-           [ 0.5625,  0.625 ,  0.8125,  1.125 ,  1.5625],
+-           [ 1.    ,  1.0625,  1.25  ,  1.5625,  2.    ]])
++    array([[0.    , 0.0625, 0.25  , 0.5625, 1.    ],
++           [0.0625, 0.125 , 0.3125, 0.625 , 1.0625],
++           [0.25  , 0.3125, 0.5   , 0.8125, 1.25  ],
++           [0.5625, 0.625 , 0.8125, 1.125 , 1.5625],
++           [1.    , 1.0625, 1.25  , 1.5625, 2.    ]])
+ 
+ You can see that :meth:`numpy.meshgrid` produces a pair of matrices, here denoted
+ `xx` and `yy`, such that `(xx[i,j],yy[i,j])` has coordinates
+@@ -324,7 +325,7 @@ equation `Ax=b` do
+     sage: b=numpy.array(range(1,6))
+     sage: x=linalg.solve(A,b)
+     sage: numpy.dot(A,x)
+-    array([ 1.,  2.,  3.,  4., 5.])
++    array([1., 2., 3., 4., 5.])
+ 
+ This creates a random 5x5 matrix ``A``, and solves `Ax=b` where
+ ``b=[0.0,1.0,2.0,3.0,4.0]``. There are many other routines in the :mod:`numpy.linalg`
+diff --git a/src/sage/calculus/riemann.pyx b/src/sage/calculus/riemann.pyx
+index df85cce43d..34ea164be0 100644
+--- a/src/sage/calculus/riemann.pyx
++++ b/src/sage/calculus/riemann.pyx
+@@ -1191,30 +1191,30 @@ cpdef complex_to_spiderweb(np.ndarray[COMPLEX_T, ndim = 2] z_values,
+         sage: zval = numpy.array([[0, 1, 1000],[.2+.3j,1,-.3j],[0,0,0]],dtype = numpy.complex128)
+         sage: deriv = numpy.array([[.1]],dtype = numpy.float64)
+         sage: complex_to_spiderweb(zval, deriv,deriv, 4,4,[0,0,0],1,False,0.001)
+-        array([[[ 1.,  1.,  1.],
+-                [ 1.,  1.,  1.],
+-                [ 1.,  1.,  1.]],
++        array([[[1., 1., 1.],
++                [1., 1., 1.],
++                [1., 1., 1.]],
+         <BLANKLINE>
+-               [[ 1.,  1.,  1.],
+-                [ 0.,  0.,  0.],
+-                [ 1.,  1.,  1.]],
++               [[1., 1., 1.],
++                [0., 0., 0.],
++                [1., 1., 1.]],
+         <BLANKLINE>
+-               [[ 1.,  1.,  1.],
+-                [ 1.,  1.,  1.],
+-                [ 1.,  1.,  1.]]])
++               [[1., 1., 1.],
++                [1., 1., 1.],
++                [1., 1., 1.]]])
+ 
+         sage: complex_to_spiderweb(zval, deriv,deriv, 4,4,[0,0,0],1,True,0.001)
+-        array([[[ 1.        ,  1.        ,  1.        ],
+-                [ 1.        ,  0.05558355,  0.05558355],
+-                [ 0.17301243,  0.        ,  0.        ]],
++        array([[[1.        , 1.        , 1.        ],
++                [1.        , 0.05558355, 0.05558355],
++                [0.17301243, 0.        , 0.        ]],
+         <BLANKLINE>
+-               [[ 1.        ,  0.96804683,  0.48044583],
+-                [ 0.        ,  0.        ,  0.        ],
+-                [ 0.77351965,  0.5470393 ,  1.        ]],
++               [[1.        , 0.96804683, 0.48044583],
++                [0.        , 0.        , 0.        ],
++                [0.77351965, 0.5470393 , 1.        ]],
+         <BLANKLINE>
+-               [[ 1.        ,  1.        ,  1.        ],
+-                [ 1.        ,  1.        ,  1.        ],
+-                [ 1.        ,  1.        ,  1.        ]]])
++               [[1.        , 1.        , 1.        ],
++                [1.        , 1.        , 1.        ],
++                [1.        , 1.        , 1.        ]]])
+      """
+     cdef Py_ssize_t i, j, imax, jmax
+     cdef FLOAT_T x, y, mag, arg, width, target, precision, dmag, darg
+@@ -1279,14 +1279,14 @@ cpdef complex_to_rgb(np.ndarray[COMPLEX_T, ndim = 2] z_values):
+         sage: from sage.calculus.riemann import complex_to_rgb
+         sage: import numpy
+         sage: complex_to_rgb(numpy.array([[0, 1, 1000]], dtype = numpy.complex128))
+-        array([[[ 1.        ,  1.        ,  1.        ],
+-                [ 1.        ,  0.05558355,  0.05558355],
+-                [ 0.17301243,  0.        ,  0.        ]]])
++        array([[[1.        , 1.        , 1.        ],
++                [1.        , 0.05558355, 0.05558355],
++                [0.17301243, 0.        , 0.        ]]])
+ 
+         sage: complex_to_rgb(numpy.array([[0, 1j, 1000j]], dtype = numpy.complex128))
+-        array([[[ 1.        ,  1.        ,  1.        ],
+-                [ 0.52779177,  1.        ,  0.05558355],
+-                [ 0.08650622,  0.17301243,  0.        ]]])
++        array([[[1.        , 1.        , 1.        ],
++                [0.52779177, 1.        , 0.05558355],
++                [0.08650622, 0.17301243, 0.        ]]])
+ 
+ 
+     TESTS::
+diff --git a/src/sage/combinat/fully_packed_loop.py b/src/sage/combinat/fully_packed_loop.py
+index 61b1003002..4baee9cbbd 100644
+--- a/src/sage/combinat/fully_packed_loop.py
++++ b/src/sage/combinat/fully_packed_loop.py
+@@ -72,11 +72,11 @@ def _make_color_list(n, colors=None,  color_map=None, randomize=False):
+         sage: _make_color_list(5, ['blue', 'red'])
+         ['blue', 'red', 'blue', 'red', 'blue']
+         sage: _make_color_list(5, color_map='summer')
+-        [(0.0, 0.5, 0.40000000000000002),
+-         (0.25098039215686274, 0.62549019607843137, 0.40000000000000002),
+-         (0.50196078431372548, 0.75098039215686274, 0.40000000000000002),
+-         (0.75294117647058822, 0.87647058823529411, 0.40000000000000002),
+-         (1.0, 1.0, 0.40000000000000002)]
++        [(0.0, 0.5, 0.4),
++         (0.25098039215686274, 0.6254901960784314, 0.4),
++         (0.5019607843137255, 0.7509803921568627, 0.4),
++         (0.7529411764705882, 0.8764705882352941, 0.4),
++         (1.0, 1.0, 0.4)]
+         sage: _make_color_list(8, ['blue', 'red'], randomize=True)
+         ['blue', 'blue', 'red', 'blue', 'red', 'red', 'red', 'blue']
+     """
+diff --git a/src/sage/finance/time_series.pyx b/src/sage/finance/time_series.pyx
+index c37700d14e..49b7298d0b 100644
+--- a/src/sage/finance/time_series.pyx
++++ b/src/sage/finance/time_series.pyx
+@@ -109,8 +109,8 @@ cdef class TimeSeries:
+ 
+             sage: import numpy
+             sage: v = numpy.array([[1,2], [3,4]], dtype=float); v
+-            array([[ 1.,  2.],
+-                   [ 3.,  4.]])
++            array([[1., 2.],
++                   [3., 4.]])
+             sage: finance.TimeSeries(v)
+             [1.0000, 2.0000, 3.0000, 4.0000]
+             sage: finance.TimeSeries(v[:,0])
+@@ -2098,14 +2098,14 @@ cdef class TimeSeries:
+ 
+             sage: w[0] = 20
+             sage: w
+-            array([ 20. ,  -3. ,   4.5,  -2. ])
++            array([20. , -3. ,  4.5, -2. ])
+             sage: v
+             [20.0000, -3.0000, 4.5000, -2.0000]
+ 
+         If you want a separate copy do not give the ``copy=False`` option. ::
+ 
+             sage: z = v.numpy(); z
+-            array([ 20. ,  -3. ,   4.5,  -2. ])
++            array([20. , -3. ,  4.5, -2. ])
+             sage: z[0] = -10
+             sage: v
+             [20.0000, -3.0000, 4.5000, -2.0000]
+diff --git a/src/sage/functions/hyperbolic.py b/src/sage/functions/hyperbolic.py
+index 931a4b41e4..bf33fc483d 100644
+--- a/src/sage/functions/hyperbolic.py
++++ b/src/sage/functions/hyperbolic.py
+@@ -214,7 +214,7 @@ class Function_coth(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2, 5)
+             sage: coth(a)
+-            array([ 1.03731472,  1.00496982,  1.00067115])
++            array([1.03731472, 1.00496982, 1.00067115])
+         """
+         return 1.0 / tanh(x)
+ 
+@@ -267,7 +267,7 @@ class Function_sech(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2, 5)
+             sage: sech(a)
+-            array([ 0.26580223,  0.09932793,  0.03661899])
++            array([0.26580223, 0.09932793, 0.03661899])
+         """
+         return 1.0 / cosh(x)
+ 
+@@ -318,7 +318,7 @@ class Function_csch(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2, 5)
+             sage: csch(a)
+-            array([ 0.27572056,  0.09982157,  0.03664357])
++            array([0.27572056, 0.09982157, 0.03664357])
+         """
+         return 1.0 / sinh(x)
+ 
+@@ -586,7 +586,7 @@ class Function_arccoth(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2,5)
+             sage: acoth(a)
+-            array([ 0.54930614,  0.34657359,  0.25541281])
++            array([0.54930614, 0.34657359, 0.25541281])
+         """
+         return arctanh(1.0 / x)
+ 
+diff --git a/src/sage/functions/orthogonal_polys.py b/src/sage/functions/orthogonal_polys.py
+index 017c85a96f..33fbb499c5 100644
+--- a/src/sage/functions/orthogonal_polys.py
++++ b/src/sage/functions/orthogonal_polys.py
+@@ -810,12 +810,12 @@ class Func_chebyshev_T(ChebyshevFunction):
+             sage: z2 = numpy.array([[1,2],[1,2]])
+             sage: z3 = numpy.array([1,2,3.])
+             sage: chebyshev_T(1,z)
+-            array([ 1.,  2.])
++            array([1., 2.])
+             sage: chebyshev_T(1,z2)
+-            array([[ 1.,  2.],
+-                   [ 1.,  2.]])
++            array([[1., 2.],
++                   [1., 2.]])
+             sage: chebyshev_T(1,z3)
+-            array([ 1.,  2.,  3.])
++            array([1., 2., 3.])
+             sage: chebyshev_T(z,0.1)
+             array([ 0.1 , -0.98])
+         """
+@@ -1095,12 +1095,12 @@ class Func_chebyshev_U(ChebyshevFunction):
+             sage: z2 = numpy.array([[1,2],[1,2]])
+             sage: z3 = numpy.array([1,2,3.])
+             sage: chebyshev_U(1,z)
+-            array([ 2.,  4.])
++            array([2., 4.])
+             sage: chebyshev_U(1,z2)
+-            array([[ 2.,  4.],
+-                   [ 2.,  4.]])
++            array([[2., 4.],
++                   [2., 4.]])
+             sage: chebyshev_U(1,z3)
+-            array([ 2.,  4.,  6.])
++            array([2., 4., 6.])
+             sage: chebyshev_U(z,0.1)
+             array([ 0.2 , -0.96])
+         """
+diff --git a/src/sage/functions/other.py b/src/sage/functions/other.py
+index 679384c907..d63b295a4c 100644
+--- a/src/sage/functions/other.py
++++ b/src/sage/functions/other.py
+@@ -390,7 +390,7 @@ class Function_ceil(BuiltinFunction):
+             sage: import numpy
+             sage: a = numpy.linspace(0,2,6)
+             sage: ceil(a)
+-            array([ 0.,  1.,  1.,  2.,  2.,  2.])
++            array([0., 1., 1., 2., 2., 2.])
+ 
+         Test pickling::
+ 
+@@ -539,7 +539,7 @@ class Function_floor(BuiltinFunction):
+             sage: import numpy
+             sage: a = numpy.linspace(0,2,6)
+             sage: floor(a)
+-            array([ 0.,  0.,  0.,  1.,  1.,  2.])
++            array([0., 0., 0., 1., 1., 2.])
+             sage: floor(x)._sympy_()
+             floor(x)
+ 
+@@ -840,7 +840,7 @@ def sqrt(x, *args, **kwds):
+             sage: import numpy
+             sage: a = numpy.arange(2,5)
+             sage: sqrt(a)
+-            array([ 1.41421356,  1.73205081,  2.        ])
++            array([1.41421356, 1.73205081, 2.        ])
+         """
+         if isinstance(x, float):
+             return math.sqrt(x)
+diff --git a/src/sage/functions/trig.py b/src/sage/functions/trig.py
+index e7e7a311cd..e7ff78a9de 100644
+--- a/src/sage/functions/trig.py
++++ b/src/sage/functions/trig.py
+@@ -731,7 +731,7 @@ class Function_arccot(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2, 5)
+             sage: arccot(a)
+-            array([ 0.46364761,  0.32175055,  0.24497866])
++            array([0.46364761, 0.32175055, 0.24497866])
+         """
+         return math.pi/2 - arctan(x)
+ 
+@@ -787,7 +787,7 @@ class Function_arccsc(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2, 5)
+             sage: arccsc(a)
+-            array([ 0.52359878,  0.33983691,  0.25268026])
++            array([0.52359878, 0.33983691, 0.25268026])
+         """
+         return arcsin(1.0/x)
+ 
+@@ -845,7 +845,7 @@ class Function_arcsec(GinacFunction):
+             sage: import numpy
+             sage: a = numpy.arange(2, 5)
+             sage: arcsec(a)
+-            array([ 1.04719755,  1.23095942,  1.31811607])
++            array([1.04719755, 1.23095942, 1.31811607])
+         """
+         return arccos(1.0/x)
+ 
+@@ -920,13 +920,13 @@ class Function_arctan2(GinacFunction):
+             sage: a = numpy.linspace(1, 3, 3)
+             sage: b = numpy.linspace(3, 6, 3)
+             sage: atan2(a, b)
+-            array([ 0.32175055,  0.41822433,  0.46364761])
++            array([0.32175055, 0.41822433, 0.46364761])
+ 
+             sage: atan2(1,a)
+-            array([ 0.78539816,  0.46364761,  0.32175055])
++            array([0.78539816, 0.46364761, 0.32175055])
+ 
+             sage: atan2(a, 1)
+-            array([ 0.78539816,  1.10714872,  1.24904577])
++            array([0.78539816, 1.10714872, 1.24904577])
+ 
+         TESTS::
+ 
+diff --git a/src/sage/matrix/constructor.pyx b/src/sage/matrix/constructor.pyx
+index 19a1d37df0..5780dfae1c 100644
+--- a/src/sage/matrix/constructor.pyx
++++ b/src/sage/matrix/constructor.pyx
+@@ -494,8 +494,8 @@ class MatrixFactory(object):
+         [7 8 9]
+         Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
+         sage: n = matrix(QQ, 2, 2, [1, 1/2, 1/3, 1/4]).numpy(); n
+-        array([[ 1.        ,  0.5       ],
+-               [ 0.33333333,  0.25      ]])
++        array([[1.        , 0.5       ],
++               [0.33333333, 0.25      ]])
+         sage: matrix(QQ, n)
+         [  1 1/2]
+         [1/3 1/4]
+diff --git a/src/sage/matrix/matrix_double_dense.pyx b/src/sage/matrix/matrix_double_dense.pyx
+index 48e0a8a97f..1be5d35b19 100644
+--- a/src/sage/matrix/matrix_double_dense.pyx
++++ b/src/sage/matrix/matrix_double_dense.pyx
+@@ -2546,7 +2546,7 @@ cdef class Matrix_double_dense(Matrix_dense):
+             sage: P.is_unitary(algorithm='orthonormal')
+             Traceback (most recent call last):
+             ...
+-            ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)
++            error: ((lwork==-1)||(lwork >= MAX(1,2*n))) failed for 3rd keyword lwork: zgees:lwork=0
+ 
+         TESTS::
+ 
+@@ -3662,8 +3662,8 @@ cdef class Matrix_double_dense(Matrix_dense):
+             [0.0 1.0 2.0]
+             [3.0 4.0 5.0]
+             sage: m.numpy()
+-            array([[ 0.,  1.,  2.],
+-                   [ 3.,  4.,  5.]])
++            array([[0., 1., 2.],
++                   [3., 4., 5.]])
+ 
+         Alternatively, numpy automatically calls this function (via
+         the magic :meth:`__array__` method) to convert Sage matrices
+@@ -3674,16 +3674,16 @@ cdef class Matrix_double_dense(Matrix_dense):
+             [0.0 1.0 2.0]
+             [3.0 4.0 5.0]
+             sage: numpy.array(m)
+-            array([[ 0.,  1.,  2.],
+-                   [ 3.,  4.,  5.]])
++            array([[0., 1., 2.],
++                   [3., 4., 5.]])
+             sage: numpy.array(m).dtype
+             dtype('float64')
+             sage: m = matrix(CDF, 2, range(6)); m
+             [0.0 1.0 2.0]
+             [3.0 4.0 5.0]
+             sage: numpy.array(m)
+-            array([[ 0.+0.j,  1.+0.j,  2.+0.j],
+-                   [ 3.+0.j,  4.+0.j,  5.+0.j]])
++            array([[0.+0.j, 1.+0.j, 2.+0.j],
++                   [3.+0.j, 4.+0.j, 5.+0.j]])
+             sage: numpy.array(m).dtype
+             dtype('complex128')
+ 
+diff --git a/src/sage/matrix/special.py b/src/sage/matrix/special.py
+index c698ba5e97..b743bab354 100644
+--- a/src/sage/matrix/special.py
++++ b/src/sage/matrix/special.py
+@@ -705,7 +705,7 @@ def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=True):
+ 
+         sage: import numpy
+         sage: entries = numpy.array([1.2, 5.6]); entries
+-        array([ 1.2,  5.6])
++        array([1.2, 5.6])
+         sage: A = diagonal_matrix(3, entries); A
+         [1.2 0.0 0.0]
+         [0.0 5.6 0.0]
+@@ -715,7 +715,7 @@ def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=True):
+ 
+         sage: j = numpy.complex(0,1)
+         sage: entries = numpy.array([2.0+j, 8.1, 3.4+2.6*j]); entries
+-        array([ 2.0+1.j ,  8.1+0.j ,  3.4+2.6j])
++        array([2. +1.j , 8.1+0.j , 3.4+2.6j])
+         sage: A = diagonal_matrix(entries); A
+         [2.0 + 1.0*I         0.0         0.0]
+         [        0.0         8.1         0.0]
+diff --git a/src/sage/modules/free_module_element.pyx b/src/sage/modules/free_module_element.pyx
+index 230f142117..2ab1c0ae68 100644
+--- a/src/sage/modules/free_module_element.pyx
++++ b/src/sage/modules/free_module_element.pyx
+@@ -982,7 +982,7 @@ cdef class FreeModuleElement(Vector):   # abstract base class
+             sage: v.numpy()
+             array([1, 2, 5/6], dtype=object)
+             sage: v.numpy(dtype=float)
+-            array([ 1.        ,  2.        ,  0.83333333])
++            array([1.        , 2.        , 0.83333333])
+             sage: v.numpy(dtype=int)
+             array([1, 2, 0])
+             sage: import numpy
+@@ -993,7 +993,7 @@ cdef class FreeModuleElement(Vector):   # abstract base class
+         be more efficient but may have unintended consequences::
+ 
+             sage: v.numpy(dtype=None)
+-            array([ 1.        ,  2.        ,  0.83333333])
++            array([1.        , 2.        , 0.83333333])
+ 
+             sage: w = vector(ZZ, [0, 1, 2^63 -1]); w
+             (0, 1, 9223372036854775807)
+diff --git a/src/sage/modules/vector_double_dense.pyx b/src/sage/modules/vector_double_dense.pyx
+index 39fc2970de..2badf98284 100644
+--- a/src/sage/modules/vector_double_dense.pyx
++++ b/src/sage/modules/vector_double_dense.pyx
+@@ -807,13 +807,13 @@ cdef class Vector_double_dense(FreeModuleElement):
+ 
+             sage: v = vector(CDF,4,range(4))
+             sage: v.numpy()
+-            array([ 0.+0.j,  1.+0.j,  2.+0.j,  3.+0.j])
++            array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])
+             sage: v = vector(CDF,0)
+             sage: v.numpy()
+             array([], dtype=complex128)
+             sage: v = vector(RDF,4,range(4))
+             sage: v.numpy()
+-            array([ 0.,  1.,  2.,  3.])
++            array([0., 1., 2., 3.])
+             sage: v = vector(RDF,0)
+             sage: v.numpy()
+             array([], dtype=float64)
+@@ -823,11 +823,11 @@ cdef class Vector_double_dense(FreeModuleElement):
+             sage: import numpy
+             sage: v = vector(CDF, 3, range(3))
+             sage: v.numpy()
+-            array([ 0.+0.j,  1.+0.j,  2.+0.j])
++            array([0.+0.j, 1.+0.j, 2.+0.j])
+             sage: v.numpy(dtype=numpy.float64)
+-            array([ 0.,  1.,  2.])
++            array([0., 1., 2.])
+             sage: v.numpy(dtype=numpy.float32)
+-            array([ 0.,  1.,  2.], dtype=float32)
++            array([0., 1., 2.], dtype=float32)
+         """
+         if dtype is None or dtype is self._vector_numpy.dtype:
+             from copy import copy
+diff --git a/src/sage/numerical/optimize.py b/src/sage/numerical/optimize.py
+index 17b5ebb84b..92ce35c502 100644
+--- a/src/sage/numerical/optimize.py
++++ b/src/sage/numerical/optimize.py
+@@ -486,9 +486,9 @@ def minimize_constrained(func,cons,x0,gradient=None,algorithm='default', **args)
+                 else:
+                     min = optimize.fmin_tnc(f, x0, approx_grad=True, bounds=cons, messages=0, **args)[0]
+         elif isinstance(cons[0], function_type) or isinstance(cons[0], Expression):
+-            min = optimize.fmin_cobyla(f, x0, cons, iprint=0, **args)
++            min = optimize.fmin_cobyla(f, x0, cons, disp=0, **args)
+     elif isinstance(cons, function_type) or isinstance(cons, Expression):
+-        min = optimize.fmin_cobyla(f, x0, cons, iprint=0, **args)
++        min = optimize.fmin_cobyla(f, x0, cons, disp=0, **args)
+     return vector(RDF, min)
+ 
+ 
+diff --git a/src/sage/plot/complex_plot.pyx b/src/sage/plot/complex_plot.pyx
+index ad9693da62..758fb709b7 100644
+--- a/src/sage/plot/complex_plot.pyx
++++ b/src/sage/plot/complex_plot.pyx
+@@ -61,9 +61,9 @@ cdef inline double mag_to_lightness(double r):
+ 
+         sage: from sage.plot.complex_plot import complex_to_rgb
+         sage: complex_to_rgb([[0, 1, 10]])
+-        array([[[ 0.        ,  0.        ,  0.        ],
+-                [ 0.77172568,  0.        ,  0.        ],
+-                [ 1.        ,  0.22134776,  0.22134776]]])
++        array([[[0.        , 0.        , 0.        ],
++                [0.77172568, 0.        , 0.        ],
++                [1.        , 0.22134776, 0.22134776]]])
+     """
+     return atan(log(sqrt(r)+1)) * (4/PI) - 1
+ 
+@@ -82,13 +82,13 @@ def complex_to_rgb(z_values):
+ 
+         sage: from sage.plot.complex_plot import complex_to_rgb
+         sage: complex_to_rgb([[0, 1, 1000]])
+-        array([[[ 0.        ,  0.        ,  0.        ],
+-                [ 0.77172568,  0.        ,  0.        ],
+-                [ 1.        ,  0.64421177,  0.64421177]]])
++        array([[[0.        , 0.        , 0.        ],
++                [0.77172568, 0.        , 0.        ],
++                [1.        , 0.64421177, 0.64421177]]])
+         sage: complex_to_rgb([[0, 1j, 1000j]])
+-        array([[[ 0.        ,  0.        ,  0.        ],
+-                [ 0.38586284,  0.77172568,  0.        ],
+-                [ 0.82210588,  1.        ,  0.64421177]]])
++        array([[[0.        , 0.        , 0.        ],
++                [0.38586284, 0.77172568, 0.        ],
++                [0.82210588, 1.        , 0.64421177]]])
+     """
+     import numpy
+     cdef unsigned int i, j, imax, jmax
+diff --git a/src/sage/plot/line.py b/src/sage/plot/line.py
+index 23f5e61446..3b1b51d7cf 100644
+--- a/src/sage/plot/line.py
++++ b/src/sage/plot/line.py
+@@ -502,14 +502,12 @@ def line2d(points, **options):
+     from sage.plot.all import Graphics
+     from sage.plot.plot import xydata_from_point_list
+     from sage.rings.all import CC, CDF
++    points = list(points) # make sure points is a python list
+     if points in CC or points in CDF:
+         pass
+     else:
+-        try:
+-            if not points:
+-                return Graphics()
+-        except ValueError: # numpy raises a ValueError if not empty
+-            pass
++        if len(points) == 0:
++            return Graphics()
+     xdata, ydata = xydata_from_point_list(points)
+     g = Graphics()
+     g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
+diff --git a/src/sage/plot/plot_field.py b/src/sage/plot/plot_field.py
+index 0025098a8d..23c80902f3 100644
+--- a/src/sage/plot/plot_field.py
++++ b/src/sage/plot/plot_field.py
+@@ -49,9 +49,10 @@ class PlotField(GraphicPrimitive):
+             sage: r.xpos_array
+             [0.0, 0.0, 1.0, 1.0]
+             sage: r.yvec_array
+-            masked_array(data = [0.0 0.70710678118... 0.70710678118... 0.89442719...],
+-                         mask = [False False False False],
+-                   fill_value = 1e+20)
++            masked_array(data=[0.0, 0.70710678118..., 0.70710678118...,
++                               0.89442719...],
++                         mask=[False, False, False, False],
++                   fill_value=1e+20)
+ 
+         TESTS:
+ 
+diff --git a/src/sage/plot/streamline_plot.py b/src/sage/plot/streamline_plot.py
+index f3da57c370..3806f4b32f 100644
+--- a/src/sage/plot/streamline_plot.py
++++ b/src/sage/plot/streamline_plot.py
+@@ -38,16 +38,14 @@ class StreamlinePlot(GraphicPrimitive):
+             sage: r.options()['plot_points']
+             2
+             sage: r.xpos_array
+-            array([ 0.,  1.])
++            array([0., 1.])
+             sage: r.yvec_array
+-            masked_array(data =
+-             [[1.0 1.0]
+-             [0.5403023058681398 0.5403023058681398]],
+-                         mask =
+-             [[False False]
+-             [False False]],
+-                   fill_value = 1e+20)
+-            <BLANKLINE>
++            masked_array(
++              data=[[1.0, 1.0],
++                    [0.5403023058681398, 0.5403023058681398]],
++              mask=[[False, False],
++                    [False, False]],
++              fill_value=1e+20)
+ 
+         TESTS:
+ 
+diff --git a/src/sage/probability/probability_distribution.pyx b/src/sage/probability/probability_distribution.pyx
+index f66cd898b9..35995886d5 100644
+--- a/src/sage/probability/probability_distribution.pyx
++++ b/src/sage/probability/probability_distribution.pyx
+@@ -130,7 +130,17 @@ cdef class ProbabilityDistribution:
+              0.0,
+              1.4650000000000003]
+             sage: b
+-            [0.0, 0.20000000000000001, 0.40000000000000002, 0.60000000000000009, 0.80000000000000004, 1.0, 1.2000000000000002, 1.4000000000000001, 1.6000000000000001, 1.8, 2.0]
++            [0.0,
++             0.2,
++             0.4,
++             0.6000000000000001,
++             0.8,
++             1.0,
++             1.2000000000000002,
++             1.4000000000000001,
++             1.6,
++             1.8,
++             2.0]
+         """
+         import pylab
+         l = [float(self.get_random_element()) for _ in range(num_samples)]
+diff --git a/src/sage/rings/rational.pyx b/src/sage/rings/rational.pyx
+index a0bfe080f5..7d95e7a1a8 100644
+--- a/src/sage/rings/rational.pyx
++++ b/src/sage/rings/rational.pyx
+@@ -1056,7 +1056,7 @@ cdef class Rational(sage.structure.element.FieldElement):
+             dtype('O')
+ 
+             sage: numpy.array([1, 1/2, 3/4])
+-            array([ 1.  ,  0.5 ,  0.75])
++            array([1.  , 0.5 , 0.75])
+         """
+         if mpz_cmp_ui(mpq_denref(self.value), 1) == 0:
+             if mpz_fits_slong_p(mpq_numref(self.value)):
+diff --git a/src/sage/rings/real_mpfr.pyx b/src/sage/rings/real_mpfr.pyx
+index 4c630867a4..64e2187f5b 100644
+--- a/src/sage/rings/real_mpfr.pyx
++++ b/src/sage/rings/real_mpfr.pyx
+@@ -1438,7 +1438,7 @@ cdef class RealNumber(sage.structure.element.RingElement):
+ 
+             sage: import numpy
+             sage: numpy.arange(10.0)
+-            array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
++            array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
+             sage: numpy.array([1.0, 1.1, 1.2]).dtype
+             dtype('float64')
+             sage: numpy.array([1.000000000000000000000000000000000000]).dtype
+diff --git a/src/sage/schemes/elliptic_curves/height.py b/src/sage/schemes/elliptic_curves/height.py
+index 3d270ebf9d..1144f168e3 100644
+--- a/src/sage/schemes/elliptic_curves/height.py
++++ b/src/sage/schemes/elliptic_curves/height.py
+@@ -1623,18 +1623,18 @@ class EllipticCurveCanonicalHeight:
+         even::
+ 
+             sage: H.wp_on_grid(v,4)
+-            array([[ 25.43920182,   5.28760943,   5.28760943,  25.43920182],
+-            [  6.05099485,   1.83757786,   1.83757786,   6.05099485],
+-            [  6.05099485,   1.83757786,   1.83757786,   6.05099485],
+-            [ 25.43920182,   5.28760943,   5.28760943,  25.43920182]])
++            array([[25.43920182,  5.28760943,  5.28760943, 25.43920182],
++                   [ 6.05099485,  1.83757786,  1.83757786,  6.05099485],
++                   [ 6.05099485,  1.83757786,  1.83757786,  6.05099485],
++                   [25.43920182,  5.28760943,  5.28760943, 25.43920182]])
+ 
+         The array of values on the half-grid::
+ 
+             sage: H.wp_on_grid(v,4,True)
+-            array([[ 25.43920182,   5.28760943],
+-            [  6.05099485,   1.83757786],
+-            [  6.05099485,   1.83757786],
+-            [ 25.43920182,   5.28760943]])
++            array([[25.43920182,  5.28760943],
++                   [ 6.05099485,  1.83757786],
++                   [ 6.05099485,  1.83757786],
++                   [25.43920182,  5.28760943]])
+         """
+         tau = self.tau(v)
+         fk, err = self.fk_intervals(v, 15, CDF)
+diff --git a/src/sage/symbolic/ring.pyx b/src/sage/symbolic/ring.pyx
+index 2dcb0492b9..2b1a06385c 100644
+--- a/src/sage/symbolic/ring.pyx
++++ b/src/sage/symbolic/ring.pyx
+@@ -1135,7 +1135,7 @@ cdef class NumpyToSRMorphism(Morphism):
+         sage: cos(numpy.int('2'))
+         cos(2)
+         sage: numpy.cos(numpy.int('2'))
+-        -0.41614683654714241
++        -0.4161468365471424
+     """
+     cdef _intermediate_ring
+ 
diff --git a/pkgs/applications/science/math/sage/patches/pari-stackwarn.patch b/pkgs/applications/science/math/sage/patches/pari-stackwarn.patch
new file mode 100644
index 000000000000..49cdfd189b92
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/pari-stackwarn.patch
@@ -0,0 +1,20 @@
+diff --git a/src/sage/doctest/parsing.py b/src/sage/doctest/parsing.py
+index 6bdc9a0..31fc780 100644
+--- a/src/sage/doctest/parsing.py
++++ b/src/sage/doctest/parsing.py
+@@ -40,6 +40,7 @@ from .external import available_software
+ 
+ float_regex = re.compile('\s*([+-]?\s*((\d*\.?\d+)|(\d+\.?))([eE][+-]?\d+)?)')
+ optional_regex = re.compile(r'(py2|py3|long time|not implemented|not tested|known bug)|([^ a-z]\s*optional\s*[:-]*((\s|\w)*))')
++pari_stack_warning_regex = re.compile(r'\s*\*\*\*.*(Warning: increasing stack size to )\d+\.')
+ find_sage_prompt = re.compile(r"^(\s*)sage: ", re.M)
+ find_sage_continuation = re.compile(r"^(\s*)\.\.\.\.:", re.M)
+ random_marker = re.compile('.*random', re.I)
+@@ -935,6 +936,7 @@ class SageOutputChecker(doctest.OutputChecker):
+             <type 'float'>
+         """
+         got = self.human_readable_escape_sequences(got)
++        got = pari_stack_warning_regex.sub('', got)
+         if isinstance(want, MarkedOutput):
+             if want.random:
+                 return True
diff --git a/pkgs/applications/science/math/sage/patches/pynac-0.7.22.patch b/pkgs/applications/science/math/sage/patches/pynac-0.7.22.patch
new file mode 100644
index 000000000000..22f274d608d4
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/pynac-0.7.22.patch
@@ -0,0 +1,479 @@
+diff --git a/src/sage/calculus/calculus.py b/src/sage/calculus/calculus.py
+index 3a417d9d5c..fadaadbaf6 100644
+--- a/src/sage/calculus/calculus.py
++++ b/src/sage/calculus/calculus.py
+@@ -231,7 +231,7 @@ Another example::
+     sage: f(x=3)
+     arcsinh(1)
+     sage: f.derivative(x)
+-    1/3/sqrt(1/9*x^2 + 1)
++    1/sqrt(x^2 + 9)
+ 
+ We compute the length of the parabola from 0 to 2::
+ 
+@@ -1509,8 +1509,8 @@ def laplace(ex, t, s, algorithm='maxima'):
+     Testing SymPy::
+ 
+         sage: laplace(t^n, t, s, algorithm='sympy')
+-        (s^(-n)*gamma(n + 1)/s, 0, -re(n) < 1)
+-        
++        (gamma(n + 1)/(s*s^n), 0, -re(n) < 1)
++
+     Testing Maxima::
+ 
+         sage: laplace(t^n, t, s, algorithm='maxima')
+diff --git a/src/sage/functions/hypergeometric.py b/src/sage/functions/hypergeometric.py
+index 1fc2db5c94..f3e49b9cdb 100644
+--- a/src/sage/functions/hypergeometric.py
++++ b/src/sage/functions/hypergeometric.py
+@@ -34,7 +34,7 @@ Simplification (note that ``simplify_full`` does not yet call
+     sage: a.simplify_hypergeometric()
+     1/((-e^x + 1)^e^x)
+     sage: a.simplify_hypergeometric(algorithm='sage')
+-    (-e^x + 1)^(-e^x)
++    1/((-e^x + 1)^e^x)
+ 
+ Equality testing::
+ 
+@@ -145,7 +145,7 @@ Series expansions of confluent hypergeometric functions::
+ 
+     sage: hypergeometric_M(2, 2, x).series(x, 3)
+     1 + 1*x + 1/2*x^2 + Order(x^3)
+-    sage: hypergeometric_U(2, 2, x).series(x == 3, 100).subs(x=1).n()
++    sage: hypergeometric_U(2, 2, x).series(x == 3, 100).subs(x=1).n() # known bug (see :trac:`25688`)
+     0.403652637676806
+     sage: hypergeometric_U(2, 2, 1).n()
+     0.403652637676806
+@@ -773,7 +773,7 @@ def closed_form(hyp):
+         sage: closed_form(hypergeometric([], [], z))
+         e^z
+         sage: closed_form(hypergeometric([a], [], z))
+-        (-z + 1)^(-a)
++        1/((-z + 1)^a)
+         sage: closed_form(hypergeometric([1, 1, 2], [1, 1], z))
+         (z - 1)^(-2)
+         sage: closed_form(hypergeometric([2, 3], [1], x))
+@@ -1121,7 +1121,7 @@ class Hypergeometric_U(BuiltinFunction):
+                 sage: var('a b z')
+                 (a, b, z)
+                 sage: hypergeometric_U(a, b, z).generalized()
+-                z^(-a)*hypergeometric((a, a - b + 1), (), -1/z)
++                hypergeometric((a, a - b + 1), (), -1/z)/z^a
+                 sage: hypergeometric_U(1, 3, 1/2).generalized()
+                 2*hypergeometric((1, -1), (), -2)
+                 sage: hypergeometric_U(3, I, 2).generalized()
+diff --git a/src/sage/functions/log.py b/src/sage/functions/log.py
+index 75d1bf8060..61968582af 100644
+--- a/src/sage/functions/log.py
++++ b/src/sage/functions/log.py
+@@ -518,17 +518,17 @@ class Function_polylog(GinacFunction):
+ 
+             sage: BF = RealBallField(100)
+             sage: polylog(2, BF(1/3))
+-            [0.36621322997706348761674629766 +/- 4.51e-30]
++            [0.36621322997706348761674629766... +/- ...]
+             sage: polylog(2, BF(4/3))
+-            nan
++            [2.27001825336107090380391448586 +/- 5.64e-30] + [-0.90377988538400159956755721265 +/- 8.39e-30]*I
+             sage: parent(_)
+-            Real ball field with 100 bits of precision
++            Complex ball field with 100 bits of precision
+             sage: polylog(2, CBF(1/3))
+-            [0.366213229977063 +/- 5.85e-16]
++            [0.366213229977063 +/- ...]
+             sage: parent(_)
+             Complex ball field with 53 bits of precision
+             sage: polylog(2, CBF(1))
+-            [1.644934066848226 +/- 6.59e-16]
++            [1.644934066848226 +/- ...]
+             sage: parent(_)
+             Complex ball field with 53 bits of precision
+         """
+diff --git a/src/sage/functions/trig.py b/src/sage/functions/trig.py
+index e7e7a311cd..1f2926d6c9 100644
+--- a/src/sage/functions/trig.py
++++ b/src/sage/functions/trig.py
+@@ -529,13 +529,8 @@ class Function_arcsin(GinacFunction):
+             arcsin
+             sage: asin(complex(1,1))
+             (0.6662394324925152+1.0612750619050357j)
+-
+-        Check that :trac:`22823` is fixed::
+-
+-            sage: bool(asin(SR(2.1)) == NaN)
+-            True
+-            sage: asin(SR(2.1)).is_real()
+-            False
++            sage: asin(SR(2.1))
++            1.57079632679490 - 1.37285914424258*I
+         """
+         GinacFunction.__init__(self, 'arcsin', latex_name=r"\arcsin",
+                 conversions=dict(maxima='asin', sympy='asin', fricas="asin", giac="asin"))
+@@ -595,13 +590,8 @@ class Function_arccos(GinacFunction):
+             arccos
+             sage: acos(complex(1,1))
+             (0.9045568943023814-1.0612750619050357j)
+-
+-        Check that :trac:`22823` is fixed::
+-
+-            sage: bool(acos(SR(2.1)) == NaN)
+-            True
+-            sage: acos(SR(2.1)).is_real()
+-            False
++            sage: acos(SR(2.1))
++            1.37285914424258*I
+         """
+         GinacFunction.__init__(self, 'arccos', latex_name=r"\arccos",
+                 conversions=dict(maxima='acos', sympy='acos', fricas='acos', giac='acos'))
+@@ -807,7 +797,7 @@ class Function_arcsec(GinacFunction):
+             sage: arcsec(2).n(100)
+             1.0471975511965977461542144611
+             sage: arcsec(1/2).n(100)
+-            NaN
++            1.3169578969248167086250463473*I
+             sage: RDF(arcsec(2))  # abs tol 1e-15
+             1.0471975511965976
+             sage: arcsec(1 + I)
+@@ -958,7 +948,9 @@ class Function_arctan2(GinacFunction):
+             sage: atan2(0,0,hold=True)
+             arctan2(0, 0)
+             sage: atan2(0,0,hold=True).n()
+-            NaN
++            Traceback (most recent call last):
++            ...
++            RuntimeError: atan2(): division by zero
+ 
+         Check if :trac:`10062` is fixed, this was caused by
+         ``(I*I).is_positive()`` returning ``True``::
+diff --git a/src/sage/geometry/hyperbolic_space/hyperbolic_geodesic.py b/src/sage/geometry/hyperbolic_space/hyperbolic_geodesic.py
+index 3b5c8d1729..1c6b73a16c 100644
+--- a/src/sage/geometry/hyperbolic_space/hyperbolic_geodesic.py
++++ b/src/sage/geometry/hyperbolic_space/hyperbolic_geodesic.py
+@@ -1422,8 +1422,10 @@ class HyperbolicGeodesicUHP(HyperbolicGeodesic):
+         expressions do not generate runtime errors. ::
+ 
+             sage: g=HyperbolicPlane().UHP().get_geodesic(-1+I,1+I)
+-            sage: g.midpoint()
+-            Point in UHP 1/2*(sqrt(2)*e^(1/2*arccosh(3)) - sqrt(2) + (I - 1)*e^(1/2*arccosh(3)) + I - 1)/((1/4*I - 1/4)*sqrt(2)*e^(1/2*arccosh(3)) - (1/4*I - 1/4)*sqrt(2) + 1/2*e^(1/2*arccosh(3)) + 1/2)
++            sage: point = g.midpoint(); point
++            Point in UHP -1/2*(sqrt(2)*...
++            sage: QQbar(point.coordinates()).radical_expression()
++            I*sqrt(2)
+ 
+         Check that floating points remain floating points
+         in :meth:`midpoint` ::
+diff --git a/src/sage/interfaces/fricas.py b/src/sage/interfaces/fricas.py
+index 82aae78e37..8501ea2a18 100644
+--- a/src/sage/interfaces/fricas.py
++++ b/src/sage/interfaces/fricas.py
+@@ -1084,14 +1084,14 @@ class FriCASElement(ExpectElement):
+              0.451026811796262,
+              0.732815101786507,
+              0.837981225008390,
+-             NaN,
+-             NaN,
++             1.57079632679490 - 0.467145308103262*I,
++             0.467145308103262*I,
+              1.11976951499863,
+              0.451026811796262,
+              0.732815101786507,
+              0.837981225008390,
+-             NaN,
+-             NaN]
++             1.57079632679490 - 0.467145308103262*I,
++             0.467145308103262*I]
+             sage: l = [tanh, sinh, cosh, coth, sech, csch, asinh, acosh, atanh, acoth, asech, acsch, arcsinh, arccosh, arctanh, arccoth, arcsech, arccsch]
+             sage: [f(x)._fricas_().sage().subs(x=0.9) for f in l]               # optional - fricas
+             [0.716297870199024,
+diff --git a/src/sage/libs/pynac/pynac.pyx b/src/sage/libs/pynac/pynac.pyx
+index 9e9d8f664b..e3406f007e 100644
+--- a/src/sage/libs/pynac/pynac.pyx
++++ b/src/sage/libs/pynac/pynac.pyx
+@@ -1824,7 +1824,7 @@ cdef py_atan2(x, y):
+         sage: atan2(CC(I), CC(I+1))
+         0.553574358897045 + 0.402359478108525*I
+         sage: atan2(CBF(I), CBF(I+1))
+-        [0.55357435889705 +/- 5.58e-15] + [0.402359478108525 +/- 7.11e-16]*I
++        [0.55357435889705 +/- ...] + [0.402359478108525 +/- ...]*I
+ 
+     Check that :trac:`23776` is fixed and RDF input gives real output::
+ 
+diff --git a/src/sage/matrix/matrix_symbolic_dense.pyx b/src/sage/matrix/matrix_symbolic_dense.pyx
+index 46a15e53eb..7a0beb437b 100644
+--- a/src/sage/matrix/matrix_symbolic_dense.pyx
++++ b/src/sage/matrix/matrix_symbolic_dense.pyx
+@@ -200,7 +200,7 @@ cdef class Matrix_symbolic_dense(Matrix_generic_dense):
+             sage: eval, [evec], mult = es[0]
+             sage: delta = eval*evec - evec*A
+             sage: abs(abs(delta)) < 1e-10
+-            sqrt(9/25*((2*sqrt(6) - 3)*(sqrt(6) - 2) + 7*sqrt(6) - 18)^2 + 9/25*((sqrt(6) - 2)*(sqrt(6) - 4) + 6*sqrt(6) - 14)^2) < (1.00000000000000e-10)
++            3/5*sqrt(((2*sqrt(6) - 3)*(sqrt(6) - 2) + 7*sqrt(6) - 18)^2 + ((sqrt(6) - 2)*(sqrt(6) - 4) + 6*sqrt(6) - 14)^2) < (1.00000000000000e-10)
+             sage: abs(abs(delta)).n() < 1e-10
+             True
+ 
+diff --git a/src/sage/modules/matrix_morphism.py b/src/sage/modules/matrix_morphism.py
+index 17a1d6e290..03892ceec0 100644
+--- a/src/sage/modules/matrix_morphism.py
++++ b/src/sage/modules/matrix_morphism.py
+@@ -214,9 +214,9 @@ class MatrixMorphism_abstract(sage.categories.morphism.Morphism):
+             sage: f((1, 0))
+             Traceback (most recent call last):
+             ...
+-            TypeError: Unable to coerce entries (=[1.00000000000000*I, 0.000000000000000]) to coefficients in Real Field with 53 bits of precision
++            TypeError: Unable to coerce entries (=[1.00000000000000*I, 0]) to coefficients in Real Field with 53 bits of precision
+             sage: f((1, 0), coerce=False)
+-            (1.00000000000000*I, 0.000000000000000)
++            (1.00000000000000*I, 0)
+ 
+         """
+         if self.domain().is_ambient():
+diff --git a/src/sage/plot/plot.py b/src/sage/plot/plot.py
+index 77cdeb8c46..e501e74036 100644
+--- a/src/sage/plot/plot.py
++++ b/src/sage/plot/plot.py
+@@ -2061,7 +2061,7 @@ def _plot(funcs, xrange, parametric=False,
+     plot properly (:trac:`13246`)::
+ 
+         sage: parametric_plot((x, arcsec(x)), (x, -2, 2))
+-        Graphics object consisting of 1 graphics primitive
++        Graphics object consisting of 2 graphics primitives
+ 
+     """
+     from sage.plot.colors import Color
+diff --git a/src/sage/repl/display/formatter.py b/src/sage/repl/display/formatter.py
+index 8ce2e839d7..948207c95e 100644
+--- a/src/sage/repl/display/formatter.py
++++ b/src/sage/repl/display/formatter.py
+@@ -25,11 +25,11 @@ generally, all sage expression as an ASCII art object::
+     sage: shell = get_test_shell()
+     sage: shell.run_cell('%display ascii_art')
+     sage: shell.run_cell('integral(x^2/pi^x, x)')
+-     / 2    2                      \  -x*log(pi)
+-    -\x *log (pi) + 2*x*log(pi) + 2/*e
+-    ---------------------------------------------
+-                         3
+-                      log (pi)
++       -x / 2    2                      \ 
++    -pi  *\x *log (pi) + 2*x*log(pi) + 2/ 
++    --------------------------------------
++                     3                   
++                   log (pi)               
+     sage: shell.run_cell("i = var('i')")
+     sage: shell.run_cell('sum(i*x^i, i, 0, 10)')
+         10      9      8      7      6      5      4      3      2
+diff --git a/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py b/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py
+index d8ebbf4f76..8c60f65e99 100644
+--- a/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py
++++ b/src/sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py
+@@ -151,8 +151,8 @@ A multiple point example (Example 6.5 of [RaWi2012]_)::
+      [(x + 2*y - 1, 1), (2*x + y - 1, 1)])
+     sage: F1 = decomp[1]
+     sage: F1.asymptotics(p, alpha, 2)
+-    (-3*((2*a^2 - 5*a*b + 2*b^2)*r^2 + (a + b)*r + 3)*((1/3)^(-a)*(1/3)^(-b))^r,
+-     (1/3)^(-a)*(1/3)^(-b), -3*(2*a^2 - 5*a*b + 2*b^2)*r^2 - 3*(a + b)*r - 9)
++    (-3*((2*a^2 - 5*a*b + 2*b^2)*r^2 + (a + b)*r + 3)*(1/((1/3)^a*(1/3)^b))^r,
++     1/((1/3)^a*(1/3)^b), -3*(2*a^2 - 5*a*b + 2*b^2)*r^2 - 3*(a + b)*r - 9)
+     sage: alpha = [4, 3]
+     sage: decomp =  F.asymptotic_decomposition(alpha)
+     sage: F1 = decomp[1]
+@@ -2159,7 +2159,7 @@ class FractionWithFactoredDenominator(RingElement):
+             sage: p = {x: 1/3, y: 1/3}
+             sage: alpha = (var('a'), var('b'))
+             sage: F.asymptotics_multiple(p, alpha, 2, var('r')) # long time
+-            (3*((1/3)^(-a)*(1/3)^(-b))^r*e^(2/3), (1/3)^(-a)*(1/3)^(-b), 3*e^(2/3))
++            (3*(1/((1/3)^a*(1/3)^b))^r*e^(2/3), 1/((1/3)^a*(1/3)^b), 3*e^(2/3))
+         """
+         from itertools import product
+         from sage.calculus.functions import jacobian
+diff --git a/src/sage/stats/basic_stats.py b/src/sage/stats/basic_stats.py
+index 16a268a02c..5fd244e93b 100644
+--- a/src/sage/stats/basic_stats.py
++++ b/src/sage/stats/basic_stats.py
+@@ -183,7 +183,7 @@ def std(v, bias=False):
+         sage: std([])
+         NaN
+         sage: std([I, sqrt(2), 3/5])
+-        sqrt(1/450*(10*sqrt(2) - 5*I - 3)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(5*sqrt(2) + 5*I - 6)^2)
++        1/15*sqrt(1/2)*sqrt((10*sqrt(2) - 5*I - 3)^2 + (5*sqrt(2) - 10*I + 3)^2 + (5*sqrt(2) + 5*I - 6)^2)
+         sage: std([RIF(1.0103, 1.0103), RIF(2)])
+         0.6998235813403261?
+         sage: import numpy
+diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx
+index ab4c05de3a..60f0dc549a 100644
+--- a/src/sage/symbolic/expression.pyx
++++ b/src/sage/symbolic/expression.pyx
+@@ -622,7 +622,7 @@ cdef class Expression(CommutativeRingElement):
+             sage: x^(-5)
+             x^(-5)
+             sage: x^(-y)
+-            x^(-y)
++            1/(x^y)
+             sage: 2*x^(-1)
+             2/x
+             sage: i*x
+@@ -3621,14 +3621,14 @@ cdef class Expression(CommutativeRingElement):
+             sage: repl_dict = {b_0: b_0, b_3: b_1, b_2: b_3, b_1: b_2}
+             sage: P = precomp.substitute(repl_dict)
+             sage: P.expand()
+-            -2^(-b_0)*2^(-b_2)*2^b_3 - 2^b_0*2^(-b_2)*2^b_3 -
+-            2^(-b_0)*2^b_2*2^b_3 + 2^b_0*2^b_2*2^b_3 - 2*2^(-b_0)*2^(-b_2)
+-            - 2*2^b_0*2^(-b_2) - 2*2^(-b_0)*2^b_2 + 2*2^b_0*2^b_2 +
+-            2^(-b_0)*2^b_3 + 2^b_0*2^b_3 + 2^(-b_2)*2^b_3 + 2^b_2*2^b_3 +
+-            2*2^(-b_0) + 2*2^b_0 + 2*2^(-b_2) + 2*2^b_2 - 9*2^b_3 -
+-            2^(-b_0)*2^(-b_2)/2^b_3 - 2^b_0*2^(-b_2)/2^b_3 -
+-            2^(-b_0)*2^b_2/2^b_3 + 2^b_0*2^b_2/2^b_3 + 2^(-b_0)/2^b_3 +
+-            2^b_0/2^b_3 + 2^(-b_2)/2^b_3 + 2^b_2/2^b_3 - 9/2^b_3 - 18
++            2^b_0*2^b_2*2^b_3 + 2*2^b_0*2^b_2 + 2^b_0*2^b_3 + 2^b_2*2^b_3 +
++            2*2^b_0 + 2*2^b_2 - 9*2^b_3 + 2^b_0*2^b_2/2^b_3 -
++            2^b_0*2^b_3/2^b_2 - 2^b_2*2^b_3/2^b_0 - 2*2^b_0/2^b_2 -
++            2*2^b_2/2^b_0 + 2^b_0/2^b_3 + 2^b_2/2^b_3 + 2^b_3/2^b_0 +
++            2^b_3/2^b_2 + 2/2^b_0 + 2/2^b_2 - 2^b_0/(2^b_2*2^b_3) -
++            2^b_2/(2^b_0*2^b_3) - 9/2^b_3 - 2^b_3/(2^b_0*2^b_2) -
++            2/(2^b_0*2^b_2) + 1/(2^b_0*2^b_3) + 1/(2^b_2*2^b_3) -
++            1/(2^b_0*2^b_2*2^b_3) - 18
+ 
+             sage: _0,b_1,b_2=var('b_0,b_1,b_2')
+             sage: f = 1/27*b_2^2/(2^b_2)^2 + 1/27*b_1^2/(2^b_1)^2 + \
+@@ -3809,7 +3809,7 @@ cdef class Expression(CommutativeRingElement):
+ 
+             sage: x = SR.symbol('x', domain='real')
+             sage: (x^3)^(1/3)
+-            (x^3)^(1/3)
++            x
+             sage: (x^4)^(1/4)
+             abs(x)
+             sage: (x^8)^(1/4)
+@@ -3872,7 +3872,7 @@ cdef class Expression(CommutativeRingElement):
+             sage: 2^(x/log(2))
+             e^x
+             sage: 2^(-x^2/2/log(2))
+-            e^(-1/2*x^2)
++            1/e^(1/2*x^2)
+             sage: x^(x/log(x))
+             x^(x/log(x))
+             sage: assume(x > 0)
+@@ -4816,18 +4816,24 @@ cdef class Expression(CommutativeRingElement):
+             sage: ((x+y)^(x+y)).match(w0^w0)
+             {$0: x + y}
+             sage: t = ((a+b)*(a+c)).match((a+w0)*(a+w1))
+-            sage: t[w0], t[w1]
+-            (c, b)
++            sage: set([t[w0], t[w1]]) == set([b, c])
++            True
+             sage: ((a+b)*(a+c)).match((w0+b)*(w0+c))
+             {$0: a}
+             sage: t = ((a+b)*(a+c)).match((w0+w1)*(w0+w2))
+-            sage: t[w0], t[w1], t[w2]
+-            (a, c, b)
+-            sage: print(((a+b)*(a+c)).match((w0+w1)*(w1+w2)))
+-            None
++            sage: t[w0]
++            a
++            sage: set([t[w1], t[w2]]) == set([b, c])
++            True
++            sage: t = ((a+b)*(a+c)).match((w0+w1)*(w1+w2))
++            sage: t[w1]
++            a
++            sage: set([t[w0], t[w2]]) == set([b, c])
++            True
+             sage: t = (a*(x+y)+a*z+b).match(a*w0+w1)
+-            sage: t[w0], t[w1]
+-            (x + y, a*z + b)
++            sage: s = set([t[w0], t[w1]])
++            sage: s == set([x+y, a*z+b]) or s == set([z, a*(x+y)+b])
++            True
+             sage: print((a+b+c+d+f+g).match(c))
+             None
+             sage: (a+b+c+d+f+g).has(c)
+@@ -4836,7 +4842,7 @@ cdef class Expression(CommutativeRingElement):
+             {$0: a + b + d + f + g}
+             sage: (a+b+c+d+f+g).match(c+g+w0)
+             {$0: a + b + d + f}
+-            sage: (a+b).match(a+b+w0)
++            sage: (a+b).match(a+b+w0) # known bug
+             {$0: 0}
+             sage: print((a*b^2).match(a^w0*b^w1))
+             None
+@@ -5009,7 +5015,7 @@ cdef class Expression(CommutativeRingElement):
+             a^3 + b^3 + (x + y)^3
+ 
+             sage: t.subs(w0 == w0^2)
+-            (x^2 + y^2)^18 + a^16 + b^16
++            a^8 + b^8 + (x^2 + y^2)^6
+ 
+             sage: t.subs(a == b, b == c)
+             (x + y)^3 + b^2 + c^2
+@@ -8392,9 +8398,13 @@ cdef class Expression(CommutativeRingElement):
+             sage: SR(I).arctan2(1)
+             arctan2(I, 1)
+             sage: SR(CDF(0,1)).arctan2(1)
+-            NaN + +infinity*I
+-            sage: SR(1).arctan2(CDF(0,1))   # known bug
+-            0.7853981633974484 - 19.012501686914433*I
++            Traceback (most recent call last):
++            ...
++            ValueError: power::eval(): division by zero
++            sage: SR(1).arctan2(CDF(0,1))
++            Traceback (most recent call last):
++            ...
++            ValueError: power::eval(): division by zero
+ 
+             sage: arctan2(0,oo)
+             0
+@@ -8656,7 +8666,7 @@ cdef class Expression(CommutativeRingElement):
+             1/2*I*pi
+             sage: SR(1/2).arccosh()
+             arccosh(1/2)
+-            sage: SR(CDF(1/2)).arccosh()  # rel tol 1e-15
++            sage: SR(CDF(1/2)).arccosh() #  rel tol 1e-15
+             1.0471975511965976*I
+             sage: maxima('acosh(0.5)')
+             1.04719755119659...*%i
+@@ -11687,7 +11697,7 @@ cdef class Expression(CommutativeRingElement):
+             sage: a.solve(t)
+             []
+             sage: b = a.canonicalize_radical(); b
+-            -23040.0*(-2.0*e^(1800*t) + 25.0*e^(900*t) - 32.0)*e^(-2400*t)
++            (46080.0*e^(1800*t) - 576000.0*e^(900*t) + 737280.0)*e^(-2400*t)
+             sage: b.solve(t)
+             []
+             sage: b.solve(t, to_poly_solve=True)
+@@ -12163,14 +12173,14 @@ cdef class Expression(CommutativeRingElement):
+ 
+             sage: (n,k,j)=var('n,k,j')
+             sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
+-            -(-1)^(-j)*sum((-1)^k*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
++            -(-1)^j*sum((-1)^k*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
+             sage: assume(j>-1)
+             sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
+             1
+             sage: forget()
+             sage: assume(n>=j)
+             sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
+-            -(-1)^(-j)*sum((-1)^k*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
++            -(-1)^j*sum((-1)^k*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
+             sage: forget()
+             sage: assume(j==-1)
+             sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
+@@ -12178,7 +12188,7 @@ cdef class Expression(CommutativeRingElement):
+             sage: forget()
+             sage: assume(j<-1)
+             sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
+-            -(-1)^(-j)*sum((-1)^k*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
++            -(-1)^j*sum((-1)^k*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
+             sage: forget()
+ 
+         Check that :trac:`16176` is fixed::
+diff --git a/src/sage/symbolic/relation.py b/src/sage/symbolic/relation.py
+index da6f0eef42..4bd65ef572 100644
+--- a/src/sage/symbolic/relation.py
++++ b/src/sage/symbolic/relation.py
+@@ -895,7 +895,7 @@ def solve(f, *args, **kwds):
+         sage: (r[0][x], r[0][y])
+         (2*lambert_w(1/2), 1)
+         sage: solve(-2*x**3 + 4*x**2 - 2*x + 6 > 0, x, algorithm='sympy')
+-        [x < (1/6*sqrt(77) + 79/54)^(1/3) + 1/9/(1/6*sqrt(77) + 79/54)^(1/3) + 2/3]
++        [x < 1/3*(1/2)^(1/3)*(9*sqrt(77) + 79)^(1/3) + 2/3*(1/2)^(2/3)/(9*sqrt(77) + 79)^(1/3) + 2/3]
+         sage: solve(sqrt(2*x^2 - 7) - (3 - x),x,algorithm='sympy')
+         [x == -8, x == 2]
+         sage: solve(sqrt(2*x + 9) - sqrt(x + 1) - sqrt(x + 4),x,algorithm='sympy')
diff --git a/pkgs/applications/science/math/sage/patches/python-5755-hotpatch.patch b/pkgs/applications/science/math/sage/patches/python-5755-hotpatch.patch
new file mode 100644
index 000000000000..1b64f39fe60f
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/python-5755-hotpatch.patch
@@ -0,0 +1,28 @@
+diff --git a/src/sage/all.py b/src/sage/all.py
+index ca309ef..be3186b 100644
+--- a/src/sage/all.py
++++ b/src/sage/all.py
+@@ -298,6 +298,23 @@ warnings.filterwarnings('ignore', module='matplotlib[.]font_manager')
+ warnings.filterwarnings('default',
+     '[\s\S]*See http://trac.sagemath.org/[0-9]* for details.')
+ 
++# Hotpatch around https://bugs.python.org/issue5755 which won't be fixed for
++# python 2.7. Idea by https://stackoverflow.com/a/36293331.
++from distutils.command.build_ext import build_ext
++from distutils.sysconfig import customize_compiler
++
++_build_extensions = build_ext.build_extensions
++
++def build_extensions_patched(self):
++    customize_compiler(self.compiler)
++    try:
++        self.compiler.compiler_so.remove("-Wstrict-prototypes")
++    except (AttributeError, ValueError):
++        pass
++    _build_extensions(self)
++
++build_ext.build_extensions = build_extensions_patched
++
+ 
+ # Set a new random number seed as the very last thing
+ # (so that printing initial_seed() and using that seed
diff --git a/pkgs/applications/science/math/sage/patches/python3-syntax-without-write.patch b/pkgs/applications/science/math/sage/patches/python3-syntax-without-write.patch
new file mode 100644
index 000000000000..ff1dcd22acf3
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/python3-syntax-without-write.patch
@@ -0,0 +1,40 @@
+diff --git a/src/sage/tests/py3_syntax.py b/src/sage/tests/py3_syntax.py
+index e564860b48..86ab3725f9 100644
+--- a/src/sage/tests/py3_syntax.py
++++ b/src/sage/tests/py3_syntax.py
+@@ -179,15 +179,30 @@ class Python3SyntaxTest(SortedDirectoryWalkerABC):
+             sage: py3_syntax = Python3SyntaxTest()
+             sage: py3_syntax.test(src.name)
+             Invalid Python 3 syntax found:
+-              File "...py", line 1
+-                print "invalid print statement"
+-                                              ^
+-            SyntaxError: Missing parentheses in call to 'print'
++            Missing parentheses in call to 'print'...
+             sage: os.unlink(src.name)
+         """
++
++        # compile all given files in memory, printing all errors
++        # inspired by the py_compile module (but without writing to file)
++        script = """
++import sys
++import importlib.machinery
++rv = 0
++for file in sys.argv[1:]:
++    loader = importlib.machinery.SourceFileLoader('<sage_test>', file)
++    source_bytes = loader.get_data(file)
++    try:
++        code = loader.source_to_code(source_bytes, file)
++    except Exception as err:
++        print(err)
++        rv = 1
++sys.exit(rv)
++"""
+         cmd = [
+             'python3',
+-            '-m', 'py_compile'
++            '-c',
++            script,
+         ] + list(filenames)
+         process = subprocess.Popen(
+             cmd,
diff --git a/pkgs/applications/science/math/sage/patches/sagenb-sphinx-1.7.patch b/pkgs/applications/science/math/sage/patches/sagenb-sphinx-1.7.patch
new file mode 100644
index 000000000000..42deef4eab0c
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/sagenb-sphinx-1.7.patch
@@ -0,0 +1,31 @@
+From 7419e0246230594ebfd5e7a2fe6b80d67abfc98a Mon Sep 17 00:00:00 2001
+From: Jeroen Demeyer <jdemeyer@cage.ugent.be>
+Date: Tue, 20 Mar 2018 10:40:41 +0100
+Subject: Fix sphinxify doctests
+
+---
+ sagenb/misc/sphinxify.py | 6 +++---
+ 1 file changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/sagenb/misc/sphinxify.py b/sagenb/misc/sphinxify.py
+index 4f76d41..15623d9 100644
+--- a/sagenb/misc/sphinxify.py
++++ b/sagenb/misc/sphinxify.py
+@@ -47,11 +47,11 @@ def sphinxify(docstring, format='html'):
+ 
+         sage: from sage.misc.sphinxify import sphinxify
+         sage: sphinxify('A test')
+-        '...<div class="docstring">\n    \n  <p>A test</p>\n\n\n</div>'
++        '<div class="docstring">\n    \n  <p>A test</p>\n\n\n</div>'
+         sage: sphinxify('**Testing**\n`monospace`')
+-        '...<div class="docstring"...<strong>Testing</strong>\n<span class="math"...</p>\n\n\n</div>'
++        '<div class="docstring"...<strong>Testing</strong>\n<span class="math notranslate"...</p>\n\n\n</div>'
+         sage: sphinxify('`x=y`')
+-        '...<div class="docstring">\n    \n  <p><span class="math">x=y</span></p>\n\n\n</div>'
++        '<div class="docstring">\n    \n  <p><span class="math notranslate">x=y</span></p>\n\n\n</div>'
+         sage: sphinxify('`x=y`', format='text')
+         'x=y\n'
+         sage: sphinxify(':math:`x=y`', format='text')
+-- 
+cgit v1.0-1-gd88e
+
diff --git a/pkgs/applications/science/math/sage/patches/sphinx-1.7.patch b/pkgs/applications/science/math/sage/patches/sphinx-1.7.patch
new file mode 100644
index 000000000000..c67b8942e28b
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/sphinx-1.7.patch
@@ -0,0 +1,62 @@
+diff --git a/src/bin/sage b/src/bin/sage
+index 397f30cbed..3fc473c343 100755
+--- a/src/bin/sage
++++ b/src/bin/sage
+@@ -980,8 +980,11 @@ if [ "$1" = '-rsyncdist' -o "$1" = "--rsyncdist" ]; then
+ fi
+ 
+ if [ "$1" = "-docbuild" -o "$1" = "--docbuild" ]; then
++    # Redirect stdin from /dev/null. This helps with running TeX which
++    # tends to ask interactive questions if something goes wrong. These
++    # cause the build to hang. If stdin is /dev/null, TeX just aborts.
+     shift
+-    exec sage-python23 -m "sage_setup.docbuild" "$@"
++    exec sage-python23 -m sage_setup.docbuild "$@" </dev/null
+ fi
+ 
+ if [ "$1" = '-gdb' -o "$1" = "--gdb" ]; then
+diff --git a/src/doc/common/conf.py b/src/doc/common/conf.py
+index 25f94f7b7d..3f07474d9b 100644
+--- a/src/doc/common/conf.py
++++ b/src/doc/common/conf.py
+@@ -627,7 +627,7 @@ def call_intersphinx(app, env, node, contnode):
+         sage: for line in open(thematic_index).readlines():
+         ....:     if "padics" in line:
+         ....:         sys.stdout.write(line)
+-        <li><a class="reference external" href="../reference/padics/sage/rings/padics/tutorial.html#sage-rings-padics-tutorial" title="(in Sage Reference Manual: p-Adics ...)"><span>Introduction to the -adics</span></a></li>
++        <li><a class="reference external" href="../reference/padics/sage/rings/padics/tutorial.html#sage-rings-padics-tutorial" title="(in Sage Reference Manual: p-Adics v...)"><span>Introduction to the -adics</span></a></li>
+     """
+     debug_inf(app, "???? Trying intersphinx for %s"%node['reftarget'])
+     builder = app.builder
+diff --git a/src/sage/misc/sphinxify.py b/src/sage/misc/sphinxify.py
+index 4f76d4113a..8f426b5989 100644
+--- a/src/sage/misc/sphinxify.py
++++ b/src/sage/misc/sphinxify.py
+@@ -47,11 +47,11 @@ def sphinxify(docstring, format='html'):
+ 
+         sage: from sage.misc.sphinxify import sphinxify
+         sage: sphinxify('A test')
+-        '...<div class="docstring">\n    \n  <p>A test</p>\n\n\n</div>'
++        '<div class="docstring">\n    \n  <p>A test</p>\n\n\n</div>'
+         sage: sphinxify('**Testing**\n`monospace`')
+-        '...<div class="docstring"...<strong>Testing</strong>\n<span class="math"...</p>\n\n\n</div>'
++        '<div class="docstring"...<strong>Testing</strong>\n<span class="math...</p>\n\n\n</div>'
+         sage: sphinxify('`x=y`')
+-        '...<div class="docstring">\n    \n  <p><span class="math">x=y</span></p>\n\n\n</div>'
++        '<div class="docstring">\n    \n  <p><span class="math notranslate nohighlight">x=y</span></p>\n\n\n</div>'
+         sage: sphinxify('`x=y`', format='text')
+         'x=y\n'
+         sage: sphinxify(':math:`x=y`', format='text')
+diff --git a/src/sage_setup/docbuild/sphinxbuild.py b/src/sage_setup/docbuild/sphinxbuild.py
+index fda76a4174..d3413239dd 100644
+--- a/src/sage_setup/docbuild/sphinxbuild.py
++++ b/src/sage_setup/docbuild/sphinxbuild.py
+@@ -207,7 +207,7 @@ def runsphinx():
+     try:
+         sys.stdout = SageSphinxLogger(sys.stdout, os.path.basename(output_dir))
+         sys.stderr = SageSphinxLogger(sys.stderr, os.path.basename(output_dir))
+-        sphinx.cmdline.main(sys.argv)
++        sphinx.cmdline.main(sys.argv[1:])
+     finally:
+         sys.stdout = saved_stdout
+         sys.stderr = saved_stderr
diff --git a/pkgs/applications/science/math/sage/patches/test-in-tmpdir.patch b/pkgs/applications/science/math/sage/patches/test-in-tmpdir.patch
new file mode 100644
index 000000000000..febab3702771
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/test-in-tmpdir.patch
@@ -0,0 +1,31 @@
+diff --git a/src/sage/repl/ipython_kernel/install.py b/src/sage/repl/ipython_kernel/install.py
+index aa23c8405d..8a87de0591 100644
+--- a/src/sage/repl/ipython_kernel/install.py
++++ b/src/sage/repl/ipython_kernel/install.py
+@@ -112,7 +112,7 @@ class SageKernelSpec(object):
+         EXAMPLES::
+ 
+             sage: from sage.repl.ipython_kernel.install import SageKernelSpec
+-            sage: spec = SageKernelSpec()
++            sage: spec = SageKernelSpec(prefix = tmp_dir())
+             sage: spec.use_local_mathjax()
+             sage: mathjax = os.path.join(spec.nbextensions_dir, 'mathjax')
+             sage: os.path.isdir(mathjax)
+@@ -129,7 +129,7 @@ class SageKernelSpec(object):
+         EXAMPLES::
+ 
+             sage: from sage.repl.ipython_kernel.install import SageKernelSpec
+-            sage: spec = SageKernelSpec()
++            sage: spec = SageKernelSpec(prefix = tmp_dir())
+             sage: spec.use_local_jsmol()
+             sage: jsmol = os.path.join(spec.nbextensions_dir, 'jsmol')
+             sage: os.path.isdir(jsmol)
+@@ -146,7 +146,7 @@ class SageKernelSpec(object):
+         EXAMPLES::
+ 
+             sage: from sage.repl.ipython_kernel.install import SageKernelSpec
+-            sage: spec = SageKernelSpec()
++            sage: spec = SageKernelSpec(prefix = tmp_dir())
+             sage: spec.use_local_threejs()
+             sage: threejs = os.path.join(spec.nbextensions_dir, 'threejs')
+             sage: os.path.isdir(threejs)
diff --git a/pkgs/applications/science/math/sage/patches/zn_poly_version.patch b/pkgs/applications/science/math/sage/patches/zn_poly_version.patch
new file mode 100644
index 000000000000..92ea4303e477
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/zn_poly_version.patch
@@ -0,0 +1,13 @@
+diff --git a/src/sage/misc/package.py b/src/sage/misc/package.py
+index 7ff4f21675..1d72168833 100644
+--- a/src/sage/misc/package.py
++++ b/src/sage/misc/package.py
+@@ -350,7 +350,7 @@ def package_versions(package_type, local=False):
+         sage: 'gap' in std
+         True
+         sage: std['zn_poly']
+-        ('0.9.p11', '0.9.p11')
++        ('0.9', '0.9.p11')
+     """
+     return {pkg['name']: (pkg['installed_version'], pkg['remote_version']) for pkg in list_packages(package_type, local=local).values()}
+ 
diff --git a/pkgs/applications/science/math/sage/pybrial.nix b/pkgs/applications/science/math/sage/pybrial.nix
new file mode 100644
index 000000000000..f5cb39603c32
--- /dev/null
+++ b/pkgs/applications/science/math/sage/pybrial.nix
@@ -0,0 +1,29 @@
+{ stdenv
+, fetchFromGitHub
+, buildPythonPackage
+}:
+# This has a cyclic dependency with sage. I don't include sage in the
+# buildInputs and let python figure it out at runtime. Because of this,
+# I don't include the package in the main nipxkgs tree. It wouldn't be useful
+# outside of sage anyways (as you could just directly depend on sage and use
+# it).
+buildPythonPackage rec {
+    pname = "pyBRiAl";
+    version = "1.2.3";
+
+    # included with BRiAl source
+    src = fetchFromGitHub {
+      owner = "BRiAl";
+      repo = "BRiAl";
+      rev = "${version}";
+      sha256 = "0qy4cwy7qrk4zg151cmws5cglaa866z461cnj9wdnalabs7v7qbg";
+    };
+
+    sourceRoot = "source/sage-brial";
+
+    meta = with stdenv.lib; {
+      description = "python implementation of BRiAl";
+      license = licenses.gpl2;
+      maintainers = with maintainers; [ timokau ];
+    };
+}
diff --git a/pkgs/applications/science/math/sage/python-openid.nix b/pkgs/applications/science/math/sage/python-openid.nix
new file mode 100644
index 000000000000..184eaf29bdd3
--- /dev/null
+++ b/pkgs/applications/science/math/sage/python-openid.nix
@@ -0,0 +1,42 @@
+{ stdenv
+, buildPythonPackage
+, fetchPypi
+, isPy3k
+, django
+, nose
+, twill
+, pycrypto
+}:
+
+buildPythonPackage rec {
+  pname = "python-openid";
+  version = "2.2.5";
+
+  disabled = isPy3k;
+
+  src = fetchPypi {
+    inherit pname version;
+    sha256 = "1vvhxlghjan01snfdc4k7ykd80vkyjgizwgg9bncnin8rqz1ricj";
+  };
+
+  propagatedBuildInputs = [
+    django
+    twill
+    pycrypto
+  ];
+
+  # Cannot access the djopenid example module.
+  # I don't know how to fix that (adding the examples dir to PYTHONPATH doesn't work)
+  doCheck = false;
+  checkInputs = [ nose ];
+  checkPhase = ''
+    nosetests
+  '';
+
+  meta = with stdenv.lib; {
+    description = "OpenID library for Python";
+    license = licenses.asl20;
+    maintainers = with maintainers; [ timokau ];
+    homepage = https://github.com/openid/python-openid/;
+  };
+}
diff --git a/pkgs/applications/science/math/sage/sage-env.nix b/pkgs/applications/science/math/sage/sage-env.nix
new file mode 100644
index 000000000000..317eb6e16c49
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sage-env.nix
@@ -0,0 +1,162 @@
+{ stdenv
+, lib
+, writeTextFile
+, python
+, sage-src
+, sagelib
+, env-locations
+, gfortran
+, bash
+, coreutils
+, gnused
+, gnugrep
+, binutils
+, pythonEnv
+, python3
+, pkg-config
+, pari
+, gap-libgap-compatible
+, libgap
+, ecl
+, maxima-ecl
+, singular
+, giac
+, palp
+, rWrapper
+, gfan
+, cddlib
+, jmol
+, tachyon
+, glpk
+, eclib
+, sympow
+, nauty
+, sqlite
+, ppl
+, ecm
+, lcalc
+, rubiks
+, flintqs
+, openblas-cblas-pc
+, flint
+, gmp
+, mpfr
+, pynac
+, zlib
+, gsl
+, ntl
+}:
+
+let
+  runtimepath = (lib.makeBinPath ([
+    "@sage-local@"
+    "@sage-local@/build"
+    pythonEnv
+    # empty python env to add python wrapper that clears PYTHONHOME (see
+    # wrapper.nix). This is necessary because sage will call the python3 binary
+    # (from python2 code). The python2 PYTHONHOME (again set in wrapper.nix)
+    # will then confuse python3, if it is not overwritten.
+    python3.buildEnv
+    gfortran # for inline fortran
+    stdenv.cc # for cython
+    bash
+    coreutils
+    gnused
+    gnugrep
+    binutils.bintools
+    pkg-config
+    pari
+    gap-libgap-compatible
+    libgap
+    ecl
+    maxima-ecl
+    singular
+    giac
+    palp
+    rWrapper
+    gfan
+    cddlib
+    jmol
+    tachyon
+    glpk
+    eclib
+    sympow
+    nauty
+    sqlite
+    ppl
+    ecm
+    lcalc
+    rubiks
+    flintqs
+  ]
+  ));
+in
+writeTextFile rec {
+  name = "sage-env";
+  destination = "/${name}";
+  text = ''
+    export PKG_CONFIG_PATH='${lib.concatStringsSep ":" (map (pkg: "${pkg}/lib/pkgconfig") [
+        # This is only needed in the src/sage/misc/cython.py test and I'm not sure if there's really a use-case
+        # for it outside of the tests. However since singular and openblas are runtime dependencies anyways
+        # and openblas-cblas-pc is tiny, it doesn't really hurt to include.
+        singular
+        openblas-cblas-pc
+      ])
+    }'
+    export SAGE_ROOT='${sage-src}'
+    export SAGE_LOCAL='@sage-local@'
+    export SAGE_SHARE='${sagelib}/share'
+    orig_path="$PATH"
+    export PATH='${runtimepath}'
+
+    # set dependent vars, like JUPYTER_CONFIG_DIR
+    source "${sage-src}/src/bin/sage-env"
+    export PATH="${runtimepath}:$orig_path" # sage-env messes with PATH
+
+    export SAGE_LOGS="$TMPDIR/sage-logs"
+    export SAGE_DOC="''${SAGE_DOC_OVERRIDE:-doc-placeholder}"
+    export SAGE_DOC_SRC="''${SAGE_DOC_SRC_OVERRIDE:-${sage-src}/src/doc}"
+
+    # set locations of dependencies
+    . ${env-locations}/sage-env-locations
+
+    # needed for cython
+    export CC='${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc'
+    # cython needs to find these libraries, otherwise will fail with `ld: cannot find -lflint` or similar
+    export LDFLAGS='${
+      lib.concatStringsSep " " (map (pkg: "-L${pkg}/lib") [
+        flint
+        libgap
+        glpk
+        gmp
+        mpfr
+        pari
+        pynac
+        zlib
+        eclib
+        gsl
+        ntl
+        jmol
+        sympow
+      ])
+    }'
+    export CFLAGS='${
+      lib.concatStringsSep " " (map (pkg: "-isystem ${pkg}/include") [
+        singular
+        gmp.dev
+        glpk
+        flint
+        libgap
+        pynac
+        mpfr.dev
+      ])
+    }'
+
+    export SAGE_LIB='${sagelib}/${python.sitePackages}'
+
+    export SAGE_EXTCODE='${sage-src}/src/ext'
+
+    # for find_library
+    export DYLD_LIBRARY_PATH="${lib.makeLibraryPath [stdenv.cc.libc singular]}:$DYLD_LIBRARY_PATH"
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/sage-src.nix b/pkgs/applications/science/math/sage/sage-src.nix
new file mode 100644
index 000000000000..c97785c574cb
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sage-src.nix
@@ -0,0 +1,212 @@
+{ stdenv
+, fetchFromGitHub
+, fetchpatch
+}:
+stdenv.mkDerivation rec {
+  version = "8.2";
+  name = "sage-src-${version}";
+
+  src = fetchFromGitHub {
+    owner = "sagemath";
+    repo = "sage";
+    rev = version;
+    sha256 = "0d7vc16s7dj23an2cb8v5bhbnc6nsw20qhnnxr0xh8qg629027b8";
+  };
+
+  nixPatches = [
+    # https://trac.sagemath.org/ticket/25309
+    (fetchpatch {
+      name = "spkg-paths.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=97f06fddee920399d4fcda65aa9b0925774aec69&id=a86151429ccce1ddd085e8090ada8ebdf02f3310";
+      sha256 = "1xb9108rzzkdhn71vw44525620d3ww9jv1fph5a77v9y7nf9wgr7";
+    })
+    (fetchpatch {
+      name = "maxima-fas.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=97f06fddee920399d4fcda65aa9b0925774aec69";
+      sha256 = "14s50yg3hpw9cp3v581dx7zfmpm2j972im7x30iwki8k45mjvk3i";
+    })
+
+    # https://trac.sagemath.org/ticket/25328
+    # https://trac.sagemath.org/ticket/25546
+    # https://trac.sagemath.org/ticket/25722
+    (fetchpatch {
+      name = "install-jupyter-kernel-in-correct-prefix.patch";
+      url = "https://git.sagemath.org/sage.git/patch?id=72167b98e3f64326df6b2c78785df25539472fcc";
+      sha256 = "0pscnjhm7r2yr2rxnv4kkkq626vwaja720lixa3m3w9rwlxll5a7";
+    })
+    ./patches/test-in-tmpdir.patch
+
+    # https://trac.sagemath.org/ticket/25358
+    (fetchpatch {
+      name = "safe-directory-test-without-patch.patch";
+      url = "https://git.sagemath.org/sage.git/patch?id2=8bdc326ba57d1bb9664f63cf165a9e9920cc1afc&id=dc673c17555efca611f68398d5013b66e9825463";
+      sha256 = "1hhannz7xzprijakn2w2d0rhd5zv2zikik9p51i87bas3nc658f7";
+    })
+
+    # https://trac.sagemath.org/ticket/25357 rebased on 8.2
+    ./patches/python3-syntax-without-write.patch
+
+    # https://trac.sagemath.org/ticket/25314
+    (fetchpatch {
+      name = "make-qepcad-test-optional.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=fe294c58bd035ef427e268901d54a6faa0058138";
+      sha256 = "003d5baf5c0n5rfg010ijwkwz8kg0s414cxwczs2vhdayxdixbix";
+    })
+
+    # https://trac.sagemath.org/ticket/25316
+    ./patches/python-5755-hotpatch.patch
+
+    # https://trac.sagemath.org/ticket/25354
+    # https://trac.sagemath.org/ticket/25531
+    (fetchpatch {
+      name = "cysignals-include.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=28778bd25a37c80884d2b24e0683fb2989300cef";
+      sha256 = "0fiiiw91pgs8avm9ggj8hb64bhqzl6jcw094d94nhirmh8w2jmc5";
+    })
+
+    # https://trac.sagemath.org/ticket/25315
+    (fetchpatch {
+      name = "find-libraries-in-dyld-library-path.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=20d4593876ce9c6004eac2ab6fd61786d0d96a06";
+      sha256 = "1k3afq3qlzmgqwx6rzs5wv153vv9dsf5rk8pi61g57l3r3npbjmc";
+    })
+
+    # Pari upstream has since accepted a patch, so this patch won't be necessary once sage updates pari.
+    # https://trac.sagemath.org/ticket/25312
+    ./patches/pari-stackwarn.patch
+
+    # https://trac.sagemath.org/ticket/25311
+    ./patches/zn_poly_version.patch
+
+    # https://trac.sagemath.org/ticket/25345
+    # (upstream patch doesn't apply on 8.2 source)
+    ./patches/dochtml-optional.patch
+  ];
+
+  packageUpgradePatches = [
+    (fetchpatch {
+      name = "cypari2-1.2.1.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=62fe6eb15111327d930336d4252d5b23cbb22ab9";
+      sha256 = "1xax7vvs8h4xip16xcsp47xdb6lig6f2r3pl8cksvlz8lhgbyxh2";
+    })
+
+    # matplotlib 2.2.2 deprecated `normed` (replaced by `density`).
+    # This patch only ignores the warning. It would be equally easy to fix it
+    # (by replacing all mentions of `normed` by `density`), but its better to
+    # stay close to sage upstream. I didn't open an upstream ticket about it
+    # because the matplotlib update also requires a new dependency (kiwisolver)
+    # and I don't want to invest the time to learn how to add it.
+    ./patches/matplotlib-normed-deprecated.patch
+
+    # Update to 20171219 broke the doctests because of insignificant precision
+    # changes, make the doctests less fragile.
+    # I didn't open an upstream ticket because its not entirely clear if
+    # 20171219 is really "released" yet. It is listed on the github releases
+    # page, but not marked as "latest release" and the homepage still links to
+    # the last version.
+    ./patches/eclib-regulator-precision.patch
+
+    # sphinx 1.6 -> 1.7 upgrade
+    # https://trac.sagemath.org/ticket/24935
+    ./patches/sphinx-1.7.patch
+
+    # Adapt hashes to new boost version
+    # https://trac.sagemath.org/ticket/22243
+    # (this ticket doesn't only upgrade boost but also avoids this problem in the future)
+    (fetchpatch {
+      name = "boost-upgrade.patch";
+      url = "https://git.sagemath.org/sage.git/patch?id=a24a9c6b30b93957333a3116196214a931325b69";
+      sha256 = "0z3870g2ms2a81vnw08dc2i4k7jr62w8fggvcdwaavgd1wvdxwfl";
+    })
+
+    # gfan 0.6.2
+    # https://trac.sagemath.org/ticket/23353
+    (fetchpatch {
+      name = "gfan-update.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=420215fc469cde733ec7a339e59b78ad6eec804c&id=112498a293ea2bf563e41aed35f1aa608f01b349";
+      sha256 = "0ga3hkx8cr23dpc919lgvpi5lmy0d728jkq9z6kf0fl9s8g31mxb";
+    })
+
+    # New glpk version has new warnings, filter those out until upstream sage has found a solution
+    # https://trac.sagemath.org/ticket/24824
+    (fetchpatch {
+      url = "https://salsa.debian.org/science-team/sagemath/raw/58bbba93a807ca2933ca317501d093a1bb4b84db/debian/patches/dt-version-glpk-4.65-ignore-warnings.patch";
+      sha256 = "0b9293v73wb4x13wv5zwyjgclc01zn16msccfzzi6znswklgvddp";
+      stripLen = 1;
+    })
+
+    # https://trac.sagemath.org/ticket/25329
+    (fetchpatch {
+      name = "dont-check-exact-glpk-version.patch";
+      url = "https://git.sagemath.org/sage.git/patch?id2=8bdc326ba57d1bb9664f63cf165a9e9920cc1afc&id=89d068d8d77316bfffa6bf8e9ebf70b3b3b88e5c";
+      sha256 = "00knwxs6fmymfgfl0q5kcavmxm9sf90a4r76y35n5s55gj8pl918";
+    })
+
+    # https://trac.sagemath.org/ticket/25355
+    (fetchpatch {
+      name = "maxima-5.41.0.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?id=87328023c4739abdf24108038201e3fa9bdfc739";
+      sha256 = "0hxi7qr5mfx1bc32r8j7iba4gzd7c6v63asylyf5cbyp86azpb7i";
+    })
+
+    # Update cddlib from 0.94g to 0.94h.
+    # https://trac.sagemath.org/ticket/25341 (doesn't apply to 8.2 sources)
+    (fetchpatch {
+      url = "https://salsa.debian.org/science-team/sagemath/raw/58bbba93a807ca2933ca317501d093a1bb4b84db/debian/patches/u2-version-cddlib-094h.patch";
+      sha256 = "0fmw7pzbaxs2dshky6iw9pr8i23p9ih2y2lw661qypdrxh5xw03k";
+      stripLen = 1;
+    })
+    (fetchpatch {
+      name = "revert-cddlib-doctest-changes.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?id=269c1e1551285566b8ba7a2b890989e5590e9f11";
+      sha256 = "12bcjhq7hm2pmmj2bgjvcffjyls2x7q61ivlnaj5v5bsvhc183iy";
+      revert = true;
+    })
+
+
+    # Only formatting changes.
+    # https://trac.sagemath.org/ticket/25260
+    ./patches/numpy-1.14.3.patch
+
+    # https://trac.sagemath.org/ticket/24374
+    (fetchpatch {
+      name = "networkx-2.1.patch";
+      url = "https://salsa.debian.org/science-team/sagemath/raw/487df9ae48ca1d93d9b1cb3af8745d31e30fb741/debian/patches/u0-version-networkx-2.1.patch";
+      sha256 = "1xxxawygbgxgvlv7b4w8hhzgdnva4rhmgdxaiaa3pwdwln0yc750";
+      stripLen = 1;
+    })
+
+    # https://trac.sagemath.org/ticket/24927 rebased
+    ./patches/arb-2.13.0.patch
+
+    # https://trac.sagemath.org/ticket/24838 rebased
+    ./patches/pynac-0.7.22.patch
+
+    # https://trac.sagemath.org/ticket/25862
+    ./patches/eclib-20180710.patch
+  ];
+
+  patches = nixPatches ++ packageUpgradePatches;
+
+  postPatch = ''
+    # make sure shebangs etc are fixed, but sage-python23 still works
+    find . -type f -exec sed \
+      -e 's/sage-python23/python/g' \
+      -i {} \;
+
+    echo '#!${stdenv.shell}
+    python "$@"' > build/bin/sage-python23
+
+    # Do not use sage-env-config (generated by ./configure).
+    # Instead variables are set manually.
+    echo '# do nothing' >  src/bin/sage-env-config
+  '';
+
+  configurePhase = "# do nothing";
+
+  buildPhase = "# do nothing";
+
+  installPhase = ''
+    cp -r . "$out"
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/sage-with-env.nix b/pkgs/applications/science/math/sage/sage-with-env.nix
new file mode 100644
index 000000000000..63b9772b8231
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sage-with-env.nix
@@ -0,0 +1,126 @@
+{ stdenv
+, lib
+, makeWrapper
+, sage-env
+, sage-src
+, openblasCompat
+, openblas-blas-pc
+, openblas-cblas-pc
+, openblas-lapack-pc
+, pkg-config
+, three
+, singular
+, libgap
+, gap-libgap-compatible
+, giac
+, maxima-ecl
+, pari
+, gmp
+, gfan
+, python2
+, flintqs
+, eclib
+, ntl
+, ecm
+, pynac
+, pythonEnv
+}:
+
+let
+  buildInputs = [
+    pythonEnv # for patchShebangs
+    makeWrapper
+    pkg-config
+    openblasCompat # lots of segfaults with regular (64 bit) openblas
+    openblas-blas-pc
+    openblas-cblas-pc
+    openblas-lapack-pc
+    singular
+    three
+    pynac
+    giac
+    libgap
+    gap-libgap-compatible
+    pari
+    gmp
+    gfan
+    maxima-ecl
+    eclib
+    flintqs
+    ntl
+    ecm
+  ];
+
+  # 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 ++ pythonEnv.extraLibs);
+  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 = sage-src.version;
+  name = "sage-with-env-${version}";
+
+  inherit buildInputs;
+
+  src = sage-src;
+
+  configurePhase = "#do nothing";
+
+  buildPhase = ''
+    mkdir installed
+    for pkg in ${lib.concatStringsSep " " input_names}; do
+      touch "installed/$pkg"
+    done
+  '';
+
+  installPhase = ''
+    mkdir -p "$out/var/lib/sage"
+    cp -r installed $out/var/lib/sage
+
+    mkdir -p "$out/etc"
+    # sage tests will try to create this file if it doesn't exist
+    touch "$out/etc/sage-started.txt"
+
+    mkdir -p "$out/build"
+    cp -r src/bin "$out/bin"
+    cp -r build/bin "$out/build/bin"
+    cp -f '${sage-env}/sage-env' "$out/bin/sage-env"
+    substituteInPlace "$out/bin/sage-env" \
+      --subst-var-by sage-local "$out"
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/sage-wrapper.nix b/pkgs/applications/science/math/sage/sage-wrapper.nix
new file mode 100644
index 000000000000..06b667f426fa
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sage-wrapper.nix
@@ -0,0 +1,41 @@
+{ stdenv
+, makeWrapper
+, sage
+, sage-src
+, sagedoc
+, withDoc
+}:
+
+stdenv.mkDerivation rec {
+  version = sage.version;
+  name = "sage-wrapper-${version}";
+
+  buildInputs = [
+    makeWrapper
+  ];
+
+  unpackPhase = "#do nothing";
+  configurePhase = "#do nothing";
+  buildPhase = "#do nothing";
+
+  installPhase = ''
+    mkdir -p "$out/bin"
+    makeWrapper "${sage}/bin/sage" "$out/bin/sage" \
+      --set SAGE_DOC_SRC_OVERRIDE "${sage-src}/src/doc" ${
+      stdenv.lib.optionalString withDoc "--set SAGE_DOC_OVERRIDE ${sagedoc}/share/doc/sage"
+    }
+  '';
+
+  doInstallCheck = withDoc;
+  installCheckPhase = ''
+    export HOME="$TMPDIR/sage-home"
+    mkdir -p "$HOME"
+    "$out/bin/sage" -c 'browse_sage_doc._open("reference", testing=True)'
+  '';
+
+  meta = with stdenv.lib; {
+    description = "Open Source Mathematics Software, free alternative to Magma, Maple, Mathematica, and Matlab";
+    license = licenses.gpl2;
+    maintainers = with maintainers; [ timokau ];
+  };
+}
diff --git a/pkgs/applications/science/math/sage/sage.nix b/pkgs/applications/science/math/sage/sage.nix
new file mode 100644
index 000000000000..b1e5d7278b0f
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sage.nix
@@ -0,0 +1,32 @@
+{ stdenv
+, sage-with-env
+, makeWrapper
+}:
+
+stdenv.mkDerivation rec {
+  version = sage-with-env.version;
+  name = "sage-${version}";
+
+  buildInputs = [
+    makeWrapper
+  ];
+
+  unpackPhase = "#do nothing";
+  configurePhase = "#do nothing";
+  buildPhase = "#do nothing";
+
+  installPhase = ''
+    mkdir -p "$out/bin"
+    # Like a symlink, but make sure that $0 points to the original.
+    makeWrapper "${sage-with-env}/bin/sage" "$out/bin/sage"
+  '';
+
+  doInstallCheck = true;
+  installCheckPhase = ''
+    export HOME="$TMPDIR/sage-home"
+    mkdir -p "$HOME"
+
+    # "--long" tests are in the order of 1h, without "--long" its 1/2h
+    "$out/bin/sage" -t --nthreads "$NIX_BUILD_CORES" --optional=sage --long --all
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/sagedoc.nix b/pkgs/applications/science/math/sage/sagedoc.nix
new file mode 100644
index 000000000000..a594428a389a
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sagedoc.nix
@@ -0,0 +1,95 @@
+{ stdenv
+, sage-src
+, sage-with-env
+, sagelib
+, python2
+, psutil
+, future
+, sphinx
+, sagenb
+, maxima-ecl
+, networkx
+, scipy
+, sympy
+, matplotlib
+, pillow
+, ipykernel
+, jupyter_client
+, tachyon
+, jmol
+, ipywidgets
+, typing
+, cddlib
+, pybrial
+}:
+
+stdenv.mkDerivation rec {
+  version = sage-src.version;
+  name = "sagedoc-${version}";
+
+
+  # Building the documentation has many dependencies, because all documented
+  # modules are imported and because matplotlib is used to produce plots.
+  buildInputs = [
+    sagelib
+    python2
+    psutil
+    future
+    sphinx
+    sagenb
+    maxima-ecl
+    networkx
+    scipy
+    sympy
+    matplotlib
+    pillow
+    ipykernel
+    jupyter_client
+    tachyon
+    jmol
+    ipywidgets
+    typing
+    cddlib
+    pybrial
+  ];
+
+  unpackPhase = ''
+    export SAGE_DOC_OVERRIDE="$PWD/share/doc/sage"
+    export SAGE_DOC_SRC_OVERRIDE="$PWD/docsrc"
+
+    cp -r "${sage-src}/src/doc" "$SAGE_DOC_SRC_OVERRIDE"
+    chmod -R 755 "$SAGE_DOC_SRC_OVERRIDE"
+  '';
+
+  buildPhase = ''
+    export SAGE_NUM_THREADS="$NIX_BUILD_CORES"
+    export HOME="$TMPDIR/sage_home"
+    mkdir -p "$HOME"
+
+    ${sage-with-env}/bin/sage -python -m sage_setup.docbuild \
+      --mathjax \
+      --no-pdf-links \
+      all html
+  '';
+
+  installPhase = ''
+    cd "$SAGE_DOC_OVERRIDE"
+
+    mkdir -p "$out/share/doc/sage"
+    cp -r html "$out"/share/doc/sage
+
+    # Replace duplicated files by symlinks (Gentoo)
+    cd "$out"/share/doc/sage
+    mv html/en/_static{,.tmp}
+    for _dir in `find -name _static` ; do
+          rm -r $_dir
+          ln -s /share/doc/sage/html/en/_static $_dir
+    done
+    mv html/en/_static{.tmp,}
+  '';
+
+  doCheck = true;
+  checkPhase = ''
+    ${sage-with-env}/bin/sage -t --optional=dochtml --all
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/sagelib.nix b/pkgs/applications/science/math/sage/sagelib.nix
new file mode 100644
index 000000000000..abcefba5e260
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sagelib.nix
@@ -0,0 +1,138 @@
+{ sage-src
+, perl
+, buildPythonPackage
+, arb
+, openblasCompat
+, openblas-blas-pc
+, openblas-cblas-pc
+, openblas-lapack-pc
+, brial
+, cliquer
+, cypari2
+, cysignals
+, cython
+, ecl
+, eclib
+, ecm
+, flint
+, gd
+, givaro
+, glpk
+, gsl
+, iml
+, jinja2
+, lcalc
+, lrcalc
+, libgap
+, linbox
+, m4ri
+, m4rie
+, libmpc
+, mpfi
+, ntl
+, numpy
+, pari
+, pkgconfig
+, planarity
+, ppl
+, pynac
+, python
+, ratpoints
+, readline
+, rankwidth
+, symmetrica
+, zn_poly
+, fflas-ffpack
+, boost
+, singular
+, pip
+, jupyter_core
+}:
+
+buildPythonPackage rec {
+  format = "other";
+  version = sage-src.version;
+  pname = "sagelib";
+
+  src = sage-src;
+
+  nativeBuildInputs = [
+    iml
+    perl
+    openblas-blas-pc
+    openblas-cblas-pc
+    openblas-lapack-pc
+    jupyter_core
+  ];
+
+  buildInputs = [
+    gd
+    readline
+  ];
+
+  propagatedBuildInputs = [
+    cypari2
+    jinja2
+    numpy
+    pkgconfig
+    boost
+    arb
+    brial
+    cliquer
+    ecl
+    eclib
+    ecm
+    fflas-ffpack
+    flint
+    givaro
+    glpk
+    gsl
+    lcalc
+    libgap
+    libmpc
+    linbox
+    lrcalc
+    m4ri
+    m4rie
+    mpfi
+    ntl
+    openblasCompat
+    pari
+    planarity
+    ppl
+    pynac
+    rankwidth
+    ratpoints
+    singular
+    symmetrica
+    zn_poly
+    pip
+    cython
+    cysignals
+  ];
+
+  buildPhase = ''
+    export SAGE_ROOT="$PWD"
+    export SAGE_LOCAL="$SAGE_ROOT"
+    export SAGE_SHARE="$SAGE_LOCAL/share"
+    export JUPYTER_PATH="$SAGE_LOCAL/jupyter"
+
+    export PATH="$SAGE_ROOT/build/bin:$SAGE_ROOT/src/bin:$PATH"
+
+    export SAGE_NUM_THREADS="$NIX_BUILD_CORES"
+
+    mkdir -p "$SAGE_SHARE/sage/ext/notebook-ipython"
+    mkdir -p "var/lib/sage/installed"
+
+    cd src
+    source bin/sage-dist-helpers
+
+    ${python.interpreter} -u setup.py --no-user-cfg build
+  '';
+
+  installPhase = ''
+    ${python.interpreter} -u setup.py --no-user-cfg install --prefix=$out
+
+    rm -r "$out/${python.sitePackages}/sage/cython_debug"
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/sagenb.nix b/pkgs/applications/science/math/sage/sagenb.nix
new file mode 100644
index 000000000000..cc883cc24b42
--- /dev/null
+++ b/pkgs/applications/science/math/sage/sagenb.nix
@@ -0,0 +1,57 @@
+# Has a cyclic dependency with sage (not expressed here) and is not useful outside of sage
+{ stdenv
+, fetchpatch
+, python
+, buildPythonPackage
+, fetchFromGitHub
+, mathjax
+, twisted
+, flask
+, flask-oldsessions
+, flask-openid
+, flask-autoindex
+, flask-babel
+}:
+
+buildPythonPackage rec {
+  pname = "sagenb";
+  version = "2018-06-26"; # not 1.0.1 because of new flask syntax
+
+  src = fetchFromGitHub {
+    owner = "sagemath";
+    repo = "sagenb";
+    rev = "b360a0172e15501fb0163d02dce713a561fee2af";
+    sha256 = "12anydw0v9w23rbc0a94bqmjhjdir9h820c5zdhipw9ccdcc2jlf";
+  };
+
+  propagatedBuildInputs = [
+    twisted
+    flask
+    flask-oldsessions
+    flask-openid
+    flask-autoindex
+    flask-babel
+  ];
+
+  # tests depend on sage
+  doCheck = false;
+
+  patches = [
+    # work with latest flask-babel
+    (fetchpatch {
+      url = "https://github.com/sagemath/sagenb/commit/ba065eca63dd34a383e4c7ba7561430a90fcd087.patch";
+      sha256 = "1lamzsrgymdd618imrasjp6ivhw2aynh83gkybsd7pm1rzjcq4x8";
+    })
+  ];
+
+  meta = with stdenv.lib; {
+    description = "Sage Notebook";
+    license = licenses.gpl3Plus;
+    maintainers = with maintainers; [ timokau ];
+  };
+
+  # let sagenb use mathjax
+  postInstall = ''
+    ln -s ${mathjax}/lib/node_modules/mathjax "$out/${python.sitePackages}/mathjax"
+  '';
+}
diff --git a/pkgs/applications/science/math/sage/shebangs.patch b/pkgs/applications/science/math/sage/shebangs.patch
deleted file mode 100644
index 96ed5a4bc6c3..000000000000
--- a/pkgs/applications/science/math/sage/shebangs.patch
+++ /dev/null
@@ -1,36 +0,0 @@
-diff --git a/build/bin/sage-spkg b/build/bin/sage-spkg
-index 83e61a7e0d..942ba206c7 100755
---- a/build/bin/sage-spkg
-+++ b/build/bin/sage-spkg
-@@ -648,8 +648,12 @@ if ! sage-apply-patches; then
-     error_msg "Error applying patches"
-     exit 1
- fi
-+
-+@bash@/bin/bash @patchSageShebangs@ .
-+
- cd ..
- 
-+
- ##################################################################
- # The package has been extracted, prepare for installation
- ##################################################################
-@@ -671,7 +675,7 @@ write_script_wrapper() {
-     local tmpscript="$(dirname "$script")/.tmp-${script##*/}"
- 
-     cat > "$tmpscript" <<__EOF__
--#!/usr/bin/env bash
-+#! @bash@/bin/bash
- 
- export SAGE_ROOT="$SAGE_ROOT"
- export SAGE_SRC="$SAGE_SRC"
-@@ -833,6 +837,9 @@ if [ "$UNAME" = "CYGWIN" ]; then
-     sage-rebase.sh "$SAGE_LOCAL" 2>/dev/null
- fi
- 
-+@bash@/bin/bash @patchSageShebangs@ .
-+@bash@/bin/bash @patchSageShebangs@ "$out/bin"
-+
- echo "Successfully installed $PKG_NAME"
- 
- if [ "$SAGE_CHECK" = "yes" ]; then
diff --git a/pkgs/applications/science/math/sage/spkg-giac.patch b/pkgs/applications/science/math/sage/spkg-giac.patch
deleted file mode 100644
index c79d4422133d..000000000000
--- a/pkgs/applications/science/math/sage/spkg-giac.patch
+++ /dev/null
@@ -1,20 +0,0 @@
-diff --git a/build/pkgs/giac/spkg-install b/build/pkgs/giac/spkg-install
-index bdd8df6cb8..3fd7a3ef8a 100644
---- a/build/pkgs/giac/spkg-install
-+++ b/build/pkgs/giac/spkg-install
-@@ -2,6 +2,15 @@
- ## Giac
- ###########################################
- 
-+# Fix hardcoded paths, while making sure to only update timestamps of actually
-+# changed files (otherwise confuses make)
-+grep -rlF '/bin/cp' . | while read file
-+do
-+	sed -e 's@/bin/cp@cp@g' -i "$file"
-+done
-+
-+# Fix input parser syntax
-+sed -e 's@yylex (&yylval)@yylex (\&yyval, scanner)@gp' -i 'src/src/input_parser.cc'
- 
- if [ "$SAGE_LOCAL" = "" ]; then
-    echo "SAGE_LOCAL undefined ... exiting";
diff --git a/pkgs/applications/science/math/sage/spkg-git.patch b/pkgs/applications/science/math/sage/spkg-git.patch
deleted file mode 100644
index 74f552dd3c36..000000000000
--- a/pkgs/applications/science/math/sage/spkg-git.patch
+++ /dev/null
@@ -1,18 +0,0 @@
-diff --git a/build/pkgs/git/spkg-install b/build/pkgs/git/spkg-install
-index 87874de3d8..b0906245fa 100644
---- a/build/pkgs/git/spkg-install
-+++ b/build/pkgs/git/spkg-install
-@@ -33,6 +33,13 @@ fi
- 
- cd src
- 
-+# Fix hardcoded paths, while making sure to only update timestamps of actually
-+# changed files (otherwise confuses make)
-+grep -rlF '/usr/bin/perl' . | while read file
-+do
-+	sed -e 's@/usr/bin/perl@perl@g' -i "$file"
-+done
-+
- # We don't want to think about Fink or Macports
- export NO_FINK=1
- export NO_DARWIN_PORTS=1
diff --git a/pkgs/applications/science/math/sage/spkg-python.patch b/pkgs/applications/science/math/sage/spkg-python.patch
deleted file mode 100644
index e39981b6552a..000000000000
--- a/pkgs/applications/science/math/sage/spkg-python.patch
+++ /dev/null
@@ -1,13 +0,0 @@
-diff --git a/build/pkgs/python3/spkg-build b/build/pkgs/python3/spkg-build
-index 56db087ae5..b450703c5f 100644
---- a/build/pkgs/python3/spkg-build
-+++ b/build/pkgs/python3/spkg-build
-@@ -27,6 +27,8 @@ fi
- export EXTRA_CFLAGS="`testcflags.sh -Wno-unused` $CFLAGS"
- unset CFLAGS
- 
-+export LDFLAGS="$LDFLAGS -lcrypt"
-+
- if [ "$UNAME" = Darwin ]; then
-     PYTHON_CONFIGURE="--disable-toolbox-glue $PYTHON_CONFIGURE"
- 
diff --git a/pkgs/applications/science/math/sage/spkg-singular.patch b/pkgs/applications/science/math/sage/spkg-singular.patch
deleted file mode 100644
index 606ffcd3ad4e..000000000000
--- a/pkgs/applications/science/math/sage/spkg-singular.patch
+++ /dev/null
@@ -1,18 +0,0 @@
-diff --git a/build/pkgs/singular/spkg-install b/build/pkgs/singular/spkg-install
-index 8caafb1699..3c34e6608a 100644
---- a/build/pkgs/singular/spkg-install
-+++ b/build/pkgs/singular/spkg-install
-@@ -2,6 +2,13 @@
- ## Singular
- ###########################################
- 
-+# Fix hardcoded paths, while making sure to only update timestamps of actually
-+# changed files (otherwise confuses make)
-+grep -rlF '/bin/rm' . | while read file
-+do
-+	sed -e 's@/bin/rm@rm@g' -i "$file"
-+done
-+
- if [ -z "$SAGE_LOCAL" ]; then
-     echo >&2 "Error: SAGE_LOCAL undefined -- exiting..."
-     echo >&2 "Maybe run 'sage -sh'?"
diff --git a/pkgs/applications/science/math/scilab-bin/default.nix b/pkgs/applications/science/math/scilab-bin/default.nix
index 154f1ba39e3c..ad0231d37b0e 100644
--- a/pkgs/applications/science/math/scilab-bin/default.nix
+++ b/pkgs/applications/science/math/scilab-bin/default.nix
@@ -21,7 +21,7 @@ stdenv.mkDerivation rec {
   inherit name;
 
   src = fetchurl {
-    url = "http://www.scilab.org/download/${ver}/scilab-${ver}.bin.linux-${architecture}.tar.gz";
+    url = "https://www.scilab.org/download/${ver}/scilab-${ver}.bin.linux-${architecture}.tar.gz";
     sha256 =
       if stdenv.system == "i686-linux" then
         "6143a95ded40411a35630a89b365875a6526cd4db1e2865ac5612929a7db937a"
diff --git a/pkgs/applications/science/math/scilab/default.nix b/pkgs/applications/science/math/scilab/default.nix
index bb52b430f8cb..6063a45a3efb 100644
--- a/pkgs/applications/science/math/scilab/default.nix
+++ b/pkgs/applications/science/math/scilab/default.nix
@@ -1,6 +1,6 @@
 {stdenv, fetchurl, lib, gfortran
 , ncurses
-, Xaw3d, withXaw3d ? false
+, withXaw3d ? false
 #, withPVMlib ? false
 , tcl, tk, withTk ? false
 , gtk2, withGtk ? false # working ?
@@ -15,7 +15,7 @@ stdenv.mkDerivation rec {
   version = "4.1.2";
   name = "scilab-${version}";
   src = fetchurl {
-    url = "http://www.scilab.org/download/${version}/${name}-src.tar.gz";
+    url = "https://www.scilab.org/download/${version}/${name}-src.tar.gz";
     sha256 = "1adk6jqlj7i3gjklvlf1j3il1nb22axnp4rvwl314an62siih0sc";
   };
 
diff --git a/pkgs/applications/science/math/scotch/default.nix b/pkgs/applications/science/math/scotch/default.nix
index 3b01c684d529..8fa020ba07c9 100644
--- a/pkgs/applications/science/math/scotch/default.nix
+++ b/pkgs/applications/science/math/scotch/default.nix
@@ -8,7 +8,7 @@ stdenv.mkDerivation rec {
   buildInputs = [ bison openmpi flex zlib ];
 
   src = fetchurl {
-    url = "http://gforge.inria.fr/frs/download.php/file/34618/${src_name}.tar.gz";
+    url = "https://gforge.inria.fr/frs/download.php/file/34618/${src_name}.tar.gz";
     sha256 = "f53f4d71a8345ba15e2dd4e102a35fd83915abf50ea73e1bf6efe1bc2b4220c7";
   };
 
diff --git a/pkgs/applications/science/math/singular/default.nix b/pkgs/applications/science/math/singular/default.nix
index cec1a831c912..98b75cd22d05 100644
--- a/pkgs/applications/science/math/singular/default.nix
+++ b/pkgs/applications/science/math/singular/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, fetchurl, gmp, bison, perl, autoconf, ncurses, readline, coreutils, pkgconfig
+{ stdenv, fetchurl, gmp, bison, perl, ncurses, readline, coreutils, pkgconfig
+, lib
 , autoreconfHook
 , file
 , flint
@@ -9,21 +10,25 @@
 }:
 
 stdenv.mkDerivation rec {
-  name = "singular-${version}${patchVersion}";
-  version = "4.1.1";
-  patchVersion = "p1";
+  name = "singular-${version}";
+  version = "4.1.1p2";
 
-  urlVersion = builtins.replaceStrings [ "." ] [ "-" ] version;
-  src = fetchurl {
-    url = "http://www.mathematik.uni-kl.de/ftp/pub/Math/Singular/SOURCES/${urlVersion}/singular-${version}${patchVersion}.tar.gz";
-    sha256 = "0wvgz7l1b7zkpmim0r3mvv4fp8xnhlbz4c7hc90rn30snlansnf1";
+  src = let
+    # singular sorts its tarballs in directories by base release (without patch version)
+    # for example 4.1.1p1 will be in the directory 4-1-1
+    baseVersion = builtins.head (lib.splitString "p" version);
+    urlVersion = builtins.replaceStrings [ "." ] [ "-" ] baseVersion;
+  in
+  fetchurl {
+    url = "http://www.mathematik.uni-kl.de/ftp/pub/Math/Singular/SOURCES/${urlVersion}/singular-${version}.tar.gz";
+    sha256 = "07x9kri8vl4galik7lr6pscq3c51n8570pyw64i7gbj0m706f7wf";
   };
 
   configureFlags = [
     "--with-ntl=${ntl}"
-  ] ++stdenv.lib.optionals enableFactory [
+  ] ++ lib.optionals enableFactory [
     "--enable-factory"
-  ] ++ stdenv.lib.optionals enableGfanlib [
+  ] ++ lib.optionals enableGfanlib [
     "--enable-gfanlib"
   ];
 
@@ -42,7 +47,7 @@ stdenv.mkDerivation rec {
     readline
     ntl
     flint
-  ] ++ stdenv.lib.optionals enableGfanlib [
+  ] ++ lib.optionals enableGfanlib [
     cddlib
   ];
   nativeBuildInputs = [
@@ -60,10 +65,12 @@ stdenv.mkDerivation rec {
       -i '{}' ';'
   '';
 
-  hardeningDisable = stdenv.lib.optional stdenv.isi686 "stackprotector";
+  hardeningDisable = lib.optional stdenv.isi686 "stackprotector";
 
   # The Makefile actually defaults to `make install` anyway
-  buildPhase = "true;";
+  buildPhase = ''
+    # do nothing
+  '';
 
   installPhase = ''
     mkdir -p "$out"
@@ -77,7 +84,7 @@ stdenv.mkDerivation rec {
   # simple test to make sure singular starts and finds its libraries
   doInstallCheck = true;
   installCheckPhase = ''
-    $out/bin/Singular -c 'LIB "freegb.lib"; exit;'
+    "$out/bin/Singular" -c 'LIB "freegb.lib"; exit;'
     if [ $? -ne 0 ]; then
         echo >&2 "Error loading the freegb library in Singular."
         exit 1
@@ -86,9 +93,10 @@ stdenv.mkDerivation rec {
 
   enableParallelBuilding = true;
 
-  meta = with stdenv.lib; {
+  meta = with lib; {
     description = "A CAS for polynomial computations";
-    maintainers = with maintainers; [ raskin ];
+    maintainers = with maintainers; [ raskin timokau ];
+    # 32 bit x86 fails with some link error: `undefined reference to `__divmoddi4@GCC_7.0.0'`
     platforms = subtractLists platforms.i686 platforms.linux;
     license = licenses.gpl3; # Or GPLv2 at your option - but not GPLv4
     homepage = http://www.singular.uni-kl.de;
diff --git a/pkgs/applications/science/math/symmetrica/default.nix b/pkgs/applications/science/math/symmetrica/default.nix
index a7212299aa63..6123fd7306d9 100644
--- a/pkgs/applications/science/math/symmetrica/default.nix
+++ b/pkgs/applications/science/math/symmetrica/default.nix
@@ -1,14 +1,15 @@
-{stdenv, fetchurl}:
+{ stdenv
+, fetchurl
+, fetchpatch
+}:
 stdenv.mkDerivation rec {
   name = "symmetrica-${version}";
   version = "2.0";
-  # or fetchFromGitHub(owner,repo,rev) or fetchgit(rev)
   src = fetchurl {
     url = "http://www.algorithm.uni-bayreuth.de/en/research/SYMMETRICA/SYM2_0_tar.gz";
     sha256 = "1qhfrbd5ybb0sinl9pad64rscr08qvlfzrzmi4p4hk61xn6phlmz";
     name = "symmetrica-2.0.tar.gz";
   };
-  buildInputs = [];
   sourceRoot = ".";
   installPhase = ''
     mkdir -p "$out"/{lib,share/doc/symmetrica,include/symmetrica}
@@ -18,6 +19,22 @@ stdenv.mkDerivation rec {
     cp *.h "$out/include/symmetrica"
     cp README *.doc "$out/share/doc/symmetrica"
   '';
+  patches = [
+      # don't show banner ("SYMMETRICA VERSION X - STARTING)
+      # it doesn't contain very much helpful information and a banner is not ideal for a library
+      (fetchpatch {
+        url = "https://git.sagemath.org/sage.git/plain/build/pkgs/symmetrica/patches/de.patch?id=07d6c37d18811e2b377a9689790a7c5e24da16ba";
+        sha256 = "0df0vqixcfpzny6dkhyj87h8aznz3xn3zfwwlj8pd10bpb90k6gb";
+      })
+
+      # use int32_t and uint32_t for type INT
+      # see https://trac.sagemath.org/ticket/13413
+      (fetchpatch {
+        name = "fix_64bit_integer_overflow.patch";
+        url = "https://git.sagemath.org/sage.git/plain/build/pkgs/symmetrica/patches/int32.patch?id=07d6c37d18811e2b377a9689790a7c5e24da16ba";
+        sha256 = "0p33c85ck4kd453z687ni4bdcqr1pqx2756j7aq11bf63vjz4cyz";
+      })
+  ];
   meta = {
     inherit version;
     description = ''A collection of routines for representation theory and combinatorics'';
diff --git a/pkgs/applications/science/misc/gephi/default.nix b/pkgs/applications/science/misc/gephi/default.nix
index 3a0bcb66813e..b81f188a91bf 100644
--- a/pkgs/applications/science/misc/gephi/default.nix
+++ b/pkgs/applications/science/misc/gephi/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, callPackage, jdk, maven, javaPackages }:
+{ stdenv, fetchFromGitHub, jdk, maven, javaPackages }:
 
 let
   version = "0.9.2";
diff --git a/pkgs/applications/science/misc/golly/beta.nix b/pkgs/applications/science/misc/golly/beta.nix
index 5b210fa4bf6b..dc768725d4fb 100644
--- a/pkgs/applications/science/misc/golly/beta.nix
+++ b/pkgs/applications/science/misc/golly/beta.nix
@@ -1,16 +1,8 @@
-{stdenv, fetchurl, fetchgit
+{stdenv, fetchgit
 , wxGTK, perl, python2, zlib, libGLU_combined, libX11
 , automake, autoconf
 }:
-let
-  s = # Generated upstream information
-  rec {
-    baseName="golly";
-    version="2.8";
-    name="${baseName}-${version}";
-    hash="0a4vn2hm7h4b47v2iwip1z3n9y8isf79v08aipl2iqms2m3p5204";
-  };
-in
+
 stdenv.mkDerivation rec {
   name = "golly-${version}";
   version = "2.8.99.2.20161122";
diff --git a/pkgs/applications/science/misc/golly/default.nix b/pkgs/applications/science/misc/golly/default.nix
index 8ecbf656a821..0f0b44ccfa5e 100644
--- a/pkgs/applications/science/misc/golly/default.nix
+++ b/pkgs/applications/science/misc/golly/default.nix
@@ -1,11 +1,11 @@
 {stdenv, fetchurl, wxGTK, perl, python2, zlib, libGLU_combined, libX11}:
 stdenv.mkDerivation rec {
   baseName="golly";
-  version = "3.1";
+  version = "3.2";
   name="${baseName}-${version}";
 
   src = fetchurl {
-    sha256 = "0dn74k3rylhx023n047lz4z6qrqijfcxi0b6jryqklhmm2n532f7";
+    sha256 = "0cg9mbwmf4q6qxhqlnzrxh9y047banxdb8pd3hgj3smmja2zf0jd";
     url="mirror://sourceforge/project/golly/golly/golly-${version}/golly-${version}-src.tar.gz";
   };
 
diff --git a/pkgs/applications/science/misc/openmvg/default.nix b/pkgs/applications/science/misc/openmvg/default.nix
index b139f30b3d18..c3e1cd60bf0b 100644
--- a/pkgs/applications/science/misc/openmvg/default.nix
+++ b/pkgs/applications/science/misc/openmvg/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchgit, pkgconfig, cmake
+{ stdenv, fetchgit, pkgconfig, cmake
 , libjpeg ? null
 , zlib ? null
 , libpng ? null
@@ -8,7 +8,7 @@
 , enableDocs ? false }:
 
 stdenv.mkDerivation rec {
-  version = "1.1";
+  version = "1.3";
   name = "openmvg-${version}";
 
   src = fetchgit {
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
 
     # Tag v1.1
     rev = "refs/tags/v${version}";
-    sha256 = "1di9i7yxnkdvl8lhflynmqw62gaxwv00r1sd7nzzs9qn63g0af0f";
+    sha256 = "1cf1gbcl8zvxp4rr6f6vaxwcg0yzc4xban2b5p9zy1m4k1f81zyb";
     fetchSubmodules = true;
   };
 
diff --git a/pkgs/applications/science/misc/openmvs/default.nix b/pkgs/applications/science/misc/openmvs/default.nix
index ef539ac9c8df..b665d5f4f687 100644
--- a/pkgs/applications/science/misc/openmvs/default.nix
+++ b/pkgs/applications/science/misc/openmvs/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, pkgconfig, cmake
+{ stdenv, fetchFromGitHub, pkgconfig, cmake
 , eigen, opencv, ceres-solver, cgal, boost, vcg
 , gmp, mpfr, glog, google-gflags, libjpeg_turbo }:
 
@@ -24,10 +24,28 @@ stdenv.mkDerivation rec {
       "-DBUILD_STATIC_RUNTIME=ON"
       "-DINSTALL_BIN_DIR=$out/bin"
       "-DVCG_DIR=${vcg}"
+      "-DCGAL_ROOT=${cgal}/lib/cmake/CGAL"
       "-DCERES_DIR=${ceres-solver}/lib/cmake/Ceres/"
     )
   '';
+  
+  postFixup = ''
+    rp=$(patchelf --print-rpath $out/bin/DensifyPointCloud)
+    patchelf --set-rpath $rp:$out/lib/OpenMVS $out/bin/DensifyPointCloud
 
+    rp=$(patchelf --print-rpath $out/bin/InterfaceVisualSFM)
+    patchelf --set-rpath $rp:$out/lib/OpenMVS $out/bin/InterfaceVisualSFM
+
+    rp=$(patchelf --print-rpath $out/bin/ReconstructMesh)
+    patchelf --set-rpath $rp:$out/lib/OpenMVS $out/bin/ReconstructMesh
+
+    rp=$(patchelf --print-rpath $out/bin/RefineMesh)
+    patchelf --set-rpath $rp:$out/lib/OpenMVS $out/bin/RefineMesh
+
+    rp=$(patchelf --print-rpath $out/bin/TextureMesh)
+    patchelf --set-rpath $rp:$out/lib/OpenMVS $out/bin/TextureMesh
+  '';
+  
   cmakeDir = "./";
 
   dontUseCmakeBuildDir = true;
diff --git a/pkgs/applications/science/misc/root/default.nix b/pkgs/applications/science/misc/root/default.nix
index 00ca1d2c7abe..e966e798ae6d 100644
--- a/pkgs/applications/science/misc/root/default.nix
+++ b/pkgs/applications/science/misc/root/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, fetchpatch, cmake, pcre, pkgconfig, python2
+{ stdenv, fetchurl, cmake, pcre, pkgconfig, python2
 , libX11, libXpm, libXft, libXext, libGLU_combined, zlib, libxml2, lz4, lzma, gsl, xxHash
 , Cocoa, OpenGL, noSplash ? false }:
 
diff --git a/pkgs/applications/science/molecular-dynamics/gromacs/default.nix b/pkgs/applications/science/molecular-dynamics/gromacs/default.nix
index 11087846e05c..295e726c679d 100644
--- a/pkgs/applications/science/molecular-dynamics/gromacs/default.nix
+++ b/pkgs/applications/science/molecular-dynamics/gromacs/default.nix
@@ -8,11 +8,11 @@
 
 
 stdenv.mkDerivation {
-  name = "gromacs-2018.1";
+  name = "gromacs-2018.2";
 
   src = fetchurl {
-    url = "ftp://ftp.gromacs.org/pub/gromacs/gromacs-2018.1.tar.gz";
-    sha256 = "1k85sz0b9rmq00g58v22y9v671gsa512sjivx3n3yclr0hs36dad";
+    url = "ftp://ftp.gromacs.org/pub/gromacs/gromacs-2018.2.tar.gz";
+    sha256 = "0mvqsg2j4h529a0vvvgpa4cb3p8zan18zcdlmx1na2si1h9fipab";
   };
 
   buildInputs = [cmake fftw]
diff --git a/pkgs/applications/science/physics/sacrifice/default.nix b/pkgs/applications/science/physics/sacrifice/default.nix
index 6c25f720ebf9..64b88dcc3d4f 100644
--- a/pkgs/applications/science/physics/sacrifice/default.nix
+++ b/pkgs/applications/science/physics/sacrifice/default.nix
@@ -5,7 +5,7 @@ stdenv.mkDerivation rec {
   version = "1.0.0";
 
   src = fetchurl {
-    url = "http://www.hepforge.org/archive/agile/Sacrifice-1.0.0.tar.gz";
+    url = "https://www.hepforge.org/archive/agile/Sacrifice-1.0.0.tar.gz";
     sha256 = "10bvpq63kmszy1habydwncm0j1dgvam0fkrmvkgbkvf804dcjp6g";
   };
 
diff --git a/pkgs/applications/science/physics/sherpa/default.nix b/pkgs/applications/science/physics/sherpa/default.nix
index 340428e8e777..66a9bebacf0d 100644
--- a/pkgs/applications/science/physics/sherpa/default.nix
+++ b/pkgs/applications/science/physics/sherpa/default.nix
@@ -5,7 +5,7 @@ stdenv.mkDerivation rec {
   version = "2.2.5";
 
   src = fetchurl {
-    url = "http://www.hepforge.org/archive/sherpa/SHERPA-MC-${version}.tar.gz";
+    url = "https://www.hepforge.org/archive/sherpa/SHERPA-MC-${version}.tar.gz";
     sha256 = "0rv14j8gvjjr3darb0wcradlmsnyq915jz7v2yybrjzqfbsr3zb5";
   };
 
diff --git a/pkgs/applications/science/programming/scyther/default.nix b/pkgs/applications/science/programming/scyther/default.nix
index beef26c6032a..e0993a580193 100644
--- a/pkgs/applications/science/programming/scyther/default.nix
+++ b/pkgs/applications/science/programming/scyther/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, buildEnv, callPackage_i686, fetchFromGitHub, python27Packages, graphviz
+{ stdenv, lib, buildEnv, pkgsi686Linux, fetchFromGitHub, python27Packages, graphviz
 , includeGUI ? true
 , includeProtocols ? true
 }:
@@ -20,7 +20,7 @@ let
     platforms = platforms.linux;
   };
 
-  cli = callPackage_i686 ./cli.nix {
+  cli = pkgsi686Linux.callPackage ./cli.nix {
     inherit version src meta;
   };
 
@@ -47,7 +47,7 @@ let
       python27Packages.wxPython
       graphviz
     ];
- 
+
     installPhase = ''
       mkdir -p "$out"/gui "$out"/bin
       cp -r gui/* "$out"/gui
diff --git a/pkgs/applications/science/robotics/gazebo/default.nix b/pkgs/applications/science/robotics/gazebo/default.nix
index 3edf75a4bffe..98ccd92c4ee5 100644
--- a/pkgs/applications/science/robotics/gazebo/default.nix
+++ b/pkgs/applications/science/robotics/gazebo/default.nix
@@ -1,11 +1,12 @@
 { stdenv, fetchurl, cmake, pkgconfig, boost, protobuf, freeimage
   , boost-build, boost_process
   , xorg_sys_opengl, tbb, ogre, tinyxml-2
-  , libtar, glxinfo,  libusb, libxslt, ruby, ignition
+  , libtar, glxinfo,  libusb, libxslt, ignition
   , pythonPackages, utillinux
 
   # these deps are hidden; cmake doesn't catch them
-  , gazeboSimulator, sdformat ? gazeboSimulator.sdformat, curl, tinyxml, qt4, x11
+  , gazeboSimulator, sdformat ? gazeboSimulator.sdformat, curl, tinyxml, qt4
+  , xlibsWrapper
   , withIgnitionTransport ? true
   , libav, withLibAvSupport ? true
   , openal, withAudioSupport ? false
@@ -26,7 +27,7 @@ stdenv.mkDerivation rec {
   name = "gazebo-${version}";
 
   src = fetchurl {
-    url = "http://osrf-distributions.s3.amazonaws.com/gazebo/releases/${name}.tar.bz2";
+    url = "https://osrf-distributions.s3.amazonaws.com/gazebo/releases/${name}.tar.bz2";
     sha256 = src-sha256;
   };
 
@@ -60,7 +61,7 @@ stdenv.mkDerivation rec {
     # TODO: add these hidden deps to cmake configuration & submit upstream
     curl
     tinyxml
-    x11
+    xlibsWrapper
     qt4
   ] ++ optional stdenv.isLinux utillinux # on Linux needs uuid/uuid.h
     ++ optional withDocs doxygen
diff --git a/pkgs/applications/science/robotics/qgroundcontrol/default.nix b/pkgs/applications/science/robotics/qgroundcontrol/default.nix
index 95462c02eb5b..ce3317da9f8f 100644
--- a/pkgs/applications/science/robotics/qgroundcontrol/default.nix
+++ b/pkgs/applications/science/robotics/qgroundcontrol/default.nix
@@ -1,8 +1,8 @@
-{ stdenv, fetchgit, git,  espeak, SDL2, udev, doxygen, cmake
+{ stdenv, fetchgit, git,  SDL2, udev, doxygen
 , qtbase, qtlocation, qtserialport, qtdeclarative, qtconnectivity, qtxmlpatterns
 , qtsvg, qtquick1, qtquickcontrols, qtgraphicaleffects, qmake, qtspeech
-, makeWrapper, lndir
-, gst_all_1, qt-gstreamer1, pkgconfig, glibc
+, makeWrapper
+, gst_all_1, pkgconfig
 }:
 
 stdenv.mkDerivation rec {
diff --git a/pkgs/applications/science/robotics/yarp/default.nix b/pkgs/applications/science/robotics/yarp/default.nix
index 98576cfd0917..507481cddab2 100644
--- a/pkgs/applications/science/robotics/yarp/default.nix
+++ b/pkgs/applications/science/robotics/yarp/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, pkgconfig, cmake, ace
+{ stdenv, fetchFromGitHub, cmake, ace
 }:
 
 stdenv.mkDerivation rec {
diff --git a/pkgs/applications/science/spyder/default.nix b/pkgs/applications/science/spyder/default.nix
index 4411d78cec21..0564f3de1b78 100644
--- a/pkgs/applications/science/spyder/default.nix
+++ b/pkgs/applications/science/spyder/default.nix
@@ -1,7 +1,7 @@
-{ stdenv, fetchPypi, unzip, buildPythonApplication, makeDesktopItem
+{ stdenv, fetchPypi, buildPythonApplication, makeDesktopItem
 # mandatory
 , numpydoc, qtconsole, qtawesome, jedi, pycodestyle, psutil
-, pyflakes, rope, sphinx, nbconvert, mccabe, pyopengl, cloudpickle
+, pyflakes, rope, nbconvert, mccabe, pyopengl, cloudpickle
 # optional
 , numpy ? null, scipy ? null, matplotlib ? null
 # optional