[arch-general] gpg-agent, ssh keys, and systemd --user

Sebastian Schwarz seschwar at gmail.com
Fri Apr 12 08:36:19 EDT 2013


On 2013-04-11 at 22:50 +1000, Robbie Smith wrote:
> if (( $+commands[gpg-agent] )); then
> 	local InfoFile=/run/user/$(id -u)/gpg-agent.info
> 	if [[ -s $InfoFile ]]; then
> 		eval "$(cat $InfoFile)"
> 	fi
> 	unset InfoFile
> fi
>
> Strangely enough, this doesn’t seem to work.  What’s the
> difference between exporting variables in /etc/profile.d/ and
> ~/.zshrc?

First, `/etc/profile` and therefore `/etc/profile.d` is sourced
by login shells and `~/.zshrc` sourced by interactive Z
shells, Thus an interactive login Z shell reads both.  See the
"STARTUP/SHUTDOWN FILES" section in man:zsh(1).

Second, you aren't "exporting" anything.  The gpg-agent env-file
is an environment file consisting of one variable assignment per
line.  When you source the file the shell sets these variables
just as shell variables.  They are only available to the current
shell process.  To make them available to child process as
environment variables you have to export them.  The simplest way
to do all this probably would be:

    export $(< "$InfoFile")

And third, a couple of other remarks.

> if (( $+commands[gpg-agent] )); then
> 	local InfoFile=/run/user/$(id -u)/gpg-agent.info

The runtime directory is available in the environment variable
XDG_RUNTIME_DIR.  You can just use

    local InfoFile=$XDG_RUNTIME_DIR/gpg-agent.info

Or if you don't want to rely on that:

    local InfoFile=${XDG_RUNTIME_DIR:-/run/user/${UID:-$(id -u)}}/gpg-agent.info

> 	if [[ -s $InfoFile ]]; then
> 		eval "$(cat $InfoFile)"
> 	fi
> 	unset InfoFile
> fi

Don't read a file with cat and the evaluate the output.  Just
let the shell source the file directly:

    source "$InfoFile"

HTH,
Sebastian


More information about the arch-general mailing list