@@ -11,8 +11,6 @@ Options:
11
11
* -s, --store-paths
12
12
set the store paths to the disko-script and nixos-system directly
13
13
if this is give, flake is not needed
14
- * --no-ssh-copy
15
- skip copying ssh-keys to target system
16
14
* --no-reboot
17
15
do not reboot after installation, allowing further customization of the target installation.
18
16
* --kexec url
@@ -49,9 +47,10 @@ nix_options=(
49
47
" --no-write-lock-file"
50
48
)
51
49
substitute_on_destination=y
52
- nix_copy_options=()
53
50
54
51
declare -A disk_encryption_keys
52
+ declare -a nix_copy_options
53
+ declare -a ssh_copy_id_args
55
54
56
55
while [[ $# -gt 0 ]]; do
57
56
case " $1 " in
@@ -76,9 +75,6 @@ while [[ $# -gt 0 ]]; do
76
75
kexec_url=$2
77
76
shift
78
77
;;
79
- --no-ssh-copy-id)
80
- no_ssh_copy=y
81
- ;;
82
78
--debug)
83
79
enable_debug=" -x"
84
80
print_build_logs=y
@@ -126,14 +122,6 @@ while [[ $# -gt 0 ]]; do
126
122
shift
127
123
done
128
124
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
-
137
125
if [[ ${print_build_logs-n} == " y" ]]; then
138
126
nix_options+=(" -L" )
139
127
fi
@@ -142,8 +130,16 @@ if [[ ${substitute_on_destination-n} == "y" ]]; then
142
130
nix_copy_options+=(" --substitute-on-destination" )
143
131
fi
144
132
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
+
145
141
nix_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 \
147
143
" ${nix_options[@]} " \
148
144
" ${nix_copy_options[@]} " \
149
145
" $@ "
@@ -160,6 +156,12 @@ if [[ -z ${ssh_connection-} ]]; then
160
156
abort " ssh-host must be set"
161
157
fi
162
158
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
+
163
165
# parse flake nixos-install style syntax, get the system attr
164
166
if [[ -n ${flake-} ]]; then
165
167
if [[ $flake =~ ^(.* )\# ([^\#\" ]* )$ ]]; then
@@ -183,6 +185,30 @@ else
183
185
abort " flake must be set"
184
186
fi
185
187
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
+
186
212
import_facts () {
187
213
local facts filtered_facts
188
214
if ! facts=$(
@@ -205,7 +231,7 @@ has_curl=\$(has curl)
205
231
FACTS
206
232
SSH
207
233
) ; then
208
- return 1
234
+ exit 1
209
235
fi
210
236
filtered_facts=$( echo " $facts " | grep -E ' ^(has|is)_[a-z0-9_]+=\S+' )
211
237
if [[ -z $filtered_facts ]]; then
216
242
export $( echo " $filtered_facts " | xargs)
217
243
}
218
244
219
- # wait for machine to become reachable (possibly forever)
220
- until import_facts; do
221
- sleep 5
222
- done
245
+ import_facts
223
246
224
247
if [[ ${has_tar-n} == " n" ]]; then
225
248
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
236
259
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."
237
260
fi
238
261
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
-
243
262
if [[ ${is_kexec-n} == " n" ]] && [[ ${is_installer-n} == " n" ]]; then
244
263
ssh_ << SSH
245
264
set -efu ${enable_debug}
@@ -279,6 +298,9 @@ nix_copy --to "ssh://$ssh_connection" "$disko_script"
279
298
ssh_ " $disko_script "
280
299
281
300
if [[ ${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
282
304
exit 0
283
305
fi
284
306
0 commit comments