Subject: [bug#49280] [PATCH 4/4] gnu: racket-next-minimal: Bootstrap from C.

From: Philip McGrath <[email-masked]>
Date: Tue, 29 Jun 2021 17:57:42 -0400

This commit bootstraps the Racket compiler and runtime system from source,
including Racket CS as well as both variants of Racket BC. (One remaining
limitation is discussed in comments added to gnu/packages/racket.scm.)

In the process, it moves to building minimal Racket from the Git repository,
rather than the packaged source tarballs. The Git repository is slightly
better as the ``corresponding source'':

 1. A few packages especially closely tied to the Racket core implementation
    (like "compiler-lib", "base", and "racket-doc") are developed in the
    same Git repository. Having them use the same Guix origin, too, will
    help to keep them in sync.

 2. The top-level Makefile in the Git repository is an important
    ``script[] used to control compilation and installation.''
    In particular, it cooperates with the "distro-build" package to
    create the source tarballs and installers for a Racket distribution.
    (Racket supports a notion of custom distributions.)

 3. It is ``the preferred form ... for making modifications'' to the core
    Racket implementation.

Racket releases are tagged in the Git repository (e.g. "v8.1"). At the
beginning of each release cycle, a branch is created to stabalize a version
for extra testing. Active development happens on the "master" branch.

* gnu/packages/racket-next-minimal-sh-via-rktio.patch: New file, coppied
from racket-sh-via-rktio.patch to accomodate an extra directory layer.
When racket-next-minimal becomes racket-minimal, this version will be
the only one needed.
* gnu/local.mk (dist_patch_DATA): Add it.
* gnu/local/racket.scm (cfg-flag:sh-for-rktio, cfg-flag:enable-lt,
cfg-flag:enable-racket, unpack-nanopass+stex,
%main-repo-main-distribution-pkgs): New private variables.
* gnu/local/racket.scm (racket-next-minimal)[source]: Use Git.
[source](snippet): Unbundle nanopass, stex, and libffi.
[inputs]: List explicitly.
[native-inputs]: Use racket-next-bootstrap-bootfiles, plus its
dependencies (for Chez, plus a Racket for bootstrappig).
[arguments]: Revise extensively.
* gnu/local/racket.scm (racket-next-minimal-bc-3m,
racket-next-minimal-bc-cgc): New packages, hidden at least for now.
(racket-next-bootstrap-chez-bootfiles): Another new package, but this one
is especially likely to stay hidden.
* gnu/local/racket.scm (racket-next)[origin](snippet): Unbundle packages
developed in the main Git repository, but leave their links.rktd and
pkgs.rktd entries in place.
[native-inputs]: Add the main Racket Git repository.
[arguments](#:phases): Adjust 'unpack-packages to also unpack package
sources from the main Racket Git repository.
---
 gnu/local.mk                                  |   1 +
 .../racket-next-minimal-sh-via-rktio.patch    |  87 +++++
 gnu/packages/racket.scm                       | 357 +++++++++++++++---
 3 files changed, 396 insertions(+), 49 deletions(-)
 create mode 100644 gnu/packages/patches/racket-next-minimal-sh-via-rktio.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 6b9202cba1..4ca35cf56a 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1671,6 +1671,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/ripperx-missing-file.patch		\
   %D%/packages/patches/rpcbind-CVE-2017-8779.patch		\
   %D%/packages/patches/rtags-separate-rct.patch			\
+ %D%/packages/patches/racket-next-minimal-sh-via-rktio.patch    \
   %D%/packages/patches/racket-sh-via-rktio.patch		\
   %D%/packages/patches/remake-impure-dirs.patch			\
   %D%/packages/patches/retroarch-LIBRETRO_DIRECTORY.patch	\
diff --git a/gnu/packages/patches/racket-next-minimal-sh-via-rktio.patch b/gnu/packages/patches/racket-next-minimal-sh-via-rktio.patch
new file mode 100644
index 0000000000..6bc2ee8331
--- /dev/null
+++ b/gnu/packages/patches/racket-next-minimal-sh-via-rktio.patch
@@ -0,0 +1,87 @@
+From 3574b567c486d264d680a37586436c3b5a8cb978 Mon Sep 17 00:00:00 2001
+From: Philip McGrath <[email-masked]>
+Date: Thu, 4 Mar 2021 04:11:50 -0500
+Subject: [PATCH] patch rktio_process for "/bin/sh" on Guix
+
+Racket provides the functions `system` and `process`,
+which execute shell commands using `sh` (or `cmd` on Windows).
+Racket assumes that `sh` can be found at "/bin/sh",
+which is not necessarily true on Guix.
+
+This patch adds a special case for "/bin/sh" to `rktio_process`,
+the C function that implements the core of `system`, `process`,
+and related Racket functions.
+
+Guix should enable the special case by defining the C preprocessor
+macro `GUIX_RKTIO_PATCH_BIN_SH` with the path to `sh` in the store.
+If:
+
+    1. The `GUIX_RKTIO_PATCH_BIN_SH` macro is defined; and
+
+    2. `rktio_process` is called with the exact path "/bin/sh"; and
+
+    3. The path specified by `GUIX_RKTIO_PATCH_BIN_SH` does exists;
+
+then `rktio_process` will execute the file specified
+by `GUIX_RKTIO_PATCH_BIN_SH` instead of "/bin/sh".
+
+Compared to previous attempts to patch the Racket sources,
+making this change at the C level is both:
+
+    - More comprehensive: it catches all attempts to execute "/bin/sh",
+      without having to track down the source of every occurance; and
+
+    - Less intrusive: by guarding the special case with a C preprocessor
+      conditional and a runtime check that the file in the store exists,
+      we make it much less likely that it will "leak" out of Guix.
+---
+ racket/src/rktio/rktio_process.c | 21 ++++++++++++++++++++-
+ 1 file changed, 20 insertions(+), 1 deletion(-)
+
+diff --git a/racket/src/rktio/rktio_process.c b/racket/src/rktio/rktio_process.c
+index 89202436c0..465ebdd5c5 100644
+--- a/racket/src/rktio/rktio_process.c
++++ b/racket/src/rktio/rktio_process.c
+@@ -1224,12 +1224,14 @@ int rktio_process_allowed_flags(rktio_t *rktio)
+ /*========================================================================*/
+ 
+ rktio_process_result_t *rktio_process(rktio_t *rktio,
+-                                      const char *command, int argc, rktio_const_string_t *argv,
++                                      /* PATCHED for Guix (next line) */
++                                      const char *_guix_orig_command, int argc, rktio_const_string_t *argv,
+                                       rktio_fd_t *stdout_fd, rktio_fd_t *stdin_fd, rktio_fd_t *stderr_fd,
+                                       rktio_process_t *group_proc,
+                                       const char *current_directory, rktio_envvars_t *envvars,
+                                       int flags)
+ {
++  const char *command; /* PATCHED for Guix */
+   rktio_process_result_t *result;
+   intptr_t to_subprocess[2], from_subprocess[2], err_subprocess[2];
+   int pid;
+@@ -1255,6 +1257,23 @@ rktio_process_result_t *rktio_process(rktio_t *rktio,
+   int i;
+ #endif
+ 
++/* BEGIN PATCH for Guix */
++#if defined(GUIX_RKTIO_PATCH_BIN_SH)
++# define GUIX_AS_a_STR_HELPER(x) #x
++# define GUIX_AS_a_STR(x) GUIX_AS_a_STR_HELPER(x)
++  /* A level of indirection makes `#` work as needed: */
++  command =
++      ((0 == strcmp(_guix_orig_command, "/bin/sh"))
++       && rktio_file_exists(rktio, GUIX_AS_a_STR(GUIX_RKTIO_PATCH_BIN_SH)))
++      ? GUIX_AS_a_STR(GUIX_RKTIO_PATCH_BIN_SH)
++      : _guix_orig_command;
++# undef GUIX_AS_a_STR
++# undef GUIX_AS_a_STR_HELPER
++#else
++  command = _guix_orig_command;
++#endif
++/* END PATCH for Guix */
++
+   /* avoid compiler warnings: */
+   to_subprocess[0] = -1;
+   to_subprocess[1] = -1;
+-- 
+2.21.1 (Apple Git-122.3)
+
diff --git a/gnu/packages/racket.scm b/gnu/packages/racket.scm
index cf0240be5c..4e2d498364 100644
--- a/gnu/packages/racket.scm
+++ b/gnu/packages/racket.scm
@@ -32,7 +32,9 @@
   #:use-module (srfi srfi-1)
   #:use-module (ice-9 match)
   #:use-module (gnu packages)
+  #:use-module (gnu packages autotools)
   #:use-module (gnu packages bash)
+  #:use-module (gnu packages chez)
   #:use-module (gnu packages compression)
   #:use-module (gnu packages databases)
   #:use-module (gnu packages fontutils)
@@ -41,6 +43,7 @@
   #:use-module (gnu packages gtk)
   #:use-module (gnu packages image)
   #:use-module (gnu packages libedit)
+  #:use-module (gnu packages libffi)
   #:use-module (gnu packages multiprecision)
   #:use-module (gnu packages sqlite)
   #:use-module (gnu packages tls)
@@ -49,6 +52,7 @@
 ;; Commentary:
 ;;
 ;; Preliminary guidelines on naming things:
+;;
 ;;   - `racket` is the main package. It corresponds to `racket-minimal`
 ;;     with the Racket-level package "main-distribution" installed.
 ;;   - `racket-minimal` is Racket runtime system and core libraries:
@@ -74,6 +78,49 @@
 ;;     individually, we will need `racket-next-pkg-` for the packages
 ;;     that make up `racket-next`.
 ;;
+;; Here's how bootstrapping minimal Racket works:
+;;
+;;   - Racket BC [CGC] can be built with only a C compiler (except for
+;;     one caveat discussed below).
+;;   - Racket BC [3M] needs an existing Racket to run "xform",
+;;     which transforms its own C source code to add additional annotations
+;;     for the precise garbage collector.
+;;   - Racket CS needs (bootfiles for) Racket's fork of Chez Scheme.
+;;     It also needs an existing Racket to compile Racket-implemented
+;;     parts of the runtime system to R6RS libraries.
+;;   - Chez Scheme also needs bootfiles for itself, but Racket can simulate
+;;     enough of Chez Scheme to load Racket's fork of the Chez Scheme compiler
+;;     purely from source into Racket and apply the compiler to itself,
+;;     producing the needed bootfiles (albeit very slowly).
+;;     Any variant of Racket since version 7.1 can run the simulation.
+;;
+;; So, we build CGC to build 3M to build bootfiles and CS.
+;;
+;; One remaining bootstrapping limitation is that Racket's reader, module
+;; system, and macro expander are implemented in Racket. For Racket CS,
+;; they are compiled to R6RS libraries as discussed above. This note from the
+;; README file applies to all such subsystems:
+;;
+;;     The Racket version must be practically the same as the current Racket
+;;     verson, although it can be the Racket BC implementation (instead of
+;;     the Racket CS implementation).
+;;
+;;     Unlike Chez Scheme boot files, the files generated in "schemified"
+;;     are human-readable and -editable Scheme code. That provides a way
+;;     out of bootstrapping black holes, even without BC.
+;;
+;; However, other Racket subsystems implemented in Racket for Racket CS
+;; use older C implementations for Racket BC, whereas the reader, expander,
+;; and module system were completely replaced with the Racket implementation
+;; as of Racket 7.0.
+;;
+;; For Racket BC, the compiled "linklet" s-expressions (primitive modules)
+;; are embeded in C as a static string constant. Eventually, they are further
+;; compiled by the C-implemented Racket BC bytecode and JIT compilers.
+;; (On platforms where Racket BC's JIT is not supported, yet another compiler
+;; instead compiles the linklets to C code, but this is not a bootstrapping
+;; issue.)
+;;
 ;; Code:

@@ -200,7 +247,6 @@ DrRacket IDE, are not included.")
     ;; https://download.racket-lang.org/license.html
     (license (list lgpl3+ asl2.0 expat))))
 
-
 (define-public racket
   (package
     (inherit racket-minimal)
@@ -243,8 +289,35 @@ The main Racket distribution comes with many bundled packages, including the
 DrRacket IDE, libraries for GUI and web programming, and implementations of
 languages such as Typed Racket, R5RS and R6RS Scheme, Algol 60, and Datalog.")))
 
-(define %pre-release-installers
-  "https://pre-release.racket-lang.org/installers/")
+
+(define cfg-flag:sh-for-rktio
+  `(string-append "CPPFLAGS=-DGUIX_RKTIO_PATCH_BIN_SH="
+                  (assoc-ref %build-inputs "sh")
+                  "/bin/sh"))
+(define cfg-flag:enable-lt
+  `(string-append "--enable-lt="
+                  (assoc-ref %build-inputs "libtool")
+                  "/bin/libtool"))
+(define cfg-flag:enable-racket
+  `(let ((racket (assoc-ref %build-inputs "racket")))
+     (string-append "--enable-racket="
+                    racket
+                    "/bin/racket")))
+
+(define unpack-nanopass+stex
+  ;; Copied from chez-scheme.
+  ;; TODO: Eventually, we should refactor Chez Scheme
+  ;; enough to share more directly, so that we can make
+  ;; Racket's version of Chez avalable as a Guix package,
+  ;; e.g. for architectures not supported upstream.
+  ;; For now, we let Racket drive the Chez build process
+  ;; other than this step.
+  `(for-each (lambda (dep)
+               (define src
+                 (assoc-ref (or native-inputs inputs) dep))
+               (copy-recursively src dep
+                                 #:keep-mtime? #t))
+             '("nanopass" "stex")))
 
 (define-public racket-next-minimal
   (package
@@ -253,12 +326,214 @@ languages such as Typed Racket, R5RS and R6RS Scheme, Algol 60, and Datalog.")))
     (version "8.1.900")
     (source
      (origin
-       (inherit (package-source racket-minimal))
+       (method git-fetch)
+       (uri (git-reference
+             (url "https://github.com/racket/racket")
+             (commit "0874b76de4f147ada46607857d8acf8445a1073d")))
        (sha256
         (base32
-         "0dm849wvlaxpfgz2qmgy2kwdslyi515rxn1m1yff38lagbn21vxq"))
-       (uri (string-append %pre-release-installers
-                           "racket-minimal-src.tgz"))))))
+         "0gy6rwyrpaij5k5pcyiif821b4vffqiaxg1vpg4iykw2c5ypfp43"))
+       (file-name
+        (git-file-name name version))
+       (patches
+        (search-patches
+         "racket-next-minimal-sh-via-rktio.patch"))
+       (snippet
+        (with-imported-modules '((guix build utils))
+          #~(begin
+              (use-modules (guix build utils))
+              ;; unbundle Chez submodules
+              (with-directory-excursion "racket/src/ChezScheme"
+                #$(origin-snippet (package-source chez-scheme)))
+              ;; unbundle libffi
+              (for-each
+               delete-file-recursively
+               '("racket/src/bc/foreign/libffi")))))))
+    (inputs
+     `(;; common to all racket-minimal variants:
+       ("openssl" ,openssl)
+       ("sqlite" ,sqlite)
+       ("sh" ,bash-minimal)
+       ;; only for CS
+       ("zlib" ,zlib)
+       ("zlib:static" ,zlib "static")
+       ("lz4" ,lz4)
+       ("lz4:static" ,lz4 "static")))
+    (native-inputs
+     `(("bootfiles" ,racket-next-bootstrap-chez-bootfiles)
+       ,@(package-native-inputs racket-next-bootstrap-chez-bootfiles)))
+    (build-system gnu-build-system)
+    (arguments
+     `(#:configure-flags
+       (list "--enable-csonly"
+             "--enable-libz"
+             "--enable-liblz4"
+             ,cfg-flag:enable-racket
+             ,cfg-flag:sh-for-rktio)
+       #:out-of-source? #true
+       #:tests? #f ;; not yet
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'unpack 'unpack-nanopass+stex
+           (lambda* (#:key inputs native-inputs #:allow-other-keys)
+             (with-directory-excursion "racket/src/ChezScheme"
+               ,unpack-nanopass+stex)))
+         (add-after 'unpack-nanopass+stex 'unpack-bootfiles
+           (lambda* (#:key inputs #:allow-other-keys)
+             (with-directory-excursion "racket/src/ChezScheme"
+               (copy-recursively
+                (string-append (assoc-ref inputs "bootfiles") "/boot")
+                "boot"))))
+         (add-before 'configure 'initialize-config.rktd
+           (lambda* (#:key inputs #:allow-other-keys)
+             (mkdir-p "racket/etc")
+             (with-output-to-file "racket/etc/config.rktd"
+               (lambda ()
+                 (format #t
+                         "#hash((lib-search-dirs . ~s))\n"
+                         (cons #f
+                               (map (lambda (lib)
+                                      (string-append (assoc-ref inputs lib)
+                                                     "/lib"))
+                                    '("openssl"
+                                      "sqlite"))))))
+             #t))
+         (replace 'configure
+           (let ((inner (assq-ref %standard-phases 'configure)))
+             (lambda args
+               (chdir "racket/src")
+               (apply inner args))))
+         (add-after 'install 'remove-pkgs-directory
+           ;; otherwise, e.g., `raco pkg show` will try and fail to
+           ;; create a lock file
+           (lambda* (#:key outputs #:allow-other-keys)
+             ;; rmdir because we want an error if it isn't empty
+             (rmdir (string-append (assoc-ref outputs "out")
+                                   "/share/racket/pkgs"))
+             #t)))))
+    ;; https://download.racket-lang.org/license.html
+    ;; The LGPL components are only used by Racket BC.
+    (license (list asl2.0 expat))))
+
+(define-public racket-next-minimal-bc-3m
+  (hidden-package
+   (package/inherit racket-next-minimal
+     (name "racket-next-minimal-bc-3m")
+     (inputs
+      `(("libffi" ,libffi) ;; <- only for BC variants
+        ,@(filter (match-lambda
+                    ((label . _)
+                     (not (member label
+                                  '("zlib" "zlib:static"
+                                    "lz4" "lz4:static")))))
+                  (package-inputs racket-next-minimal))))
+     (native-inputs
+      `(("libtool" ,libtool)
+        ("racket" ,(if (%current-target-system)
+                       racket-next-minimal
+                       racket-next-minimal-bc-cgc))))
+     (arguments
+      (substitute-keyword-arguments (package-arguments racket-next-minimal)
+        ((#:configure-flags _ '())
+         `(list "--enable-bconly"
+                ,cfg-flag:enable-racket
+                ,cfg-flag:enable-lt
+                ,cfg-flag:sh-for-rktio))
+        ((#:phases usual-phases)
+         `(modify-phases ,usual-phases
+            (delete 'unpack-nanopass+stex)
+            (delete 'unpack-bootfiles)))))
+     (synopsis "Minimal Racket with the BC [3M] runtime system")
+     (description "The Racket BC (``before Chez'' or ``bytecode'') implementation was the default before Racket 8.0. It uses a compiler written in C targeting architecture-independent bytecode, plus a JIT compiler on most platforms. Racket BC has a different C API than the newer runtune system (Racket CS) supports a slightly different set of architectures than the current runtime system, Racket CS (based on ``Chez Scheme'').
+
+This packackage is the normal implementation of Racket BC with a precise garbage collector, 3M (``Moving Memory Manager'')."))))
+
+(define-public racket-next-minimal-bc-cgc
+  (package/inherit racket-next-minimal-bc-3m
+    (name "racket-next-minimal-bc-cgc")
+    (native-inputs
+     (filter (match-lambda
+               (("racket" . _)
+                #f)
+               (_
+                #t))
+             (package-native-inputs racket-next-minimal-bc-3m)))
+    (arguments
+     (substitute-keyword-arguments (package-arguments racket-next-minimal-bc-3m)
+       ((#:configure-flags _ '())
+        `(list "--enable-cgcdefault"
+               ,cfg-flag:enable-lt
+               ,cfg-flag:sh-for-rktio))))
+    (synopsis "Old Racket implemetation used for bootstapping")
+    (description "This variant of the Racket BC (``before Chez'' or ``bytecode'') implementation is not recommended for general use. It uses CGC (a ``Conservative Garbage Collector''), which was succeeded as default in PLT Scheme version 370 (which translates to 3.7 in the current versioning scheme) by the 3M variant, which in turn was succeeded in version 8.0 by the Racket CS implementation.
+
+Racket BC [CGC] is primarily used for bootstrapping Racket BC [3M]. It may also be used for embedding applications without the annotations needed in C code to use the 3M garbage collector.")))
+
+(define-public racket-next-bootstrap-chez-bootfiles
+  (hidden-package
+   (package/inherit racket-next-minimal
+     (name "racket-next-bootstrap-chez-bootfiles")
+     (inputs `())
+     (native-inputs
+      `(("racket" ,(if (%current-target-system)
+                       racket-next-minimal
+                       racket-next-minimal-bc-3m))
+        ("stex" ,@(assoc-ref (package-native-inputs chez-scheme) "stex"))
+        ("nanopass" ,@(assoc-ref (package-native-inputs chez-scheme)
+                                 "nanopass"))))
+     (arguments
+      `(#:phases
+        (modify-phases %standard-phases
+          (add-after 'unpack 'unpack-nanopass+stex
+            (lambda* (#:key inputs native-inputs #:allow-other-keys)
+              (with-directory-excursion "racket/src/ChezScheme"
+                ,unpack-nanopass+stex)))
+          (delete 'configure)
+          (delete 'patch-generated-file-shebangs)
+          (replace 'build
+            (lambda* (#:key inputs outputs #:allow-other-keys)
+              (with-directory-excursion "racket/src/ChezScheme"
+                (invoke (string-append (assoc-ref inputs "racket")
+                                       "/bin/racket")
+                        "rktboot/main.rkt"
+                        "--dest" (assoc-ref outputs "out")))
+              #t))
+          (delete 'check)
+          (delete 'install))))
+     (synopsis "Chez Scheme bootfiles bootstrapped by Racket")
+     (description "Chez Scheme is a self-hosting compiler: building it requires ``bootfiles'' containing the Scheme-implemented portions compiled for the current platform. (Chez can then cross-compile bootfiles for all other supported platforms.)
+
+The Racket package ``cs-bootstrap'' (part of the main Racket Git repository) implements enough of a Chez Scheme simulation to load the Chez Scheme compiler purely from source into Racket and apply the compiler to itself, thus bootstrapping Chez Scheme. Bootstrapping takes about 10 times as long as using an existing Chez Scheme, but ``cs-bootstap'' supports Racket 7.1 and later, including the Racket BC variant.
+
+Note that the generated bootfiles are specific to Racket's fork of Chez Scheme, and ``cs-bootstrap'' does not currently support building upstream Chez Scheme.")
+     (license (package-license chez-scheme)))))
+
+
+(define %pre-release-installers
+  "https://pre-release.racket-lang.org/installers/")
+
+(define %main-repo-main-distribution-pkgs
+  ;; These are the packages developed in the main Racket Git repository
+  ;; that are part of the main distribution.
+  '("at-exp-lib"
+    "base"
+    "compiler-lib"
+    ;; NOT "compiler-test"
+    "compiler"
+    "net-doc"
+    "net-lib"
+    ;; NOT "net-test"
+    "net"
+    ;; NOT "plt-services"
+    ;; NOT "racket-benchmarks"
+    ;; NOT "racket-build-guide"
+    "racket-doc"
+    "racket-index"
+    "racket-lib"
+    ;; NOT "racket-test-core"
+    ;; NOT "racket-test-extra"
+    ;; NOT "racket-test"
+    "zo-lib"))
 
 (define-public racket-next
   (package
@@ -289,36 +564,10 @@ languages such as Typed Racket, R5RS and R6RS Scheme, Algol 60, and Datalog.")))
                           "README"
                           "src"))
               ;; unbundle package sources included elsewhere
-              (define (substitute/delete file pattern)
-                (substitute
-                 file
-                 (list (cons pattern
-                             (lambda (line matches)
-                               ;; must match exactly once
-                               (match matches
-                                 ((m)
-                                  (string-append (match:prefix m)
-                                                 (match:suffix m)))))))))
-              (define (unbundle-pkg pkg)
-                (define quoted-pkg (regexp-quote pkg))
-                (with-directory-excursion "share"
-                  (substitute/delete
-                   "links.rktd"
-                   (string-append
-                    "[(][^()]+[(]#\"pkgs\" #\""
-                    quoted-pkg
-                    "\"[)][)]"))
-                  (with-directory-excursion "pkgs"
-                    (substitute/delete
-                     "pkgs.rktd"
-                     (string-append
-                      "[(]\""
-                      quoted-pkg
-                      "\" \\. #s[(]"
-                      "(pkg-info|[(]sc-pkg-info pkg-info 3[)])"
-                      " [(][^()]+[)] [^()]+[)][)]"))
-                    (delete-file-recursively pkg))))
-              (unbundle-pkg "racket-lib"))))))
+              (with-directory-excursion "share/pkgs"
+                (for-each delete-file-recursively
+                          '#+%main-repo-main-distribution-pkgs))
+              #t)))))
     (inputs
      `(("cairo" ,cairo)
        ("fontconfig" ,fontconfig)
@@ -336,22 +585,32 @@ languages such as Typed Racket, R5RS and R6RS Scheme, Algol 60, and Datalog.")))
        ("libedit" ,libedit)))
     (native-inputs
      `(("racket" ,racket-next-minimal)
-       ("extend-layer" ,extend-layer)))
+       ("extend-layer" ,extend-layer)
+       ("main-repo" ,(package-source racket-next-minimal))))
     (arguments
      `(#:phases
        (modify-phases %standard-phases
          (add-before 'configure 'unpack-packages
-           (lambda* (#:key native-inputs inputs outputs #:allow-other-keys)
-             (let ((racket (assoc-ref (or native-inputs inputs) "racket"))
-                   (prefix (assoc-ref outputs "out")))
-               (mkdir-p (string-append prefix "/share/racket/pkgs"))
-               (copy-recursively
-                "share/links.rktd"
-                (string-append prefix "/share/racket/links.rktd"))
-               (copy-recursively
-                "share/pkgs"
-                (string-append prefix "/share/racket/pkgs"))
-               #t)))
+           (let ((unpack (assoc-ref %standard-phases 'unpack)))
+             (lambda* (#:key  native-inputs inputs outputs #:allow-other-keys)
+               (let* ((racket (assoc-ref (or native-inputs inputs) "racket"))
+                      (prefix (assoc-ref outputs "out"))
+                      (pkgs-dir (string-append prefix "/share/racket/pkgs")))
+                 (mkdir-p pkgs-dir)
+                 (copy-recursively
+                  "share/links.rktd"
+                  (string-append prefix "/share/racket/links.rktd"))
+                 (copy-recursively "share/pkgs" pkgs-dir)
+                 ;; NOTE: unpack changes the working directory
+                 (unpack #:source (assoc-ref (or native-inputs inputs)
+                                             "main-repo"))
+                 (for-each (lambda (pkg)
+                             (define dest (string-append pkgs-dir "/" pkg))
+                             (mkdir-p dest)
+                             (copy-recursively (string-append "pkgs/" pkg)
+                                               dest))
+                           ',%main-repo-main-distribution-pkgs)
+                 #t))))
          (replace 'configure
            (lambda* (#:key native-inputs inputs outputs #:allow-other-keys)
              (let ((racket (assoc-ref (or native-inputs inputs) "racket"))
-- 
2.30.2