[gentoo-portage-dev] [PATCH] make.conf: Treat __* variables as local and do not propagate them.

2020-09-05 Thread Zac Medico
From: Arfrever Frehtes Taifersar Arahesis 

Bug: https://bugs.gentoo.org/740588
Signed-off-by: Arfrever Frehtes Taifersar Arahesis 
Signed-off-by: Zac Medico 
---
 lib/portage/package/ebuild/config.py | 6 ++
 man/make.conf.5  | 4 +++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/portage/package/ebuild/config.py 
b/lib/portage/package/ebuild/config.py
index b62ad3069..a09fdbced 100644
--- a/lib/portage/package/ebuild/config.py
+++ b/lib/portage/package/ebuild/config.py
@@ -370,6 +370,9 @@ class config:
_("Found 2 make.conf files, using both 
'%s' and '%s'") %
tuple(make_conf_paths), noiselevel=-1)
 
+   # __* variables set in make.conf are local and are not 
be propagated.
+   make_conf = {k: v for k, v in make_conf.items() if not 
k.startswith("__")}
+
# Allow ROOT setting to come from make.conf if it's not 
overridden
# by the constructor argument (from the calling 
environment).

locations_manager.set_root_override(make_conf.get("ROOT"))
@@ -621,6 +624,9 @@ class config:
tolerant=tolerant, allow_sourcing=True,
expand=expand_map, recursive=True) or 
{})
 
+   # __* variables set in make.conf are local and are not 
be propagated.
+   mygcfg = {k: v for k, v in mygcfg.items() if not 
k.startswith("__")}
+
# Don't allow the user to override certain variables in 
make.conf
profile_only_variables = 
self.configdict["defaults"].get(
"PROFILE_ONLY_VARIABLES", "").split()
diff --git a/man/make.conf.5 b/man/make.conf.5
index eb812150f..914121b16 100644
--- a/man/make.conf.5
+++ b/man/make.conf.5
@@ -1,4 +1,4 @@
-.TH "MAKE.CONF" "5" "Jun 2020" "Portage VERSION" "Portage"
+.TH "MAKE.CONF" "5" "Sep 2020" "Portage VERSION" "Portage"
 .SH "NAME"
 make.conf \- custom settings for Portage
 .SH "SYNOPSIS"
@@ -36,6 +36,8 @@ make.defaults to make.globals to make.conf to the environment
 settings.  Clearing these variables requires a clear\-all as in:
 export USE="\-*"
 .br
+__* variables set in make.conf are local and are not be propagated.
+.br
 In order to create per\-package environment settings, refer to
 \fBpackage.env\fR in \fBportage\fR(5).
 .SH "VARIABLES"
-- 
2.25.3




[gentoo-portage-dev] [PATCH v5] env-update: create systemd user-session environment definition

2020-09-05 Thread 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 as 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 a systemd user session
environment configuration file named

/etc/environment.d/10-gentoo-env.conf

is created.

Thanks to Michael 'veremitz' Everitt, Arfrever Frehtes Taifersar
Arahesis, Ulrich Müller, Joakim Tjernlund, and Zac Medico for the
useful feedback.

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

Closes: https://bugs.gentoo.org/704416
Signed-off-by: Florian Schmaus 
---

Notes:
- Shorten created filename to 10-gentoo-env.conf
- Minor fixes in the commit message
- Use atomic_ofstream()
- Use os.makedirs() (Thanks Zac)

 lib/portage/util/env_update.py | 42 +++---
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
index f130b6f6b..ab3caee47 100644
--- a/lib/portage/util/env_update.py
+++ b/lib/portage/util/env_update.py
@@ -333,14 +333,16 @@ def _env_update(makelinks, target_root, prev_mtimes, 
contents, env,
 
del specials["LDPATH"]
 
-   penvnotice  = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
-   penvnotice += "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n"
+   notice  = "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n"
+   notice += "# DO NOT EDIT THIS FILE."
+   penvnotice  = notice + " CHANGES TO STARTUP PROFILES\n"
cenvnotice  = penvnotice[:]
penvnotice += "# GO INTO /etc/profile NOT /etc/profile.env\n\n"
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,40 @@ 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
+   # /etc/environment.d/10-gentoo-env.conf with the
+   # environment configuration from /etc/env.d.
+   systemd_environment_dir = os.path.join(eroot, "etc", "environment.d")
+   os.makedirs(systemd_environment_dir, exist_ok=True)
+
+   systemd_gentoo_env_path = os.path.join(systemd_environment_dir,
+"10-gentoo-env.conf")
+   systemd_gentoo_env = atomic_ofstream(systemd_gentoo_env_path)
+   try:
+   senvnotice = notice + "\n\n"
+   systemd_gentoo_env.write(senvnotice)
+
+   for env_key in env_keys:
+   env_key_value = env[env_key]
+
+   # 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 not env_key_value:
+   continue
+
+   # Transform into systemd environment.d
+   # conf syntax, basically shell variable
+   # assignment (without "export ").
+   line = f"{env_key}={env_key_value}\n"
+
+   systemd_gentoo_env.write(line)
+   except:
+   systemd_gentoo_env.abort()
+   raise
+   systemd_gentoo_env.close()
+
#create /etc/csh.env for (t)csh support
outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
outfile.write(cenvnotice)
-- 
2.26.2