From 68233970a2de5d1b1e2c23cd698d1b576326ca7f Mon Sep 17 00:00:00 2001
From: Timo Kaufmann <timokau@zoho.com>
Date: Tue, 9 Apr 2019 11:40:00 +0200
Subject: [PATCH 1/4] gmpy2: 2.0.8 -> 2.1a4

---
 .../python-modules/gmpy2/default.nix          | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/pkgs/development/python-modules/gmpy2/default.nix b/pkgs/development/python-modules/gmpy2/default.nix
index a03188bb8f2..edca1d30d8e 100644
--- a/pkgs/development/python-modules/gmpy2/default.nix
+++ b/pkgs/development/python-modules/gmpy2/default.nix
@@ -1,8 +1,15 @@
-{ stdenv, buildPythonPackage, fetchurl, isPyPy, gmp, mpfr, libmpc } :
+{ stdenv
+, buildPythonPackage
+, fetchFromGitHub
+, isPyPy
+, gmp
+, mpfr
+, libmpc
+}:
 
 let
   pname = "gmpy2";
-  version = "2.0.8";
+  version = "2.1a4";
 in
 
 buildPythonPackage {
@@ -10,9 +17,11 @@ buildPythonPackage {
 
   disabled = isPyPy;
 
-  src = fetchurl {
-    url = "mirror://pypi/g/gmpy2/${pname}-${version}.zip";
-    sha256 = "0grx6zmi99iaslm07w6c2aqpnmbkgrxcqjrqpfq223xri0r3w8yx";
+  src = fetchFromGitHub {
+    owner = "aleaxit";
+    repo = "gmpy";
+    rev = "gmpy2-${version}";
+    sha256 = "1wg4w4q2l7n26ksrdh4rwqmifgfm32n7x29cgdvmmbv5lmilb5hz";
   };
 
   buildInputs = [ gmp mpfr libmpc ];

From 1f643b9c49154d55e56adb26a684303294e504e9 Mon Sep 17 00:00:00 2001
From: Timo Kaufmann <timokau@zoho.com>
Date: Tue, 5 Mar 2019 21:59:18 +0100
Subject: [PATCH 2/4] python.pkgs.pplpy: init at 0.8.4

This is a new dependency for the upcoming sage version.
---
 .../python-modules/pplpy/default.nix          | 64 +++++++++++++++++++
 pkgs/top-level/python-packages.nix            |  2 +
 2 files changed, 66 insertions(+)
 create mode 100644 pkgs/development/python-modules/pplpy/default.nix

diff --git a/pkgs/development/python-modules/pplpy/default.nix b/pkgs/development/python-modules/pplpy/default.nix
new file mode 100644
index 00000000000..6f118a51c87
--- /dev/null
+++ b/pkgs/development/python-modules/pplpy/default.nix
@@ -0,0 +1,64 @@
+{ lib
+, python
+, fetchPypi
+, buildPythonPackage
+, gmp
+, mpfr
+, libmpc
+, ppl
+, pari
+, cython
+, cysignals
+, gmpy2
+, sphinx
+}:
+
+buildPythonPackage rec {
+  pname = "pplpy";
+  version = "0.8.4";
+
+  src = fetchPypi {
+    inherit pname version;
+    sha256 = "0dk8l5r3f2jbkkasddvxwvhlq35pjsiirh801lrapv8lb16r2qmr";
+  };
+
+  buildInputs = [
+    gmp
+    mpfr
+    libmpc
+    ppl
+  ];
+
+  nativeBuildInputs = [
+    sphinx # docbuild, called by make
+  ];
+
+  propagatedBuildInputs = [
+    cython
+    cysignals
+    gmpy2
+  ];
+
+  outputs = [ "out" "doc" ];
+
+  postBuild = ''
+    # Find the build result in order to put it into PYTHONPATH. The doc
+    # build needs to import pplpy.
+    build_result="$PWD/$( find build/ -type d -name 'lib.*' | head -n1 )"
+
+    echo "Building documentation"
+    PYTHONPATH="$build_result:$PYTHONPATH" make -C docs html
+  '';
+
+  postInstall = ''
+    mkdir -p "$doc/share/doc"
+    mv docs/build/html "$doc/share/doc/pplpy"
+  '';
+
+  meta = with lib; {
+    description = "A Python wrapper for ppl";
+    homepage = https://gitlab.com/videlec/pplpy;
+    maintainers = with maintainers; [ timokau ];
+    license = licenses.gpl3;
+  };
+}
diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix
index 1572eb7f654..eb53f2dd5e0 100644
--- a/pkgs/top-level/python-packages.nix
+++ b/pkgs/top-level/python-packages.nix
@@ -585,6 +585,8 @@ in {
 
   poetry = callPackage ../development/python-modules/poetry { };
 
+  pplpy = callPackage ../development/python-modules/pplpy { };
+
   pprintpp = callPackage ../development/python-modules/pprintpp { };
 
   progress = callPackage ../development/python-modules/progress { };

From 930fa9b92ed8fcd8e976eeb3c519b6ce7c2a96bc Mon Sep 17 00:00:00 2001
From: Timo Kaufmann <timokau@zoho.com>
Date: Tue, 5 Mar 2019 21:55:51 +0100
Subject: [PATCH 3/4] python.pkgs.cysignals: propagate pari

This didn't cause issues earlier since all packages that depend on
cysignals have a pari dependency anyways.
---
 pkgs/development/python-modules/cysignals/default.nix | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/pkgs/development/python-modules/cysignals/default.nix b/pkgs/development/python-modules/cysignals/default.nix
index c1f85ce75ac..1cd55acdbeb 100644
--- a/pkgs/development/python-modules/cysignals/default.nix
+++ b/pkgs/development/python-modules/cysignals/default.nix
@@ -31,12 +31,13 @@ buildPythonPackage rec {
     export PATH="$out/bin:$PATH"
   '';
 
-  buildInputs = lib.optionals pariSupport [
-    pari
-  ];
-
   propagatedBuildInputs = [
     cython
+  ] ++ lib.optionals pariSupport [
+    # When cysignals is built with pari, including cysignals into the
+    # buildInputs of another python package will cause cython to link against
+    # pari.
+    pari
   ];
 
   enableParallelBuilding = true;

From 49b8efcb841fd8f4896f1ac83e7ee672e82a18cf Mon Sep 17 00:00:00 2001
From: Timo Kaufmann <timokau@zoho.com>
Date: Sun, 27 Jan 2019 11:28:39 +0100
Subject: [PATCH 4/4] sage: 8.6 -> 8.7

https://groups.google.com/forum/#!topic/sage-release/3m8PV9JkFro
---
 .../science/math/sage/env-locations.nix       |  1 +
 .../patches/do-not-test-find-library.patch    | 95 +++++++++++++++++++
 .../do-not-test-package-manifests.patch       | 26 +++++
 .../math/sage/patches/giac-1.5.0.patch        | 14 ---
 .../sage/patches/ignore-pip-deprecation.patch | 22 -----
 .../patches/revert-sphinx-always-fork.patch   | 71 --------------
 .../science/math/sage/sage-src.nix            | 89 ++++++-----------
 .../science/math/sage/sagedoc.nix             |  3 +
 .../science/math/sage/sagelib.nix             |  4 +
 9 files changed, 158 insertions(+), 167 deletions(-)
 create mode 100644 pkgs/applications/science/math/sage/patches/do-not-test-find-library.patch
 create mode 100644 pkgs/applications/science/math/sage/patches/do-not-test-package-manifests.patch
 delete mode 100644 pkgs/applications/science/math/sage/patches/giac-1.5.0.patch
 delete mode 100644 pkgs/applications/science/math/sage/patches/ignore-pip-deprecation.patch
 delete mode 100644 pkgs/applications/science/math/sage/patches/revert-sphinx-always-fork.patch

diff --git a/pkgs/applications/science/math/sage/env-locations.nix b/pkgs/applications/science/math/sage/env-locations.nix
index 8354629cab5..bda2d23b9e5 100644
--- a/pkgs/applications/science/math/sage/env-locations.nix
+++ b/pkgs/applications/science/math/sage/env-locations.nix
@@ -28,6 +28,7 @@ writeTextFile rec {
     export GPDOCDIR="${pari}/share/pari/doc"
     export SINGULARPATH='${singular}/share/singular'
     export SINGULAR_SO='${singular}/lib/libSingular.so'
+    export GAP_SO='${gap}/lib/libgap.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}"
diff --git a/pkgs/applications/science/math/sage/patches/do-not-test-find-library.patch b/pkgs/applications/science/math/sage/patches/do-not-test-find-library.patch
new file mode 100644
index 00000000000..3f844e1b1ef
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/do-not-test-find-library.patch
@@ -0,0 +1,95 @@
+diff --git a/src/sage/env.py b/src/sage/env.py
+index 061b94f3f1..67cd091540 100644
+--- a/src/sage/env.py
++++ b/src/sage/env.py
+@@ -189,88 +189,13 @@ var('MAXIMA_FAS')
+ var('SAGE_BANNER', '')
+ var('SAGE_IMPORTALL', 'yes')
+ 
+-
+-def _get_shared_lib_filename(libname, *additional_libnames):
+-    """
+-    Return the full path to a shared library file installed in the standard
+-    location for the system within the ``LIBDIR`` prefix (or
+-    ``$SAGE_LOCAL/lib`` in the case of manual build of Sage).
+-
+-    This can also be passed more than one library name (e.g. for cases where
+-    some library may have multiple names depending on the platform) in which
+-    case the first one found is returned.
+-
+-    This supports most *NIX variants (in which ``lib<libname>.so`` is found
+-    under ``$SAGE_LOCAL/lib``), macOS (same, but with the ``.dylib``
+-    extension), and Cygwin (under ``$SAGE_LOCAL/bin/cyg<libname>.dll``,
+-    or ``$SAGE_LOCAL/bin/cyg<libname>-*.dll`` for versioned DLLs).
+-
+-    For distributions like Debian that use a multiarch layout, we also try the
+-    multiarch lib paths (i.e. ``/usr/lib/<arch>/``).
+-
+-    Returns ``None`` if the file does not exist.
+-
+-    EXAMPLES::
+-
+-        sage: import sys
+-        sage: from fnmatch import fnmatch
+-        sage: from sage.env import _get_shared_lib_filename
+-        sage: lib_filename = _get_shared_lib_filename("Singular",
+-        ....:                                         "singular-Singular")
+-        sage: if sys.platform == 'cygwin':
+-        ....:     pattern = "*/cygSingular-*.dll"
+-        ....: elif sys.platform == 'darwin':
+-        ....:     pattern = "*/libSingular.dylib"
+-        ....: else:
+-        ....:     pattern = "*/lib*Singular.so"
+-        sage: fnmatch(lib_filename, pattern)
+-        True
+-        sage: _get_shared_lib_filename("an_absurd_lib") is None
+-        True
+-    """
+-
+-    for libname in (libname,) + additional_libnames:
+-        if sys.platform == 'cygwin':
+-            bindir = sysconfig.get_config_var('BINDIR')
+-            pats = ['cyg{}.dll'.format(libname), 'cyg{}-*.dll'.format(libname)]
+-            filenames = []
+-            for pat in pats:
+-                filenames += glob.glob(os.path.join(bindir, pat))
+-
+-            # Note: This is not very robust, since if there are multi DLL
+-            # versions for the same library this just selects one more or less
+-            # at arbitrary.  However, practically speaking, on Cygwin, there
+-            # will only ever be one version
+-            if filenames:
+-                return filenames[-1]
+-        else:
+-            if sys.platform == 'darwin':
+-                ext = 'dylib'
+-            else:
+-                ext = 'so'
+-
+-            libdirs = [sysconfig.get_config_var('LIBDIR')]
+-            multilib = sysconfig.get_config_var('MULTILIB')
+-            if multilib:
+-                libdirs.insert(0, os.path.join(libdirs[0], multilib))
+-
+-            for libdir in libdirs:
+-                basename = 'lib{}.{}'.format(libname, ext)
+-                filename = os.path.join(libdir, basename)
+-                if os.path.exists(filename):
+-                    return filename
+-
+-    # Just return None if no files were found
+-    return None
+-
+-
+ # locate singular shared object
+ # On Debian it's libsingular-Singular so try that as well
+-SINGULAR_SO = _get_shared_lib_filename('Singular', 'singular-Singular')
++SINGULAR_SO = '/default'
+ var('SINGULAR_SO', SINGULAR_SO)
+ 
+ # locate libgap shared object
+-GAP_SO= _get_shared_lib_filename('gap','')
++GAP_SO= '/default'
+ var('GAP_SO', GAP_SO)
+ 
+ # post process
diff --git a/pkgs/applications/science/math/sage/patches/do-not-test-package-manifests.patch b/pkgs/applications/science/math/sage/patches/do-not-test-package-manifests.patch
new file mode 100644
index 00000000000..9e3f0f5ae88
--- /dev/null
+++ b/pkgs/applications/science/math/sage/patches/do-not-test-package-manifests.patch
@@ -0,0 +1,26 @@
+diff --git a/src/sage/misc/package.py b/src/sage/misc/package.py
+index 3bca15d53b..7cf04ff8d1 100644
+--- a/src/sage/misc/package.py
++++ b/src/sage/misc/package.py
+@@ -478,16 +478,16 @@ def package_manifest(package):
+ 
+     EXAMPLES::
+ 
+-        sage: from sage.misc.package import package_manifest
+-        sage: sagetex_manifest = package_manifest('sagetex')
+-        sage: sagetex_manifest['package_name'] == 'sagetex'
++        sage: from sage.misc.package import package_manifest # optional - buildsystem
++        sage: sagetex_manifest = package_manifest('sagetex') # optional - buildsystem
++        sage: sagetex_manifest['package_name'] == 'sagetex' # optional - buildsystem
+         True
+-        sage: 'files' in sagetex_manifest
++        sage: 'files' in sagetex_manifest # optional - buildsystem
+         True
+ 
+     Test a nonexistent package::
+ 
+-        sage: package_manifest('dummy-package')
++        sage: package_manifest('dummy-package') # optional - buildsystem
+         Traceback (most recent call last):
+         ...
+         KeyError: 'dummy-package'
diff --git a/pkgs/applications/science/math/sage/patches/giac-1.5.0.patch b/pkgs/applications/science/math/sage/patches/giac-1.5.0.patch
deleted file mode 100644
index 58090b241d2..00000000000
--- a/pkgs/applications/science/math/sage/patches/giac-1.5.0.patch
+++ /dev/null
@@ -1,14 +0,0 @@
---- a/src/sage/interfaces/giac.py	2018-12-08 22:11:56.285500644 +0100
-+++ b/src/sage/interfaces/giac.py	2018-12-08 22:11:56.285500644 +0100
-@@ -617,10 +617,7 @@
-             '4\n3'
-             sage: s='g(x):={\nx+1;\nx+2;\n}'
-             sage: giac(s)
--            (x)->{
--            x+1;
--            x+2;
--            }
-+            (x)->[x+1,x+2]
-             sage: giac.g(5)
-             7
-         """
diff --git a/pkgs/applications/science/math/sage/patches/ignore-pip-deprecation.patch b/pkgs/applications/science/math/sage/patches/ignore-pip-deprecation.patch
deleted file mode 100644
index 95b377dc955..00000000000
--- a/pkgs/applications/science/math/sage/patches/ignore-pip-deprecation.patch
+++ /dev/null
@@ -1,22 +0,0 @@
-diff --git a/src/sage/misc/package.py b/src/sage/misc/package.py
-index 689e5a23b9..4e16fe3a8d 100644
---- a/src/sage/misc/package.py
-+++ b/src/sage/misc/package.py
-@@ -142,9 +142,14 @@ def pip_installed_packages():
-         sage: d['beautifulsoup']   # optional - beautifulsoup
-         u'...'
-     """
--    proc = subprocess.Popen(["pip", "list", "--no-index", "--format", "json"], stdout=subprocess.PIPE)
--    stdout = proc.communicate()[0].decode()
--    return {package['name'].lower():package['version'] for package in json.loads(stdout)}
-+    with open(os.devnull, 'w')  as devnull:
-+        proc = subprocess.Popen(
-+            ["pip", "list", "--no-index", "--format", "json"],
-+            stdout=subprocess.PIPE,
-+            stderr=devnull,
-+        )
-+        stdout = proc.communicate()[0].decode()
-+        return {package['name'].lower():package['version'] for package in json.loads(stdout)}
- 
- def list_packages(*pkg_types, **opts):
-     r"""
diff --git a/pkgs/applications/science/math/sage/patches/revert-sphinx-always-fork.patch b/pkgs/applications/science/math/sage/patches/revert-sphinx-always-fork.patch
deleted file mode 100644
index 64dd6fd9377..00000000000
--- a/pkgs/applications/science/math/sage/patches/revert-sphinx-always-fork.patch
+++ /dev/null
@@ -1,71 +0,0 @@
-commit f1c59929c3c180ac283334c2b3c901ac8c82f6b1
-Author: Timo Kaufmann <timokau@zoho.com>
-Date:   Sat Oct 20 20:07:41 2018 +0200
-
-    Revert "Something related to the sphinxbuild seems to be leaking memory"
-    
-    This reverts commit 7d85dc796c58c3de57401bc22d3587b94e205091.
-
-diff --git a/src/sage_setup/docbuild/__init__.py b/src/sage_setup/docbuild/__init__.py
-index 0b24b1a60b..084c3f89d7 100644
---- a/src/sage_setup/docbuild/__init__.py
-+++ b/src/sage_setup/docbuild/__init__.py
-@@ -265,29 +265,35 @@ class DocBuilder(object):
-     # import the customized builder for object.inv files
-     inventory = builder_helper('inventory')
- 
--def build_many(target, args):
--    # Pool() uses an actual fork() to run each new instance. This is important
--    # for performance reasons, i.e., don't use a forkserver when it becomes
--    # available with Python 3: Here, sage is already initialized which is quite
--    # costly, with a forkserver we would have to reinitialize it for every
--    # document we build. At the same time, don't serialize this by taking the
--    # pool (and thus the call to fork()) out completely: The call to Sphinx
--    # leaks memory, so we need to build each document in its own process to
--    # control the RAM usage.
--    from multiprocessing import Pool
--    pool = Pool(NUM_THREADS, maxtasksperchild=1)
--    # map_async handles KeyboardInterrupt correctly. Plain map and
--    # apply_async does not, so don't use it.
--    x = pool.map_async(target, args, 1)
--    try:
--        ret = x.get(99999)
--        pool.close()
--        pool.join()
--    except Exception:
--        pool.terminate()
--        if ABORT_ON_ERROR:
--            raise
--    return ret
-+if NUM_THREADS > 1:
-+    def build_many(target, args):
-+        from multiprocessing import Pool
-+        pool = Pool(NUM_THREADS, maxtasksperchild=1)
-+        # map_async handles KeyboardInterrupt correctly. Plain map and
-+        # apply_async does not, so don't use it.
-+        x = pool.map_async(target, args, 1)
-+        try:
-+            ret = x.get(99999)
-+            pool.close()
-+            pool.join()
-+        except Exception:
-+            pool.terminate()
-+            if ABORT_ON_ERROR:
-+                raise
-+        return ret
-+else:
-+    def build_many(target, args):
-+        results = []
-+
-+        for arg in args:
-+            try:
-+                results.append(target(arg))
-+            except Exception:
-+                if ABORT_ON_ERROR:
-+                    raise
-+
-+        return results
-+
- 
- ##########################################
- #      Parallel Building Ref Manual      #
diff --git a/pkgs/applications/science/math/sage/sage-src.nix b/pkgs/applications/science/math/sage/sage-src.nix
index e14d24cf70e..f2d65b68a33 100644
--- a/pkgs/applications/science/math/sage/sage-src.nix
+++ b/pkgs/applications/science/math/sage/sage-src.nix
@@ -10,14 +10,14 @@
 # all get the same sources with the same patches applied.
 
 stdenv.mkDerivation rec {
-  version = "8.6";
-  name = "sage-src-${version}";
+  version = "8.7";
+  pname = "sage-src";
 
   src = fetchFromGitHub {
     owner = "sagemath";
     repo = "sage";
     rev = version;
-    sha256 = "1vs3pbgbqpg0qnwr018bqsdmm7crgjp310cx8zwh7za3mv1cw5j3";
+    sha256 = "05vvrd6syh0hlmrk6kzjrwd0hpmvxp8vr8p3mkjb0jh5p2kjdd27";
   };
 
   # Patches needed because of particularities of nix or the way this is packaged.
@@ -37,12 +37,6 @@ stdenv.mkDerivation rec {
     # https://github.com/python/cpython/pull/7476
     ./patches/python-5755-hotpatch.patch
 
-    # Revert the commit that made the sphinx build fork even in the single thread
-    # case. For some yet unknown reason, that breaks the docbuild on nix and archlinux.
-    # See https://groups.google.com/forum/#!msg/sage-packaging/VU4h8IWGFLA/mrmCMocYBwAJ.
-    # https://trac.sagemath.org/ticket/26608
-    ./patches/revert-sphinx-always-fork.patch
-
     # Make sure py2/py3 tests are only run when their expected context (all "sage"
     # tests) are also run. That is necessary to test dochtml individually. See
     # https://trac.sagemath.org/ticket/26110 for an upstream discussion.
@@ -59,6 +53,21 @@ stdenv.mkDerivation rec {
       sha256 = "07p9i0fwjgapmfvmi436yn6v60p8pvmxqjc93wsssqgh5kd8qw3n";
       stripLen = 1;
     })
+
+    # Part of the build system. Should become unnecessary with sage 8.8.
+    # Upstream discussion here: https://trac.sagemath.org/ticket/27124#comment:33
+    ./patches/do-not-test-package-manifests.patch
+
+    # Not necessary since library location is set explicitly
+    # https://trac.sagemath.org/ticket/27660#ticket
+    ./patches/do-not-test-find-library.patch
+
+    # https://trac.sagemath.org/ticket/27697#ticket
+    (fetchpatch {
+      name = "pplpy-doc-location-configurable.patch";
+      url = "https://git.sagemath.org/sage.git/patch/?h=c4d966e7cb0c7b87c55d52dc6f46518433a2a0a2";
+      sha256 = "0pqbbsx8mriwny422s9mp3z5d095cnam32sm62q4mxk8g8jb9vm9";
+    })
   ];
 
   # Since sage unfortunately does not release bugfix releases, packagers must
@@ -72,12 +81,6 @@ stdenv.mkDerivation rec {
       url = "https://git.sagemath.org/sage.git/patch?id2=10407524b18659e14e184114b61c043fb816f3c2&id=c9b0cc9d0b8748ab85e568f8f57f316c5e8cbe54";
       sha256 = "0wgp7yvn9sm1ynlhcr4l0hzmvr2n28llg4xc01p6k1zz4im64c17";
     })
-    # https://trac.sagemath.org/ticket/27224
-    (fetchpatch {
-      name = "sig_on_in_matrix_misc.patch";
-      url = "https://git.sagemath.org/sage.git/patch?id=85d25bf2eb73f7d3c6de4ee6222b0c399be43b07";
-      sha256 = "1ciwhh57pnb9b4i8m3vb07wfsibsay5sg0jp5giq1pzc5zc79a4p";
-    })
   ];
 
   # Patches needed because of package updates. We could just pin the versions of
@@ -110,52 +113,12 @@ stdenv.mkDerivation rec {
       stripLen = 1;
     })
 
-    # https://trac.sagemath.org/ticket/26315
-    ./patches/giac-1.5.0.patch
-
-    # https://trac.sagemath.org/ticket/26442
-    (fetchSageDiff {
-      name = "cypari2-2.0.3.patch";
-      base = "8.6.rc1";
-      rev = "cd62d45bcef93fb4f7ed62609a46135e6de07051";
-      sha256 = "08l2b9w0rn1zrha6188j72f7737xs126gkgmydjd31baa6367np2";
-    })
-
-    # https://trac.sagemath.org/ticket/26949
-    (fetchpatch {
-      name = "sphinx-1.8.3-dependency.patch";
-      url = "https://git.sagemath.org/sage.git/patch?id=d305eda0fedc73fdbe0447b5d6d2b520b8d112c4";
-      sha256 = "1x3q5j8lq35vlj893gj5gq9fhzs60szm9r9rx6ri79yiy9apabph";
-    })
     # https://trac.sagemath.org/ticket/26451
-    (fetchpatch {
-      name = "sphinx-1.8.3.patch";
-      url = "https://git.sagemath.org/sage.git/patch?id2=0cb494282d7b4cea50aba7f4d100e7932a4c00b1&id=62b989d5ee1d9646db85ea56053cd22e9ffde5ab";
-      sha256 = "1n5c61mvhalcr2wbp66wzsynwwk59aakvx3xqa5zw9nlkx3rd0h1";
-    })
-
-    # https://trac.sagemath.org/ticket/27061
-    (fetchpatch {
-      name = "numpy-1.16-inline-fortran.patch";
-      url = "https://git.sagemath.org/sage.git/patch?id=a05b6b038e1571ab15464e98f76d1927c0c3fd12";
-      sha256 = "05yq97pq84xi60wb1p9skrad5h5x770gq98ll4frr7hvvmlwsf58";
-    })
-
-    # https://trac.sagemath.org/ticket/27405
-    ./patches/ignore-pip-deprecation.patch
-
-    # https://trac.sagemath.org/ticket/27360
-    (fetchpatch {
-      name = "eclib-20190226.patch";
-      url = "https://git.sagemath.org/sage.git/patch/?id=f570e3a7fc2965764b84c04ce301a88ded2c42df";
-      sha256 = "0l5c4giixkn15v2a06sfzq5mkxila6l67zkjbacirwprrlpcnmmp";
-    })
-
-    # https://trac.sagemath.org/ticket/27420
-    (fetchpatch {
-      name = "cypari-2.1.patch";
-      url = "https://git.sagemath.org/sage.git/patch/?id=e351bf2f2914e683d5e2028597c45ae8d1b7f855";
-      sha256 = "00faa7fl0vaqcqbw0bidkhl78qa8l34d3a07zirbcl0vm74bdn1p";
+    (fetchSageDiff {
+      name = "sphinx-1.8.patch";
+      base = "8.7";
+      rev = "737afd8f314bd1e16feaec562bb4b5efa2effa8b";
+      sha256 = "0n56ss88ds662bp49j23z5c2i6hsn3jynxw13wv76hyl0h7l1hjh";
     })
 
     # https://trac.sagemath.org/ticket/27653
@@ -177,6 +140,12 @@ stdenv.mkDerivation rec {
     echo '#!${runtimeShell}
     python "$@"' > build/bin/sage-python23
 
+    # Make sure sage can at least be imported without setting any environment
+    # variables. It won't be close to feature complete though.
+    sed -i \
+      "s|var('SAGE_LOCAL',.*|var('SAGE_LOCAL', '$out/src')|" \
+      src/sage/env.py
+
     # Do not use sage-env-config (generated by ./configure).
     # Instead variables are set manually.
     echo '# do nothing' >  src/bin/sage-env-config
diff --git a/pkgs/applications/science/math/sage/sagedoc.nix b/pkgs/applications/science/math/sage/sagedoc.nix
index cc1a4fc61e0..91c880673a1 100644
--- a/pkgs/applications/science/math/sage/sagedoc.nix
+++ b/pkgs/applications/science/math/sage/sagedoc.nix
@@ -52,6 +52,9 @@ stdenv.mkDerivation rec {
     export HOME="$TMPDIR/sage_home"
     mkdir -p "$HOME"
 
+    # needed to link them in the sage docs using intersphinx
+    export PPLPY_DOCS=${python.pkgs.pplpy.doc}/share/doc/pplpy
+
     ${sage-with-env}/bin/sage -python -m sage_setup.docbuild \
       --mathjax \
       --no-pdf-links \
diff --git a/pkgs/applications/science/math/sage/sagelib.nix b/pkgs/applications/science/math/sage/sagelib.nix
index d7f9cb9ee32..d745532d5d7 100644
--- a/pkgs/applications/science/math/sage/sagelib.nix
+++ b/pkgs/applications/science/math/sage/sagelib.nix
@@ -47,6 +47,8 @@
 , jupyter_core
 , libhomfly
 , libbraiding
+, gmpy2
+, pplpy
 }:
 
 # This is the core sage python package. Everything else is just wrappers gluing
@@ -115,6 +117,8 @@ buildPythonPackage rec {
     cysignals
     libhomfly
     libbraiding
+    gmpy2
+    pplpy
   ];
 
   buildPhase = ''