Skip to content

enter: better control over copied host envvars#1695

Open
45mg wants to merge 7 commits into
89luca89:mainfrom
45mg:enter-envvar-control
Open

enter: better control over copied host envvars#1695
45mg wants to merge 7 commits into
89luca89:mainfrom
45mg:enter-envvar-control

Conversation

@45mg
Copy link
Copy Markdown

@45mg 45mg commented Feb 21, 2025

Currently, all host environment variables are copied into the container, except for a hardcoded few that are ignored:

grep -Ev '^(CONTAINER_ID|FPATH|HOST|HOSTNAME|HOME|PATH|PROFILEREAD|SHELL|XDG_SEAT|XDG_VTNR|XDG_.*_DIRS|^_)'); do

This is not always the desired behavior, particularly on host distros like NixOS or Guix that break from the FHS and need to set a lot of environment variables to work around the resulting issues:
#1516

Therefore, provide a --no-envvars option to disable the default copying of environment variables, and an --additional-envvars option to copy specific ones.

It was suggested to use env -u distrobox in order to unset specific environment variables:
#656
However, there are usually too many copied environment variables to unset them all individually.
env -i distrobox will unset all environment variables, but that includes PATH and other things needed for distrobox to execute correctly on the host.

Other instances of this feature being requested:
#508
#743
#1173

Currently, all host environment variables are copied into the container,
except for a hardcoded few that are ignored:
https://github.com/89luca89/distrobox/blob/3b9f0e8d3d8bd102e1636a22afffafe00777d30b/distrobox-enter#L434

This is not always the desired behavior, particularly on host distros
like NixOS or Guix that break from the FHS and need to set a lot of
environment variables to work around the resulting issues:
89luca89#1516

Therefore, provide a --no-envvars option to disable the default copying
of environment variables, and an --additional-envvars option to copy
specific ones.

It was suggested to use `env -u distrobox` in order to unset specific
environment variables:
89luca89#656
However, there are usually too many copied environment variables to
unset them all individually.
`env -i distrobox` will unset environment variables, but that includes
PATH and other things needed for distrobox to execute correctly on the
host.

Other instances of this feature being requested:
89luca89#508
89luca89#743
89luca89#1173
@45mg
Copy link
Copy Markdown
Author

45mg commented Feb 21, 2025

I thought I should give a specific example of how this is useful, so here goes:

I installed Distrobox for the first time, on Guix. When I tried to run Emacs from a Fedora container, it segfaulted. I spent hours trawling through bug trackers and gdbing the coredump, to no avail.

Then I noticed that git was also broken in the container. This time the error message was easier to search for, and I realised that GIT_EXEC_PATH=/run/current-system/profile/libexec/git-core set by Guix was being copied into the container by distrobox-enter, which was preventing Git from looking under /usr/libexec/git-core. (There were several other such environment variables, too.)

After fixing this, it dawned on me that I was probably seeing the same issue with Emacs. Eventually, after painstakingly unsetting dozens of environment variables, I managed to get it to launch fine.

With this PR, I just have to pass --clean-path --no-envvars --additional-envvars WAYLAND_DISPLAY,XDG_SESSION_TYPE.

Hopefully that gives an idea of why this might be a desirable change.

Copy link
Copy Markdown
Collaborator

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I took the liberty to give you some suggestions about the naming. We would like to merge this but we also would like the naming of the options (and of course the naming of the associated variables) to be --empty-env and --add-env.

Can I also ask you to rebase this PR against the latest main?

Thank you so much!

Comment thread docs/usage/distrobox-enter.md Outdated
--no-tty/-T: do not instantiate a tty
--no-workdir/-nw: always start the container from container's home directory
--no-envvars: don't copy host environment variables (default is to copy whenever reasonable)
--additional-envvars: comma-separated list of additional host environment variables to copy
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
--additional-envvars: comma-separated list of additional host environment variables to copy
--add-env: comma-separated list of additional host environment variables to copy

Comment thread distrobox-enter Outdated
Comment thread docs/usage/distrobox-enter.md Outdated
Comment thread distrobox-enter Outdated
Comment thread distrobox-enter Outdated
Comment thread distrobox-enter Outdated
89luca89 and others added 6 commits August 24, 2025 12:31
Co-authored-by: Alessio Biancalana <alessio@dottorblaster.it>
Co-authored-by: Alessio Biancalana <alessio@dottorblaster.it>
Co-authored-by: Alessio Biancalana <alessio@dottorblaster.it>
Co-authored-by: Alessio Biancalana <alessio@dottorblaster.it>
Co-authored-by: Alessio Biancalana <alessio@dottorblaster.it>
Co-authored-by: Alessio Biancalana <alessio@dottorblaster.it>
Comment thread distrobox-enter
if [ "${empty_env}" -eq 0 ]; then
set +o xtrace
# disable logging for this snippet, or it will be too talkative.
for i in $(printenv | grep '=' | grep -Ev ' |"|`|\$' |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #1811 this needs to be reworked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants