branch: elpa/nix-mode
commit 7556c032dc0c84a7d3b982169205102dcbf0dc42
Author: Zachary Newman <[email protected]>
Commit: Zachary Newman <[email protected]>
Use the located nixfmt binary for formatting.
Allows use with `nixfmt` installed using direnv ([envrc-mode]).
Before:
1. `nix-format-buffer` checks for `nixfmt`: `(executable-find
nix-nixfmt-bin)`
2. If it's missing, error. Otherwise, disregard the path to `nixfmt`
that we just found.
3. Open a new buffer (if using `envrc-mode`, with a totally different
`$PATH`) and try to run `nix-nixfmt-bin`.
If you had `nixfmt` installed in your direnv but not globally, this
gives an error: "Searching for program: No such file or directory, nixfmt".
Now, we use the path that we just found and `call-process-region` on that.
[envrc-mode]: https://github.com/purcell/envrc
---
nix-format.el | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/nix-format.el b/nix-format.el
index f43dc4d908..1f97e8a086 100644
--- a/nix-format.el
+++ b/nix-format.el
@@ -13,24 +13,29 @@
:group 'nix
:type 'string)
-(defun nix--format-call (buf)
+(defun nix--format-call (buf nixfmt-bin)
"Format BUF using nixfmt."
(with-current-buffer (get-buffer-create "*nixfmt*")
(erase-buffer)
(insert-buffer-substring buf)
- (if (zerop (call-process-region (point-min) (point-max) nix-nixfmt-bin t t
nil))
+ (if (zerop (call-process-region (point-min) (point-max) nixfmt-bin t t
nil))
(progn
(if (not (string= (buffer-string) (with-current-buffer buf
(buffer-string))))
(copy-to-buffer buf (point-min) (point-max)))
(kill-buffer))
(error "Nixfmt failed, see *nixfmt* buffer for details"))))
+(defun nix--find-nixfmt ()
+ "Find the nixfmt binary, or error if it's missing."
+ (let ((nixfmt-bin (executable-find nix-nixfmt-bin)))
+ (unless nixfmt-bin
+ (error "Could not locate executable \"%s\"" nix-nixfmt-bin))
+ nixfmt-bin))
+
(defun nix-format-buffer ()
"Format the current buffer using nixfmt."
(interactive)
- (unless (executable-find nix-nixfmt-bin)
- (error "Could not locate executable \"%s\"" nix-nixfmt-bin))
- (nix--format-call (current-buffer))
+ (nix--format-call (current-buffer) (nix--find-nixfmt))
(message "Formatted buffer with nixfmt."))
(provide 'nix-format)