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.

kjo
  • 3,145
  • 14
  • 42

2 Answers2

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))
NickD
  • 27,023
  • 3
  • 23
  • 42
g-gundam
  • 1,096
  • 1
  • 3
  • 13
  • 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 '22 at 00:50
  • 2
    An alternative is the way aquamacs does this - it pipes the shell command env to a file and then parses it. https://github.com/aquamacs-emacs/aquamacs-emacs/blob/aquamacs3/aquamacs/src/site-lisp/macosx/mac-extra-functions.el see functions mac-read-environment-vars* It is more complex but does the whole environment – mmmmmm Sep 18 '22 at 09: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.

lazyseq
  • 11
  • 1
  • The ```emacs-plus``` uses the [injected path](https://github.com/d12frosted/homebrew-emacs-plus#injected-path) approach to get same thing. May be added to your tutorial. – Ian Aug 11 '23 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](https://www.funtoo.org/Funtoo: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 '23 at 08:50