[systemd-devel] How to set environment variable that is available for later services/shell

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed Mar 15 00:21:49 UTC 2017


On Sat, Mar 11, 2017 at 06:14:17PM +0100, Sam Ravnborg wrote:
> Hi all.
> 
> How can we set environment variables so they are available for everyone?
> 
> 
> Background:
> 
> On an embedded target we use QT applications.
> 
> To start our QT application we need to set environment variables:
> 
> QT_QPA_GENERIC_PLUGINS=tslib:/dev/input/event0                 
> QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0:mmsize=154x86:size=800x480 
> ...
> 
> 
> For now we use:
> [Service]                                                                       
> EnvironmentFile=/etc/foo.conf
> 
> This has two drawbacks:
> 1) We cannot start the QT application from the
>    command-line (unless we source the .conf file)
> 2) We need to use different .conf files (thus different
>    .service files) depending on the HW configuration (display type/size)
> 
> So we are looking for a way to set the environment variables early
> such that they are available in the services launced later,
> and they are available in the shell.
> The actual system configuration will determine the values and
> it may differ from boot to boot.

There are a few different mechanisms, and which one is the best
fit depends on when the information about the content of those
variables is known and how widely it should be disseminated.

There are at least the following possibilities:
1. you could generate a file like /run/qtvars.conf from some service,
and make sure that this service is started at boot before any
relevant services (including user sessions), and import it there,
by doing EnvironmentFile=/run/qtvars.conf.

The advantage is that this gets run asynchronously during boot,
so it can wait for hardware to be detected.

2. (with systemd 233) you could also create a file like
/run/environment.d/60-qt.conf with similar contents,
and variables set therein would be available for services
started from systemd --user, and possibly some login environments.
This part is a work in progress, in particular ssh logins do not
inherit those variables. So this might be a solution in the
future, but probably not atm.

It sounds like doing 1. + adding '[ -f /run/qtvars.conf ] && . /run/qtvars.conf'
somewhere in /etc/profile.d might be your best option.

HTH,
Zbyszek


More information about the systemd-devel mailing list