@@ -430,28 +430,15 @@ runSsh() {
430430
431431# Helper function to authenticate sudo with password if needed
432432maybeSudo () {
433- # Early return if no command provided and no sudo password
434- if [[ $# -eq 0 && -z ${SUDO_PASSWORD} ]]; then
435- return
436- fi
437-
438- # Use 'true' as default command if none provided but we have sudo password
439- local cmd=(" ${@:- true} " )
440-
441433 if [[ -n ${SUDO_PASSWORD} ]] && [[ ${maybeSudoCommand} == " sudo" ]]; then
442434 # If debug is enabled and we have a sudo password, warn about potential issues
443-
444435 # Use sudo with password authentication - pipe password to all sudo commands
445436 printf " printf %%s %q | sudo -S " " $SUDO_PASSWORD "
446- printf ' %q ' " ${cmd[@]} "
437+ # Restore debug state if it was enabled
447438 elif [[ -n ${maybeSudoCommand} ]]; then
448439 printf ' %s ' " ${maybeSudoCommand} "
449- printf ' %q ' " ${cmd[@]} "
450- else
451- # No sudo command needed (e.g., already root after kexec)
452- printf ' %q ' " ${cmd[@]} "
453440 fi
454- echo
441+ # No output if no sudo needed (e.g., already root after kexec)
455442}
456443
457444# Test and cache sudo password if needed
@@ -547,7 +534,7 @@ buildStoreUrl() {
547534 # Use password authentication for nix-daemon
548535 remoteProgram=" sh -c $( urlEncode " $( printf %s " $( printf ' %q' " $SUDO_PASSWORD " ) " | sudo -S nix-daemon) " ) "
549536 else
550- remoteProgram=" ${maybeSudoCommand} , nix-daemon"
537+ remoteProgram=" ${maybeSudoCommand} nix-daemon"
551538 fi
552539
553540 if [[ $storeUrl == * " ?" * ]]; then
@@ -752,7 +739,7 @@ generateHardwareConfig() {
752739 fi
753740
754741 step " Generating hardware-configuration.nix using nixos-facter"
755- runSshNoTty -o ConnectTimeout=10 " $( maybeSudo nixos-facter) " > " $hardwareConfigPath "
742+ runSshNoTty -o ConnectTimeout=10 " $( maybeSudo) nixos-facter" > " $hardwareConfigPath "
756743 ;;
757744 nixos-generate-config)
758745 step " Generating hardware-configuration.nix using nixos-generate-config"
@@ -806,10 +793,10 @@ runKexec() {
806793 local remoteCommandTemplate
807794 remoteCommandTemplate="
808795set -eu ${enableDebug}
809- $( maybeSudo rm -rf /root/kexec)
810- $( maybeSudo mkdir -p /root/kexec)
796+ $( maybeSudo) rm -rf /root/kexec
797+ $( maybeSudo) mkdir -p /root/kexec
811798%TAR_COMMAND%
812- $( maybeSudo TMPDIR=/root/kexec setsid --wait /root/kexec/kexec/run --kexec-extra-flags " $kexecExtraFlags " )
799+ $( maybeSudo) TMPDIR=/root/kexec setsid --wait /root/kexec/kexec/run${kexecExtraFlags : + --kexec-extra-flags \ "$kexecExtraFlags \" }
813800"
814801
815802 # Define upload commands
@@ -870,7 +857,7 @@ runDisko() {
870857 local diskoScript=$1
871858 for path in " ${! diskEncryptionKeys[@]} " ; do
872859 step " Uploading ${diskEncryptionKeys[$path]} to $path "
873- runSsh " $( maybeSudo sh ) -c $( printf ' %q' " umask 077; mkdir -p $( dirname " $path " ) ; cat > $path " ) " < " ${diskEncryptionKeys[$path]} "
860+ runSsh " $( maybeSudo) sh -c $( printf ' %q' " umask 077; mkdir -p $( dirname " $path " ) ; cat > $path " ) " < " ${diskEncryptionKeys[$path]} "
874861 done
875862 if [[ -n ${diskoScript} ]]; then
876863 nixCopy --to " ssh-ng://$sshConnection " " $diskoScript "
@@ -887,7 +874,7 @@ runDisko() {
887874 fi
888875
889876 step Formatting hard drive with disko
890- runSsh " $( maybeSudo " $diskoScript " ) "
877+ runSsh " $( maybeSudo) $diskoScript "
891878}
892879
893880nixosInstall () {
@@ -912,12 +899,12 @@ nixosInstall() {
912899 step Copying extra files
913900 tar -C " $extraFiles " -cpf- . | runSsh " ${maybeSudoCommand} tar -C /mnt -xf- --no-same-owner"
914901
915- runSsh " $( maybeSudo chmod 755 /mnt) " # tar also changes permissions of /mnt
902+ runSsh " $( maybeSudo) chmod 755 /mnt" # tar also changes permissions of /mnt
916903 fi
917904
918905 if [[ ${# extraFilesOwnership[@]} -gt 0 ]]; then
919- # shellcheck disable=SC2016
920- printf " %s\n" " ${! extraFilesOwnership[@]} " " ${extraFilesOwnership[@]} " | pr -2t | runSsh ' while read file ownership; do ' " $( maybeSudo chown -R \$ ownership \" /mnt/\$ file\" ) " ' ; done'
906+ # shellcheck disable=SC2016,SC2086
907+ printf " %s\n" " ${! extraFilesOwnership[@]} " " ${extraFilesOwnership[@]} " | pr -2t | runSsh " while read file ownership; do $( maybeSudo) chown -R \$ ownership /mnt/\$ file; done"
921908 fi
922909
923910 step Installing NixOS
@@ -929,27 +916,27 @@ export PATH="\$PATH:/run/current-system/sw/bin"
929916
930917if [ ! -d "/mnt/tmp" ]; then
931918 # needed for installation if initrd-secrets are used
932- $( maybeSudo mkdir -p /mnt/tmp)
933- $( maybeSudo chmod 777 /mnt/tmp)
919+ $( maybeSudo) mkdir -p /mnt/tmp
920+ $( maybeSudo) chmod 777 /mnt/tmp
934921fi
935922
936923if [ ${copyHostKeys-n} = "y" ]; then
937924 # NB we copy host keys that are in turn copied by kexec installer.
938- $( maybeSudo mkdir -m 755 -p /mnt/etc/ssh)
925+ $( maybeSudo) mkdir -m 755 -p /mnt/etc/ssh
939926 for p in /etc/ssh/ssh_host_*; do
940927 # Skip if the source file does not exist (i.e. glob did not match any files)
941928 # or the destination already exists (e.g. copied with --extra-files).
942929 if [ ! -e "\$ p" ] || [ -e "/mnt/\$ p" ]; then
943930 continue
944931 fi
945- $( maybeSudo cp -a ' $p ' ' /mnt/$p ' )
932+ $( maybeSudo) cp -a " \$ p" " /mnt/\$ p"
946933 done
947934fi
948935# https://stackoverflow.com/a/13864829
949936if [ ! -z ${NIXOS_NO_CHECK+0} ]; then
950937 export NIXOS_NO_CHECK
951938fi
952- $( maybeSudo nixos-install --no-root-passwd --no-channel-copy --system " $nixosSystem " )
939+ $( maybeSudo) nixos-install --no-root-passwd --no-channel-copy --system "$nixosSystem "
953940SSH
954941
955942}
@@ -959,11 +946,11 @@ nixosReboot() {
959946 runSsh sh << SSH
960947 if command -v zpool >/dev/null && [ "\$ (zpool list)" != "no pools available" ]; then
961948 # we always want to export the zfs pools so people can boot from it without force import
962- $( maybeSudo umount -Rv /mnt/)
963- $( maybeSudo swapoff -a)
964- $( maybeSudo zpool export -a || true)
949+ $( maybeSudo) umount -Rv /mnt/
950+ $( maybeSudo) swapoff -a
951+ $( maybeSudo) zpool export -a || true
965952 fi
966- $( maybeSudo nohup sh -c ' sleep 6 && reboot' ) >/dev/null &
953+ $( maybeSudo) nohup sh -c 'sleep 6 && reboot' >/dev/null &
967954SSH
968955
969956 step Waiting for the machine to become unreachable due to reboot
0 commit comments