Skip to content

Commit 8441ad7

Browse files
committed
nixos-anywhere: always set maybeSudo after facts are imported
1 parent c2f0f1e commit 8441ad7

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/get-facts.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ isArch=$(uname -m)
1010
isNixos=$isNixos
1111
isInstaller=$(if [ "$isNixos" = "y" ] && grep -Eq 'VARIANT_ID="?installer"?' /etc/os-release; then echo "y"; else echo "n"; fi)
1212
isContainer=$(if [ "$(has systemd-detect-virt)" = "y" ]; then systemd-detect-virt --container; else echo "none"; fi)
13+
isRoot=$(if [ "$(id -u)" -eq 0 ]; then echo "y"; else echo "n"; fi)
1314
hasIpv6Only=$(if [ "$(has ip)" = "n" ] || ip r g 1 >/dev/null 2>/dev/null || ! ip -6 r g :: >/dev/null 2>/dev/null; then echo "n"; else echo "y"; fi)
1415
hasTar=$(has tar)
1516
hasCpio=$(has cpio)

src/nixos-anywhere.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ isOs=
4848
isArch=
4949
isInstaller=
5050
isContainer=
51+
isRoot=
5152
hasIpv6Only=
5253
hasTar=
5354
hasCpio=
@@ -519,20 +520,31 @@ importFacts() {
519520
fi
520521
filteredFacts=$(echo "$facts" | grep -E '^(has|is)[A-Za-z0-9_]+=\S+')
521522
if [[ -z $filteredFacts ]]; then
522-
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"
523524
fi
524525
# make facts available in script
525526
# shellcheck disable=SC2046
526527
export $(echo "$filteredFacts" | xargs)
527528

528529
# Necessary to prevent Bash erroring before printing out which fact had an issue
529530
set +u
530-
for var in isOs isArch isInstaller isContainer hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
531+
for var in isOs isArch isInstaller isContainer isRoot hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
531532
if [[ -z ${!var} ]]; then
532533
abort "Failed to retrieve fact $var from host"
533534
fi
534535
done
535536
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
536548
}
537549

538550
checkBuildLocally() {
@@ -579,7 +591,6 @@ checkBuildLocally() {
579591
}
580592
581593
generateHardwareConfig() {
582-
local maybeSudo="$maybeSudo"
583594
mkdir -p "$(dirname "$hardwareConfigPath")"
584595
case "$hardwareConfigBackend" in
585596
nixos-facter)
@@ -703,9 +714,6 @@ TMPDIR=/root/kexec setsid --wait ${maybeSudo} /root/kexec/kexec/run --kexec-extr
703714
# After kexec we explicitly set the user to root@
704715
sshConnection="root@${sshHost}"
705716
706-
# TODO: remove this after we reimport facts post-kexec and set this as a fact
707-
maybeSudo=""
708-
709717
# waiting for machine to become available again
710718
until runSsh -o ConnectTimeout=10 -- exit 0; do sleep 5; done
711719
@@ -872,13 +880,6 @@ main() {
872880
abort "no setsid command found, but required to run the kexec script under a new session"
873881
fi
874882
875-
maybeSudo=""
876-
if [[ ${hasSudo-n} == "y" ]]; then
877-
maybeSudo="sudo"
878-
elif [[ ${hasDoas-n} == "y" ]]; then
879-
maybeSudo="doas"
880-
fi
881-
882883
if [[ ${isOs} != "Linux" ]]; then
883884
abort "This script requires Linux as the operating system, but got $isOs"
884885
fi

0 commit comments

Comments
 (0)