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 v4] env-update: create systemd env configuration if required
Date: Thu, 03 Sep 2020 17:57:23
Message-Id: 20200903175718.1270426-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 /etc/environment.d/10-gentoo-profile-env.conf
19
20 is created.
21
22 Thanks to Michael 'veremitz' Everitt, Arfrever Frehtes Taifersar
23 Arahesis, Ulrich Müller, and Joakim Tjernlund for the useful
24 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 - Prefix generated conf with "10-", for easier overwriting (thanks Joakim)
34
35 lib/portage/util/env_update.py | 38 +++++++++++++++++++++++++++++++---
36 1 file changed, 35 insertions(+), 3 deletions(-)
37
38 diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
39 index f130b6f6bacb..8ba86c0bfd47 100644
40 --- a/lib/portage/util/env_update.py
41 +++ b/lib/portage/util/env_update.py
42 @@ -333,14 +333,16 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
43
44 del specials["LDPATH"]
45
46 - penvnotice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
47 - penvnotice += "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n"
48 + notice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
49 + notice += "# DO NOT EDIT THIS FILE."
50 + penvnotice = notice + " CHANGES TO STARTUP PROFILES\n"
51 cenvnotice = penvnotice[:]
52 penvnotice += "# GO INTO /etc/profile NOT /etc/profile.env\n\n"
53 cenvnotice += "# GO INTO /etc/csh.cshrc NOT /etc/csh.env\n\n"
54
55 #create /etc/profile.env for bash support
56 - outfile = atomic_ofstream(os.path.join(eroot, "etc", "profile.env"))
57 + profile_env_path = os.path.join(eroot, "etc", "profile.env")
58 + outfile = atomic_ofstream(profile_env_path)
59 outfile.write(penvnotice)
60
61 env_keys = [x for x in env if x != "LDPATH"]
62 @@ -353,6 +355,36 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
63 outfile.write("export %s='%s'\n" % (k, v))
64 outfile.close()
65
66 + # Create the systemd user environment configuration file
67 + # /etc/environment.d/gentoo-profile-env.conf with the
68 + # environment variables of /etc/profile.env.
69 + systemd_environment_dir = os.path.join(eroot, "etc", "environment.d")
70 + if not os.path.isdir(systemd_environment_dir):
71 + os.mkdir(systemd_environment_dir)
72 +
73 + systemd_profile_env_path = os.path.join(systemd_environment_dir,
74 + "10-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