about summary refs log tree commit diff
path: root/tools
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-01-17T16·43+0000
committerVincent Ambo <tazjin@google.com>2020-01-17T16·43+0000
commit381a859b3ba8d3c60ce38c5ba81ec08c9da03f74 (patch)
tree6b5303b05536bed9a15dc74c803fdf8bbbf52f81 /tools
parent21e9a65a3593b3770a93026ea05bcfbe9e2f6b04 (diff)
feat(emacs-pkgs/nix-util): Add nix/sly-from-depot function r/387
Adds a function that can launch Sly with a pre-configured SBCL for a
Lisp derivation in the depot.

This makes it convenient to spin up development environments for Lisp
libraries and programs by simply calling `M-x nix/sly-from-depot RET
tools.something`.

This relies on `nix-depot-path` being configured currently as I have
not yet reliably added the depot to my NIX_PATH on all machines.
Diffstat (limited to 'tools')
-rw-r--r--tools/emacs-pkgs/nix-util/nix-util.el38
1 files changed, 37 insertions, 1 deletions
diff --git a/tools/emacs-pkgs/nix-util/nix-util.el b/tools/emacs-pkgs/nix-util/nix-util.el
index 533e7e6f34c9..b561ead16cf2 100644
--- a/tools/emacs-pkgs/nix-util/nix-util.el
+++ b/tools/emacs-pkgs/nix-util/nix-util.el
@@ -9,11 +9,13 @@
 ;;; Commentary:
 ;;
 ;; This package adds some functionality that I find useful when
-;; working in Nix buffers.
+;; working in Nix buffers or programs installed from Nix.
 
 (require 'json)
 (require 'map)
 
+(defvar nix-depot-path "/home/tazjin/depot")
+
 (defun nix/prefetch-github (owner repo) ; TODO(tazjin): support different branches
   "Fetch the master branch of a GitHub repository and insert the
   call to `fetchFromGitHub' at point."
@@ -64,4 +66,38 @@
                   :stderr errbuf
                   :sentinel prefetch-handler)))
 
+(defun nix/sly-from-depot (attribute)
+  "Start a Sly REPL configured with a Lisp matching a derivation
+  from my depot.
+
+  The derivation invokes nix.buildLisp.sbclWith and is built
+  asynchronously. The build output is included in the error
+  thrown on build failures."
+
+  (interactive "sAttribute: ")
+  (let* ((outbuf (get-buffer-create (format "*depot-out/%s*" attribute)))
+         (errbuf (get-buffer-create (format "*depot-errors/%s*" attribute)))
+         (expression (format "let depot = import <depot> {}; in depot.nix.buildLisp.sbclWith [ depot.%s ]" attribute))
+         (command (list "nix-build" "-I" (format "depot=%s" nix-depot-path) "-E" expression))
+         (build-handler
+          (lambda (process event)
+            (unwind-protect
+                (pcase event
+                  ("finished\n"
+                   (let* ((outpath (s-trim (with-current-buffer outbuf (buffer-string))))
+                          (lisp-path (s-concat outpath "/bin/sbcl")))
+                     (message "Acquired Lisp for <depot>.%s at %s" attribute lisp-path)
+                     (sly lisp-path)))
+                  (_ (with-current-buffer errbuf
+                       (error "Failed to build '%s':\n%s" attribute (buffer-string)))))
+              (kill-buffer outbuf)
+              (kill-buffer errbuf)))))
+
+    (message "Acquiring Lisp for <depot>.%s" attribute)
+    (make-process :name (format "depot-nix-build/%s" attribute)
+                  :buffer outbuf
+                  :stderr errbuf
+                  :sentinel build-handler
+                  :command command))) ; TODO(tazjin): use <depot>
+
 (provide 'nix-util)