2

On my Mac laptop, I can start Emacs in several ways, including (1) clicking on an icon on the dock, or (2) running

% emacs

from the Terminal app.

These two ways of launching Emacs produce instances of Emacs that run differently, because they have different settings for their environment variables.

How can I give the instance of Emacs that I start by clicking on an icon the "same"1 environment as that of an instance of Emacs started from the Unix shell command-line (as described above) right after the parent shell is initialized?


1 Well, the two environments cannot be exactly the same, because there are some environment settings, such as the value of the $$ parameter, that are specific to the respective processes. So, when I say that two environments are "the same", I mean that they agree on all the settings for which such agreement is possible in principle.

2 Answers 2

2

There's a package called exec-path-from-shell that was made to solve this problem. By default, it'll try to figure out PATH and MANPATH from your shell setup, but you can customize the variable exec-path-from-shell-variables to pull in more environment variables. You'll need to figure out for yourself which environment variables matter to you.

;; note env vars of interest
(dolist (var '("SSH_AUTH_SOCK" "SSH_AGENT_PID" "GPG_AGENT_INFO" "LANG" "LC_CTYPE" "NIX_SSL_CERT_FILE" "NIX_PATH"))
  (add-to-list 'exec-path-from-shell-variables var))
;; set env vars in emacs
(when (memq window-system '(mac ns x))
  (exec-path-from-shell-initialize))
2
  • 1
    The problem with an emacs solution like the above is that it is disconnected from the rest of your system: there is no single source of truth. Every time you make a change in your environment for shell purposes, you will have to make (or at least think about) the corresponding change for emacs purposes - and vice versa.
    – NickD
    Sep 18, 2022 at 0:50
  • 2
    An alternative is the way aquamacs does this - it pipes the shell command env to a file and then parses it. github.com/aquamacs-emacs/aquamacs-emacs/blob/aquamacs3/… see functions mac-read-environment-vars* It is more complex but does the whole environment
    – mmmmmm
    Sep 18, 2022 at 9:51
1

I wrote a tutorial about this. exec-path-from-shell can significantly increase Emacs's startup time. I prefer the Doom Emacs approach: save the shell environment to a file, and load it when Emacs starts. My article shows how to do that.

3
  • The emacs-plus uses the injected path approach to get same thing. May be added to your tutorial.
    – Ian
    Aug 11, 2023 at 12:00
  • @Ian Thanks. Yes, I also noticed that emacs-plus injects PATH by modifying Emacs.app/Contents/Info.plist. I will add a note to my tutorial. Though there are other env variables that are useful in Emacs. For instance, I use Keychain to avoid typing ssh key passwords. SSH_AUTH_SOCK changes on every boot, so it's easier to update it with a script.
    – lazyseq
    Aug 25, 2023 at 8:50
  • Also see mac-read-environment-vars-from-shell in AAquamacs github.com/aquamacs-emacs/aquamacs-emacs/blob/aquamacs3/…
    – mmmmmm
    Apr 12 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.