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 v5] env-update: create systemd user-session environment definition
Date: Sat, 05 Sep 2020 07:18:22
Message-Id: 20200905071817.13236-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, an
10 emacs daemon run as 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 a systemd user session
16 environment configuration file named
17
18 /etc/environment.d/10-gentoo-env.conf
19
20 is created.
21
22 Thanks to Michael 'veremitz' Everitt, Arfrever Frehtes Taifersar
23 Arahesis, Ulrich Müller, Joakim Tjernlund, and Zac Medico for the
24 useful feedback.
25
26 1: https://bugs.gentoo.org/704412
27
28 Closes: https://bugs.gentoo.org/704416
29 Signed-off-by: Florian Schmaus <flo@×××××××××.eu>
30 ---
31
32 Notes:
33 - Shorten created filename to 10-gentoo-env.conf
34 - Minor fixes in the commit message
35 - Use atomic_ofstream()
36 - Use os.makedirs() (Thanks Zac)
37
38 lib/portage/util/env_update.py | 42 +++++++++++++++++++++++++++++++---
39 1 file changed, 39 insertions(+), 3 deletions(-)
40
41 diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
42 index f130b6f6b..ab3caee47 100644
43 --- a/lib/portage/util/env_update.py
44 +++ b/lib/portage/util/env_update.py
45 @@ -333,14 +333,16 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
46
47 del specials["LDPATH"]
48
49 - penvnotice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
50 - penvnotice += "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n"
51 + notice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
52 + notice += "# DO NOT EDIT THIS FILE."
53 + penvnotice = notice + " CHANGES TO STARTUP PROFILES\n"
54 cenvnotice = penvnotice[:]
55 penvnotice += "# GO INTO /etc/profile NOT /etc/profile.env\n\n"
56 cenvnotice += "# GO INTO /etc/csh.cshrc NOT /etc/csh.env\n\n"
57
58 #create /etc/profile.env for bash support
59 - outfile = atomic_ofstream(os.path.join(eroot, "etc", "profile.env"))
60 + profile_env_path = os.path.join(eroot, "etc", "profile.env")
61 + outfile = atomic_ofstream(profile_env_path)
62 outfile.write(penvnotice)
63
64 env_keys = [x for x in env if x != "LDPATH"]
65 @@ -353,6 +355,40 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
66 outfile.write("export %s='%s'\n" % (k, v))
67 outfile.close()
68
69 + # Create the systemd user environment configuration file
70 + # /etc/environment.d/10-gentoo-env.conf with the
71 + # environment configuration from /etc/env.d.
72 + systemd_environment_dir = os.path.join(eroot, "etc", "environment.d")
73 + os.makedirs(systemd_environment_dir, exist_ok=True)
74 +
75 + systemd_gentoo_env_path = os.path.join(systemd_environment_dir,
76 + "10-gentoo-env.conf")
77 + systemd_gentoo_env = atomic_ofstream(systemd_gentoo_env_path)
78 + try:
79 + senvnotice = notice + "\n\n"
80 + systemd_gentoo_env.write(senvnotice)
81 +
82 + for env_key in env_keys:
83 + env_key_value = env[env_key]
84 +
85 + # Skip variables with the empty string
86 + # as value. Those sometimes appear in
87 + # profile.env (e.g. "export GCC_SPECS=''"),
88 + # but are invalid in systemd's syntax.
89 + if not env_key_value:
90 + continue
91 +
92 + # Transform into systemd environment.d
93 + # conf syntax, basically shell variable
94 + # assignment (without "export ").
95 + line = f"{env_key}={env_key_value}\n"
96 +
97 + systemd_gentoo_env.write(line)
98 + except:
99 + systemd_gentoo_env.abort()
100 + raise
101 + systemd_gentoo_env.close()
102 +
103 #create /etc/csh.env for (t)csh support
104 outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
105 outfile.write(cenvnotice)
106 --
107 2.26.2

Replies