[bug#74962,v3,3/5] etc/guix-install.sh: Fix quoting and other issues.

Message ID 093604124092d3b024127068cbbeff3f9aa62d76.1734594333.git.maxim.cournoyer@gmail.com
State New
Headers
Series [bug#74962,v3,1/5] etc/teams.scm: Add etc/guix-install.sh to installer team scope. |

Commit Message

Maxim Cournoyer Dec. 19, 2024, 7:45 a.m. UTC
  This change fixes most issues reported by the 'shellcheck' command.

* etc/guix-install.sh (add_init_sys_require): Use -n instead of ! -z.
(sys_create_build_user): Quote variable expansion.
(sys_delete_build_user): Likewise.
(sys_create_shell_completion): Likewise.
(sys_delete_user_profiles): Likewise.
(sys_delete_guix_daemon): Explicitly access first array item.
(sys_create_store): Update shellcheck code to ignore.
(SYSV_INIT_REQUIRE): Ignore unused warning.
(sys_customize_bashrc): Ignore warnings due to using variables inside a
literal.
(main): Replace $@ with $* inside string.

Change-Id: Ia88509b461b3844f2dd5abf9fb21a5b2bbb8a1e1
---

Changes in v3:
 - Add comments to avoid remaining shellcheck warnings.

 etc/guix-install.sh | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
  

Comments

Ludovic Courtès Dec. 26, 2024, 10:56 a.m. UTC | #1
Maxim Cournoyer <maxim.cournoyer@gmail.com> skribis:

> This change fixes most issues reported by the 'shellcheck' command.
>
> * etc/guix-install.sh (add_init_sys_require): Use -n instead of ! -z.
> (sys_create_build_user): Quote variable expansion.
> (sys_delete_build_user): Likewise.
> (sys_create_shell_completion): Likewise.
> (sys_delete_user_profiles): Likewise.
> (sys_delete_guix_daemon): Explicitly access first array item.
> (sys_create_store): Update shellcheck code to ignore.
> (SYSV_INIT_REQUIRE): Ignore unused warning.
> (sys_customize_bashrc): Ignore warnings due to using variables inside a
> literal.
> (main): Replace $@ with $* inside string.
>
> Change-Id: Ia88509b461b3844f2dd5abf9fb21a5b2bbb8a1e1

LGTM.
  

Patch

diff --git a/etc/guix-install.sh b/etc/guix-install.sh
index 481eb6f12a..8d3d9d224b 100755
--- a/etc/guix-install.sh
+++ b/etc/guix-install.sh
@@ -77,6 +77,7 @@  REQUIRE=(
 
 # Add variables using form FOO_INIT_REQUIRE when init system FOO dependencies
 # should be checked.
+# shellcheck disable=SC2034  # the variable name is computed
 SYSV_INIT_REQUIRE=(
     "daemonize"
 )
@@ -151,7 +152,7 @@  chk_require()
 add_init_sys_require()
 { # Add the elements of FOO_INIT_SYS to REQUIRE
     local init_require="${INIT_SYS}_REQUIRE[@]"
-    if [[ ! -z "$init_require" ]]; then
+    if [[ -n "$init_require" ]]; then
         # Have to add piecemeal because ${!foo[@]} performs direct array key
         # expansion, not indirect plain array expansion.
         for r in "${!init_require}"; do
@@ -390,7 +391,7 @@  sys_create_store()
        ~root/.config/guix/current
 
     GUIX_PROFILE=~root/.config/guix/current
-    # shellcheck disable=SC1090
+    # shellcheck disable=SC1091
     source "${GUIX_PROFILE}/etc/profile"
     _msg "${PAS}activated root profile at ${GUIX_PROFILE}"
 }
@@ -427,12 +428,12 @@  sys_create_build_user()
     for i in $(seq -w 1 10); do
         if id "guixbuilder${i}" &>/dev/null; then
             _msg "${INF}user is already in the system, reset"
-            usermod -g guixbuild -G guixbuild${KVMGROUP}     \
+            usermod -g guixbuild -G "guixbuild${KVMGROUP}"     \
                     -d /var/empty -s "$(which nologin)" \
                     -c "Guix build user $i"             \
                     "guixbuilder${i}";
         else
-            useradd -g guixbuild -G guixbuild${KVMGROUP}     \
+            useradd -g guixbuild -G "guixbuild${KVMGROUP}"     \
                     -d /var/empty -s "$(which nologin)" \
                     -c "Guix build user $i" --system    \
                     "guixbuilder${i}";
@@ -445,7 +446,7 @@  sys_delete_build_user()
 {
     for i in $(seq -w 1 10); do
         if id -u "guixbuilder${i}" &>/dev/null; then
-            userdel -f guixbuilder${i}
+            userdel -f "guixbuilder${i}"
         fi
     done
 
@@ -551,7 +552,7 @@  sys_delete_guix_daemon()
     local local_bin
     local var_guix
 
-    _debug "--- [ $FUNCNAME ] ---"
+    _debug "--- [ ${FUNCNAME[0]} ] ---"
 
     info_path="/usr/local/share/info"
     local_bin="/usr/local/bin"
@@ -681,7 +682,7 @@  sys_create_shell_completion()
 
     { # Just in case
         for dir_shell in $bash_completion $zsh_completion $fish_completion; do
-            [ -d "$dir_shell" ] || mkdir -p $dir_shell
+            [ -d "$dir_shell" ] || mkdir -p "$dir_shell"
         done;
 
         ln -sf ${var_guix}/etc/bash_completion.d/* "$bash_completion";
@@ -696,8 +697,10 @@  sys_customize_bashrc()
 
     for bashrc in /home/*/.bashrc /root/.bashrc; do
         test -f "$bashrc" || continue
+        # shellcheck disable=SC2016  # checking for literal $GUIX_ENVIRONMENT
         grep -Fq '$GUIX_ENVIRONMENT' "$bashrc" && continue
         cp "${bashrc}" "${bashrc}.bak"
+        # shellcheck disable=SC2016,SC2028  # that's also a string literal
         echo '
 # Automatically added by the Guix install script.
 if [ -n "$GUIX_ENVIRONMENT" ]; then
@@ -747,10 +750,10 @@  sys_delete_user_profiles()
     rm -rf ~root/.cache/guix
 
     _msg "${INF}removing .guix-profile, .cache/guix and .config/guix of all /home users"
-    for user in `ls -1 /home`; do
-        rm -f /home/$user/.guix-profile
-        rm -rf /home/$user/.cache/guix
-        rm -rf /home/$user/.config/guix
+    for user in /home/*; do
+        rm -f "/home/$user/.guix-profile"
+        rm -rf "/home/$user/.cache/guix"
+        rm -rf "/home/$user/.config/guix"
     done
 }
 
@@ -898,7 +901,7 @@  main()
         if [ '--uninstall' = "${uninstall_flag}" ]; then
             main_uninstall
         else
-            echo "unsupported parameters: $@"
+            echo "unsupported parameters: $*"
             exit 1
         fi
     fi