From 0a24fdf7548e255656fe19aaa8cf96b4fa329b39 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 15 Feb 2018 13:51:22 -0600 Subject: [PATCH 1/7] Add skip-safety option --- nix-buffer.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nix-buffer.el b/nix-buffer.el index f0b5bf8..37b3dea 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -102,12 +102,13 @@ LISP-FILE The file in question." (defvar nix-buffer-after-load-hook nil "Hook run after ‘nix-buffer’ loads an expression.") -(defun nix-buffer--load-result (expr-file out) +(defun nix-buffer--load-result (expr-file out &optional skip-safety) "Load the result of a ‘nix-buffer’ build, checking for safety. EXPR-FILE The nix expression being built. OUT The build result." - (when (or (gethash out nix-buffer--trusted-exprs) + (when (or skip-safety + (gethash out nix-buffer--trusted-exprs) (nix-buffer--query-safety expr-file out)) (load out t t nil t) (run-hooks 'nix-buffer-after-load-hook))) @@ -138,7 +139,7 @@ EVENT The process status change event string." (ignore-errors (delete-file out-link)) (unless (string= last-out cur-out) (with-current-buffer user-buf - (nix-buffer--load-result expr-file cur-out))))) + (nix-buffer--load-result expr-file cur-out nil))))) (with-current-buffer (get-buffer-create "*nix-buffer errors*") (insert "nix-build for nix-buffer for " @@ -181,7 +182,7 @@ EXPR-FILE The file containing the nix expression to build." err) :stderr err) (when current-out - (nix-buffer--load-result expr-file current-out)))) + (nix-buffer--load-result expr-file current-out nil)))) (defcustom nix-buffer-root-file "dir-locals.nix" "File name to use for determining Nix expression to use." From f2e8a93264b910bed7e786a7d464c9397033879a Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 15 Feb 2018 13:52:07 -0600 Subject: [PATCH 2/7] Make root optional --- nix-buffer.el | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/nix-buffer.el b/nix-buffer.el index 37b3dea..ec7a977 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -153,7 +153,7 @@ EVENT The process status change event string." (kill-buffer out-buf) (kill-buffer err-buf)))))) -(defun nix-buffer--nix-build (root expr-file) +(defun nix-buffer--nix-build (expr-file &optional root) "Start the nix build. ROOT The path we started from. @@ -162,17 +162,18 @@ EXPR-FILE The file containing the nix expression to build." (nix-buffer--unique-filename root))) (out-link (f-join state-dir "result")) (current-out (file-symlink-p out-link)) - (err (generate-new-buffer " nix-buffer-nix-build-stderr"))) + (err (generate-new-buffer " nix-buffer-nix-build-stderr")) + (command (list "nix-build" expr-file + "--out-link" out-link))) (ignore-errors (make-directory state-dir t)) + (when root + (push "--arg" 'command) + (push "root" 'command) + (push root 'command)) (make-process :name "nix-buffer-nix-build" :buffer (generate-new-buffer " nix-buffer-nix-build-stdout") - :command (list - "nix-build" - "--arg" "root" root - "--out-link" out-link - expr-file - ) + :command command :noquery t :sentinel (apply-partially 'nix-buffer--sentinel out-link @@ -225,7 +226,7 @@ is removed." (expr-dir (locate-dominating-file root nix-buffer-root-file))) (when expr-dir (let ((expr-file (f-expand nix-buffer-root-file expr-dir))) - (nix-buffer--nix-build root expr-file))))) + (nix-buffer--nix-build expr-file root))))) (add-hook 'kill-emacs-hook 'nix-buffer-unload-function) From d93aef4fdc9d74768a4ba1798d0e503bfd43d52b Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 15 Feb 2018 14:56:19 -0600 Subject: [PATCH 3/7] Add nix-buffer-with-string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add-nix-buffer-with-string can be used to call nix-buffer with a string so you don’t need to have a dir-locals.nix This is how I set it up in a Haskell program: (put at the end of the file) -- Local Variables: -- eval: (nix-buffer-with-string "let pkgs = import {}; -- in pkgs.nixBufferBuilders.withPackages -- [(pkgs.haskellPackages.ghcWithPackages (self: with self; [parsec QuickCheck hpc]))]") -- End: Fixes #8 --- nix-buffer.el | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/nix-buffer.el b/nix-buffer.el index ec7a977..267c4ca 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -114,7 +114,7 @@ OUT The build result." (run-hooks 'nix-buffer-after-load-hook))) (defun nix-buffer--sentinel - (out-link last-out expr-file user-buf err-buf process event) + (out-link last-out expr-file user-buf err-buf skip-safety process event) "Handle the results of the nix build. OUT-LINK The path to the output symlink. @@ -126,6 +126,8 @@ USER-BUF The buffer to apply the results to. ERR-BUF The standard error buffer of the nix-build +SKIP-SAFETY Skip safety checks. + PROCESS The process whose status changed. EVENT The process status change event string." @@ -139,7 +141,7 @@ EVENT The process status change event string." (ignore-errors (delete-file out-link)) (unless (string= last-out cur-out) (with-current-buffer user-buf - (nix-buffer--load-result expr-file cur-out nil))))) + (nix-buffer--load-result expr-file cur-out skip-safety))))) (with-current-buffer (get-buffer-create "*nix-buffer errors*") (insert "nix-build for nix-buffer for " @@ -153,13 +155,14 @@ EVENT The process status change event string." (kill-buffer out-buf) (kill-buffer err-buf)))))) -(defun nix-buffer--nix-build (expr-file &optional root) +(defun nix-buffer--nix-build (expr-file &optional root skip-safety) "Start the nix build. ROOT The path we started from. EXPR-FILE The file containing the nix expression to build." (let* ((state-dir (f-join nix-buffer-directory-name - (nix-buffer--unique-filename root))) + (nix-buffer--unique-filename (or root + default-directory)))) (out-link (f-join state-dir "result")) (current-out (file-symlink-p out-link)) (err (generate-new-buffer " nix-buffer-nix-build-stderr")) @@ -180,16 +183,26 @@ EXPR-FILE The file containing the nix expression to build." current-out expr-file (current-buffer) - err) + err + skip-safety) :stderr err) (when current-out - (nix-buffer--load-result expr-file current-out nil)))) + (nix-buffer--load-result expr-file current-out skip-safety)))) (defcustom nix-buffer-root-file "dir-locals.nix" "File name to use for determining Nix expression to use." :group 'nix-buffer :type '(string)) +;;;###autoload +(defun nix-buffer-with-string (string) + "Start ‘nix-buffer’ but with a string expression." + (interactive) + (let ((expr-file (make-temp-file "nix-buffer"))) + (with-temp-file expr-file + (insert string)) + (nix-buffer--nix-build expr-file nil t))) + ;;;###autoload (defun nix-buffer () "Set up the buffer according to the directory-local nix expression. @@ -226,7 +239,7 @@ is removed." (expr-dir (locate-dominating-file root nix-buffer-root-file))) (when expr-dir (let ((expr-file (f-expand nix-buffer-root-file expr-dir))) - (nix-buffer--nix-build expr-file root))))) + (nix-buffer--nix-build expr-file root nil))))) (add-hook 'kill-emacs-hook 'nix-buffer-unload-function) From e82a4fd50550c49f164a59add0ba1a758e9a64ac Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 15 Feb 2018 14:59:56 -0600 Subject: [PATCH 4/7] Fix checkdoc --- nix-buffer.el | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nix-buffer.el b/nix-buffer.el index 267c4ca..d82e3ac 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -106,7 +106,9 @@ LISP-FILE The file in question." "Load the result of a ‘nix-buffer’ build, checking for safety. EXPR-FILE The nix expression being built. -OUT The build result." +OUT The build result. + +SKIP-SAFETY whether to skip safety checks." (when (or skip-safety (gethash out nix-buffer--trusted-exprs) (nix-buffer--query-safety expr-file out)) @@ -157,9 +159,11 @@ EVENT The process status change event string." (defun nix-buffer--nix-build (expr-file &optional root skip-safety) "Start the nix build. +EXPR-FILE The file containing the nix expression to build. + ROOT The path we started from. -EXPR-FILE The file containing the nix expression to build." +SKIP-SAFETY whether to skip the safety checks." (let* ((state-dir (f-join nix-buffer-directory-name (nix-buffer--unique-filename (or root default-directory)))) @@ -195,12 +199,12 @@ EXPR-FILE The file containing the nix expression to build." :type '(string)) ;;;###autoload -(defun nix-buffer-with-string (string) - "Start ‘nix-buffer’ but with a string expression." +(defun nix-buffer-with-string (expression) + "Start ‘nix-buffer’ but with a string EXPRESSION." (interactive) (let ((expr-file (make-temp-file "nix-buffer"))) (with-temp-file expr-file - (insert string)) + (insert expression)) (nix-buffer--nix-build expr-file nil t))) ;;;###autoload From 25b04975a0ffb855c8cca7e767c7d52c35f32cad Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 15 Feb 2018 15:58:34 -0600 Subject: [PATCH 5/7] Cache based on buffer file name not default directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each file will have a unique Nix expression so use that as our ‘state-dir’ name --- nix-buffer.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix-buffer.el b/nix-buffer.el index d82e3ac..1168456 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -166,7 +166,7 @@ ROOT The path we started from. SKIP-SAFETY whether to skip the safety checks." (let* ((state-dir (f-join nix-buffer-directory-name (nix-buffer--unique-filename (or root - default-directory)))) + buffer-file-name)))) (out-link (f-join state-dir "result")) (current-out (file-symlink-p out-link)) (err (generate-new-buffer " nix-buffer-nix-build-stderr")) From ce9a1b5145f8a011d071c1bc6a67f0808dea892f Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Fri, 16 Feb 2018 23:30:39 -0600 Subject: [PATCH 6/7] Make nix-buffer-with-string interactive --- nix-buffer.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix-buffer.el b/nix-buffer.el index 1168456..154e5bf 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -201,7 +201,7 @@ SKIP-SAFETY whether to skip the safety checks." ;;;###autoload (defun nix-buffer-with-string (expression) "Start ‘nix-buffer’ but with a string EXPRESSION." - (interactive) + (interactive "sNix expression: ") (let ((expr-file (make-temp-file "nix-buffer"))) (with-temp-file expr-file (insert expression)) From 32c9bc3456d1bfb5d1ab3aadea37bf069e761074 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Fri, 16 Feb 2018 23:39:46 -0600 Subject: [PATCH 7/7] Fixup to use nix-build -E --- nix-buffer.el | 59 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/nix-buffer.el b/nix-buffer.el index 154e5bf..7b5f581 100644 --- a/nix-buffer.el +++ b/nix-buffer.el @@ -157,30 +157,26 @@ EVENT The process status change event string." (kill-buffer out-buf) (kill-buffer err-buf)))))) -(defun nix-buffer--nix-build (expr-file &optional root skip-safety) +(defun nix-buffer--nix-build (root expr-file) "Start the nix build. EXPR-FILE The file containing the nix expression to build. -ROOT The path we started from. - -SKIP-SAFETY whether to skip the safety checks." +ROOT The path we started from." (let* ((state-dir (f-join nix-buffer-directory-name - (nix-buffer--unique-filename (or root - buffer-file-name)))) + (nix-buffer--unique-filename root))) (out-link (f-join state-dir "result")) (current-out (file-symlink-p out-link)) - (err (generate-new-buffer " nix-buffer-nix-build-stderr")) - (command (list "nix-build" expr-file - "--out-link" out-link))) + (err (generate-new-buffer " nix-buffer-nix-build-stderr"))) (ignore-errors (make-directory state-dir t)) - (when root - (push "--arg" 'command) - (push "root" 'command) - (push root 'command)) (make-process :name "nix-buffer-nix-build" :buffer (generate-new-buffer " nix-buffer-nix-build-stdout") - :command command + :command (list + "nix-build" + "--arg" "root" root + "--out-link" out-link + expr-file + ) :noquery t :sentinel (apply-partially 'nix-buffer--sentinel out-link @@ -188,10 +184,10 @@ SKIP-SAFETY whether to skip the safety checks." expr-file (current-buffer) err - skip-safety) + nil) :stderr err) (when current-out - (nix-buffer--load-result expr-file current-out skip-safety)))) + (nix-buffer--load-result expr-file current-out nil)))) (defcustom nix-buffer-root-file "dir-locals.nix" "File name to use for determining Nix expression to use." @@ -202,10 +198,31 @@ SKIP-SAFETY whether to skip the safety checks." (defun nix-buffer-with-string (expression) "Start ‘nix-buffer’ but with a string EXPRESSION." (interactive "sNix expression: ") - (let ((expr-file (make-temp-file "nix-buffer"))) - (with-temp-file expr-file - (insert expression)) - (nix-buffer--nix-build expr-file nil t))) + (let* ((state-dir (f-join nix-buffer-directory-name + (secure-hash 'sha256 expression))) + (out-link (f-join state-dir "result")) + (current-out (file-symlink-p out-link)) + (err (generate-new-buffer " nix-buffer-nix-build-stderr"))) + (ignore-errors (make-directory state-dir t)) + (make-process + :name "nix-buffer-nix-build" + :buffer (generate-new-buffer " nix-buffer-nix-build-stdout") + :command (list + "nix-build" + "--out-link" out-link + "-E" expression + ) + :noquery t + :sentinel (apply-partially 'nix-buffer--sentinel + out-link + current-out + nil + (current-buffer) + err + t) + :stderr err) + (when current-out + (nix-buffer--load-result nil current-out t)))) ;;;###autoload (defun nix-buffer () @@ -243,7 +260,7 @@ is removed." (expr-dir (locate-dominating-file root nix-buffer-root-file))) (when expr-dir (let ((expr-file (f-expand nix-buffer-root-file expr-dir))) - (nix-buffer--nix-build expr-file root nil))))) + (nix-buffer--nix-build root expr-file))))) (add-hook 'kill-emacs-hook 'nix-buffer-unload-function)