diff --git a/gnu/packages/maths.scm b/gnu/packages/maths.scm
index de8e886485..8ab18cedf2 100644
--- a/gnu/packages/maths.scm
+++ b/gnu/packages/maths.scm
@@ -3544,38 +3544,41 @@ (define-public petsc
               (let* ((prefix (assoc-ref outputs "out"))
                      (flags `(,(string-append "--prefix=" prefix)
                               ,@configure-flags)))
+                ;; sort source files in configure (for reproducibility)
+                (substitute* "config/gmakegen.py"
+                   (("join\\(srcs\\[lang\\]\\)") "join(sorted(srcs[lang]))"))
                 (format #t "build directory: ~s~%" (getcwd))
                 (format #t "configure flags: ~s~%" flags)
                 (apply invoke "./configure" flags)
-
+                ;; Prevent build directory Linux version from leaking into
+                ;; compiled code
+                (substitute* (find-files "." "petscmachineinfo.h")
+                  (((getcwd)) out)
+                  ;; Scrub build machine characteristic
+                  (("Machine characteristics: Linux-[0-9]+\\.[0-9]+\\.[0-9]+")
+                    "Machine characteristics: Linux-x.x.x"))
                 ;; Generate test scripts with the right shebang.
                 (substitute* "config/example_template.py"
                   (("#!/usr/bin/env bash")
                    (string-append "#!" (which "bash")))))))
-          (add-after 'configure 'clean-local-references
-            (lambda* (#:key outputs #:allow-other-keys)
-              (let ((out (assoc-ref outputs "out")))
-                (substitute* (find-files "." "^petsc(conf|machineinfo).h$")
-                  ;; Prevent build directory from leaking into compiled code
-                  (((getcwd)) out)
-                  ;; Scrub timestamp for reproducibility
-                  ((".*Libraries compiled on.*") ""))
-                (substitute* (find-files "." "petscvariables")
-                  ;; Do not expose build machine characteristics, set to defaults.
-                  (("MAKE_NP = [:digit:]+") "MAKE_NP = 2")
-                  (("NPMAX = [:digit:]+") "NPMAX = 2")))))
           (add-after 'install 'clean-install
-            ;; Try to keep installed files from leaking build directory names.
             (lambda* (#:key inputs outputs #:allow-other-keys)
               (let ((out (assoc-ref outputs "out")))
-                (substitute* (map (lambda (file)
-                                    (string-append out "/lib/petsc/conf/" file))
-                                  '("petscvariables"))
-                  (((getcwd)) out))
-                ;; Make compiler references point to the store
                 (substitute* (string-append out "/lib/petsc/conf/petscvariables")
-                  (("= (gcc|g\\+\\+|gfortran)" _ compiler)
-                   (string-append "= " (which compiler))))
+                  ;; Try to keep installed files from leaking build directory names.
+                  (((getcwd)) out)
+                  ;; Do not expose build machine characteristics, set to defaults.
+                  (("MAKE_NP = [[:digit:]]+") "MAKE_NP = 2")
+                  (("MAKE_TEST_NP = [[:digit:]]+") "MAKE_TEST_NP = 2")
+                  (("MAKE_LOAD = [[:digit:]]+\\.[[:digit:]]+") "MAKE_LOAD = 2.0")
+                  (("NPMAX = [[:digit:]]+") "NPMAX = 2")
+                  ;; Do not retain a reference to GCC and other build only inputs.
+                  (("([[:graph:]]+)/bin/gcc") "gcc")
+                  (("([[:graph:]]+)/bin/g\\+\\+") "g++")
+                  (("([[:graph:]]+)/bin/make") "make")
+                  (("([[:graph:]]+)/bin/diff") "diff")
+                  (("([[:graph:]]+)/bin/sed") "sed")
+                  (("([[:graph:]]+)/bin/gfortran") "gfortran"))
                 ;; PETSc installs some build logs, which aren't necessary.
                 (for-each (lambda (file)
                             (let ((f (string-append out "/lib/petsc/conf/" file)))
@@ -3587,17 +3590,6 @@ (define-public petsc
                             "configure-hash"
                             ;; Once installed, should uninstall with Guix
                             "uninstall.py")))))
-          (add-after 'clean-install 'clear-reference-to-compiler
-            (lambda* (#:key inputs outputs #:allow-other-keys)
-              ;; Do not retain a reference to GCC and other build only inputs.
-              (let ((out (assoc-ref outputs "out")))
-              (substitute* (string-append out "/lib/petsc/conf/petscvariables")
-                (("([[:graph:]]+)/bin/gcc") "gcc")
-                (("([[:graph:]]+)/bin/g\\+\\+") "g++")
-                (("([[:graph:]]+)/bin/make") "make")
-                (("([[:graph:]]+)/bin/diff") "diff")
-                (("([[:graph:]]+)/bin/sed") "sed")
-                (("([[:graph:]]+)/bin/gfortran") "gfortran")))))
           (add-after 'install 'move-examples
             (lambda* (#:key outputs #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
