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

Sam Ravnborg sam at ravnborg.org
Thu Mar 16 19:54:33 UTC 2017


On Wed, Mar 15, 2017 at 12:21:49AM +0000, Zbigniew Jędrzejewski-Szmek wrote:
> 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.

Hi Zbyszek.

Thanks the the response.
What we ended up with was your solution 1 - with a service that
generates a file in /run/etc/*. The service is "WantedBy=multi-user.target"
and thus will run before any QT applications.

For the shell (which is both serial and ssh) we implemented
a script we located in /etc/profile.d/ which sets and exports relevant
variables. So again more or less as suggested by you.

We (a colleague of mine) implemented this before your reply,
but it was great to be confirmed that what we have is a good solution.

So thanks again,

	Sam


More information about the systemd-devel mailing list