2

When I don't start it from a terminal, Emacs 24.5.1 (compiled from source) doesn't pick up my environment variables. In particular, to "import" the environment variables from shell, I have in my .emacs

(setq exec-path-from-shell-variables
      (quote ("PATH" "MANPATH" "INFOPATH" "C_INCLUDE_PATH" "CPLUS_INCLUDE_PATH"
              "LIBRARY_PATH" "LD_LIBRARY_PATH" "SSH_AUTH_SOCK")))
(exec-path-from-shell-initialize)

and to provide my unlocked private key to ssh using GNOME Keyring's "ssh-agent" (aka gnome-keyring-daemon) I have in my .bashrc (my user shell is bash)

if [ -n "$DESKTOP_SESSION" ];then
    eval $(gnome-keyring-daemon --start --components=ssh)
    export SSH_AUTH_SOCK
fi

But upon doing any remote-related task (e.g., push or pull) in Magit I get the error

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

in the *magit-process* buffer, which means that Emacs does not see my unlocked private key and, hence, that Emacs is not using the socket provided by SSH_AUTH_SOCK, because the same remote-related git commands run without any problems in a terminal.

I've checked in eshell and SSH_AUTH_SOCK has the same value in Emacs as it does in an outside terminal with bash. Also, when I start Emacs from a terminal (in X or a TTY), all the environment variables are picked up and there are no problems. I know when run in a terminal, Emacs inherits the environment variables from the shell. My question is why aren't my environment variables (which are apparently "imported" correctly into Emacs by exec-path-from-shell-variables) used by Emacs when I don't start it from a terminal? The same setup has been working on my other machine for several years with Emacs 24.3, but I doubt the culprit could be the newer Emacs version.

Edit: My operating system is Trisquel 7.0 which is based on Ubuntu 14.04 GNU/Linux. I usually use LXDE, but the problem is there with other desktop environments too. Outside a terminal I start Emacs by pressing Alt+F2 (Run) and entering emacs. In a terminal I start Emacs by entering emacs. My environment variables are set in ~/.bashrc. I am aware of the intricacies of login and non-login shells (interactive or not) and I've checked that my ~/.bashrc is run regardless of how I log in (e.g., in X to a desktop environment or window manager, in a TTY, or through ssh).

5
  • Where are you setting your environment variables, and how do you start Emacs? What operating system are you using (if Linux, what distribution), with what desktop environment if Linux or other Unix? May 10, 2015 at 15:35
  • @Gilles I've edited my question and added extra information that may help.
    – 0mid
    May 10, 2015 at 17:24
  • 3
    I'm voting to close this question as off-topic because it is not about Emacs but about the environment that Emacs is running in. It would be on-topic on Unix & Linux. May 10, 2015 at 17:35
  • What do you get when you evaluate (getenv "SSH_AUTH_SOCK") inside of Emacs? Does it help if you manually set it with (setenv ...)?
    – user19541
    May 11, 2015 at 9:34
  • @rekado (getenv "SSH_AUTH_SOCK") in an Emacs session started "from a menu" or from a terminal shows the same socket as echo $SSH_AUTH_SOCK does in a terminal (outside Emacs) in X, just like echo $SSH_AUTH_SOCK does in Emac's eshell. Using (setenv ...) manually didn't help. (That's what exec-path-from-shell-variables is doing, essentially.)
    – 0mid
    May 14, 2015 at 18:16

1 Answer 1

3

My environment variables are set in ~/.bashrc.

There's your problem. Then your environment variables are only set in applications that you start from a terminal, they are not set in applications started from a GUI menu.

I am aware of the intricacies of login and non-login shells (interactive or not) and I've checked that my ~/.bashrc is run regardless of how I log in (e.g., in X to a desktop environment or window manager, in a TTY, or through ssh).

Evidently you are not aware of the intricacies of login and non-login shells. Your .bashrc is not run at all when you log in. It's run when you start an interactive shell.

The solution is to set your variables where they should be set, i.e. in ~/.pam_environment or ~/.profile (which is read by sessions invoked by some display managers such as GDM) or ~/.xsessionrc (which is read by sessions invoked by some display managers such as Lightdm).

See Alternative to .bashrc, Correctly setting environment, What's the best distro/shell-agnostic way to set environment variables?, How do I set the PATH or other environment variables so that X apps can access it?, Difference between Login Shell and Non-Login Shell? and many more.

2
  • Thank you for your answer. (I've read all of the material you've linked to in the past and learned much from your detailed answers in particular.) When I said "... my ~/.bashrc is run regardless of how I log in ..." I didn't mean it is run necessarily directly. My ~/.profile runs my ~/.bashrc. In any case, I'll take this question somewhere else.
    – 0mid
    May 10, 2015 at 18:54
  • 1
    @Omid I've flagged the question for migration. Please don't repost. Depending on your display manager (i.e. the program where you log in), .profile may or may not be read — you may need to use .xsessionrc or some other file instead. You should not load .bashrc unconditionally from .profile, only if the shell is interactive. May 10, 2015 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .