[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/nix-mode 53d1fb9687 469/500: nix-flake: Add nix-flake-init
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/nix-mode 53d1fb9687 469/500: nix-flake: Add nix-flake-init |
Date: |
Sat, 29 Jan 2022 08:27:57 -0500 (EST) |
branch: elpa/nix-mode
commit 53d1fb96875a25fd43e1543000da991c08f4d290
Author: Akira Komamura <akira.komamura@gmail.com>
Commit: Akira Komamura <akira.komamura@gmail.com>
nix-flake: Add nix-flake-init
---
nix-flake.el | 81 +++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 64 insertions(+), 17 deletions(-)
diff --git a/nix-flake.el b/nix-flake.el
index ca16c75709..475d53a4df 100644
--- a/nix-flake.el
+++ b/nix-flake.el
@@ -11,6 +11,32 @@
(require 'nix)
(require 'transient)
+;;;; Transient classes
+
+;;;;; flake-ref
+
+(defclass nix-flake-ref-variable (transient-variable)
+ ((variable :initarg :variable)
+ (constant-value :initarg :constant-value :initform nil)))
+
+(cl-defmethod transient-init-value ((obj nix-flake-ref-variable))
+ (unless (oref obj value)
+ (oset obj value (eval (oref obj variable)))))
+
+(cl-defmethod transient-infix-read ((obj nix-flake-ref-variable))
+ (if-let (value (oref obj constant-value))
+ (if (symbolp value)
+ (symbol-value value)
+ value)
+ (nix-flake--select-flake nil (oref obj value))))
+
+(cl-defmethod transient-infix-set ((obj nix-flake-ref-variable) value)
+ (oset obj value value)
+ (set (oref obj variable) value))
+
+(cl-defmethod transient-format-value ((obj nix-flake-ref-variable))
+ "")
+
;;;; Utility functions
(defun nix-flake--to-list (x)
@@ -31,14 +57,17 @@
(thread-last (nix--process-lines "registry" "list")
(mapcar #'split-entry))))
-(defun nix-flake--select-flake ()
+;; This argument complies the standard reader interface of transient
+;; just in case, but it may not be necessary.
+(defun nix-flake--select-flake (&optional prompt initial-input history)
"Select a flake from the registry."
- (completing-read "Flake URL: "
- (thread-last (nix-flake--registry-list)
- (cl-remove-if-not (pcase-lambda (`(,type . ,_))
+ (completing-read (or prompt "Flake URL: ")
+ (thread-last (nix-flake--registry-list)
+ (cl-remove-if-not (pcase-lambda (`(,type . ,_))
(member type '("user" "global"))))
- (mapcar (pcase-lambda (`(,_ ,_ ,ref))
- ref)))))
+ (mapcar (pcase-lambda (`(,_ ,_ ,ref))
+ ref)))
+ nil nil nil history initial-input))
;;;; nix-flake command
@@ -306,8 +335,7 @@ whatever supported by Nix."
(nix-flake--command '("flake" "lock") nil
(nix-flake--directory-ref dir))))
(t
- ;; TODO: Let the user run 'nix flake init' to create flake.nix
- (user-error "The directory does not contain flake.nix"))))
+ (nix-flake-init-dispatch))))
(defun nix-flake--directory-ref (dir)
"Return the flake ref for a local DIR."
@@ -315,18 +343,26 @@ whatever supported by Nix."
;;;; nix flake init
+;;;;; Setting the template repository
+
(defvar nix-flake-template-repository nil)
(defun nix-flake--init-source ()
"Describe the current template repository for init command."
(format "Template repository: %s" nix-flake-template-repository))
-(defun nix-flake--templates (flake-ref)
- "Return a list of templates in FLAKE-REF."
- (thread-last (nix--process-json "flake" "show" "--json" flake-ref)
- (alist-get 'templates)
- (mapcar #'car)
- (mapcar #'symbol-name)))
+(transient-define-infix nix-flake-init:from-registry ()
+ :class 'nix-flake-ref-variable
+ :variable 'nix-flake-template-repository
+ :description "Select from the registry")
+
+(transient-define-infix nix-flake-init:default-templates ()
+ :class 'nix-flake-ref-variable
+ :variable 'nix-flake-template-repository
+ :constant-value "flake:templates"
+ :description "Use the default template set")
+
+;;;;; Running the command
(defun nix-flake--init (flake-ref template-name)
"Initialize a flake from a template.
@@ -341,6 +377,15 @@ template, TEMPLATE-NAME is the name of the template."
,(concat flake-ref "#" template-name))
" ")))
+;;;;; Selecting a template
+
+(defun nix-flake--templates (flake-ref)
+ "Return a list of templates in FLAKE-REF."
+ (thread-last (nix--process-json "flake" "show" "--json" flake-ref)
+ (alist-get 'templates)
+ (mapcar #'car)
+ (mapcar #'symbol-name)))
+
;; It might be better to use `transient-define-suffix', but I don't know for
;; sure.
(defun nix-flake-init-select-template ()
@@ -353,13 +398,16 @@ template, TEMPLATE-NAME is the name of the template."
(nix-flake--templates flake-ref))))
(nix-flake--init flake-ref template-name)))
+;;;;; The transient interface
+
;;;###autoload (autoload 'nix-flake-init "nix-flake" nil t)
(transient-define-prefix nix-flake-init-dispatch (&optional flake-ref)
"Scaffold a project from a template."
[:description "Initialize a flake"]
[:description
nix-flake--init-source
- ("r" nix-flake-init--from-registry)]
+ ("r" nix-flake-init:from-registry)
+ ("d" nix-flake-init:default-templates)]
["Initialize a flake"
("t" "Select template" nix-flake-init-select-template)]
(interactive (list nil))
@@ -371,8 +419,7 @@ template, TEMPLATE-NAME is the name of the template."
(defun nix-flake-init ()
"Run \"nix flake init\" command via a transient interface."
(interactive)
- (let* ((root (ignore-errors
- (vc-root-dir)))
+ (let* ((root (locate-dominating-file default-directory ".git"))
(default-directory
(if (and root
(not (file-equal-p root default-directory))
- [nongnu] elpa/nix-mode 471a90ac96 394/500: Release 1.4.5, (continued)
- [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, 2022/01/29
- [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 <=
- [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
- [nongnu] elpa/nix-mode 9ee2a867e6 481/500: nix-flake: Follow the terminology and syntax of the guideline, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode b97d0a0aae 488/500: Merge pull request #141 from akirak/dependabot, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 83980ca7e7 498/500: nix-mode: Add missing transient dependency for nix-flake, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode ef2efae30a 482/500: nix-flake: Use flatten-list and bump Emacs to 27.1, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode c4e4f386b7 490/500: chore: Add nix-flake.el to Cask, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 10fc00dba8 015/500: Fix bad setting for indent-tabs-mode., ELPA Syncer, 2022/01/29