branch: elpa/nix-mode
commit c19e103eee7091a79cdc9ecff320a3cea640abab
Author: Matthew Bauer <[email protected]>
Commit: Matthew Bauer <[email protected]>
Add support nix 2.4 flakes in search
This lets you search flakes like so:
M-x nix-search<RET>
abc
---
nix-search.el | 30 ++++++++++++++++++------------
nix-shell.el | 13 +++++++++++++
nix.el | 26 +++++++++++++++++++++++++-
3 files changed, 56 insertions(+), 13 deletions(-)
diff --git a/nix-search.el b/nix-search.el
index ccd0f03645..435e17d45a 100644
--- a/nix-search.el
+++ b/nix-search.el
@@ -17,24 +17,29 @@
(require 'json)
;;;###autoload
-(defun nix-search--search (search file &optional no-cache)
+(defun nix-search--search (search file &optional no-cache use-flakes)
(with-temp-buffer
- (call-process nix-executable nil (list t nil) nil
- "search" "--json" (if no-cache "--no-cache" "") "--file" file search)
+ (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)))
;;;###autoload
-(defun nix-search--display (results &optional display-buffer)
+(defun nix-search--display (results &optional display-buffer use-flakes)
(unless display-buffer (setq display-buffer (generate-new-buffer "*nix
search*")))
(with-current-buffer display-buffer
(dolist (entry results)
(widget-insert
- (format "attr: %s\nname: %s\nversion: %s\ndescription: %s\n\n"
- (car entry)
- (alist-get 'pkgName (cdr entry))
- (alist-get 'version (cdr entry))
- (alist-get 'description (cdr entry))))))
+ (format "attr: %s\nname: %s\nversion: %s\ndescription: %s\n\n"
+ (car entry)
+ (if use-flakes
+ (alist-get 'pname (cdr entry))
+ (alist-get 'pkgName (cdr entry)))
+ (alist-get 'version (cdr entry))
+ (alist-get 'description (cdr entry))))))
(display-buffer display-buffer))
;;;###autoload
@@ -43,10 +48,11 @@
SEARCH a search term to use.
FILE a Nix expression to search in."
(interactive "snix-search> \n")
- (setq file (or file (nix-read-file)))
- (let ((results (nix-search--search search file)))
+ (setq use-flakes (nix-has-flakes))
+ (setq file (or file (if use-flakes (nix-read-flake) (nix-read-file))))
+ (let ((results (nix-search--search search file nil use-flakes)))
(when (called-interactively-p 'any)
- (nix-search--display results))
+ (nix-search--display results nil use-flakes))
results))
(defun nix-search-read-attr (file)
diff --git a/nix-shell.el b/nix-shell.el
index e8b5c8d21f..dcab7e5df4 100644
--- a/nix-shell.el
+++ b/nix-shell.el
@@ -60,6 +60,12 @@ Should only be set in dir-locals.el file."
:type 'stringp
:group 'nix-shell)
+(defcustom nix-flake nil
+ "Nix flake to build expressions from.
+Should only be set in dir-locals.el file."
+ :type 'stringp
+ :group 'nix-shell)
+
(defcustom nix-attr nil
"Nix attribute path to use.
Should only be set in dir-locals.el file."
@@ -80,6 +86,13 @@ ATTR is the attribute to unpack."
"Get nix attribute from user."
(read-string "Nix attr: "))
+(defun nix-read-flake ()
+ "Get nix flake from user."
+ (cond
+ (nix-flake nix-flake)
+ ((and (nix-has-flakes) (file-exists-p "flake.nix")) ".")
+ (t (read-string "Nix flake: " "nixpkgs"))))
+
(defun nix-read-file ()
"Get nix file from user."
(cond
diff --git a/nix.el b/nix.el
index 0507ae7e7d..ede66d7790 100644
--- a/nix.el
+++ b/nix.el
@@ -78,6 +78,17 @@
(kill-buffer stdout)
result)))
+(defun nix-show-config ()
+ "Show nix config."
+ (let ((stdout (generate-new-buffer "nix config"))
+ result)
+ (call-process nix-executable nil (list stdout nil) nil "show-config"
"--json")
+ (setq result (with-current-buffer stdout
+ (goto-char (point-min))
+ (json-read)))
+ (kill-buffer stdout)
+ result))
+
(defvar nix-commands
'("add-to-store"
"build"
@@ -187,10 +198,23 @@ OPTIONS a list of options to accept."
((or (string= "-s" last-arg) (string= "--substituter" last-arg))
(pcomplete-here))))))
+(defun nix-is-24 ()
+ "Whether Nix is a version with Flakes support."
+ ;; earlier versions reported as 3, now it’s just nix-2.4
+ (let ((version (nix-version)))
+ (or (string-prefix-p "nix (Nix) 3" version)
+ (string-prefix-p "nix (Nix) 2.4" version))))
+
+(defun nix-has-flakes ()
+ "Whether Nix is a version with Flakes support."
+ ;; earlier versions reported as 3, now it’s just nix-2.4
+ (and (nix-is-24)
+ (seq-contains-p (alist-get 'value (alist-get 'experimental-features
(nix-show-config))) "flakes")))
+
;;;###autoload
(defun pcomplete/nix ()
"Completion for the nix command."
- (if (string-prefix-p "nix (Nix) 3" (nix-version))
+ (if (nix-is-24)
(let ((stdout (generate-new-buffer "nix-completions"))
(process-environment
(cons (format "NIX_GET_COMPLETIONS=%s" (1- (length
pcomplete-args)))