@@ -46,9 +46,9 @@ envPassword=n
4646# Facts set by get-facts.sh
4747isOs=
4848isArch=
49- isKexec=
5049isInstaller=
5150isContainer=
51+ isRoot=
5252hasIpv6Only=
5353hasTar=
5454hasCpio=
@@ -520,17 +520,31 @@ importFacts() {
520520 fi
521521 filteredFacts=$( echo " $facts " | grep -E ' ^(has|is)[A-Za-z0-9_]+=\S+' )
522522 if [[ -z $filteredFacts ]]; then
523- abort " Retrieving host facts via ssh failed. Check with --debug for the root cause, unless you have done so already"
523+ abort " Retrieving host facts via SSH failed. Check with --debug for the root cause, unless you have done so already"
524524 fi
525525 # make facts available in script
526526 # shellcheck disable=SC2046
527527 export $( echo " $filteredFacts " | xargs)
528528
529- for var in isOs isArch isKexec isInstaller isContainer hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
529+ # Necessary to prevent Bash erroring before printing out which fact had an issue
530+ set +u
531+ for var in isOs isArch isInstaller isContainer isRoot hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
530532 if [[ -z ${! var} ]]; then
531533 abort " Failed to retrieve fact $var from host"
532534 fi
533535 done
536+ set -u
537+
538+ if [[ ${isRoot} == " y" ]]; then
539+ maybeSudo=
540+ elif [[ ${hasSudo} == " y" ]]; then
541+ maybeSudo=sudo
542+ elif [[ ${hasDoas} == " y" ]]; then
543+ maybeSudo=doas
544+ else
545+ # shellcheck disable=SC2016
546+ abort ' Unable to find a command to use to escalate privileges: Could not find `sudo` or `doas`'
547+ fi
534548}
535549
536550checkBuildLocally () {
@@ -577,7 +591,6 @@ checkBuildLocally() {
577591}
578592
579593generateHardwareConfig () {
580- local maybeSudo=" $maybeSudo "
581594 mkdir -p " $( dirname " $hardwareConfigPath " ) "
582595 case " $hardwareConfigBackend " in
583596 nixos-facter)
@@ -618,7 +631,7 @@ generateHardwareConfig() {
618631}
619632
620633runKexec () {
621- if [[ ${isKexec} == " y " ]] || [[ ${ isInstaller} == " y" ]]; then
634+ if [[ ${isInstaller} == " y" ]]; then
622635 return
623636 fi
624637
@@ -701,11 +714,14 @@ TMPDIR=/root/kexec setsid --wait ${maybeSudo} /root/kexec/kexec/run --kexec-extr
701714 # After kexec we explicitly set the user to root@
702715 sshConnection=" root@${sshHost} "
703716
704- # TODO: remove this after we reimport facts post-kexec and set this as a fact
705- maybeSudo=" "
706-
707717 # waiting for machine to become available again
708718 until runSsh -o ConnectTimeout=10 -- exit 0; do sleep 5; done
719+
720+ importFacts
721+
722+ if [[ ${isInstaller} == " n" ]]; then
723+ abort " Failed to kexec into NixOS installer"
724+ fi
709725}
710726
711727runDisko () {
@@ -864,13 +880,6 @@ main() {
864880 abort " no setsid command found, but required to run the kexec script under a new session"
865881 fi
866882
867- maybeSudo=" "
868- if [[ ${hasSudo-n} == " y" ]]; then
869- maybeSudo=" sudo"
870- elif [[ ${hasDoas-n} == " y" ]]; then
871- maybeSudo=" doas"
872- fi
873-
874883 if [[ ${isOs} != " Linux" ]]; then
875884 abort " This script requires Linux as the operating system, but got $isOs "
876885 fi
0 commit comments