You are not logged in.

#1 2013-05-16 04:31:19

Morrad
Member
Registered: 2012-01-18
Posts: 54

How to export environment variables in systemd user session?

Hi.

I'm slowly converting my .xinitrc into a systemd user session.  I'm stumped as to how to replace exporting environment variables though.

My current .xinitrc has the following:

#!/bin/bash                                                                                                                
# Source scripts in /etc/X11/xinit/xinitrc.d/ (ie, dbus)
# Need dbus already active to do thing with gnome-keyring-daemon next.
# In event I can get exporting to work, I can just have the 
# gnome-keyring-daemon.service file depend on dbus and this goes away
if [ -d /etc/X11/xinit/xinitrc.d ]; then
    for f in /etc/X11/xinit/xinitrc.d/*; do
        [ -x "$f" ] && . "$f"
    done
    unset f
fi

if [ -x /usr/bin/gnome-keyring-daemon ] ; then
    eval $( /usr/bin/gnome-keyring-daemon --start --components=gpg,pks11,secrets,ssh )
    export SSH_AUTH_SOCK
    export GPG_AGENT_INFO
    export GNOME_KEYRING_CONTROL
    export GNOME_KEYRING_PID
fi

/usr/lib/systemd/systemd --user

I'm not sure how to replace the export statements.  I'm aware of the Environment= and EnvironmentFile= directives in unit files, but these seem to be inputs to the executed command.  I don't see a way to export anything.

My current gnome-keyring-daemon.service file:

[Unit]
Description=Gnome Keyring Daemon
Requires=dbus.socket

[Service]
Type=forking
ExecStart=/usr/bin/gnome-keyring-daemon --start --components=gpg,pkcs11,secrets,ssh
Restart=always

[Install]
WantedBy=mystuff.target

Does anyone have any ideas on what to do here?

Offline

#2 2013-05-16 05:57:17

Morrad
Member
Registered: 2012-01-18
Posts: 54

Re: How to export environment variables in systemd user session?

So I've done a bit more research and experimenting.  I've found a good way to export environment variables using systemctl so that they are available to systemd spawned processes, but not to me as a user on the command line (or via cron).

# http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/8995

[Unit]
Description=Gnome Keyring Daemon
Requires=dbus.socket

[Service]
Type=oneshot
ExecStart=/bin/sh -c "for env in $( /usr/bin/gnome-keyring-daemon --start --components=gpg,pks11,secrets,ssh ); do /usr/bin/systemctl --user set-environment $env; done"
ExecStop=/bin/sh -c "for env in GNOME_KEYRING_PID GNOME_KEYRING_CONTROL SSH_AUTH_SOCK GPG_AGENT_INFO; do /usr/bin/systemctl --no-block --user unset-environment $env; done"
RemainAfterExit=yes

[Install]
WantedBy=mystuff.target

After that, I can see the desired environmental variables in systemctl --user show-environment, but they don't show up in my shell when I printenv.

Last edited by Morrad (2013-05-16 05:57:32)

Offline

#3 2013-05-16 08:42:16

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: How to export environment variables in systemd user session?

Your shell (or whatever spawns your shell later on) is probably started before this service. You need to add some ordering directives. Create env.target and make services that set environment variables start Before=env.target and services that may need the environment variables After=env.target.

Offline

Board footer

Powered by FluxBB