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/bash
72
73 # set the default value
74 XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share}"
75
76 # add a directory if it exists
77 if [[ -d /opt/foo/share ]]; then
78 XDG_DATA_DIRS=/opt/foo/share:${XDG_DATA_DIRS}
79 fi
80
81 # write our output
82 echo XDG_DATA_DIRS=$XDG_DATA_DIRS
83
84 Example 2. A more complicated generator which reads existing
85 configuration and mutates one variable
86
87 # 90-rearrange-path.py
88
89 #!/usr/bin/env python3
90
91 """
92
93 Proof-of-concept systemd environment generator that makes sure that bin dirs
94 are always after matching sbin dirs in the path.
95 (Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar.)
96
97 This generator shows how to override the configuration possibly created by
98 earlier generators. It would be easier to write in bash, but let's have it
99 in Python just to prove that we can, and to serve as a template for more
100 interesting generators.
101
102 """
103
104 import os
105 import pathlib
106
107 def rearrange_bin_sbin(path):
108 """Make sure any pair of .../bin, .../sbin directories is in this order
109
110 >>> rearrange_bin_sbin('/bin:/sbin:/usr/sbin:/usr/bin')
111 '/bin:/sbin:/usr/bin:/usr/sbin'
112 """
113 items = [pathlib.Path(p) for p in path.split(':')]
114 for i in range(len(items)):
115 if 'sbin' in items[i].parts:
116 ind = items[i].parts.index('sbin')
117 bin = pathlib.Path(*items[i].parts[:ind], 'bin', *items[i].parts[ind+1:])
118 if bin in items[i+1:]:
119 j = i + 1 + items[i+1:].index(bin)
120 items[i], items[j] = items[j], items[i]
121 return ':'.join(p.as_posix() for p in items)
122
123 if __name__ == '__main__':
124 path = os.environ['PATH'] # This should be always set.
125 # If it's not, we'll just crash, we is OK too.
126 new = rearrange_bin_sbin(path)
127 if new != path:
128 print('PATH={}'.format(new))
129
130 Example 3. Debugging a generator
131
132 SYSTEMD_LOG_LEVEL=debug VAR_A=something VAR_B="something else" \
133 /usr/lib/systemd/system-environment-generators/path-to-generator
134
136 systemd-environment-d-generator(8), systemd.generator(7), systemd(1),
137 systemctl(1)
138
139
140
141systemd 245 SYSTEMD.ENVIRONMENT-GENERATOR(7)