Skip to content

Commit

Permalink
fix: use container's $PATH when entering (#209)
Browse files Browse the repository at this point in the history
The PATH fixes in 6b60c73 use the
host's $PATH instead of the $PATH defined in the container.  This
change fixes that by inspecting the container and parsing $PATH
out of its environment.  The inspect was already being done so
this shouldn't be a signficiant performance impact.  Future
features may desire further information out of the inspect
invocation and the parsing method can be extended.

The container's $PATH is merged with the host $PATH and the
standard set of binary paths as the existing logic uses.

Co-authored-by: Gerald Britton <gbritton@sekrit.org>
  • Loading branch information
gbritton and gbritton authored Mar 20, 2022
1 parent 8d1aad6 commit 0bbfcf6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions distrobox-enter
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,25 @@ generate_command() {
result_command="${result_command} --env=\"${i}\""
done

# Start with the $PATH set in the container's config
container_paths="${container_PATH}"
# Ensure the standard FHS program paths are in PATH environment
standard_paths="/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin"
container_paths="${PATH}"
# add to the PATH only after the host's paths, and only if not already present.
# add to the PATH after the existing paths, and only if not already present
for standard_path in ${standard_paths}; do
if [ -n "${container_paths##*:"${standard_path}"*}" ]; then
container_paths="${container_paths}:${standard_path}"
fi
done
# Ensure the $PATH entries from the host are appended as well
for standard_path in $(
IFS=:
for p in ${PATH}; do echo "${p}"; done
); do
if [ -n "${container_paths##*:"${standard_path}"*}" ]; then
container_paths="${container_paths}:${standard_path}"
fi
done
result_command="${result_command} --env=\"PATH=${container_paths}\""

# Ensure the standard FHS program paths are in XDG_DATA_DIRS environment
Expand Down Expand Up @@ -300,8 +310,11 @@ if [ "${dryrun}" -ne 0 ]; then
fi

# Inspect the container we're working with.
container_status="$(${container_manager} inspect --type container \
"${container_name}" --format '{{.State.Status}}')"
container_status=unknown
container_PATH="${PATH}"
eval "$(${container_manager} inspect --type container "${container_name}" --format \
'container_status={{.State.Status}};
{{range .Config.Env}}{{if slice . 0 5 | eq "PATH="}}container_PATH={{slice . 5 | printf "%q"}}{{end}}{{end}}')"
container_exists="$?"
# Does the container exists? check if inspect reported errors
if [ "${container_exists}" -gt 0 ]; then
Expand Down

0 comments on commit 0bbfcf6

Please sign in to comment.