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

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

[nongnu] elpa/nix-mode e4bc711d81 094/500: Add some more modes.


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode e4bc711d81 094/500: Add some more modes.
Date: Sat, 29 Jan 2022 08:26:39 -0500 (EST)

branch: elpa/nix-mode
commit e4bc711d81fcde8e16e8cb32bb6a5a1030470c10
Author: Matthew Bauer <mjbauer95@gmail.com>
Commit: Matthew Bauer <mjbauer95@gmail.com>

    Add some more modes.
---
 company-nixos-options.el |  75 +++++++++++++++++++++++++++
 helm-nixos-options.el    |  49 ++++++++++++++++++
 nix-sandbox.el           | 127 ++++++++++++++++++++++++++++++++++++++++++++++
 nixos-options.el         | 128 +++++++++++++++++++++++++++++++++++++++++++++++
 nixos-packages.el        | 115 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 494 insertions(+)

diff --git a/company-nixos-options.el b/company-nixos-options.el
new file mode 100644
index 0000000000..d42cb30bb3
--- /dev/null
+++ b/company-nixos-options.el
@@ -0,0 +1,75 @@
+;;; company-nixos-options.el --- Company Backend for nixos-options
+
+;; Copyright (C) 2015 Diego Berrocal and Travis B. Hartwell
+
+;; Author: Diego Berrocal <cestdiego@gmail.com>
+;;      Travis B. Hartwell <nafai@travishartwell.net>
+;; Created: 18 July 2015
+
+;; Keywords: unix
+;; Homepage: http://www.github.com/travisbhartwell/nix-emacs/
+;; Version: 0.1.0
+;; Package-Requires: ((company "0.8.0") (nixos-options "0.0.1") (cl-lib 
"0.5.0"))
+
+;; This file is not part of GNU Emacs.
+
+;;; License: GPLv3
+
+;;; Commentary:
+
+;; Useful functions for exploring the NixOS options.  Inspired by
+;; https://nixos.org/nixos/options.html.
+
+;;; Code:
+(require 'nixos-options)
+(require 'company)
+(require 'cl-lib)
+
+(defun company-nixos-options--doc-buffer (candidate)
+  "Return documentation buffer for chosen CANDIDATE."
+  (let ((doc (nixos-options-get-documentation-for-option
+              (nixos-options-get-option-by-name candidate))))
+    (and doc (nixos-options-doc-buffer doc))))
+
+(defun company-nixos-options--candidates (prefix)
+  (let ((res))
+    (dolist (option nixos-options)
+      (let ((name (nixos-options-get-name option)))
+        (when (string-prefix-p prefix name)
+          (push name res))))
+    res))
+
+(defun company-nixos-options--annotation (candidate)
+  (let ((type (nixos-options-get-type
+               (nixos-options-get-option-by-name
+                candidate))))
+    (format "  <%s>" type)))
+
+(defun company-nixos--grab-symbol ()
+  (buffer-substring (point) (save-excursion (skip-syntax-backward "w_.")
+                                            (point))))
+
+(defun company-nixos--in-nix-context-p ()
+  (or (eq major-mode 'nix-mode)
+      (equal "nix" (file-name-extension
+                    (buffer-file-name (current-buffer))))))
+
+(defun company-nixos-options--prefix ()
+  "Grab prefix at point."
+  (and (company-nixos--in-nix-context-p)
+       (or (company-nixos--grab-symbol)
+           'stop)))
+
+;;;###autoload
+(defun company-nixos-options (command &optional arg &rest ignored)
+  (interactive (list 'interactive))
+  (cl-case command
+    (interactive (company-begin-backend 'company-nixos-options))
+    (prefix (company-nixos-options--prefix))
+    (candidates (company-nixos-options--candidates arg))
+    (doc-buffer (company-nixos-options--doc-buffer arg))
+    (annotation (company-nixos-options--annotation arg))))
+
+(provide 'company-nixos-options)
+;;; company-nixos-options.el ends here
+
diff --git a/helm-nixos-options.el b/helm-nixos-options.el
new file mode 100644
index 0000000000..974274d425
--- /dev/null
+++ b/helm-nixos-options.el
@@ -0,0 +1,49 @@
+;;; helm-nixos-options.el --- Helm Interface for nixos-options
+
+;; Copyright (C) 2015 Diego Berrocal and Travis B. Hartwell
+
+;; Author: Diego Berrocal <cestdiego@gmail.com>
+;;      Travis B. Hartwell <nafai@travishartwell.net>
+;; Created: 18 July 2015
+
+;; Keywords: unix
+;; Homepage: http://www.github.com/travisbhartwell/nix-emacs/
+;; Version: 0.1.0
+;; Package-Requires: ((nixos-options "0.0.1") (helm "1.5.6"))
+
+;; This file is not part of GNU Emacs.
+
+;;; License: GPLv3
+
+;;; Commentary:
+
+;; Useful functions for exploring the NixOS options.  Inspired by
+;; https://nixos.org/nixos/options.html.
+
+;;; Code:
+(require 'nixos-options)
+(require 'helm)
+
+(defun helm-source-nixos-options-search ()
+  `((name . "NixOS Options")
+    (requires-pattern . 2)
+    (candidates . nixos-options)
+    (follow . 1)
+    (persistent-action . (lambda (f) (message (format "%s" 
(nixos-options-get-description f)))))
+    (action . (("View documentation" . (lambda (f)
+                                         (switch-to-buffer-other-window
+                                          (nixos-options-doc-buffer
+                                           
(nixos-options-get-documentation-for-option f)))))
+               ("Insert into buffer" . (lambda (f) (insert 
(nixos-options-get-name f))))
+               ("Pretty print" . (lambda (f) (message "Pretty Printed: %s" (pp 
f))))
+               ("Display name" . (lambda (f) (message "Name: %s" 
(nixos-options-get-name f))))))))
+
+;;;###autoload
+(defun helm-nixos-options ()
+  (interactive)
+  (helm :sources `(,(helm-source-nixos-options-search))
+        :buffer "*helm-nixos-options*"))
+
+(provide 'helm-nixos-options)
+;;; helm-nixos-options.el ends here
+
diff --git a/nix-sandbox.el b/nix-sandbox.el
new file mode 100644
index 0000000000..492a68d422
--- /dev/null
+++ b/nix-sandbox.el
@@ -0,0 +1,127 @@
+;;; nix-sandbox.el --- Utility functions to work with nix-shell sandboxes
+
+;; Copyright (C) 2015 Sven Keidel
+
+;; Author: Sven Keidel <svenkeidel@gmail.com>
+;; Package-Version: 0.1
+;; Package-Requires: ((dash "2.12.1") (s "1.10.0"))
+;; Homepage: https://github.com/travisbhartwell/nix-emacs
+
+;; This file is not part of GNU Emacs.
+
+;;; License: GPLv3
+
+;;; Commentary:
+
+;; Useful functions for working with nix-shell sandboxes
+
+;;; Code:
+
+(require 'dash)
+(require 's)
+
+(defgroup nix nil
+  "customizations for nix"
+  :prefix "nix-"
+  :group 'external)
+
+(defcustom nix-nixpkgs-path nil
+  "Absolute path to a nixpkgs directory.
+
+Can be customized to select a nix-channel
+e.g. /home/user/.nix-defexpr/channels/unstable/nixpkgs"
+  :group 'nix
+  :type '(choice (const :tag "No channel" nil)
+                 (directory "Custom path to a nixpkgs distribution")))
+
+(defun nix-create-sandbox-rc (sandbox)
+  "Create a new rc file containing the environment for the given SANDBOX."
+  (let ((env-str (shell-command-to-string
+                  (concat "nix-shell "
+                          (or (and nix-nixpkgs-path (concat "-I nixpkgs=" 
nix-nixpkgs-path))
+                              "")
+                          " --run 'declare +x shellHook; declare -x; declare 
-xf' "
+                          (shell-quote-argument sandbox)
+                          " 2> /dev/null")))
+        (tmp-file (make-temp-file "nix-sandbox-rc-")))
+    (write-region env-str nil tmp-file 'append)
+    tmp-file))
+
+(defvar nix-sandbox-rc-map (make-hash-table :test 'equal
+                                            :size 4))
+
+(defun nix-sandbox-rc (sandbox)
+  "Return the rc file for the given SANDBOX or create one."
+  (or (gethash sandbox nix-sandbox-rc-map)
+      (puthash sandbox (nix-create-sandbox-rc sandbox) nix-sandbox-rc-map)))
+
+;;;###autoload
+(defun nix-shell-command (sandbox &rest args)
+  "Assemble a command from ARGS that can be executed in the specified SANDBOX."
+  (list "bash" "-c" (format "source %s; %s" (nix-sandbox-rc sandbox) (s-join " 
" args))))
+
+(defun nix-shell-string (sandbox &rest args)
+  "Assemble a command string from ARGS that can be executed in the specifed 
SANDBOX."
+  (combine-and-quote-strings
+   (apply 'nix-shell-command sandbox args)))
+
+;;;###autoload
+(defun nix-compile (sandbox &rest command)
+  "Compile a program using the given COMMAND in SANDBOX."
+  (interactive "Dsandbox: \nMcommand: ")
+  (compile (apply 'nix-shell-string sandbox command)))
+
+;;;###autoload
+(defun nix-shell (sandbox &rest command)
+  "Run a COMMAND in the given SANDBOX and return the output."
+  (shell-command-to-string (apply 'nix-shell-string sandbox command)))
+
+(defvar nix-exec-path-map (make-hash-table :test 'equal
+                                           :size 4))
+
+;;;###autoload
+(defun nix-exec-path (sandbox)
+  "Return the `exec-path' of the given SANDBOX."
+
+  (or (gethash sandbox nix-exec-path-map)
+      (puthash sandbox
+               (split-string (nix-shell sandbox "printenv" "PATH") ":")
+               nix-exec-path-map)))
+
+;;;###autoload
+(defun nix-executable-find (sandbox executable)
+  "Search for an EXECUTABLE in the given SANDBOX."
+  (let ((exec-path (nix-exec-path sandbox)))
+    (and exec-path (executable-find executable))))
+
+;;;###autoload
+(defun nix-find-sandbox (path)
+  "Search for a sandbox starting at PATH traversing upwards the directory tree.
+If the directory contains a `shell.nix' file, the path to this
+file is returned.  Otherwise if the directory contains a
+`default.nix' file, the parent directory is returned."
+  (and (file-exists-p path)
+       (let* ((map-nil (lambda (f x) (if x (funcall f x) nil)))
+              (sandbox-directory
+               (funcall map-nil 'expand-file-name
+                        (locate-dominating-file path
+                                                '(lambda (dir) 
(directory-files dir t ".*\.nix$")))))
+              (shell-nix (and sandbox-directory (concat sandbox-directory 
"shell.nix"))))
+         (if (and sandbox-directory (file-exists-p shell-nix))
+             shell-nix
+           sandbox-directory))))
+
+;;;###autoload
+(defun nix-current-sandbox ()
+  "Return the path of the sandbox that is closest to the current working 
directory."
+  (nix-find-sandbox default-directory))
+
+(defun nix-clear-caches ()
+  "Clear cached information for all sandboxes."
+  (interactive)
+  (clrhash nix-sandbox-rc-map)
+  (clrhash nix-exec-path-map))
+
+(provide 'nix-sandbox)
+
+;;; nix-sandbox.el ends here
diff --git a/nixos-options.el b/nixos-options.el
new file mode 100644
index 0000000000..89ace9bbc4
--- /dev/null
+++ b/nixos-options.el
@@ -0,0 +1,128 @@
+;;; nixos-options.el --- Interface for browsing and completing NixOS options.
+
+;; Copyright (C) 2015 Diego Berrocal and Travis B. Hartwell
+
+;; Author: Diego Berrocal <cestdiego@gmail.com>
+;;      Travis B. Hartwell <nafai@travishartwell.net>
+;; Created: 18 July 2015
+
+;; Keywords: unix
+;; Homepage: http://www.github.com/travisbhartwell/nix-emacs/
+;; Version: 0.0.1
+;; Package-Requires: ((emacs "24"))
+
+;; This file is not part of GNU Emacs.
+
+;;; License: GPLv3
+
+;;; Commentary:
+
+;; Useful functions for exploring the NixOS options.  Inspired by
+;; https://nixos.org/nixos/options.html.
+
+;;; Code:
+
+(require 'json)
+
+(defvar nixos-options-name-indent-amount 0
+  "Indent by the maximum length, plus a colon, plus two spaces.")
+
+;; Macros for defining constants and functions for working with options
+(defmacro define-nixos-options-item (item long-name)
+  (let* ((name-const (intern (concat "nixos-options-" item)))
+         (long-name-const (intern (concat "nixos-options-" item "-long-name")))
+         (long-name-length-plus-padding (+ 3 (length long-name)))
+         (long-name-docstring (format "The long description for %s." item))
+         (item-getter (intern (concat "nixos-options-get-" item)))
+         (item-getter-docstring
+          (format "Get the value of %s from OPTION." item))
+         (item-display (intern (concat "nixos-options-display-" item)))
+         (item-display-docstring
+          (format "Display the value for %s from OPTION." item)))
+    `(progn
+       (defconst ,name-const ,item)
+       (defconst ,long-name-const ,long-name ,long-name-docstring)
+       (if (> ,long-name-length-plus-padding nixos-options-name-indent-amount)
+           (setq nixos-options-name-indent-amount
+                 ,long-name-length-plus-padding))
+       (defun ,item-getter (option)
+         ,item-getter-docstring
+         (cdr (assoc ,name-const option)))
+       (defun ,item-display (option)
+         ,item-display-docstring
+         (let ((item (,item-getter option))
+               (format-string
+                (format "%%-%ds %%s\n" nixos-options-name-indent-amount)))
+           (if (not (null item))
+               (format format-string (concat ,long-name-const ":") item)
+             ""))))))
+
+(define-nixos-options-item "name" "Name")
+(define-nixos-options-item "type" "Type")
+(define-nixos-options-item "description" "Description")
+(define-nixos-options-item "default" "Default value")
+(define-nixos-options-item "example" "Example value")
+(define-nixos-options-item "declarations" "Declared in")
+
+(defvar nixos-options-json-file
+  (let* ((cmd
+          "export NIXPKGS_ALLOW_UNFREE=1; nix-build -Q --no-out-link 
'<nixpkgs/nixos/release.nix>' -A options 2>/dev/null")
+         (dir (replace-regexp-in-string "\n\\'" ""
+                                        (shell-command-to-string cmd))))
+    (expand-file-name "share/doc/nixos/options.json" dir))
+  "Location of the options file.")
+
+(defun nixos-options--boolean-string (value)
+  "Return the string representation of the boolean VALUE.
+Returns VALUE unchanged if not a boolean."
+  (cond ((eq value 't) "true")
+        ((eq value :json-false) "false")
+        (t value)))
+
+(defun nixos-options--make-alist (option)
+  (let ((name (car option))
+        (data (cdr option))
+        (default (nixos-options-get-default option))
+        (example (nixos-options-get-example option)))
+    (progn
+      (if (not (null default))
+          (setcdr (assoc nixos-options-default option)
+                  (nixos-options--boolean-string default)))
+      (if (not (null example))
+          (setcdr (assoc nixos-options-example option)
+                  (nixos-options--boolean-string example)))
+      (add-to-list 'data `(,nixos-options-name . ,name))
+      `(,name . ,data))))
+
+(defvar nixos-options
+  (if (file-exists-p nixos-options-json-file)
+      (let* ((json-key-type 'string)
+             (raw-options (json-read-file nixos-options-json-file)))
+        (mapcar 'nixos-options--make-alist raw-options))
+    (message "Warning: Cannot find nixos option file.")))
+
+(defun nixos-options-get-documentation-for-option (option)
+  (concat (nixos-options-display-name option)
+          (nixos-options-display-type option)
+          (nixos-options-display-description option)
+          (nixos-options-display-default option)
+          (nixos-options-display-example option)
+          (nixos-options-display-declarations option)))
+
+;; Borrowed from anaconda-mode
+(defun nixos-options-doc-buffer (doc)
+  "Display documentation buffer with contents DOC."
+  (let ((buf (get-buffer-create "*nixos-options-doc*")))
+    (with-current-buffer buf
+      (view-mode -1)
+      (erase-buffer)
+      (insert doc)
+      (goto-char (point-min))
+      (view-mode 1)
+      buf)))
+
+(defun nixos-options-get-option-by-name (name)
+  (assoc name nixos-options))
+
+(provide 'nixos-options)
+;;; nixos-options.el ends here
diff --git a/nixos-packages.el b/nixos-packages.el
new file mode 100644
index 0000000000..bc21c4e3e4
--- /dev/null
+++ b/nixos-packages.el
@@ -0,0 +1,115 @@
+;;; nixos-packages.el --- Interface for browsing and completing NixOS packages.
+
+;; Copyright (C) 2015 Diego Berrocal and Travis B. Hartwell
+
+;; Author: Diego Berrocal <cestdiego@gmail.com>
+;;      Travis B. Hartwell <nafai@travishartwell.net>
+;; Created: 18 July 2015
+
+;; Keywords: unix
+;; Homepage: http://www.github.com/travisbhartwell/nix-emacs/
+;; Version: 0.0.1
+;; Package-Requires: ((emacs "24"))
+
+;; This file is not part of GNU Emacs.
+
+;;; License: GPLv3
+
+;;; Commentary:
+
+;; Useful functions for exploring the NixOS packages.  Inspired by
+;; https://nixos.org/nixos/packages.html.
+
+;;; Code:
+
+(require 'json)
+
+(defvar nixos-packages-json-file
+  (expand-file-name "nixos-packages.json" spacemacs-cache-directory)
+  "Where to store the nixos-packages in JSON format")
+
+(defvar nixos-packages-name-indent-amount 0
+  "Indent by the maximum length, plus a colon, plus two spaces.")
+
+(setq nixos-packages
+      (if (file-exists-p nixos-packages-json-file)
+          (let ((json-key-type 'string))
+            (json-read-file nixos-packages-json-file))
+        (let* ((cmd "nix-env -qaP hello --json")
+               (data (replace-regexp-in-string "\n\\'" ""
+                                               (shell-command-to-string cmd)))
+               (json-key-type 'string))
+          (append-to-file data nil nixos-packages-json-file)
+          (json-read-from-string data))))
+
+
+(assoc "name" (car nixos-packages))
+(assoc "system" (car nixos-packages))
+(assoc "description" (assoc "meta" (car nixos-packages)))
+(assoc "homepage" (assoc "meta" (car nixos-packages)))
+(assoc "license" (assoc "meta" (car nixos-packages)))
+(assoc "longDescription" (assoc "meta" (car nixos-packages)))
+(assoc "platforms" (assoc "meta" (car nixos-packages)))
+(assoc "position" (assoc "meta" (car nixos-packages)))
+(assoc "maintainers" (assoc "meta" (car nixos-packages)))
+(assoc "maintainers" (assoc "meta" (car nixos-packages)))
+
+;; Macros for defining constants and functions for working with options
+(defmacro define-nixos-packages-item (item long-name &optional isMeta?)
+  (let* ((name-const (intern (concat "nixos-packages-" item)))
+         (long-name-const (intern (concat "nixos-packages-" item 
"-long-name")))
+         (long-name-length-plus-padding (+ 3 (length long-name)))
+         (long-name-docstring (format "The long description for %s." item))
+         (item-getter (intern (concat "nixos-packages-get-" item)))
+         (item-getter-docstring
+          (format "Get the value of %s from PACKAGE" item))
+         (item-display (intern (concat "nixos-packages-display-" item)))
+         (item-display-docstring
+          (format "Display the value for %s from PACKAGE" item)))
+    `(progn
+       (defconst ,name-const ,item)
+       (defconst ,long-name-const ,long-name ,long-name-docstring)
+       (if (> ,long-name-length-plus-padding nixos-packages-name-indent-amount)
+           (setq nixos-packages-name-indent-amount
+                 ,long-name-length-plus-padding))
+       (if ,isMeta?
+           (defun ,item-getter (option)
+             ,item-getter-docstring
+             (cdr (assoc ,name-const (assoc "meta" option))))
+         (defun ,item-getter (option)
+           ,item-getter-docstring
+           (cdr (assoc ,name-const option))))
+
+       (defun ,item-display (option)
+         ,item-display-docstring
+         (let ((item (,item-getter option))
+               (format-string
+                (format "%%-%ds %%s\n" nixos-packages-name-indent-amount)))
+           (if (not (null item))
+               (format format-string (concat ,long-name-const ":") item)
+             ""))))))
+
+(define-nixos-packages-item "description" "Description" t)
+(define-nixos-packages-item "homepage" "Home Page URL" t)
+(define-nixos-packages-item "license" "License" t)
+(define-nixos-packages-item "longDescription" "Long Description" t)
+(define-nixos-packages-item "platforms" "Supported Platforms" t)
+(define-nixos-packages-item "maintainers" "List of Maintainers" t)
+(define-nixos-packages-item "position" "Path to the nix-expression with line" 
t)
+(define-nixos-packages-item "name" "Name")
+(define-nixos-packages-item "system" "System")
+
+(defun nixos-packages--make-alist (package)
+  (let ((name (car package))
+        (data (cdr package))
+        (default (nixos-options-get-default package))
+        (example (nixos-options-get-example package)))
+    (progn
+      (if (not (null default))
+          (setcdr (assoc nixos-options-default package)
+                  (nixos-options--boolean-string default)))
+      (if (not (null example))
+          (setcdr (assoc nixos-options-example package)
+                  (nixos-options--boolean-string example)))
+      (add-to-list 'data `(,nixos-options-name . ,name))
+      `(,name . ,data))))



reply via email to

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