1SYSTEMD.ENVIRONMENT-GENERAsTyOsRt(e7m)d.environment-geSnYeSrTaEtMoDr.ENVIRONMENT-GENERATOR(7)
2
3
4
6 systemd.environment-generator - systemd environment file generators
7
9 /usr/lib/systemd/system-environment-generators/some-generator
10
11 /usr/lib/systemd/user-environment-generators/some-generator
12
13 /run/systemd/system-environment-generators/*
14 /etc/systemd/system-environment-generators/*
15 /usr/local/lib/systemd/system-environment-generators/*
16 /usr/lib/systemd/system-environment-generators/*
17
18 /run/systemd/user-environment-generators/*
19 /etc/systemd/user-environment-generators/*
20 /usr/local/lib/systemd/user-environment-generators/*
21 /usr/lib/systemd/user-environment-generators/*
22
23
25 Generators are small executables that live in
26 /usr/lib/systemd/system-environment-generators/ and other directories
27 listed above. systemd(1) will execute those binaries very early at the
28 startup of each manager and at configuration reload time, before
29 running the generators described in systemd.generator(7) and before
30 starting any units. Environment generators can override the environment
31 that the manager exports to services and other processes.
32
33 Generators are loaded from a set of paths determined during
34 compilation, as listed above. System and user environment generators
35 are loaded from directories with names ending in
36 system-environment-generators/ and user-environment-generators/,
37 respectively. Generators found in directories listed earlier override
38 the ones with the same name in directories lower in the list. A symlink
39 to /dev/null or an empty file can be used to mask a generator, thereby
40 preventing it from running. Please note that the order of the two
41 directories with the highest priority is reversed with respect to the
42 unit load path, and generators in /run/ overwrite those in /etc/.
43
44 After installing new generators or updating the configuration,
45 systemctl daemon-reload may be executed. This will re-run all
46 generators, updating environment configuration. It will be used for any
47 services that are started subsequently.
48
49 Environment file generators are executed similarly to unit file
50 generators described in systemd.generator(7), with the following
51 differences:
52
53 • Generators are executed sequentially in the alphanumerical order of
54 the final component of their name. The output of each generator
55 output is immediately parsed and used to update the environment for
56 generators that run after that. Thus, later generators can use
57 and/or modify the output of earlier generators.
58
59 • Generators are run by every manager instance, their output can be
60 different for each user.
61
62 It is recommended to use numerical prefixes for generator names to
63 simplify ordering.
64
66 Example 1. A simple generator that extends an environment variable if a
67 directory exists in the file system
68
69 # 50-xdg-data-dirs.sh
70
71 #!/bin/sh
72 # SPDX-License-Identifier: MIT-0
73
74 # set the default value
75 XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share}"
76
77 # add a directory if it exists
78 if [ -d /opt/foo/share ]; then
79 XDG_DATA_DIRS="/opt/foo/share:${XDG_DATA_DIRS}"
80 fi
81
82 # write our output
83 echo "XDG_DATA_DIRS=${XDG_DATA_DIRS}"
84
85 Example 2. A more complicated generator which reads existing
86 configuration and mutates one variable
87
88 # 90-rearrange-path.py
89
90 #!/usr/bin/env python3
91 # SPDX-License-Identifier: MIT-0
92
93 """
94
95 Proof-of-concept systemd environment generator that makes sure that bin dirs
96 are always after matching sbin dirs in the path.
97 (Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar.)
98
99 This generator shows how to override the configuration possibly created by
100 earlier generators. It would be easier to write in bash, but let's have it
101 in Python just to prove that we can, and to serve as a template for more
102 interesting generators.
103
104 """
105
106 import os
107 import pathlib
108
109 def rearrange_bin_sbin(path):
110 """Make sure any pair of .../bin, .../sbin directories is in this order
111
112 >>> rearrange_bin_sbin('/bin:/sbin:/usr/sbin:/usr/bin')
113 '/bin:/sbin:/usr/bin:/usr/sbin'
114 """
115 items = [pathlib.Path(p) for p in path.split(':')]
116 for i in range(len(items)):
117 if 'sbin' in items[i].parts:
118 ind = items[i].parts.index('sbin')
119 bin = pathlib.Path(*items[i].parts[:ind], 'bin', *items[i].parts[ind+1:])
120 if bin in items[i+1:]:
121 j = i + 1 + items[i+1:].index(bin)
122 items[i], items[j] = items[j], items[i]
123 return ':'.join(p.as_posix() for p in items)
124
125 if __name__ == '__main__':
126 path = os.environ['PATH'] # This should be always set.
127 # If it's not, we'll just crash, which is OK too.
128 new = rearrange_bin_sbin(path)
129 if new != path:
130 print('PATH={}'.format(new))
131
132 Example 3. Debugging a generator
133
134 SYSTEMD_LOG_LEVEL=debug VAR_A=something VAR_B="something else" \
135 /usr/lib/systemd/system-environment-generators/path-to-generator
136
138 systemd-environment-d-generator(8), systemd.generator(7), systemd(1),
139 systemctl(1)
140
141
142
143systemd 253 SYSTEMD.ENVIRONMENT-GENERATOR(7)