@@ -11,8 +11,6 @@ Options:
1111* -s, --store-paths
1212 set the store paths to the disko-script and nixos-system directly
1313 if this is give, flake is not needed
14- * --no-ssh-copy
15- skip copying ssh-keys to target system
1614* --no-reboot
1715 do not reboot after installation, allowing further customization of the target installation.
1816* --kexec url
@@ -49,9 +47,10 @@ nix_options=(
4947 " --no-write-lock-file"
5048)
5149substitute_on_destination=y
52- nix_copy_options=()
5350
5451declare -A disk_encryption_keys
52+ declare -a nix_copy_options
53+ declare -a ssh_copy_id_args
5554
5655while [[ $# -gt 0 ]]; do
5756 case " $1 " in
@@ -76,9 +75,6 @@ while [[ $# -gt 0 ]]; do
7675 kexec_url=$2
7776 shift
7877 ;;
79- --no-ssh-copy-id)
80- no_ssh_copy=y
81- ;;
8278 --debug)
8379 enable_debug=" -x"
8480 print_build_logs=y
@@ -126,14 +122,6 @@ while [[ $# -gt 0 ]]; do
126122 shift
127123done
128124
129- # ssh wrapper
130- timeout_ssh_ () {
131- timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " $ssh_connection " " $@ "
132- }
133- ssh_ () {
134- ssh -T -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " $ssh_connection " " $@ "
135- }
136-
137125if [[ ${print_build_logs-n} == " y" ]]; then
138126 nix_options+=(" -L" )
139127fi
@@ -142,8 +130,16 @@ if [[ ${substitute_on_destination-n} == "y" ]]; then
142130 nix_copy_options+=(" --substitute-on-destination" )
143131fi
144132
133+ # ssh wrapper
134+ timeout_ssh_ () {
135+ timeout 10 ssh -i " $ssh_key_dir " /nixos-remote -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " $ssh_connection " " $@ "
136+ }
137+ ssh_ () {
138+ ssh -T -i " $ssh_key_dir " /nixos-remote -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " $ssh_connection " " $@ "
139+ }
140+
145141nix_copy () {
146- NIX_SSHOPTS=' -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' nix copy \
142+ NIX_SSHOPTS=" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $ssh_key_dir /nixos-remote " nix copy \
147143 " ${nix_options[@]} " \
148144 " ${nix_copy_options[@]} " \
149145 " $@ "
@@ -160,6 +156,12 @@ if [[ -z ${ssh_connection-} ]]; then
160156 abort " ssh-host must be set"
161157fi
162158
159+ # we generate a temporary ssh keypair that we can use during nixos-remote
160+ ssh_key_dir=$( mktemp -d)
161+ trap ' rm -rf "$ssh_key_dir"' EXIT
162+ mkdir -p " $ssh_key_dir "
163+ ssh-keygen -t ed25519 -f " $ssh_key_dir " /nixos-remote -P " " -C " nixos-remote" > /dev/null
164+
163165# parse flake nixos-install style syntax, get the system attr
164166if [[ -n ${flake-} ]]; then
165167 if [[ $flake =~ ^(.* )\# ([^\#\" ]* )$ ]]; then
@@ -183,6 +185,30 @@ else
183185 abort " flake must be set"
184186fi
185187
188+ if [[ -n ${SSH_PRIVATE_KEY-} ]]; then
189+ sshPrivateKeyFile=$( mktemp)
190+ trap ' rm "$sshPrivateKeyFile"' EXIT
191+ (
192+ umask 077
193+ printf ' %s' " $SSH_PRIVATE_KEY " > " $sshPrivateKeyFile "
194+ )
195+ unset SSH_AUTH_SOCK # don't use system agent if key was supplied
196+ ssh_copy_id_args+=(-o " IdentityFile=${sshPrivateKeyFile} " )
197+ ssh_copy_id_args+=(-f)
198+ fi
199+
200+ until
201+ ssh-copy-id \
202+ -i " $ssh_key_dir " /nixos-remote.pub \
203+ -o ConnectTimeout=10 \
204+ -o UserKnownHostsFile=/dev/null \
205+ -o StrictHostKeyChecking=no \
206+ " ${ssh_copy_id_args[@]} " \
207+ " $ssh_connection "
208+ do
209+ sleep 3
210+ done
211+
186212import_facts () {
187213 local facts filtered_facts
188214 if ! facts=$(
@@ -205,7 +231,7 @@ has_curl=\$(has curl)
205231FACTS
206232SSH
207233 ) ; then
208- return 1
234+ exit 1
209235 fi
210236 filtered_facts=$( echo " $facts " | grep -E ' ^(has|is)_[a-z0-9_]+=\S+' )
211237 if [[ -z $filtered_facts ]]; then
216242 export $( echo " $filtered_facts " | xargs)
217243}
218244
219- # wait for machine to become reachable (possibly forever)
220- until import_facts; do
221- sleep 5
222- done
245+ import_facts
223246
224247if [[ ${has_tar-n} == " n" ]]; then
225248 abort " no tar command found, but required to unpack kexec tarball"
@@ -236,10 +259,6 @@ if [[ ${is_arch-n} != "x86_64" ]] && [[ $kexec_url == "$default_kexec_url" ]]; t
236259 abort " The default kexec image only support x86_64 cpus. Checkout https://github.com/numtide/nixos-remote/#using-your-own-kexec-image for more information."
237260fi
238261
239- if [[ ${is_kexec-n} != " y" ]] && [[ ${no_ssh_copy-n} != " y" ]]; then
240- ssh-copy-id -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " $ssh_connection "
241- fi
242-
243262if [[ ${is_kexec-n} == " n" ]] && [[ ${is_installer-n} == " n" ]]; then
244263 ssh_ << SSH
245264set -efu ${enable_debug}
@@ -279,6 +298,9 @@ nix_copy --to "ssh://$ssh_connection" "$disko_script"
279298ssh_ " $disko_script "
280299
281300if [[ ${stop_after_disko-n} == " y" ]]; then
301+ # Should we also do this for `--no-reboot`?
302+ echo " WARNING: leaving temporary ssh key at '$ssh_key_dir /nixos-remote' to login to the machine" >&2
303+ trap - EXIT
282304 exit 0
283305fi
284306
0 commit comments