about summary refs log tree commit diff
path: root/nix/buildLisp
diff options
context:
space:
mode:
authorsterni <sternenseemann@systemli.org>2021-08-15T11·15+0200
committersterni <sternenseemann@systemli.org>2021-08-26T11·26+0000
commit2dd8bc7977aae7c5f545f8f61415f0bcfc30e02b (patch)
treedad113305fbe7e997fdf6760feac51e00de40962 /nix/buildLisp
parenta0be4fd90272106856994301ff8d8e07f266bce6 (diff)
fix(nix/buildLisp): prevent image loader from parsing arguments r/2777
CCL and SBCL create executables by dumping their image. As a
consequence, some part of the respective compiler is embedded in the
resulting executable which is executed and loads the image. For CCL and
SBCL this piece of software seems to unconditionally parse arguments
which can't be prevented since it happens before any lisp is loaded.

Luckily in both cases the parsing stops at `--`, so we can just pass
this via the wrapper — we just need to work around the problem that this
will of course be left in argv and confuse any later code. This can be
rectified by deleting everything prior to the first `--` in the global
argument list on startup in both cases.

In cases we do want to pass arguments to the image loader, we can use
the special NIX_BUILDLISP_LISP_ARGS environment variable which is
understood by the wrapper.

Note: This fix doesn't interfere with ECL since it is not using the
wrapper script at the moment.

Fixes b/136.

Change-Id: I3f95aa61e945e51428021ca18232ff15c923f870
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3357
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'nix/buildLisp')
-rw-r--r--nix/buildLisp/default.nix25
1 files changed, 22 insertions, 3 deletions
diff --git a/nix/buildLisp/default.nix b/nix/buildLisp/default.nix
index 8f95c76916..7e38c0283e 100644
--- a/nix/buildLisp/default.nix
+++ b/nix/buildLisp/default.nix
@@ -305,7 +305,15 @@ let
                                        :directory bindir)))
           (save-lisp-and-die outpath
                              :executable t
-                             :toplevel (function ${main})
+                             :toplevel
+                             (lambda ()
+                               ;; Filter out everything prior to the `--` we
+                               ;; insert in the wrapper to prevent SBCL from
+                               ;; parsing arguments at startup
+                               (replace sb-ext:*posix-argv*
+                                        (cdr (member "--" sb-ext:*posix-argv*
+                                                     :test #'string=)))
+                               (${main}))
                              :purify t))
       '';
 
@@ -539,7 +547,16 @@ let
           (save-application executable
                             :purify t
                             :error-handler :quit
-                            :toplevel-function (function ${main})
+                            :toplevel-function
+                            (lambda ()
+                              ;; Filter out everything prior to the `--` we
+                              ;; insert in the wrapper to prevent SBCL from
+                              ;; parsing arguments at startup
+                              (replace ccl:*command-line-argument-list*
+                                       (cdr (member "--"
+                                                    ccl:*command-line-argument-list*
+                                                    :test #'string=)))
+                              (${main}))
                             :mode #o755
                             ;; TODO(sterni): use :native t on macOS
                             :prepend-kernel t))
@@ -676,7 +693,9 @@ let
         }
       }
     '' + lib.optionalString implementation.wrapProgram ''
-      wrapProgram $out/bin/${name} --prefix LD_LIBRARY_PATH : "${libPath}"
+      wrapProgram $out/bin/${name} \
+        --prefix LD_LIBRARY_PATH : "${libPath}" \
+        --add-flags "\$NIX_BUILDLISP_LISP_ARGS --"
     ''));
 
   # 'bundled' creates a "library" which makes a built-in package available,