Skip to content

Commit 06c404e

Browse files
committed
tools/direnv: replace direnv package with envrc
wbolster/emacs-direnv uses an approach that mutates global state, allowing a direnv to bleed into unrelated buffers and contexts. For this to work it must rereads the direnv every time you switch buffers, which can be very slow. purcell/envrc, on the other hand, makes env state buffer-local, so the direnv only needs to be read once, when a is first initialized. This is faster and less error prone. However, it's necessary to manually reload the direnv if you've changed your .envrc outside of Emacs (with `M-x envrc-reload` or `M-x envrc-reload-all`).
1 parent e54bb17 commit 06c404e

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

modules/tools/direnv/README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ direnv and then made available to the current shell.
3434
This module provides no flags.
3535

3636
** Plugins
37-
+ [[https://github.com/wbolster/emacs-direnv][direnv]]
37+
+ [[https://github.com/purcell/envrc][envrc]]
3838

3939
** Hacks
4040
+ Normally, the direnv environment is updated on ~post-command-hook~. We've

modules/tools/direnv/config.el

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,39 @@
55
"join_args" "expand_path" "dotenv" "user_rel_path" "find_up" "source_env"
66
"watch_file" "source_up" "direnv_load" "MANPATH_add" "load_prefix" "layout"
77
"use" "rvm" "use_nix" "use_guix")
8-
"TODO")
8+
"A list of direnv keywords, which are fontified when in `+direnv-rc-mode'.")
99

1010

1111
;;
1212
;;; Packages
1313

14-
(use-package! direnv
15-
:hook (before-hack-local-variables . direnv--maybe-update-environment)
16-
:hook (flycheck-before-syntax-check . direnv--maybe-update-environment)
17-
:hook (direnv-envrc-mode . +direnv-envrc-fontify-keywords-h)
14+
(use-package! envrc
15+
:when (executable-find "direnv")
16+
:after-call doom-first-file
17+
:mode ("\\.envrc\\'" . +direnv-rc-mode)
1818
:config
19-
(add-to-list 'direnv-non-file-modes 'vterm-mode)
19+
(add-to-list 'doom-debug-variables 'envrc-debug)
2020

21-
(defun +direnv-envrc-fontify-keywords-h ()
22-
"Fontify special .envrc keywords; it's a good indication of whether or not
23-
we've typed them correctly."
21+
;; I'm avoiding `global-envrc-mode' intentionally, because it has the
22+
;; potential to run too late in the mode startup process (and after, say,
23+
;; server hooks that may rely on that local direnv environment).
24+
(add-hook! 'change-major-mode-after-body-hook
25+
(defun +direnv-init-h ()
26+
(unless (or envrc-mode
27+
(minibufferp)
28+
(file-remote-p default-directory))
29+
(envrc-mode 1))))
30+
31+
(define-derived-mode +direnv-rc-mode sh-mode "envrc"
32+
"Major mode for .envrc files."
33+
;; Fontify .envrc keywords; it's a good indication of whether or not we've
34+
;; typed them correctly, and that we're in the correct major mode.
2435
(font-lock-add-keywords
2536
nil `((,(regexp-opt +direnv-keywords 'symbols)
2637
(0 font-lock-keyword-face)))))
2738

2839
(defadvice! +direnv--fail-gracefully-a (&rest _)
2940
"Don't try to use direnv if the executable isn't present."
30-
:before-while #'direnv-update-directory-environment
41+
:before-while #'envrc-mode
3142
(or (executable-find "direnv")
32-
(ignore (doom-log "Couldn't find direnv executable"))))
33-
34-
(direnv-mode +1))
43+
(ignore (doom-log "Couldn't find direnv executable")))))

modules/tools/direnv/packages.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
;; -*- no-byte-compile: t; -*-
22
;;; tools/direnv/packages.el
33

4-
(package! direnv :pin "f5484b0fc33d4e5116612626294efb362ff9ecd4")
4+
(package! envrc :pin "1dc5aad14d2c27211c7c288d2d9dffeb2e27cb2d")

0 commit comments

Comments
 (0)