@@ -64,7 +64,7 @@ mkdir -p "$tempDir"
64
64
65
65
declare -A diskEncryptionKeys=()
66
66
declare -A extraFilesOwnership=()
67
- declare -a nixCopyOptions=()
67
+ declare -a nixCopyOptions=(--no-check-sigs )
68
68
declare -a sshArgs=(" -o" " IdentitiesOnly=yes" " -i" " $tempDir /nixos-anywhere" " -o" " UserKnownHostsFile=/dev/null" " -o" " StrictHostKeyChecking=no" )
69
69
70
70
showUsage () {
@@ -420,19 +420,64 @@ runSsh() {
420
420
ssh " $sshTtyParam " " ${sshArgs[@]} " " $sshConnection " " $@ "
421
421
}
422
422
423
+ addRemoteProgram () {
424
+ local storeUrl=" $1 "
425
+ # Add remote-program parameter when sudo is needed
426
+ if [[ -n ${maybeSudo} ]] && [[ $storeUrl == ssh-ng://* ]]; then
427
+ if [[ $storeUrl == * " ?" * ]]; then
428
+ echo " ${storeUrl} &remote-program=${maybeSudo} nix-daemon"
429
+ else
430
+ echo " ${storeUrl} ?remote-program=${maybeSudo} nix-daemon"
431
+ fi
432
+ else
433
+ echo " $storeUrl "
434
+ fi
435
+ }
436
+
423
437
nixCopy () {
424
- NIX_SSHOPTS=" ${sshArgs[*]} " nix copy \
425
- " ${nixOptions[@]} " \
426
- " ${nixCopyOptions[@]} " \
427
- " $@ "
438
+ # Process arguments to add remote-program parameter when sudo is needed
439
+ local processedArgs=()
440
+ local i=0
441
+ while [[ $i -lt $# ]]; do
442
+ if [[ ${! i} == " --to" ]]; then
443
+ processedArgs+=(" ${! i} " )
444
+ (( i++ ))
445
+ local storeUrl=" ${! i} "
446
+ storeUrl=$( addRemoteProgram " $storeUrl " )
447
+ processedArgs+=(" $storeUrl " )
448
+ else
449
+ processedArgs+=(" ${! i} " )
450
+ fi
451
+ (( i++ ))
452
+ done
453
+
454
+ local nixCopyArgs=(" ${nixOptions[@]} " " ${nixCopyOptions[@]} " )
455
+
456
+ NIX_SSHOPTS=" ${sshArgs[*]} " nix copy " ${nixCopyArgs[@]} " " ${processedArgs[@]} "
428
457
}
429
458
nixBuild () {
459
+ # Process arguments to add remote-program parameter when sudo is needed
460
+ local processedArgs=()
461
+ local i=0
462
+ while [[ $i -lt $# ]]; do
463
+ if [[ ${! i} == " --store" ]]; then
464
+ processedArgs+=(" ${! i} " )
465
+ (( i++ ))
466
+ local storeUrl=" ${! i} "
467
+ storeUrl=$( addRemoteProgram " $storeUrl " )
468
+ processedArgs+=(" $storeUrl " )
469
+ else
470
+ processedArgs+=(" ${! i} " )
471
+ fi
472
+ (( i++ ))
473
+ done
474
+
430
475
NIX_SSHOPTS=" ${sshArgs[*]} " nix build \
431
476
--print-out-paths \
432
477
--no-link \
433
478
" ${nixBuildFlags[@]} " \
434
479
" ${nixOptions[@]} " \
435
- " $@ "
480
+ " ${processedArgs[@]} "
436
481
}
437
482
438
483
runVmTest () {
@@ -688,6 +733,8 @@ TMPDIR=/root/kexec setsid --wait ${maybeSudo} /root/kexec/kexec/run --kexec-extr
688
733
689
734
# After kexec we explicitly set the user to root@
690
735
sshConnection=" root@${sshHost} "
736
+ # After kexec, we're running as root in the NixOS installer, so no need for sudo
737
+ maybeSudo=" "
691
738
692
739
# waiting for machine to become available again
693
740
until runSsh -o ConnectTimeout=10 -- exit 0; do sleep 5; done
@@ -697,16 +744,15 @@ runDisko() {
697
744
local diskoScript=$1
698
745
for path in " ${! diskEncryptionKeys[@]} " ; do
699
746
step " Uploading ${diskEncryptionKeys[$path]} to $path "
700
- runSsh " umask 077; mkdir -p \" $( dirname " $path " ) \" ; cat > $path " < " ${diskEncryptionKeys[$path]} "
747
+ runSsh " ${maybeSudo} sh -c $( printf ' %q ' " umask 077; mkdir -p $( dirname " $path " ) ; cat > $path " ) " < " ${diskEncryptionKeys[$path]} "
701
748
done
702
749
if [[ -n ${diskoScript} ]]; then
703
- nixCopy --to " ssh://$sshConnection ?$sshStoreSettings " " $diskoScript "
750
+ nixCopy --to " ssh-ng ://$sshConnection ?$sshStoreSettings " " $diskoScript "
704
751
elif [[ ${buildOn} == " remote" ]]; then
705
752
step Building disko script
706
753
# We need to do a nix copy first because nix build doesn't have --no-check-sigs
707
754
# Use ssh:// here to avoid https://github.com/NixOS/nix/issues/7359
708
- nixCopy --to " ssh://$sshConnection ?$sshStoreSettings " " ${flake} #${flakeAttr} .system.build.${diskoMode} Script" \
709
- --derivation --no-check-sigs
755
+ nixCopy --to " ssh-ng://$sshConnection ?$sshStoreSettings " --derivation " ${flake} #${flakeAttr} .system.build.${diskoMode} Script"
710
756
# If we don't use ssh-ng here, we get `error: operation 'getFSAccessor' is not supported by store`
711
757
diskoScript=$(
712
758
nixBuild " ${flake} #${flakeAttr} .system.build.${diskoAttr} " \
@@ -715,37 +761,37 @@ runDisko() {
715
761
fi
716
762
717
763
step Formatting hard drive with disko
718
- runSsh " $diskoScript "
764
+ runSsh " ${maybeSudo} $ diskoScript"
719
765
}
720
766
721
767
nixosInstall () {
722
768
local nixosSystem=$1
769
+ local remoteStoreUrl=" remote-store=local%3Froot=%2Fmnt"
723
770
if [[ -n ${nixosSystem} ]]; then
724
771
step Uploading the system closure
725
- nixCopy --to " ssh://$sshConnection ?remote-store=local%3Froot=%2Fmnt &$sshStoreSettings " " $nixosSystem "
772
+ nixCopy --to " ssh-ng ://$sshConnection ?${remoteStoreUrl} &$sshStoreSettings " " $nixosSystem "
726
773
elif [[ ${buildOn} == " remote" ]]; then
727
774
step Building the system closure
728
775
# We need to do a nix copy first because nix build doesn't have --no-check-sigs
729
776
# Use ssh:// here to avoid https://github.com/NixOS/nix/issues/7359
730
- nixCopy --to " ssh://$sshConnection ?remote-store=local%3Froot=%2Fmnt&$sshStoreSettings " " ${flake} #${flakeAttr} .system.build.toplevel" \
731
- --derivation --no-check-sigs
777
+ nixCopy --to " ssh-ng://$sshConnection ?${remoteStoreUrl} &$sshStoreSettings " --derivation " ${flake} #${flakeAttr} .system.build.toplevel"
732
778
# If we don't use ssh-ng here, we get `error: operation 'getFSAccessor' is not supported by store`
733
779
nixosSystem=$(
734
780
nixBuild " ${flake} #${flakeAttr} .system.build.toplevel" \
735
- --eval-store auto --store " ssh-ng://$sshConnection ?ssh-key=$tempDir %2Fnixos-anywhere&remote-store=local%3Froot=%2Fmnt &$sshStoreSettings "
781
+ --eval-store auto --store " ssh-ng://$sshConnection ?ssh-key=$tempDir %2Fnixos-anywhere&${remoteStoreUrl} &$sshStoreSettings "
736
782
)
737
783
fi
738
784
739
785
if [[ -n ${extraFiles} ]]; then
740
786
step Copying extra files
741
- tar -C " $extraFiles " -cpf- . | runSsh " tar -C /mnt -xf- --no-same-owner"
787
+ tar -C " $extraFiles " -cpf- . | runSsh " ${maybeSudo} tar -C /mnt -xf- --no-same-owner"
742
788
743
- runSsh " chmod 755 /mnt" # tar also changes permissions of /mnt
789
+ runSsh " ${maybeSudo} chmod 755 /mnt" # tar also changes permissions of /mnt
744
790
fi
745
791
746
792
if [[ ${# extraFilesOwnership[@]} -gt 0 ]]; then
747
793
# shellcheck disable=SC2016
748
- printf " %s\n" " ${! extraFilesOwnership[@]} " " ${extraFilesOwnership[@]} " | pr -2t | runSsh ' while read file ownership; do chown -R "$ownership" "/mnt/$file"; done'
794
+ printf " %s\n" " ${! extraFilesOwnership[@]} " " ${extraFilesOwnership[@]} " | pr -2t | runSsh ' while read file ownership; do ' " ${maybeSudo} " ' chown -R "$ownership" "/mnt/$file"; done'
749
795
fi
750
796
751
797
step Installing NixOS
@@ -756,27 +802,27 @@ export PATH="\$PATH:/run/current-system/sw/bin"
756
802
757
803
if [ ! -d "/mnt/tmp" ]; then
758
804
# needed for installation if initrd-secrets are used
759
- mkdir -p /mnt/tmp
760
- chmod 777 /mnt/tmp
805
+ ${maybeSudo} mkdir -p /mnt/tmp
806
+ ${maybeSudo} chmod 777 /mnt/tmp
761
807
fi
762
808
763
809
if [ ${copyHostKeys-n} = "y" ]; then
764
810
# NB we copy host keys that are in turn copied by kexec installer.
765
- mkdir -m 755 -p /mnt/etc/ssh
811
+ ${maybeSudo} mkdir -m 755 -p /mnt/etc/ssh
766
812
for p in /etc/ssh/ssh_host_*; do
767
813
# Skip if the source file does not exist (i.e. glob did not match any files)
768
814
# or the destination already exists (e.g. copied with --extra-files).
769
815
if [ ! -e "\$ p" ] || [ -e "/mnt/\$ p" ]; then
770
816
continue
771
817
fi
772
- cp -a "\$ p" "/mnt/\$ p"
818
+ ${maybeSudo} cp -a "\$ p" "/mnt/\$ p"
773
819
done
774
820
fi
775
821
# https://stackoverflow.com/a/13864829
776
822
if [ ! -z ${NIXOS_NO_CHECK+0} ]; then
777
823
export NIXOS_NO_CHECK
778
824
fi
779
- nixos-install --no-root-passwd --no-channel-copy --system "$nixosSystem "
825
+ ${maybeSudo} nixos-install --no-root-passwd --no-channel-copy --system "$nixosSystem "
780
826
SSH
781
827
782
828
}
@@ -786,11 +832,11 @@ nixosReboot() {
786
832
runSsh sh << SSH
787
833
if command -v zpool >/dev/null && [ "\$ (zpool list)" != "no pools available" ]; then
788
834
# we always want to export the zfs pools so people can boot from it without force import
789
- umount -Rv /mnt/
790
- swapoff -a
791
- zpool export -a || true
835
+ ${maybeSudo} umount -Rv /mnt/
836
+ ${maybeSudo} swapoff -a
837
+ ${maybeSudo} zpool export -a || true
792
838
fi
793
- nohup sh -c 'sleep 6 && reboot' >/dev/null &
839
+ ${maybeSudo} nohup sh -c 'sleep 6 && reboot' >/dev/null &
794
840
SSH
795
841
796
842
step Waiting for the machine to become unreachable due to reboot
@@ -840,7 +886,6 @@ main() {
840
886
fi
841
887
842
888
sshSettings=$( ssh " ${sshArgs[@]} " -G " ${sshConnection} " )
843
- sshUser=$( echo " $sshSettings " | awk ' /^user / { print $2 }' )
844
889
sshHost=$( echo " $sshSettings " | awk ' /^hostname / { print $2 }' )
845
890
846
891
uploadSshKey
@@ -898,13 +943,6 @@ main() {
898
943
fi
899
944
fi
900
945
901
- # Installation will fail if non-root user is used for installer.
902
- # Switch to root user by copying authorized_keys.
903
- if [[ ${isInstaller} == " y" ]] && [[ ${sshUser} != " root" ]]; then
904
- # Allow copy to fail if authorized_keys does not exist, like if using /etc/ssh/authorized_keys.d/
905
- runSsh " ${maybeSudo} mkdir -p /root/.ssh; ${maybeSudo} cp ~/.ssh/authorized_keys /root/.ssh || true"
906
- sshConnection=" root@${sshHost} "
907
- fi
908
946
909
947
if [[ ${phases[disko]} == 1 ]]; then
910
948
runDisko " $diskoScript "
0 commit comments