emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/nix-mode b4b7518231 441/500: Refactor process calling


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode b4b7518231 441/500: Refactor process calling
Date: Sat, 29 Jan 2022 08:27:54 -0500 (EST)

branch: elpa/nix-mode
commit b4b75182313c135110069275833efa94d0a19621
Author: Daniel Nagy <danielnagy@posteo.de>
Commit: Daniel Nagy <danielnagy@posteo.de>

    Refactor process calling
    
    This commit reworks the usage of calling a nix process. With this you
    can now get more meaningful error messages:
    
    ``` emacs-lisp
    ELISP> (nix-instantiate--parsed "xxx")
    *** Eval error ***  error: attribute ’xxx’ in selection path ’xxx’ not found
    ```
    
    Before this commit, a call such as this would result in:
    ``` emacs-lisp
    ELISP> (nix-instantiate--parsed "xxx")
    *** Eval error ***  Nix’s show-derivation xxx failed to produce any output
    ```
---
 flake.nix          |  1 +
 nix-instantiate.el | 15 ++-------------
 nix-mode.el        |  8 +++-----
 nix-search.el      | 12 ++++--------
 nix.el             | 47 ++++++++++++++++++++++++++++++++++-------------
 5 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/flake.nix b/flake.nix
index 8c16c7ddfa..cc5199796c 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,6 +13,7 @@
           org-plus-contrib
           company
           mmm-mode
+          f
         ]);
       in stdenvNoCC.mkDerivation {
         name = "nix-mode-1.4.5";
diff --git a/nix-instantiate.el b/nix-instantiate.el
index 9f047d2170..e7230ed0f1 100644
--- a/nix-instantiate.el
+++ b/nix-instantiate.el
@@ -17,19 +17,8 @@
 (defun nix-instantiate--parsed (drv)
   "Get the parsed version of the .drv file.
 DRV file to load from."
-  (let ((stdout (generate-new-buffer "nix show-derivation"))
-       result)
-    (call-process nix-executable nil (list stdout nil) nil
-                 "show-derivation" drv)
-    (setq result
-         (cdar (with-current-buffer stdout
-                 (when (eq (buffer-size) 0)
-                   (error "Nix’s show-derivation %s failed to produce any 
output"
-                          drv))
-                 (goto-char (point-min))
-                 (json-read))))
-    (kill-buffer stdout)
-    result))
+  (cdar
+    (nix--process-json "show-derivation" drv)))
 
 (defun nix-instantiate (nix-file &optional attribute parse)
   "Run nix-instantiate on a Nix expression.
diff --git a/nix-mode.el b/nix-mode.el
index 0c1c0596f6..1d79abea5d 100644
--- a/nix-mode.el
+++ b/nix-mode.el
@@ -913,11 +913,9 @@ location of STR. If `nix-instantiate' has a nonzero exit 
code,
 don’t do anything"
   (when (and (string-match nix-re-bracket-path str)
              (executable-find nix-instantiate-executable))
-    (with-temp-buffer
-      (when (eq (call-process nix-instantiate-executable nil (current-buffer)
-                              nil "--eval" "-E" str) 0)
-        ;; Remove trailing newline
-        (substring (buffer-string) 0 (- (buffer-size) 1))))))
+    (let ((nix-executable nix-instantiate-executable))
+      (ignore-errors
+       (nix--process-string "--eval" "-E" str)))))
 
 ;; Key maps
 
diff --git a/nix-search.el b/nix-search.el
index 38116bf678..beaebb111e 100644
--- a/nix-search.el
+++ b/nix-search.el
@@ -18,14 +18,10 @@
 
 ;;;###autoload
 (defun nix-search--search (search file &optional no-cache use-flakes)
-  (with-temp-buffer
-    (if use-flakes
-       (call-process nix-executable nil (list t nil) nil
-                     "search" "--json" file (if (string= search "") "." 
search))
-      (call-process nix-executable nil (list t nil) nil
-                   "search" "--json" (if no-cache "--no-cache" "") "--file" 
file search))
-    (goto-char (point-min))
-    (json-read)))
+  (nix--process-json-nocheck "search" "--json"
+    (if use-flakes "" "--file") file
+    (if no-cache  "--no-cache" "")
+    search))
 
 (defface nix-search-pname
   '((t :height 1.5
diff --git a/nix.el b/nix.el
index 35735abaef..48f802c005 100644
--- a/nix.el
+++ b/nix.el
@@ -18,6 +18,7 @@
 
 (require 'pcomplete)
 (require 'json)
+(require 'f)
 
 (defgroup nix nil
   "Nix-related customizations"
@@ -60,26 +61,20 @@
 
 (defun nix-system ()
   "Get the current system tuple."
-  (with-temp-buffer
-    (if (nix-is-24)
-       (call-process nix-executable nil (list t nil) nil "eval" "--impure" 
"--raw" "--expr" "(builtins.currentSystem)")
-      (call-process nix-executable nil (list t nil) nil "eval" "--raw" 
"(builtins.currentSystem)"))
-    (buffer-string)))
+  (nix--process-string "eval"
+    "--raw"
+    (if (nix-is-24) "--impure" )
+    (if (nix-is-24) "--expr" )
+    "(builtins.currentSystem)"))
 
 (defvar nix-version nil)
 (defun nix-version ()
   "Get the version of Nix"
-  (or nix-version
-    (with-temp-buffer
-      (call-process nix-executable nil (list t nil) nil "--version")
-      (buffer-string))))
+  (or nix-version (nix--process-string "--version")))
 
 (defun nix-show-config ()
   "Show nix config."
-  (with-temp-buffer
-    (call-process nix-executable nil (list t nil) nil "show-config" "--json")
-    (goto-char (point-min))
-    (json-read)))
+  (nix--process-json "show-config" "--json"))
 
 (defvar nix-commands
   '("add-to-store"
@@ -331,5 +326,31 @@ OPTIONS a list of options to accept."
         (_ (nix--pcomplete-flags nix-toplevel-options)))
       (pcomplete-here (pcomplete-entries)))))
 
+(defun nix--process (&rest args)
+  (with-temp-buffer
+    (let* ((tmpfile  (make-temp-file "nix--process-stderr"))
+        (cleaned-args (seq-filter #'stringp args))
+        (exitcode (apply #'call-process `(,nix-executable nil (t ,tmpfile) nil 
,@cleaned-args )))
+        (stderr (f-read-text tmpfile)))
+      (delete-file tmpfile)
+      (list (buffer-string) stderr exitcode))))
+
+(defun nix--process-string (&rest args)
+  (cl-multiple-value-bind (stdout stderr exitcode) (apply #'nix--process args)
+    (if (not (eq exitcode 0))
+      (error stderr))
+    ;; cut-off the trailing newline
+    (string-trim-right stdout)))
+
+(defun nix--process-json (&rest args)
+  (json-read-from-string
+    (apply #'nix--process-string args)))
+
+(defun nix--process-json-nocheck (&rest args)
+  ;; No checking of exitcode is possible here until
+  ;; https://github.com/NixOS/nix/issues/2474 is resolved
+  (let ((result (apply #'nix--process args)))
+    (json-read-from-string (car result))))
+
 (provide 'nix)
 ;;; nix.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]