Debian Codegraph @

upstream/debian-unstable/man7/systemd.environment-generator.7

raw
1 '\" t
2 .TH "SYSTEMD\&.ENVIRONMENT\-GENERATOR" "7" "" "systemd 261~rc3" "systemd.environment-generator"
3 .\" -----------------------------------------------------------------
4 .\" * Define some portability stuff
5 .\" -----------------------------------------------------------------
6 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 .\" http://bugs.debian.org/507673
8 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
9 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 .ie \n(.g .ds Aq \(aq
11 .el .ds Aq '
12 .\" -----------------------------------------------------------------
13 .\" * set default formatting
14 .\" -----------------------------------------------------------------
15 .\" disable hyphenation
16 .nh
17 .\" disable justification (adjust text to left margin only)
18 .ad l
19 .\" -----------------------------------------------------------------
20 .\" * MAIN CONTENT STARTS HERE *
21 .\" -----------------------------------------------------------------
22 .SH "NAME"
23 systemd.environment-generator \- systemd environment file generators
24 .SH "SYNOPSIS"
25 .HP \w'\fB/usr/lib/systemd/system\-environment\-generators/some\-generator\fR\ 'u
26 \fB/usr/lib/systemd/system\-environment\-generators/some\-generator\fR
27 .HP \w'\fB/usr/lib/systemd/user\-environment\-generators/some\-generator\fR\ 'u
28 \fB/usr/lib/systemd/user\-environment\-generators/some\-generator\fR
29 .PP
30 .RS 4
31 /run/systemd/system\-environment\-generators/*
32 .RE
33 .RS 4
34 /etc/systemd/system\-environment\-generators/*
35 .RE
36 .RS 4
37 /usr/local/lib/systemd/system\-environment\-generators/*
38 .RE
39 .RS 4
40 /usr/lib/systemd/system\-environment\-generators/*
41 .RE
42 .PP
43 .RS 4
44 /run/systemd/user\-environment\-generators/*
45 .RE
46 .RS 4
47 /etc/systemd/user\-environment\-generators/*
48 .RE
49 .RS 4
50 /usr/local/lib/systemd/user\-environment\-generators/*
51 .RE
52 .RS 4
53 /usr/lib/systemd/user\-environment\-generators/*
54 .RE
55 .SH "DESCRIPTION"
56 .PP
57 Generators are small executables that live in
58 /usr/lib/systemd/system\-environment\-generators/
59 and other directories listed above\&.
60 \fBsystemd\fR(1)
61 will execute those binaries very early at the startup of each manager and at configuration reload time, before running the generators described in
62 \fBsystemd.generator\fR(7)
63 and before starting any units\&. Environment generators can override the environment that the manager exports to services and other processes\&.
64 .PP
65 Generators are loaded from a set of paths determined during compilation, as listed above\&. System and user environment generators are loaded from directories with names ending in
66 system\-environment\-generators/
67 and
68 user\-environment\-generators/, respectively\&. Generators found in directories listed earlier override the ones with the same name in directories lower in the list
69 \&\s-2\u[1]\d\s+2\&. A symlink to
70 /dev/null
71 or an empty file can be used to mask a generator, thereby preventing it from running\&. Please note that the order of the two directories with the highest priority is reversed with respect to the unit load path, and generators in
72 /run/
73 overwrite those in
74 /etc/\&.
75 .PP
76 After installing new generators or updating the configuration,
77 \fBsystemctl daemon\-reload\fR
78 may be executed\&. This will re\-run all generators, updating environment configuration\&. It will be used for any services that are started subsequently\&.
79 .PP
80 Environment file generators are executed similarly to unit file generators described in
81 \fBsystemd.generator\fR(7), with the following differences:
82 .sp
83 .RS 4
84 .ie n \{\
85 \h'-04'\(bu\h'+03'\c
86 .\}
87 .el \{\
88 .sp -1
89 .IP \(bu 2.3
90 .\}
91 Generators are executed sequentially in the alphanumerical order of the final component of their name\&. The output of each generator output is immediately parsed and used to update the environment for generators that run after that\&. Thus, later generators can use and/or modify the output of earlier generators\&.
92 .RE
93 .sp
94 .RS 4
95 .ie n \{\
96 \h'-04'\(bu\h'+03'\c
97 .\}
98 .el \{\
99 .sp -1
100 .IP \(bu 2.3
101 .\}
102 Generators are run by every manager instance, their output can be different for each user\&.
103 .RE
104 .PP
105 It is recommended to use numerical prefixes for generator names to simplify ordering\&.
106 .SH "EXAMPLES"
107 .PP
108 \fBExample\ \&1.\ \&A simple generator that extends an environment variable if a directory exists in the file system\fR
109 .sp
110 .if n \{\
111 .RS 4
112 .\}
113 .nf
114 # 50\-xdg\-data\-dirs\&.sh
115
116 #!/bin/sh
117 # SPDX\-License\-Identifier: MIT\-0
118
119 # set the default value
120 XDG_DATA_DIRS="${XDG_DATA_DIRS:\-/usr/local/share/:/usr/share}"
121
122 # add a directory if it exists
123 if [ \-d /opt/foo/share ]; then
124 XDG_DATA_DIRS="/opt/foo/share:${XDG_DATA_DIRS}"
125 fi
126
127 # write our output
128 echo "XDG_DATA_DIRS=${XDG_DATA_DIRS}"
129 .fi
130 .if n \{\
131 .RE
132 .\}
133 .PP
134 \fBExample\ \&2.\ \&A more complicated generator which reads existing configuration and mutates one variable\fR
135 .sp
136 .if n \{\
137 .RS 4
138 .\}
139 .nf
140 # 90\-rearrange\-path\&.py
141
142 #!/usr/bin/python
143 # SPDX\-License\-Identifier: MIT\-0
144
145 """
146
147 Proof\-of\-concept systemd environment generator that makes sure that bin dirs
148 are always after matching sbin dirs in the path\&.
149 (Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar\&.)
150
151 This generator shows how to override the configuration possibly created by
152 earlier generators\&. It would be easier to write in bash, but let\*(Aqs have it
153 in Python just to prove that we can, and to serve as a template for more
154 interesting generators\&.
155
156 """
157
158 import os
159 import pathlib
160
161
162 def rearrange_bin_sbin(path):
163 """Make sure any pair of \&.../bin, \&.../sbin directories is in this order
164
165 >>> rearrange_bin_sbin(\*(Aq/bin:/sbin:/usr/sbin:/usr/bin\*(Aq)
166 \*(Aq/bin:/sbin:/usr/bin:/usr/sbin\*(Aq
167 """
168 items = [pathlib\&.Path(p) for p in path\&.split(\*(Aq:\*(Aq)]
169 for i in range(len(items)):
170 if \*(Aqsbin\*(Aq in items[i]\&.parts:
171 ind = items[i]\&.parts\&.index(\*(Aqsbin\*(Aq)
172 bin = pathlib\&.Path(*items[i]\&.parts[:ind], \*(Aqbin\*(Aq, *items[i]\&.parts[ind + 1 :])
173 if bin in items[i + 1 :]:
174 j = i + 1 + items[i + 1 :]\&.index(bin)
175 items[i], items[j] = items[j], items[i]
176 return \*(Aq:\*(Aq\&.join(p\&.as_posix() for p in items)
177
178
179 if __name__ == \*(Aq__main__\*(Aq:
180 # This should be always set\&. If it is not, we will just crash, which is OK too\&.
181 path = os\&.environ[\*(AqPATH\*(Aq]
182 new = rearrange_bin_sbin(path)
183 if new != path:
184 print(f\*(AqPATH={new}\*(Aq)
185 .fi
186 .if n \{\
187 .RE
188 .\}
189 .PP
190 \fBExample\ \&3.\ \&Debugging a generator\fR
191 .sp
192 .if n \{\
193 .RS 4
194 .\}
195 .nf
196 SYSTEMD_LOG_LEVEL=debug VAR_A=something VAR_B="something else" \e
197 /usr/lib/systemd/system\-environment\-generators/path\-to\-generator
198 .fi
199 .if n \{\
200 .RE
201 .\}
202 .SH "SEE ALSO"
203 .PP
204 \fBsystemd-environment-d-generator\fR(8), \fBsystemd.generator\fR(7), \fBsystemd\fR(1), \fBsystemctl\fR(1)
205 .SH "NOTES"
206 .IP " 1." 4
207 💣💥🧨💥💥💣 Please note that those configuration files must be available at all times. If
208 /usr/local/
209 is a separate partition, it may not be available during early boot, and must not be used for configuration.
210