Skip to content

Commit c19e103

Browse files
committed
Add support nix 2.4 flakes in search
This lets you search flakes like so: M-x nix-search<RET> abc
1 parent 0de9c70 commit c19e103

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

nix-search.el

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,29 @@
1717
(require 'json)
1818

1919
;;;###autoload
20-
(defun nix-search--search (search file &optional no-cache)
20+
(defun nix-search--search (search file &optional no-cache use-flakes)
2121
(with-temp-buffer
22-
(call-process nix-executable nil (list t nil) nil
23-
"search" "--json" (if no-cache "--no-cache" "") "--file" file search)
22+
(if use-flakes
23+
(call-process nix-executable nil (list t nil) nil
24+
"search" "--json" file (if (string= search "") "." search))
25+
(call-process nix-executable nil (list t nil) nil
26+
"search" "--json" (if no-cache "--no-cache" "") "--file" file search))
2427
(goto-char (point-min))
2528
(json-read)))
2629

2730
;;;###autoload
28-
(defun nix-search--display (results &optional display-buffer)
31+
(defun nix-search--display (results &optional display-buffer use-flakes)
2932
(unless display-buffer (setq display-buffer (generate-new-buffer "*nix search*")))
3033
(with-current-buffer display-buffer
3134
(dolist (entry results)
3235
(widget-insert
33-
(format "attr: %s\nname: %s\nversion: %s\ndescription: %s\n\n"
34-
(car entry)
35-
(alist-get 'pkgName (cdr entry))
36-
(alist-get 'version (cdr entry))
37-
(alist-get 'description (cdr entry))))))
36+
(format "attr: %s\nname: %s\nversion: %s\ndescription: %s\n\n"
37+
(car entry)
38+
(if use-flakes
39+
(alist-get 'pname (cdr entry))
40+
(alist-get 'pkgName (cdr entry)))
41+
(alist-get 'version (cdr entry))
42+
(alist-get 'description (cdr entry))))))
3843
(display-buffer display-buffer))
3944

4045
;;;###autoload
@@ -43,10 +48,11 @@
4348
SEARCH a search term to use.
4449
FILE a Nix expression to search in."
4550
(interactive "snix-search> \n")
46-
(setq file (or file (nix-read-file)))
47-
(let ((results (nix-search--search search file)))
51+
(setq use-flakes (nix-has-flakes))
52+
(setq file (or file (if use-flakes (nix-read-flake) (nix-read-file))))
53+
(let ((results (nix-search--search search file nil use-flakes)))
4854
(when (called-interactively-p 'any)
49-
(nix-search--display results))
55+
(nix-search--display results nil use-flakes))
5056
results))
5157

5258
(defun nix-search-read-attr (file)

nix-shell.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ Should only be set in dir-locals.el file."
6060
:type 'stringp
6161
:group 'nix-shell)
6262

63+
(defcustom nix-flake nil
64+
"Nix flake to build expressions from.
65+
Should only be set in dir-locals.el file."
66+
:type 'stringp
67+
:group 'nix-shell)
68+
6369
(defcustom nix-attr nil
6470
"Nix attribute path to use.
6571
Should only be set in dir-locals.el file."
@@ -80,6 +86,13 @@ ATTR is the attribute to unpack."
8086
"Get nix attribute from user."
8187
(read-string "Nix attr: "))
8288

89+
(defun nix-read-flake ()
90+
"Get nix flake from user."
91+
(cond
92+
(nix-flake nix-flake)
93+
((and (nix-has-flakes) (file-exists-p "flake.nix")) ".")
94+
(t (read-string "Nix flake: " "nixpkgs"))))
95+
8396
(defun nix-read-file ()
8497
"Get nix file from user."
8598
(cond

nix.el

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@
7878
(kill-buffer stdout)
7979
result)))
8080

81+
(defun nix-show-config ()
82+
"Show nix config."
83+
(let ((stdout (generate-new-buffer "nix config"))
84+
result)
85+
(call-process nix-executable nil (list stdout nil) nil "show-config" "--json")
86+
(setq result (with-current-buffer stdout
87+
(goto-char (point-min))
88+
(json-read)))
89+
(kill-buffer stdout)
90+
result))
91+
8192
(defvar nix-commands
8293
'("add-to-store"
8394
"build"
@@ -187,10 +198,23 @@ OPTIONS a list of options to accept."
187198
((or (string= "-s" last-arg) (string= "--substituter" last-arg))
188199
(pcomplete-here))))))
189200

201+
(defun nix-is-24 ()
202+
"Whether Nix is a version with Flakes support."
203+
;; earlier versions reported as 3, now it’s just nix-2.4
204+
(let ((version (nix-version)))
205+
(or (string-prefix-p "nix (Nix) 3" version)
206+
(string-prefix-p "nix (Nix) 2.4" version))))
207+
208+
(defun nix-has-flakes ()
209+
"Whether Nix is a version with Flakes support."
210+
;; earlier versions reported as 3, now it’s just nix-2.4
211+
(and (nix-is-24)
212+
(seq-contains-p (alist-get 'value (alist-get 'experimental-features (nix-show-config))) "flakes")))
213+
190214
;;;###autoload
191215
(defun pcomplete/nix ()
192216
"Completion for the nix command."
193-
(if (string-prefix-p "nix (Nix) 3" (nix-version))
217+
(if (nix-is-24)
194218
(let ((stdout (generate-new-buffer "nix-completions"))
195219
(process-environment
196220
(cons (format "NIX_GET_COMPLETIONS=%s" (1- (length pcomplete-args)))

0 commit comments

Comments
 (0)