about summary refs log tree commit diff
path: root/nix/buildLisp
diff options
context:
space:
mode:
authorVincent Ambo <Vincent Ambo>2020-01-08T21·39+0000
committerVincent Ambo <Vincent Ambo>2020-01-08T21·39+0000
commitbdad8f664220798bd5183933145e62bfceeea130 (patch)
tree425ae93de2dc910a06ea126d60799c78b141dde1 /nix/buildLisp
parent2bfe073eb2fea9b4ff207ff204b33e3cd4762f88 (diff)
feat(buildLisp): Implement buildLisp.program to dump executables r/349
Dumps the executable image from SBCL to $out/bin/$name.

Image compression is disabled.
Diffstat (limited to 'nix/buildLisp')
-rw-r--r--nix/buildLisp/default.nix34
1 files changed, 30 insertions, 4 deletions
diff --git a/nix/buildLisp/default.nix b/nix/buildLisp/default.nix
index 9d96b66d595c..641a9a8c4ed7 100644
--- a/nix/buildLisp/default.nix
+++ b/nix/buildLisp/default.nix
@@ -61,10 +61,25 @@ let
     lib.unique (lib.flatten (deps ++ (map (d: d.lispDeps) deps)))
   );
 
-
-  #
-  # Public API functions
+  # 'genDumpLisp' generates a Lisp file that instructs SBCL to dump
+  # the currently loaded image as an executable to $out/bin/$name.
   #
+  # TODO(tazjin): Compression is currently unsupported because the
+  # SBCL in nixpkgs is, by default, not compiled with zlib support.
+  genDumpLisp = name: main: deps: writeText "dump.lisp" ''
+    (require 'sb-posix)
+
+    ${genLoadLisp deps}
+
+    (let* ((bindir (concatenate 'string (sb-posix:getenv "out") "/bin"))
+           (outpath (make-pathname :name "${name}"
+                                   :directory bindir)))
+      (save-lisp-and-die outpath
+                         :executable t
+                         :toplevel (function ${main})
+                         :purify t))
+    ;;
+  '';
 
   # Add an `overrideLisp` attribute to a function result that works
   # similar to `overrideAttrs`, but is used specifically for the
@@ -73,6 +88,10 @@ let
     overrideLisp = new: makeOverridable f (orig // (new orig));
   };
 
+  #
+  # Public API functions
+  #
+
   # 'library' builds a list of Common Lisp files into a single FASL
   # which can then be loaded into SBCL.
   library = { name, srcs, deps ? [] }: runCommandNoCC "${name}-cllib" {} ''
@@ -85,7 +104,14 @@ let
 
   # 'program' creates an executable containing a dumped image of the
   # specified sources and dependencies.
-  program = {};
+  program = { name, main ? "${name}:main", srcs, deps ? [] }: runCommandNoCC "${name}" {} ''
+    mkdir -p $out/bin
+    ${sbcl}/bin/sbcl --script ${
+      genDumpLisp name main (lib.singleton (library {
+        inherit name srcs deps;
+      }))
+    }
+  '';
 
   # 'sbclWith' creates an image with the specified libraries /
   # programs loaded.