summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorzimbatm <zimbatm@zimbatm.com>2019-11-13 15:56:49 +0000
committerzimbatm <zimbatm@zimbatm.com>2020-01-21 13:17:38 +0100
commitab10bac1b177832b5e6b883ba95bf35f87229267 (patch)
tree44ecb56ae042d9a8b8f9b0cf2ccf1bd6b1806c94 /nixos
parent93204f1d8a95ed7b340ae9ad33fec86367100a79 (diff)
downloadnixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.tar
nixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.tar.gz
nixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.tar.bz2
nixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.tar.lz
nixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.tar.xz
nixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.tar.zst
nixpkgs-ab10bac1b177832b5e6b883ba95bf35f87229267.zip
nixos-rebuild: fix the maybeSudo usage
* properly expand the command using arrays instead of strings
* also handle sudo on the localhost
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/installer/tools/nixos-rebuild.sh16
1 files changed, 10 insertions, 6 deletions
diff --git a/nixos/modules/installer/tools/nixos-rebuild.sh b/nixos/modules/installer/tools/nixos-rebuild.sh
index c53dc1000c4..61b4af11027 100644
--- a/nixos/modules/installer/tools/nixos-rebuild.sh
+++ b/nixos/modules/installer/tools/nixos-rebuild.sh
@@ -22,7 +22,7 @@ repair=
 profile=/nix/var/nix/profiles/system
 buildHost=
 targetHost=
-maybeSudo=
+maybeSudo=()
 
 while [ "$#" -gt 0 ]; do
     i="$1"; shift 1
@@ -92,7 +92,7 @@ while [ "$#" -gt 0 ]; do
         ;;
       --use-remote-sudo)
         # note the trailing space
-        maybeSudo="sudo "
+        maybeSudo=(sudo --)
         shift 1
         ;;
       *)
@@ -102,6 +102,10 @@ while [ "$#" -gt 0 ]; do
     esac
 done
 
+if [ -n "$SUDO_USER" ]; then
+    maybeSudo=(sudo --)
+fi
+
 if [ -z "$buildHost" -a -n "$targetHost" ]; then
     buildHost="$targetHost"
 fi
@@ -116,17 +120,17 @@ buildHostCmd() {
     if [ -z "$buildHost" ]; then
         "$@"
     elif [ -n "$remoteNix" ]; then
-        ssh $SSHOPTS "$buildHost" env PATH="$remoteNix:$PATH" "$maybeSudo$@"
+        ssh $SSHOPTS "$buildHost" env PATH="$remoteNix:$PATH" "${maybeSudo[@]}" "$@"
     else
-        ssh $SSHOPTS "$buildHost" "$maybeSudo$@"
+        ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" "$@"
     fi
 }
 
 targetHostCmd() {
     if [ -z "$targetHost" ]; then
-        "$@"
+        "${maybeSudo[@]}" "$@"
     else
-        ssh $SSHOPTS "$targetHost" "$maybeSudo$@"
+        ssh $SSHOPTS "$targetHost" "${maybeSudo[@]}" "$@"
     fi
 }