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 v3] env-update: create systemd env configuration if required
Date: Thu, 03 Sep 2020 13:06:03
Message-Id: 20200903130600.3056-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/gentoo-profile-env.conf
19
20 is created.
21
22 Thanks to Michael 'veremitz' Everitt, Arfrever Frehtes Taifersar
23 Arahesis and Ulrich Müller 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 - Place generated file in /etc/environment.d (Thanks ulm)
33
34 lib/portage/util/env_update.py | 38 +++++++++++++++++++++++++++++++---
35 1 file changed, 35 insertions(+), 3 deletions(-)
36
37 diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
38 index f130b6f6bacb..0baca8a98676 100644
39 --- a/lib/portage/util/env_update.py
40 +++ b/lib/portage/util/env_update.py
41 @@ -333,14 +333,16 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
42
43 del specials["LDPATH"]
44
45 - penvnotice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
46 - penvnotice += "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n"
47 + notice = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
48 + notice += "# DO NOT EDIT THIS FILE."
49 + penvnotice = notice + " CHANGES TO STARTUP PROFILES\n"
50 cenvnotice = penvnotice[:]
51 penvnotice += "# GO INTO /etc/profile NOT /etc/profile.env\n\n"
52 cenvnotice += "# GO INTO /etc/csh.cshrc NOT /etc/csh.env\n\n"
53
54 #create /etc/profile.env for bash support
55 - outfile = atomic_ofstream(os.path.join(eroot, "etc", "profile.env"))
56 + profile_env_path = os.path.join(eroot, "etc", "profile.env")
57 + outfile = atomic_ofstream(profile_env_path)
58 outfile.write(penvnotice)
59
60 env_keys = [x for x in env if x != "LDPATH"]
61 @@ -353,6 +355,36 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
62 outfile.write("export %s='%s'\n" % (k, v))
63 outfile.close()
64
65 + # Create the systemd user environment configuration file
66 + # /etc/environment.d/gentoo-profile-env.conf with the
67 + # environment variables of /etc/profile.env.
68 + systemd_environment_dir = os.path.join(eroot, "etc", "environment.d")
69 + if not os.path.isdir(systemd_environment_dir):
70 + os.mkdir(systemd_environment_dir)
71 +
72 + systemd_profile_env_path = os.path.join(systemd_environment_dir,
73 + "gentoo-profile-env.conf")
74 + with open(systemd_profile_env_path, "w") as systemd_profile_env:
75 + senvnotice = notice + "\n\n"
76 + systemd_profile_env.write(senvnotice)
77 +
78 + for env_key in env_keys:
79 + env_key_value = env[env_key]
80 +
81 + # Skip variables with the empty string
82 + # as value. Those sometimes appear in
83 + # profile.env (e.g. "export GCC_SPECS=''"),
84 + # but are invalid in systemd's syntax.
85 + if not env_key_value:
86 + continue
87 +
88 + # Transform into systemd environment.d
89 + # conf syntax, basically shell variable
90 + # assignment (without "export ").
91 + line = f"{env_key}={env_key_value}\n"
92 +
93 + systemd_profile_env.write(line)
94 +
95 #create /etc/csh.env for (t)csh support
96 outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
97 outfile.write(cenvnotice)
98 --
99 2.26.2

Replies

Subject Author
Re: [gentoo-portage-dev] [PATCH v3] env-update: create systemd env configuration if required Joakim Tjernlund <Joakim.Tjernlund@××××××××.com>