Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.7 KiB

4 years ago
  1. ;;; nix.el -- run nix commands in Emacs -*- lexical-binding: t -*-
  2. ;; Author: Matthew Bauer <mjbauer95@gmail.com>
  3. ;; Homepage: https://github.com/NixOS/nix-mode
  4. ;; Keywords: nix
  5. ;; This file is NOT part of GNU Emacs.
  6. ;;; Commentary:
  7. ;; To use this just run:
  8. ;; M-x RET nix-shell RET
  9. ;; This will give you some
  10. ;;; Code:
  11. (require 'pcomplete)
  12. (defgroup nix nil
  13. "Nix-related customizations"
  14. :group 'languages)
  15. (defcustom nix-executable "nix"
  16. "Nix executable location."
  17. :group 'nix
  18. :type 'string)
  19. (defcustom nix-build-executable "nix-build"
  20. "Nix-build executable location."
  21. :group 'nix
  22. :type 'string)
  23. (defcustom nix-instantiate-executable "nix-instantiate"
  24. "Nix executable location."
  25. :group 'nix
  26. :type 'string)
  27. (defcustom nix-store-executable "nix-store"
  28. "Nix executable location."
  29. :group 'nix
  30. :type 'string)
  31. (defcustom nix-shell-executable "nix-shell"
  32. "Location of ‘nix-shell’ executable."
  33. :group 'nix
  34. :type 'string)
  35. (defcustom nix-store-dir "/nix/store"
  36. "Nix store directory."
  37. :group 'nix
  38. :type 'string)
  39. (defcustom nix-state-dir "/nix/var"
  40. "Nix store directory."
  41. :group 'nix
  42. :type 'string)
  43. (defun nix-system ()
  44. "Get the current system tuple."
  45. (let ((stdout (generate-new-buffer "nix eval"))
  46. result)
  47. (call-process nix-executable nil (list stdout nil) nil
  48. "eval" "--raw" "(builtins.currentSystem)")
  49. (with-current-buffer stdout (setq result (buffer-string)))
  50. (kill-buffer stdout)
  51. result))
  52. (defvar nix-commands
  53. '("add-to-store"
  54. "build"
  55. "cat-nar"
  56. "cat-store"
  57. "copy"
  58. "copy-sigs"
  59. "dump-path"
  60. "edit"
  61. "eval"
  62. "hash-file"
  63. "hash-path"
  64. "log"
  65. "ls-nar"
  66. "ls-store"
  67. "optimise-store"
  68. "path-info"
  69. "ping-store"
  70. "repl"
  71. "run"
  72. "search"
  73. "show-config"
  74. "show-derivation"
  75. "sign-paths"
  76. "to-base16"
  77. "to-base32"
  78. "to-base64"
  79. "upgrade-nix"
  80. "verify"
  81. "why-depends"))
  82. (defvar nix-toplevel-options
  83. '("-v"
  84. "--verbose"
  85. "-h"
  86. "--help"
  87. "--debug"
  88. "--help-config"
  89. "--option"
  90. "--version"))
  91. (defvar nix-config-options
  92. '("allowed-uris"
  93. "allow-import-from-derivation"
  94. "allow-new-priveleges"
  95. "allowed-users"
  96. "auto-optimise-store"
  97. "builders"
  98. "builders-use-substitutes"
  99. "build-users-group"
  100. "compress-build-log"
  101. "connect-timeout"
  102. "cores"
  103. "extra-sandbox-paths"
  104. "extra-substituters"
  105. "fallback"
  106. "fsync-metadata"
  107. "hashed-mirrors"
  108. "http-connections"
  109. "keep-build-log"
  110. "keep-derivations"
  111. "keep-env-derivations"
  112. "keep-outputs"
  113. "max-build-log-size"
  114. "max-jobs"
  115. "max-silent-time"
  116. "netrc-file"
  117. "plugin-files"
  118. "pre-build-hook"
  119. "repeat"
  120. "require-sigs"
  121. "restrict-eval"
  122. "sandbox"
  123. "sandbox-dev-shm-size"
  124. "sandbox-paths"
  125. "secret-key-files"
  126. "show-trace"
  127. "substitute"
  128. "substituters"
  129. "system"
  130. "timeout"
  131. "trusted-public-keys"
  132. "trusted-subtituters"
  133. "trusted-users"))
  134. (defun nix--pcomplete-flags (options)
  135. "Complete flags to the Nix command.
  136. OPTIONS a list of options to accept."
  137. (while (pcomplete-match "^-" 0)
  138. (pcomplete-here options)
  139. (let ((last-arg (nth (1- pcomplete-index) pcomplete-args)))
  140. (cond
  141. ((string= "--option" last-arg)
  142. (pcomplete-here nix-config-options)
  143. (pcomplete-here))
  144. ((or (string= "-f" last-arg) (string= "--file" last-arg))
  145. (pcomplete-here (pcomplete-entries nil 'file-exists-p)))
  146. ((or (string= "--arg" last-arg) (string= "--argstr" last-arg))
  147. (pcomplete-here)
  148. (pcomplete-here))
  149. ((or (string= "-I" last-arg) (string= "--include" last-arg))
  150. (pcomplete-here (pcomplete-entries nil 'file-exists-p)))
  151. ((or (string= "-k" last-arg) (string= "--keep" last-arg))
  152. (pcomplete-here))
  153. ((or (string= "-u" last-arg) (string= "--unset" last-arg))
  154. (pcomplete-here))
  155. ((or (string= "-s" last-arg) (string= "--substituter" last-arg))
  156. (pcomplete-here))))))
  157. ;;;###autoload
  158. (defun pcomplete/nix ()
  159. "Completion for the nix command."
  160. (nix--pcomplete-flags nix-toplevel-options)
  161. (pcomplete-here nix-commands)
  162. (pcase (nth (1- pcomplete-index) pcomplete-args)
  163. ("run"
  164. (nix--pcomplete-flags
  165. (append nix-toplevel-options '("--arg" "--argstr" "-c" "--command"
  166. "-f" "--file" "-i" "-I" "--include"
  167. "-k" "--keep" "-u" "--unset"))))
  168. ("build"
  169. (nix--pcomplete-flags
  170. (append nix-toplevel-options '("--arg" "--argstr" "--dry-run"
  171. "-f" "--file" "-I" "--include"
  172. "--no-link" "-o" "--out-link"))))
  173. ("add-to-store"
  174. (nix--pcomplete-flags
  175. (append nix-toplevel-options '("--dry-run" "-n" "--name"))))
  176. ("copy"
  177. (nix--pcomplete-flags
  178. (append nix-toplevel-options '("--all" "--arg" "--argstr"
  179. "-f" "--file" "--from"
  180. "-I" "--include" "--no-check-sigs"
  181. "--no-recursive" "-s" "--substitute"
  182. "--to"))))
  183. ("copy-sigs"
  184. (nix--pcomplete-flags
  185. (append nix-toplevel-options '("--all" "--arg" "--argstr"
  186. "-f" "--file" "-I" "--include"
  187. "-r" "--recursive" "-s" "--substituter"))))
  188. ("dump-path"
  189. (nix--pcomplete-flags
  190. (append nix-toplevel-options '("--arg" "--argstr"
  191. "-f" "--file" "-I" "--include"))))
  192. ("edit"
  193. (nix--pcomplete-flags
  194. (append nix-toplevel-options '("--arg" "--argstr"
  195. "-f" "--file" "-I" "--include"))))
  196. ("eval"
  197. (nix--pcomplete-flags
  198. (append nix-toplevel-options '("--arg" "--argstr"
  199. "-f" "--file" "-I" "--include"
  200. "--json" "--raw"))))
  201. ("hash-file"
  202. (nix--pcomplete-flags
  203. (append nix-toplevel-options '("--base16" "--base32"
  204. "--base64" "--type"))))
  205. ("hash-path"
  206. (nix--pcomplete-flags
  207. (append nix-toplevel-options '("--base16" "--base32"
  208. "--base64" "--type"))))
  209. ("log"
  210. (nix--pcomplete-flags
  211. (append nix-toplevel-options '("--arg" "--argstr"
  212. "-f" "--file" "-I" "--include"
  213. "--json" "--raw"))))
  214. ("ls-nar"
  215. (nix--pcomplete-flags
  216. (append nix-toplevel-options '("-d" "--directory"
  217. "--json" "-l" "--long"
  218. "-R" "--recursive"))))
  219. ("ls-store"
  220. (nix--pcomplete-flags
  221. (append nix-toplevel-options '("-d" "--directory"
  222. "--json" "-l" "--long"
  223. "-R" "--recursive"))))
  224. ("repl"
  225. (nix--pcomplete-flags
  226. (append nix-toplevel-options '("--arg" "--argstr"
  227. "-I" "--include"))))
  228. ("search"
  229. (nix--pcomplete-flags
  230. (append nix-toplevel-options '("--arg" "--argstr"
  231. "-f" "--file"
  232. "-I" "--include"
  233. "--json" "--no-cache"
  234. "-u" "--update-cache"))))
  235. ("show-config"
  236. (nix--pcomplete-flags
  237. (append nix-toplevel-options '("--json"))))
  238. ("show-derivation"
  239. (nix--pcomplete-flags
  240. (append nix-toplevel-options '("--arg" "--argstr"
  241. "-f" "--file"
  242. "-I" "--include"
  243. "-r" "--recursive"))))
  244. ("sign-paths"
  245. (nix--pcomplete-flags
  246. (append nix-toplevel-options '("--all" "--arg" "--argstr"
  247. "-f" "--file" "-I" "--include"
  248. "-k" "--key-file" "-r" "--recursive"))))
  249. ("upgrade-nix"
  250. (nix--pcomplete-flags
  251. (append nix-toplevel-options '("-p" "--profile"))))
  252. ("verify"
  253. (nix--pcomplete-flags
  254. (append nix-toplevel-options '("--all" "--arg" "--argstr"
  255. "-f" "--file" "-I" "--include"
  256. "--no-contents" "--no-trust"
  257. "-r" "--recursive" "-n" "--sigs-needed"
  258. "-s" "--substuter"))))
  259. ("why-depends"
  260. (nix--pcomplete-flags
  261. (append nix-toplevel-options '("-a" "--all" "--arg" "--argstr"
  262. "-f" "--file" "-I" "--include"))))
  263. (_ (nix--pcomplete-flags nix-toplevel-options)))
  264. (pcomplete-here (pcomplete-entries)))
  265. (provide 'nix)
  266. ;;; nix.el ends here