Subject: [bug#40601] [PATCH 12/28] guix-install.sh: Rework user & group handling, adding busybox support.

From: Vincent Legoll <[email-masked]>
Date: Sun, 17 May 2020 19:17:09 +0200

* etc/guix-install.sh (REQUIRE): Remove groupadd, add comment.
(sys_create_build_user): Add adduser handling.
(sys_create_build_group): New function, add addgroup handling...
(main): ...call it here.
---
 etc/guix-install.sh | 57 +++++++++++++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 15 deletions(-)

diff --git a/etc/guix-install.sh b/etc/guix-install.sh
index 043357d9c4..d6966f851a 100755
--- a/etc/guix-install.sh
+++ b/etc/guix-install.sh
@@ -32,6 +32,8 @@ set -e
 
 [ "$UID" -eq 0 ] || { echo "This script must be run as root."; exit 1; }
 
+# groupadd, useradd, usermod, adduser, addgroup are handled in:
+# sys_create_build_user & sys_create_build_group functions.
 REQUIRE=(
     "wget"
     "gpg"
@@ -45,7 +47,6 @@ REQUIRE=(
     "rm"
     "chmod"
     "uname"
-    "groupadd"
     "tail"
     "realpath"
     "tar"
@@ -298,31 +299,56 @@ sys_create_store()
     _msg "${PAS}activated root profile at ${ROOT_HOME}/.config/guix/current"
 }
 
-sys_create_build_user()
-{ # Create the group and user accounts for build users.
+sys_create_build_group()
+{ # Create the group for build users.
 
     _debug "--- [ $FUNCNAME ] ---"
 
     if getent group guixbuild >/dev/null 2>&1; then
-        _msg "${INF}group guixbuild exists"
-    else
+        _msg "${INF}group guixbuild already exists"
+    elif command -v groupadd &>/dev/null; then
         groupadd --system guixbuild
         _msg "${PAS}group <guixbuild> created"
+    elif command -v addgroup &>/dev/null; then
+        addgroup -S guixbuild
+        _msg "${PAS}group <guixbuild> created"
+    else
+        _err "${ERR}cannot add group for guix build users"
+        exit 1
     fi
+}
+
+sys_create_build_user()
+{ # Create the user accounts for build users.
+
+    _debug "--- [ $FUNCNAME ] ---"
 
     for i in $(seq -w 1 10); do
         if getent passwd "guixbuilder${i}" >/dev/null 2>&1; then
-            _msg "${INF}user is already in the system, reset"
-            usermod -g guixbuild -G guixbuild           \
-                    -d /var/empty -s "$(which nologin)" \
-                    -c "Guix build user $i"             \
-                    "guixbuilder${i}";
+            if command -v usermod &>/dev/null; then
+                _msg "${INF}user is already in the system, resetting"
+                usermod -g guixbuild -G guixbuild           \
+                        -d /var/empty -s "$(which nologin)" \
+                        -c "Guix build user $i"             \
+                        "guixbuilder${i}"
+            else
+                _msg "${ERR}cannot reset user environment, doing nothing"
+            fi
         else
-            useradd -g guixbuild -G guixbuild           \
-                    -d /var/empty -s "$(which nologin)" \
-                    -c "Guix build user $i" --system    \
-                    "guixbuilder${i}";
-            _msg "${PAS}user added <guixbuilder${i}>"
+            if command -v useradd &>/dev/null; then
+                useradd -g guixbuild -G guixbuild           \
+                        -d /var/empty -s "$(which nologin)" \
+                        -c "Guix build user $i" --system    \
+                        "guixbuilder${i}"
+                _msg "${PAS}user added <guixbuilder${i}>"
+            elif command -v adduser &>/dev/null; then
+                adduser -G guixbuild -h /var/empty -s "$(which nologin)" \
+                        -H -S "guixbuilder${i}"
+                _msg "${PAS}user added <guixbuilder${i}>"
+            else
+                _msg "${ERR}cannot add user: <guixbuilder${i}>"
+                exit 1
+            fi
         fi
     done
 }
@@ -509,6 +535,7 @@ main()
         TARBALL="$(realpath $1)"
     fi
     sys_create_store "${TARBALL}" "${tmp_path}"
+    sys_create_build_group
     sys_create_build_user
     sys_enable_guix_daemon
     sys_make_guix_available
-- 
2.26.2