0

In my dotfiles I have the following setup to make ssh connections using my gpg key:

.bash_profile:

export GPG_TTY=$(tty)
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
gpgconf --launch gpg-agent

.gnupg/gpg-agent.conf:

enable-ssh-support
default-cache-ttl 600
max-cache-ttl 7200

and calling ssh-add -l shows me my correspondig gpg key.

This does obviously not translate into tramp/eshell. I tried setting the SSH_AUTH_SOCK variable inside .config/emacs/eshell/profile:

(let ((ssh_auth_sock (shell-command-to-string "gpgconf --list-dirs agent-ssh-socket")))
(setenv "SSH_AUTH_SOCK" ssh_auth_sock))

but this does not work and ssh-add -l only displays the error

Error connecting to agent: No such file or directory

What is the correct way of getting this setup working inside tramp/eshell?

Reza
  • 103
  • 2
  • What is the value of the SSH_AUTH_SOCK variable in the shell? Does it have a newline at the end of it? Does the socket filename on disk have a newline at the end of its name? – db48x Feb 10 '22 at 01:32
  • Hi thanks for your feedback, no there is not a newline at the end of the value: /run/user/1000/gnupg/S.gpg-agent.ssh – Reza Feb 11 '22 at 09:09
  • Are you sure? What do you get if you type ls $SSH_AUTH_SOCK into eshell? – db48x Feb 11 '22 at 15:23
  • Sorry for the late reply, this is the output: ~ $ ls $SSH_AUTH_SOCK /run/user/1000/gnupg/S.gpg-agent.ssh : No such file or directory – Reza Feb 13 '22 at 16:40
  • I see a space after the end of the file name and before the colon that starts the error message. Either SSH_AUTH_SOCK has a space after it, or it has a newline and StackExchange mangled it because this is a comment rather than an answer. Either way, this is the source of your problem. – db48x Feb 13 '22 at 16:46
  • Yeah it's a newline it got mangled in the comment, but why is this a problem, in shell it is just working fine – Reza Feb 13 '22 at 21:37
  • I just tested SSH_AUTH_SOCK=/run/user/1000/gnupg/S.gpg-agent.ssh ssh-add -l and it is working, what can I do to prevent eshell from inserting a newline into my env var? – Reza Feb 13 '22 at 21:41
  • Eshell is not inserting a newline into anything :P – db48x Feb 13 '22 at 21:47
  • Just found this answer: https://emacs.stackexchange.com/questions/21901/why-is-there-a-newline-in-the-result-of-shell-command-to-string, thanks for pointing me into the right direction, if you post your comment as an answer I will accept it as the right answer – Reza Feb 13 '22 at 21:49
  • 1
    I am in the middle of writing one :) – db48x Feb 13 '22 at 21:53

1 Answers1

1

As we determined in the comments, ssh-add cannot open the socket file because that value of the SSH_AUTH_SOCK has a newline in it. This newline is coming from your call to gpgconf. Every well–behaved program will ensure that the final line it prints ends in a newline character so that your prompt always ends up at the start of a new line. In this case that is perhaps unfortunate, because what you wanted it to print was not a well–formed line of text but a filename. Luckily removing the unwanted newline character is not hard. Incidentally, you should check the man page for gpgconf as there is an additional level of encoding here that could trip you up in the future, but which I will ignore for the purpose of answering this question.

As you found, there are a number of ways to remove this extraneous newline character after calling shell-command-to-string and before putting it into the environment. I like this one, possibly because I learned Perl at an early age:

(defun chomp (str)
  "Chomp tailing whitespace from STR."
  (replace-regexp-in-string (rx (* (any " \t\n")) eos)
                            ""
                            str))

You could use it like this:

(let ((ssh_auth_sock (chomp (shell-command-to-string "gpgconf --list-dirs agent-ssh-socket"))))
  (setenv "SSH_AUTH_SOCK" ssh_auth_sock))
db48x
  • 17,977
  • 1
  • 22
  • 28