Subject: [PATCH 2/2] home: setup-environment: Avoid prepending duplicated PATH.

From: Hilton Chain <[email-masked]>
Date: Sat, 15 Mar 2025 21:24:46 +0800

This makes best effort to order /run/privileged/bin first.

* gnu/home/services.scm (environment-variables->setup-environment-script):
Avoid prepending duplicated PATH.

Change-Id: I580fbbf4786b56b903c1a4d6eb5fa92a70e331b4
---
 gnu/home/services.scm | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/gnu/home/services.scm b/gnu/home/services.scm
index 85a43f80ca..9855e26d97 100644
--- a/gnu/home/services.scm
+++ b/gnu/home/services.scm
@@ -278,9 +278,21 @@ (define (environment-variables->setup-environment-script vars)
 # NOTE: Set HOME_ENVIRONMENT before sourcing (home-shell-profile-service-type ensures
 # ~/.profile does)
 GUIX_PROFILE=\"$HOME_ENVIRONMENT/profile\"
+
+# Check if PATH for home profile was already set by /etc/profile.
+case $PATH in
+  *$GUIX_PROFILE/bin*) OLD_PATH=$PATH ;;
+esac
+
 PROFILE_FILE=\"$GUIX_PROFILE/etc/profile\"
 [ -f $PROFILE_FILE ] && . $PROFILE_FILE
 
+# Don't modify PATH if it was already set for home profile.
+# This ensures /run/privileged/bin to be ordered first.
+if [ -n \"$OLD_PATH\" ]; then
+  export PATH=$OLD_PATH
+fi
+
 case $GUIX_LOCPATH in
   *$GUIX_PROFILE/lib/locale*) ;;
   *) export GUIX_LOCPATH=$GUIX_PROFILE/lib/locale:$GUIX_LOCPATH ;;
@@ -307,7 +319,7 @@ (define (environment-variables->setup-environment-script vars)
 esac
 
 # Keep the shell environment clean.
-unset GUIX_PROFILE PROFILE_FILE
+unset OLD_PATH GUIX_PROFILE PROFILE_FILE
 
 " port)
                              (display
-- 
2.48.1