diff --git a/modules/lang/nix/README.org b/modules/lang/nix/README.org index 1752a3b84..e4931cee7 100644 --- a/modules/lang/nix/README.org +++ b/modules/lang/nix/README.org @@ -19,20 +19,18 @@ Includes: [[doom-contrib-maintainer:][Become a maintainer?]] ** Module flags +- +lsp :: + Enable an LSP hook for ~nix-mode~. Requires [[doom-module::tools lsp]] and a + language server (one of either ~nil~ or ~rnix-lsp~). - +tree-sitter :: Leverages tree-sitter for better syntax highlighting and structural text - editing. Requires [[doom-module::tools tree-sitter]]. -- +lsp :: - Enable an LSP hook for ~nix-mode~. Requires [[doom-module::tools lsp]] and a language - server (one of either ~nil~ or ~rnix-lsp~). - - You can still start a nix lsp manually without this flag, this just adds - a hook to always start the lsp when loading ~nix-mode~. + editing. Requires Emacs 29.1+ and [[doom-module::tools tree-sitter]]. ** Packages - [[doom-package:company-nixos-options]] if [[doom-module::completion company]] - [[doom-package:helm-nixos-options]] if [[doom-module::completion helm]] - [[doom-package:nix-mode]] +- [[doom-package:nix-ts-mode]] if [[doom-module:+tree-sitter]] - [[doom-package:nix-update]] ** Hacks diff --git a/modules/lang/nix/config.el b/modules/lang/nix/config.el index 10bfb1731..ced4ef98a 100644 --- a/modules/lang/nix/config.el +++ b/modules/lang/nix/config.el @@ -7,33 +7,25 @@ ;; ;;; Plugins -(use-package! nix-mode - :interpreter ("\\(?:cached-\\)?nix-shell" . +nix-shell-init-mode) - :mode "\\.nix\\'" - :init - (add-to-list 'auto-mode-alist - (cons "/flake\\.lock\\'" - (if (modulep! :lang json) - 'json-mode - 'js-mode))) - :config - (set-repl-handler! 'nix-mode #'+nix/open-repl) - (set-company-backend! 'nix-mode 'company-nixos-options) - (set-lookup-handlers! 'nix-mode +(add-to-list 'auto-mode-alist + (cons "/flake\\.lock\\'" + (if (modulep! :lang json) + 'json-mode + 'js-mode))) + + +(defun +nix-common-config (mode) + (set-repl-handler! mode #'+nix/open-repl) + (set-company-backend! mode 'company-nixos-options) + (set-lookup-handlers! mode :documentation '(+nix/lookup-option :async t)) (set-popup-rule! "^\\*nixos-options-doc\\*$" :ttl 0 :quit t) - ;; Fix #3927: disable idle completion because `company-nixos-options' is - ;; dreadfully slow. It can still be invoked manually.. - (setq-hook! 'nix-mode-hook company-idle-delay nil) - (when (modulep! +lsp) - (add-hook 'nix-mode-local-vars-hook #'lsp! 'append)) - (when (modulep! +tree-sitter) - (add-hook 'nix-mode-local-vars-hook #'tree-sitter! 'append)) + (add-hook (intern (format "%s-local-vars-hook" mode)) #'lsp! 'append)) (map! :localleader - :map nix-mode-map + :map ,(symbol-value (intern (format "%s-map" mode))) "f" #'nix-update-fetch "p" #'nix-format-buffer "r" #'nix-repl-show @@ -43,6 +35,32 @@ "o" #'+nix/lookup-option)) +(use-package! nix-mode + :interpreter ("\\(?:cached-\\)?nix-shell" . +nix-shell-init-mode) + :mode "\\.nix\\'" + :config + (+nix-common-config 'nix-mode)) + + +(use-package! nix-ts-mode + :when (modulep! +tree-sitter) + :when (fboundp 'treesit-available-p) + :defer t + :init + (set-tree-sitter! 'nix-mode 'nix-ts-mode + '((nix :url "https://github.com/nix-community/tree-sitter-nix"))) + :config + (+nix-common-config 'nix-ts-mode)) + + +(use-package! company-nixos-options + :defer t + :init + ;; Fix #3927: disable idle completion because `company-nixos-options' is + ;; dreadfully slow. It can still be invoked manually.. + (setq-hook! '(nix-mode-hook nix-ts-mode-hook) company-idle-delay nil)) + + (use-package! nix-update :commands nix-update-fetch) diff --git a/modules/lang/nix/doctor.el b/modules/lang/nix/doctor.el index 366d273e7..1a8c0f0e3 100644 --- a/modules/lang/nix/doctor.el +++ b/modules/lang/nix/doctor.el @@ -1,13 +1,13 @@ ;; -*- lexical-binding: t; no-byte-compile: t; -*- ;;; lang/nix/doctor.el +(assert! (or (modulep! -tree-sitter) + (modulep! :tools tree-sitter)) + "This module requires (:tools tree-sitter)") + (unless (executable-find "nix") (warn! "Couldn't find the nix package manager. nix-mode won't work.")) (when (require 'nix-mode nil t) (unless (executable-find nix-nixfmt-bin) (warn! (concat "Couldn't find " nix-nixfmt-bin ". nix-format-buffer won't work.")))) - -(assert! (or (modulep! -tree-sitter) - (modulep! :tools tree-sitter)) - "This module requires (:tools tree-sitter)") diff --git a/modules/lang/nix/packages.el b/modules/lang/nix/packages.el index 6ce4debbc..f76d55da8 100644 --- a/modules/lang/nix/packages.el +++ b/modules/lang/nix/packages.el @@ -4,6 +4,10 @@ (package! nix-mode :pin "719feb7868fb567ecfe5578f6119892c771ac5e5") (package! nix-update :pin "77022ccd918d665acbb519b243e7e3dc5eae1c47") +(when (and (modulep! +tree-sitter) + (fboundp 'treesit-available-p)) + (package! nix-ts-mode :pin "62ce3a2dc39529c5db3516427e84b2c96b8efcfd")) + (when (modulep! :completion company) (package! company-nixos-options :pin "053a2d5110ce05b7f99bcc2ac4804b70cbe87916")) diff --git a/modules/tools/tree-sitter/config.el b/modules/tools/tree-sitter/config.el index 4857193f5..299cf2796 100644 --- a/modules/tools/tree-sitter/config.el +++ b/modules/tools/tree-sitter/config.el @@ -32,7 +32,6 @@ (javascript "https://github.com/tree-sitter/tree-sitter-javascript" "master" "src" nil nil) (latex "https://github.com/latex-lsp/tree-sitter-latex" nil nil nil nil) (make "https://github.com/tree-sitter-grammars/tree-sitter-make" nil nil nil nil) - (nix "https://github.com/nix-community/tree-sitter-nix" nil nil nil nil) (nu "https://github.com/nushell/tree-sitter-nu" nil nil nil nil) (org "https://github.com/milisims/tree-sitter-org" nil nil nil nil) (perl "https://github.com/ganezdragon/tree-sitter-perl" nil nil nil nil)