Subject: [PATCH v2 2/3] home: setup-environment: Skip if already initialized.

From: Hilton Chain <[email-masked]>
Date: Tue, 1 Apr 2025 00:25:57 +0800

* gnu/home/services.scm (environment-variables->setup-environment-script):
Skip initializing home profile if already finished.
Address some issues reported by ‘shellcheck’.
* gnu/home/services/shells.scm (add-shell-profile-file): Skip
setup-environment if already finished.
Address some issues reported by ‘shellcheck’.

Change-Id: Ife4100c6b19f61272525eebc82931c81784fe9e0
---
 gnu/home/services.scm        | 18 +++++++++++-------
 gnu/home/services/shells.scm | 10 +++++++---
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/gnu/home/services.scm b/gnu/home/services.scm
index 85a43f80ca..39ad31515c 100644
--- a/gnu/home/services.scm
+++ b/gnu/home/services.scm
@@ -279,31 +279,35 @@ (define (environment-variables->setup-environment-script vars)
 # ~/.profile does)
 GUIX_PROFILE=\"$HOME_ENVIRONMENT/profile\"
 PROFILE_FILE=\"$GUIX_PROFILE/etc/profile\"
-[ -f $PROFILE_FILE ] && . $PROFILE_FILE
+# Skip if already initialized.
+case $INFOPATH in
+  *$GUIX_PROFILE/share/info*) ;;
+  *) [ -f \"$PROFILE_FILE\" ] && . \"$PROFILE_FILE\" ;;
+esac
 
 case $GUIX_LOCPATH in
   *$GUIX_PROFILE/lib/locale*) ;;
-  *) export GUIX_LOCPATH=$GUIX_PROFILE/lib/locale:$GUIX_LOCPATH ;;
+  *) export GUIX_LOCPATH=\"$GUIX_PROFILE/lib/locale:$GUIX_LOCPATH\" ;;
 esac
 case $XDG_DATA_DIRS in
   *$GUIX_PROFILE/share*) ;;
-  *) export XDG_DATA_DIRS=$GUIX_PROFILE/share:$XDG_DATA_DIRS ;;
+  *) export XDG_DATA_DIRS=\"$GUIX_PROFILE/share:$XDG_DATA_DIRS\" ;;
 esac
 case $MANPATH in
   *$GUIX_PROFILE/share/man*) ;;
-  *) export MANPATH=$GUIX_PROFILE/share/man:$MANPATH
+  *) export MANPATH=\"$GUIX_PROFILE/share/man:$MANPATH\" ;;
 esac
 case $INFOPATH in
   *$GUIX_PROFILE/share/info*) ;;
-  *) export INFOPATH=$GUIX_PROFILE/share/info:$INFOPATH ;;
+  *) export INFOPATH=\"$GUIX_PROFILE/share/info:$INFOPATH\" ;;
 esac
 case $XDG_CONFIG_DIRS in
   *$GUIX_PROFILE/etc/xdg*) ;;
-  *) export XDG_CONFIG_DIRS=$GUIX_PROFILE/etc/xdg:$XDG_CONFIG_DIRS ;;
+  *) export XDG_CONFIG_DIRS=\"$GUIX_PROFILE/etc/xdg:$XDG_CONFIG_DIRS\" ;;
 esac
 case $XCURSOR_PATH in
   *$GUIX_PROFILE/share/icons*) ;;
-  *) export XCURSOR_PATH=$GUIX_PROFILE/share/icons:$XCURSOR_PATH ;;
+  *) export XCURSOR_PATH=\"$GUIX_PROFILE/share/icons:$XCURSOR_PATH\" ;;
 esac
 
 # Keep the shell environment clean.
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index bab5730c3d..ce61eaa4b0 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -87,9 +87,13 @@ (define (add-shell-profile-file config)
      ,(mixed-text-file
        "shell-profile"
        "\
-HOME_ENVIRONMENT=$HOME/.guix-home
-. $HOME_ENVIRONMENT/setup-environment
-$HOME_ENVIRONMENT/on-first-login
+HOME_ENVIRONMENT=\"$HOME/.guix-home\"
+# Skip if already sourced.
+case $GUIX_LOCPATH in
+  *$HOME_ENVIRONMENT/profile/lib/locale*) ;;
+  *) . \"$HOME_ENVIRONMENT/setup-environment\" ;;
+esac
+\"$HOME_ENVIRONMENT/on-first-login\"
 unset HOME_ENVIRONMENT\n"
        (serialize-configuration
         config
-- 
2.49.0