Skip to content
Draft
Changes from 5 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
55 changes: 37 additions & 18 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 @@ -152,42 +157,56 @@ 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 skip-safety)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Erm, why is root optional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess it will break anything that needs 'root' like haskellMonoRepo. My thinking was in the context of 'nix-buffer-with-string' you wouldn't want to have a 'root' path set. For instance, you could use it in a script that didn't have any directory or buffer associated.

"Start the nix build.
EXPR-FILE The file containing the nix expression to build.

Copy link
Copy Markdown
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?


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 root)))
(nix-buffer--unique-filename (or root
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")))
(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
current-out
expr-file
(current-buffer)
err)
err
skip-safety)
:stderr err)
(when current-out
(nix-buffer--load-result expr-file current-out))))
(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 (expression)
"Start ‘nix-buffer’ but with a string EXPRESSION."
(interactive)
(let ((expr-file (make-temp-file "nix-buffer")))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Hm, can't we just use nix-build -E instead of making a temp file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually that's probably a good idea. I had thought it would save on code duplication but it won't make a big difference.

(with-temp-file expr-file
(insert expression))
(nix-buffer--nix-build expr-file nil t)))

;;;###autoload
(defun nix-buffer ()
"Set up the buffer according to the directory-local nix expression.
Expand Down Expand Up @@ -224,7 +243,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 nil)))))

(add-hook 'kill-emacs-hook 'nix-buffer-unload-function)

Expand Down