Gentoo Archives: gentoo-portage-dev

From: Florian Schmaus <flo@×××××××××.eu>
To: gentoo-portage-dev@l.g.o
Cc: Florian Schmaus <flo@×××××××××.eu>
Subject: [gentoo-portage-dev] [PATCH v2] env-update: create systemd env configuration if required
Date: Thu, 03 Sep 2020 11:11:17
Message-Id: 20200903111113.12744-1-flo@geekplace.eu
In Reply to: [gentoo-portage-dev] [PATCH] env-update: create systemd env configuration if required by Florian Schmaus
1 Portage's env-update currently transforms the environment information
2 from /etc/env.d into /etc/profile.env, which is typically sourced by
3 every user session, setting up its environment.
4
5 However, /etc/profile.env is not sourced by systemd user
6 services. Instead, for the definition of a systemd user session
7 environment, the 'environment.d' machinery exists. Unfortunately, up
8 to now, env-update does not produce a profile.env equivalent for this
9 machinery, causing issues for systemd user services. For example,
10 an emacs daemon run a user systemd service does not have a complete
11 PATH (bug #704412 [1]), because some PATH components are injected by
12 packages via /etc/env.d. For example, an LLVM ebuild may set
13 PATH="/usr/lib/llvm/9/bin".
14
15 This commit changes env-update so that, after profile.env has was
16 generated, a systemd user session environment configuration file named
17
18 /usr/lib/environment.d/gentoo-profile-env.conf
19
20 is created, if the directory /usr/lib/environment.d exists.
21
22 Thanks to Michael 'veremitz' Everitt and Arfrever Frehtes Taifersar
23 Arahesis for the useful feedback.
24
25 1: https://bugs.gentoo.org/704412
26
27 Closes: https://bugs.gentoo.org/704416
28 Signed-off-by: Florian Schmaus <flo@×××××××××.eu>
29 ---
30
31 Notes:
32 - Fix typo s/profile.enf/profile.env/ (thanks Michael)
33 - Use env_keys to generate gentoo-profile-env.conf (thanks Arfrever)
34 - Add "file is auto generated" notice header to generated conf
35
36 lib/portage/util/env_update.py | 37 +++++++++++++++++++++++++++++++---
37 1 file changed, 34 insertions(+), 3 deletions(-)
38
39 diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
40 index f130b6f6bacb..0aace8f4c5f4 100644
41 --- a/lib/portage/util/env_update.py
42 +++ b/lib/portage/util/env_update.py
43 @@ -333,14 +333,16 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
44
45 del specials["LDPATH"]
46
47 - penvnotice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
48 - penvnotice += "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n"
49 + notice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
50 + notice += "# DO NOT EDIT THIS FILE."
51 + penvnotice = notice + " CHANGES TO STARTUP PROFILES\n"
52 cenvnotice = penvnotice[:]
53 penvnotice += "# GO INTO /etc/profile NOT /etc/profile.env\n\n"
54 cenvnotice += "# GO INTO /etc/csh.cshrc NOT /etc/csh.env\n\n"
55
56 #create /etc/profile.env for bash support
57 - outfile = atomic_ofstream(os.path.join(eroot, "etc", "profile.env"))
58 + profile_env_path = os.path.join(eroot, "etc", "profile.env")
59 + outfile = atomic_ofstream(profile_env_path)
60 outfile.write(penvnotice)
61
62 env_keys = [x for x in env if x != "LDPATH"]
63 @@ -353,6 +355,35 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
64 outfile.write("export %s='%s'\n" % (k, v))
65 outfile.close()
66
67 + # Create the systemd user environment configuration file
68 + # /usr/lib/environment.d/gentoo-profile-env.conf with the
69 + # environment variables of /etc/profile.env if
70 + # /usr/lib/environment.d exists.
71 + systemd_environment_dir = os.path.join(eroot, "usr", "lib", "environment.d")
72 + if os.path.isdir(systemd_environment_dir):
73 + systemd_profile_env_path = os.path.join(systemd_environment_dir,
74 + "gentoo-profile-env.conf")
75 + with open(systemd_profile_env_path, "w") as systemd_profile_env:
76 + senvnotice = notice + "\n\n"
77 + systemd_profile_env.write(senvnotice)
78 +
79 + for env_key in env_keys:
80 + env_key_value = env[env_key]
81 +
82 + # Skip variables with the empty string
83 + # as value. Those sometimes appear in
84 + # profile.env (e.g. "export GCC_SPECS=''"),
85 + # but are invalid in systemd's syntax.
86 + if not env_key_value:
87 + continue
88 +
89 + # Transform into systemd environment.d
90 + # conf syntax, basically shell variable
91 + # assignment (without "export ").
92 + line = f"{env_key}={env_key_value}\n"
93 +
94 + systemd_profile_env.write(line)
95 +
96 #create /etc/csh.env for (t)csh support
97 outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
98 outfile.write(cenvnotice)
99 --
100 2.26.2

Replies