Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make safety checks optional and add nix-buffer-with-string #12

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions nix-buffer.el
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,21 @@ 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)
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))
(load out t t nil t)
(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.

Expand All @@ -125,6 +128,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."
Expand All @@ -138,7 +143,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 skip-safety)))))
(with-current-buffer
(get-buffer-create "*nix-buffer errors*")
(insert "nix-build for nix-buffer for "
Expand All @@ -154,9 +159,9 @@ EVENT The process status change event string."

(defun nix-buffer--nix-build (root expr-file)
"Start the nix build.
ROOT The path we started from.
EXPR-FILE The file containing the nix expression to build.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be switched back?


EXPR-FILE The file containing the nix expression to build."
ROOT The path we started from."
(let* ((state-dir (f-join nix-buffer-directory-name
(nix-buffer--unique-filename root)))
(out-link (f-join state-dir "result"))
Expand All @@ -178,16 +183,47 @@ EXPR-FILE The file containing the nix expression to build."
current-out
expr-file
(current-buffer)
err)
err
nil)
: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."
:group 'nix-buffer
:type '(string))

;;;###autoload
(defun nix-buffer-with-string (expression)
"Start ‘nix-buffer’ but with a string EXPRESSION."
(interactive "sNix expression: ")
(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 ()
"Set up the buffer according to the directory-local nix expression.
Expand Down