89luca89 / distrobox

Use any linux distribution inside your terminal. Enable both backward and forward compatibility with software and freedom to use whatever distribution you’re more comfortable with. Mirror available at: https://gitlab.com/89luca89/distrobox
https://distrobox.it/
GNU General Public License v3.0
10.19k stars 419 forks source link

[Error] Environment Variables Not Set Correctly When Using distrobox-enter -- command #1488

Open ZefQ opened 4 months ago

ZefQ commented 4 months ago

Describe the bug When exporting applications installed in a distrobox container to the host, the environment variables within the container are not correctly set up when executing commands via distrobox-enter -- command. This results in the PATH environment variable from the host being used instead of the one from the container, causing the application to fail to find its executable.

To Reproduce

  1. Export an application from a distrobox container to the host, that is not installed in a path that is also a part of the host $PATH creating a .desktop file.
  2. Attempt to run the exported application from the host.
  3. Check the PATH environment variable by running echo $PATH through distrobox-enter.

Expected behavior The PATH environment variable should reflect the container's environment, ensuring that the application can locate its executable.

Actual behavior The PATH environment variable reflects the host's environment, causing the application to fail due to missing paths.

Example On the host:

❯ echo $PATH
/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/username/.local/bin:/home/username/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

Using distrobox-enter -- command:

❯ /usr/bin/distrobox-enter -n withnix -- echo $PATH
/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/username/.local/bin:/home/username/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

Entering the container interactively:

❯ /usr/bin/distrobox-enter -n withnix
username@fedora:/home/username/Dev/distrobox/with-nix> echo $PATH
/home/username/.distrobox/withnix/.nix-profile/bin:/usr/local/bin:/usr/bin:/bin
username@fedora:/home/username/Dev/distrobox/with-nix> logout

Desktop (please complete the following information):

If you would like to reproduce it with a nix package there is also a bug with the distrobox-export command. because the path .nix-profile/share/applications is a symlinc to /nix/store/... I solved that problem by adding a trailing / to line 435 so the find command only checks for files and links inside applications

[ -d "${xdg_data_dir}/applications" ] && canon_dirs="${canon_dirs} ${xdg_data_dir}/applications/"

trianglecirclexyz commented 3 months ago

I am having the same issue. I have installed and exported Android Studio in an Ubuntu container as well as the Flutter/Dart SDKs which are all in the container path. Exporting Android Studio works but the path is still the hosts' which breaks everything.

SadSock commented 3 months ago

I am having the same issue.

89luca89 commented 1 month ago

The problem relies in the command you're using to test this, just doing enter -- echo $PATH will just print the host PATH because $PATH is evaluated by the shell BEFORE entering into distrobox

try

luca-linux@framework13:~$ distrobox enter default_distrobox -- sh -c "echo \$PATH"
/var/lib/flatpak/exports/bin:/home/luca-linux/bin:/home/luca-linux/.local/share/flatpak/exports/bin:/home/luca-linux/.local/bin:/home/luca-linux/.config/emacs/bin/:/var/lib/flatpak/exports/bin:/home/luca-linux/bin:/home/luca-linux/.local/share/flatpak/exports/bin:/home/luca-linux/.local/bin:/home/luca-linux/.config/emacs/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/luca-linux/.local/flutter/flutter/bin:/home/luca-linux/.local/flutter/flutter/bin/cache/dart-sdk/bin/snapshots:/usr/sbin:/usr/local/sbin:/home/luca-linux/.local/flutter/flutter/bin:/home/luca-linux/.local/flutter/flutter/bin/cache/dart-sdk/bin/snapshots:/usr/sbin:/usr/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
luca-linux@framework13:~$ echo $PATH
/var/lib/flatpak/exports/bin:/home/luca-linux/bin:/home/luca-linux/.local/share/flatpak/exports/bin:/home/luca-linux/.local/bin:/home/luca-linux/.config/emacs/bin/:/var/lib/flatpak/exports/bin:/home/luca-linux/bin:/home/luca-linux/.local/share/flatpak/exports/bin:/home/luca-linux/.local/bin:/home/luca-linux/.config/emacs/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/luca-linux/.local/flutter/flutter/bin:/home/luca-linux/.local/flutter/flutter/bin/cache/dart-sdk/bin/snapshots:/usr/sbin:/usr/local/sbin:/home/luca-linux/.local/flutter/flutter/bin:/home/luca-linux/.local/flutter/flutter/bin/cache/dart-sdk/bin/snapshots:/usr/sbin:/usr/local/sbin
luca-linux@framework13:~$ 

This indeed shows a different path