diff --git a/modules/tools/direnv/config.el b/modules/tools/direnv/config.el index 0579dd606..f434f1b37 100644 --- a/modules/tools/direnv/config.el +++ b/modules/tools/direnv/config.el @@ -2,7 +2,7 @@ (use-package! envrc :when (executable-find "direnv") - :after-call doom-first-file-hook + :hook (doom-first-file . envrc-global-mode) :config (add-to-list 'doom-debug-variables 'envrc-debug) @@ -26,27 +26,12 @@ (defadvice! +direnv--fail-gracefully-a (&rest _) "Don't try to use direnv if the executable isn't present." :before-while #'envrc-mode - (or (executable-find "direnv") + (or (get 'envrc-mode 'direnv-executable) + (put 'envrc-mode 'direnv-executable (executable-find "direnv" t)) (ignore (doom-log "Couldn't find direnv executable")))) - ;; HACK envrc-mode only affects the current buffer's environment, which is - ;; generally what we want, except when we're running babel blocks in - ;; org-mode, because there may be state or envvars those blocks need to - ;; read. In order to perpetuate the org buffer's environment into the - ;; execution of the babel block we need to temporarily change the global - ;; environment. Let's hope it runs quickly enough that its effects aren't - ;; felt in other buffers in the meantime! - (defvar +direnv--old-environment nil) - (defadvice! +direnv-persist-environment-a (orig-fn &rest args) - :around #'org-babel-execute-src-block - (if +direnv--old-environment - (apply orig-fn args) - (setq-default +direnv--old-environment - (cons (default-value 'process-environment) - (default-value 'exec-path)) - exec-path exec-path - process-environment process-environment) - (unwind-protect (apply orig-fn args) - (setq-default process-environment (car +direnv--old-environment) - exec-path (cdr +direnv--old-environment) - +direnv--old-environment nil))))) + ;; Ensure babel's execution environment matches the host buffer's. + (advice-add #'org-babel-execute-src-block :around #'envrc-propagate-environment) + + ;; Make sure any envrc changes are propagated after a `doom/reload' + (add-hook 'doom-after-reload-hook #'envrc-reload-all))