purcell / envrc

Emacs support for direnv which operates buffer-locally
378 stars 35 forks source link

Unable to use in a git repository #84

Closed Eliot00 closed 4 months ago

Eliot00 commented 4 months ago

I installed direnv and git using nix-darwin and home-manager. By setting PATH, envrc found direnv. However, if my .envrc directories use git, an error occurs:

direnv: loading /Volumes/Extra/Code/sunman-programmable/.envrc
direnv: using flake
error:
       … while fetching the input 'git+file:///Volumes/Extra/Code/sunman-programmable'

       error: getting the HEAD of the Git tree '/Volumes/Extra/Code/sunman-programmable' failed with exit code 1:
       error: executing 'git': No such file or directory

direnv: nix-direnv: Evaluating current devShell failed. Falling back to previous environment!
direnv: export +NIX_DIRENV_DID_FALLBACK ~PATH ~XPC_SERVICE_NAME

my config:

(when (eq system-type 'darwin)
  ;; 加载你的 Nix shell 配置文件
  (defun my/load-nix-shell-env ()
    "Load the environment variables from the nix shell."
    (interactive)
    (let ((path-from-shell (replace-regexp-in-string
                            "[ \t\n]*$"
                            ""
                            (shell-command-to-string "/run/current-system/sw/bin/bash --login -i -c 'echo $PATH'"))))
      (setenv "PATH" path-from-shell)
      (setq eshell-path-env path-from-shell)
      (setq exec-path (split-string path-from-shell path-separator))))

  ;; 调用这个函数来加载 Nix shell 环境
  (my/load-nix-shell-env))

(use-package envrc
  :hook (after-init . envrc-global-mode))
purcell commented 4 months ago

What makes you think this is specific to envrc.el? Does nix develop work in that directory in the terminal? What's the global value of (getenv "PATH") and exec-path? Probably your .envrc assumes that git is on the path of the parent shell, and that isn't the case, but that's not the fault of envrc.el.

I notice you're using a snippet from StackOverflow that I published years ago to set exec-path from your shell, but you should consider using my exec-path-from-shell package for that instead. It fixes various potential problems with the snippet, plus if you use it you can enable exec-path-from-shell-debug to get more info about how it is changing your Emacs environment.

Eliot00 commented 4 months ago

Does nix develop work in that directory in the terminal?

Yes, and if I remove .git, envrc.el can work with direnv.

Finally, exec-path-from-shell solved my problem, thanks.

purcell commented 4 months ago

Great!