@@ -430,28 +430,15 @@ runSsh() {
430
430
431
431
# Helper function to authenticate sudo with password if needed
432
432
maybeSudo () {
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
-
441
433
if [[ -n ${SUDO_PASSWORD} ]] && [[ ${maybeSudoCommand} == " sudo" ]]; then
442
434
# If debug is enabled and we have a sudo password, warn about potential issues
443
-
444
435
# Use sudo with password authentication - pipe password to all sudo commands
445
436
printf " printf %%s %q | sudo -S " " $SUDO_PASSWORD "
446
- printf ' %q ' " ${cmd[@]} "
437
+ # Restore debug state if it was enabled
447
438
elif [[ -n ${maybeSudoCommand} ]]; then
448
439
printf ' %s ' " ${maybeSudoCommand} "
449
- printf ' %q ' " ${cmd[@]} "
450
- else
451
- # No sudo command needed (e.g., already root after kexec)
452
- printf ' %q ' " ${cmd[@]} "
453
440
fi
454
- echo
441
+ # No output if no sudo needed (e.g., already root after kexec)
455
442
}
456
443
457
444
# Test and cache sudo password if needed
@@ -547,7 +534,7 @@ buildStoreUrl() {
547
534
# Use password authentication for nix-daemon
548
535
remoteProgram=" sh -c $( urlEncode " $( printf %s " $( printf ' %q' " $SUDO_PASSWORD " ) " | sudo -S nix-daemon) " ) "
549
536
else
550
- remoteProgram=" ${maybeSudoCommand} , nix-daemon"
537
+ remoteProgram=" ${maybeSudoCommand} nix-daemon"
551
538
fi
552
539
553
540
if [[ $storeUrl == * " ?" * ]]; then
@@ -752,7 +739,7 @@ generateHardwareConfig() {
752
739
fi
753
740
754
741
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 "
756
743
;;
757
744
nixos-generate-config)
758
745
step " Generating hardware-configuration.nix using nixos-generate-config"
@@ -806,10 +793,10 @@ runKexec() {
806
793
local remoteCommandTemplate
807
794
remoteCommandTemplate="
808
795
set -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
811
798
%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 \" }
813
800
"
814
801
815
802
# Define upload commands
@@ -870,7 +857,7 @@ runDisko() {
870
857
local diskoScript=$1
871
858
for path in " ${! diskEncryptionKeys[@]} " ; do
872
859
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]} "
874
861
done
875
862
if [[ -n ${diskoScript} ]]; then
876
863
nixCopy --to " ssh-ng://$sshConnection " " $diskoScript "
@@ -887,7 +874,7 @@ runDisko() {
887
874
fi
888
875
889
876
step Formatting hard drive with disko
890
- runSsh " $( maybeSudo " $diskoScript " ) "
877
+ runSsh " $( maybeSudo) $diskoScript "
891
878
}
892
879
893
880
nixosInstall () {
@@ -912,12 +899,12 @@ nixosInstall() {
912
899
step Copying extra files
913
900
tar -C " $extraFiles " -cpf- . | runSsh " ${maybeSudoCommand} tar -C /mnt -xf- --no-same-owner"
914
901
915
- runSsh " $( maybeSudo chmod 755 /mnt) " # tar also changes permissions of /mnt
902
+ runSsh " $( maybeSudo) chmod 755 /mnt" # tar also changes permissions of /mnt
916
903
fi
917
904
918
905
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"
921
908
fi
922
909
923
910
step Installing NixOS
@@ -929,27 +916,27 @@ export PATH="\$PATH:/run/current-system/sw/bin"
929
916
930
917
if [ ! -d "/mnt/tmp" ]; then
931
918
# 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
934
921
fi
935
922
936
923
if [ ${copyHostKeys-n} = "y" ]; then
937
924
# 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
939
926
for p in /etc/ssh/ssh_host_*; do
940
927
# Skip if the source file does not exist (i.e. glob did not match any files)
941
928
# or the destination already exists (e.g. copied with --extra-files).
942
929
if [ ! -e "\$ p" ] || [ -e "/mnt/\$ p" ]; then
943
930
continue
944
931
fi
945
- $( maybeSudo cp -a ' $p ' ' /mnt/$p ' )
932
+ $( maybeSudo) cp -a " \$ p" " /mnt/\$ p"
946
933
done
947
934
fi
948
935
# https://stackoverflow.com/a/13864829
949
936
if [ ! -z ${NIXOS_NO_CHECK+0} ]; then
950
937
export NIXOS_NO_CHECK
951
938
fi
952
- $( maybeSudo nixos-install --no-root-passwd --no-channel-copy --system " $nixosSystem " )
939
+ $( maybeSudo) nixos-install --no-root-passwd --no-channel-copy --system "$nixosSystem "
953
940
SSH
954
941
955
942
}
@@ -959,11 +946,11 @@ nixosReboot() {
959
946
runSsh sh << SSH
960
947
if command -v zpool >/dev/null && [ "\$ (zpool list)" != "no pools available" ]; then
961
948
# 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
965
952
fi
966
- $( maybeSudo nohup sh -c ' sleep 6 && reboot' ) >/dev/null &
953
+ $( maybeSudo) nohup sh -c 'sleep 6 && reboot' >/dev/null &
967
954
SSH
968
955
969
956
step Waiting for the machine to become unreachable due to reboot
0 commit comments