Skip to content

Commit

Permalink
nix-profile*.sh.in: append PATHs idempotently
Browse files Browse the repository at this point in the history
When people source the Nix profile scripts more than once,
they can cause appended PATH variables to grow longer each
time. This is somewhat benign, but it does spook people and
cause support load.
  • Loading branch information
abathur committed Oct 27, 2023
1 parent e21e289 commit cc5db08
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
11 changes: 9 additions & 2 deletions scripts/nix-profile-daemon.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Only execute this file once per shell.
if [ -n "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then return; fi
__ETC_PROFILE_NIX_SOURCED=1
export __ETC_PROFILE_NIX_SOURCED=1

NIX_LINK=$HOME/.nix-profile
if [ -n "${XDG_STATE_HOME-}" ]; then
Expand Down Expand Up @@ -60,5 +60,12 @@ else
unset -f check_nix_profiles
fi

export PATH="$NIX_LINK/bin:@localstatedir@/nix/profiles/default/bin:$PATH"
# only append once
case ":$PATH:" in
*:"$NIX_LINK/bin:@localstatedir@/nix/profiles/default/bin":*)
;;
*)
export PATH="$NIX_LINK/bin:@localstatedir@/nix/profiles/default/bin:$PATH"
;;
esac
unset NIX_LINK
23 changes: 21 additions & 2 deletions scripts/nix-profile.sh.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Only execute this file once per shell.
if [ -n "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then return; fi
export __ETC_PROFILE_NIX_SOURCED=1

if [ -n "$HOME" ] && [ -n "$USER" ]; then

# Set up the per-user profile.
Expand Down Expand Up @@ -51,9 +55,24 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
# pick up `.nix-profile/share/man` because is it close to `.nix-profile/bin`
# which is in the $PATH. For more info, run `manpath -d`.
if [ -n "${MANPATH-}" ]; then
export MANPATH="$NIX_LINK/share/man:$MANPATH"
# only append once
case ":$MANPATH:" in
*:"$NIX_LINK/share/man":*)
;;
*)
export MANPATH="$NIX_LINK/share/man:$MANPATH"
;;
esac
fi

export PATH="$NIX_LINK/bin:$PATH"
# only append once
case ":$PATH:" in
*:"$NIX_LINK/bin":*)
;;
*)
export PATH="$NIX_LINK/bin:$PATH"
;;
esac

unset NIX_LINK NIX_LINK_NEW
fi

0 comments on commit cc5db08

Please sign in to comment.