From da701cafb86cf02099dca52880efeb1af9cea346 Mon Sep 17 00:00:00 2001 From: "Travis A. Everett" Date: Wed, 25 Oct 2023 21:07:10 -0500 Subject: [PATCH] nix-profile*.sh.in: append PATHs idempotently 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. (cherry picked from commit cc5db08ff610db6769c1a70b8fd61d816ff406be) --- scripts/nix-profile-daemon.sh.in | 11 +++++++++-- scripts/nix-profile.sh.in | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/scripts/nix-profile-daemon.sh.in b/scripts/nix-profile-daemon.sh.in index 8cfd3149ee8..98adf46f768 100644 --- a/scripts/nix-profile-daemon.sh.in +++ b/scripts/nix-profile-daemon.sh.in @@ -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 @@ -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 diff --git a/scripts/nix-profile.sh.in b/scripts/nix-profile.sh.in index c4d60cf376a..b41437e536d 100644 --- a/scripts/nix-profile.sh.in +++ b/scripts/nix-profile.sh.in @@ -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. @@ -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