[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
- [nongnu] elpa/nix-mode 5b5961780f 358/500: Merge pull request #90 from purcell/dash-syntax, (continued)
- [nongnu] elpa/nix-mode 5b5961780f 358/500: Merge pull request #90 from purcell/dash-syntax, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode aa1fa07344 342/500: Fix handling of keywords in identifiers, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 7d681ff81b 357/500: Merge pull request #91 from purcell/relative-executable-paths, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 47ec698474 422/500: Add version to invocation of define-obsolete-function-alias, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode e32c6bf7ad 415/500: Merge pull request #115 from taku0/ffap-in-string, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 30587b2205 411/500: Merge pull request #114 from TristanCacqueray/master, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 471a90ac96 394/500: Release 1.4.5, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode fe00d5930b 423/500: Merge pull request #119 from NixOS/derived-nix-drv-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 7e081a560b 427/500: nix-drv-mode.el: make regexp more selective, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 91e1a05ea6 399/500: Merge pull request #107 from rossabaker/check-for-proc, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode b4b7518231 441/500: Refactor process calling,
ELPA Syncer <=
- [nongnu] elpa/nix-mode 75d2111891 433/500: Merge pull request #129 from nagy/nix-search-separate, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 949cb98ba5 432/500: Set meta.description, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode a8a513770a 436/500: Simplify some functions, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 7f689c4161 450/500: In flake.nix, split name into pname and version, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode b12fa70dd7 446/500: Indicate constants, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 53d1fb9687 469/500: nix-flake: Add nix-flake-init, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1604a9cec8 484/500: nix-flake: Allow updating the lockfile, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode e167b82e64 493/500: nix-flake: Fix inconsistent state after switching the flake, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 3d04d92d9c 496/500: Merge pull request #144 from akirak/update-cask-on-flake, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode cf957244e7 499/500: Merge pull request #149 from akirak/transient-dep, ELPA Syncer, 2022/01/29