public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-portage-dev] [PATCH] env-update: create systemd env configuration if required
@ 2020-09-02 16:43 Florian Schmaus
  2020-09-03 11:11 ` [gentoo-portage-dev] [PATCH v2] " Florian Schmaus
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Florian Schmaus @ 2020-09-02 16:43 UTC (permalink / raw
  To: gentoo-portage-dev; +Cc: Florian Schmaus

Portage's env-update currently transforms the environment information
from /etc/env.d into /etc/profile.env, which is typically sourced by
every user session, setting up its environment.

However, /etc/profile.env is not sourced by systemd user
services. Instead, for the definition of a systemd user session
environment, the 'environment.d' machinery exists. Unfortunately, up
to now, env-update does not produce a profile.env equivalent for this
machinery, causing issues for systemd user services. For example,
an emacs daemon run a user systemd service does not have a complete
PATH (bug #704412 [1]), because some PATH components are injected by
packages via /etc/env.d. For example, an LLVM ebuild may set
PATH="/usr/lib/llvm/9/bin".

This commit changes env-update so that, after profile.env has was
generated, a systemd user session environment configuration file named

/usr/lib/environment.d/gentoo-profile-env.conf

is created, if the directory /usr/lib/environment.d exists.

1: https://bugs.gentoo.org/704412

Closes: https://bugs.gentoo.org/704416
Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
 lib/portage/util/env_update.py | 40 +++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
index f130b6f6b..9bddf281b 100644
--- a/lib/portage/util/env_update.py
+++ b/lib/portage/util/env_update.py
@@ -6,6 +6,7 @@ __all__ = ['env_update']
 import errno
 import glob
 import io
+import re
 import stat
 import time
 
@@ -340,7 +341,8 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
 	cenvnotice += "# GO INTO /etc/csh.cshrc NOT /etc/csh.env\n\n"
 
 	#create /etc/profile.env for bash support
-	outfile = atomic_ofstream(os.path.join(eroot, "etc", "profile.env"))
+	profile_env_path = os.path.join(eroot, "etc", "profile.env")
+	outfile = atomic_ofstream(profile_env_path)
 	outfile.write(penvnotice)
 
 	env_keys = [x for x in env if x != "LDPATH"]
@@ -353,6 +355,42 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
 			outfile.write("export %s='%s'\n" % (k, v))
 	outfile.close()
 
+	# Create the systemd user environment configuration file
+	# /usr/lib/environment.d/gentoo-profile-env.conf with the
+	# environment variables of /etc/profile.enf if
+	# /usr/lib/environment.d exists.
+	systemd_environment_dir = os.path.join(eroot, "usr", "lib", "environment.d")
+	if os.path.isdir(systemd_environment_dir):
+		with open(profile_env_path, "r") as profile_env:
+			lines = profile_env.readlines()
+
+		systemd_profile_env_path = os.path.join(systemd_environment_dir,
+						     "gentoo-profile-env.conf")
+
+		with open(systemd_profile_env_path, "w") as systemd_profile_env:
+			for line in lines:
+				# Skip comments.
+				if re.match(r"^[ \t]*#", line):
+					continue
+
+				# Skip empty lines.
+				if re.match(r"^[ \t]*$", line):
+					continue
+
+				# Skip variables with the empty string
+				# as value. Those sometimes appear in
+				# profile.env (e.g. "export GCC_SPECS=''"),
+				# but are invalid in systemd's syntax.
+				if line.endswith("=''\n"):
+					continue
+
+				# Transform into systemd environment.d
+				# conf syntax, basically shell variable
+				# assignment (without "export ").
+				line = re.sub(r"^export ", "", line)
+
+				systemd_profile_env.write(line)
+
 	#create /etc/csh.env for (t)csh support
 	outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
 	outfile.write(cenvnotice)
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-09-08  0:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-02 16:43 [gentoo-portage-dev] [PATCH] env-update: create systemd env configuration if required Florian Schmaus
2020-09-03 11:11 ` [gentoo-portage-dev] [PATCH v2] " Florian Schmaus
2020-09-03 11:30   ` Ulrich Mueller
2020-09-03 12:07     ` Florian Schmaus
2020-09-03 12:43       ` Ulrich Mueller
2020-09-03 12:58         ` Florian Schmaus
2020-09-03 13:06 ` [gentoo-portage-dev] [PATCH v3] " Florian Schmaus
2020-09-03 15:21   ` Joakim Tjernlund
2020-09-03 17:57 ` [gentoo-portage-dev] [PATCH v4] " Florian Schmaus
2020-09-04 15:39   ` [gentoo-portage-dev] " Florian Schmaus
2020-09-05  1:12     ` Zac Medico
2020-09-05  7:18 ` [gentoo-portage-dev] [PATCH v5] env-update: create systemd user-session environment definition Florian Schmaus
2020-09-08  0:06   ` Zac Medico

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox