@@ -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,29 +156,58 @@ 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
166
168
flake=" ${BASH_REMATCH[1]} "
167
169
flakeAttr=" ${BASH_REMATCH[2]} "
168
170
fi
169
171
if [[ -z ${flakeAttr-} ]]; then
170
- echo " Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri."
171
- echo ' For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.'
172
+ echo " Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." >&2
173
+ echo ' For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
172
174
exit 1
173
175
fi
174
176
disko_script=$( nix_build " ${flake} #nixosConfigurations.${flakeAttr} .config.system.build.disko" )
175
177
nixos_system=$( nix_build " ${flake} #nixosConfigurations.${flakeAttr} .config.system.build.toplevel" )
176
178
elif [[ -n ${disko_script-} ]] && [[ -n ${nixos_system-} ]]; then
177
179
if [[ ! -e ${disko_script} ]] || [[ ! -e ${nixos_system} ]]; then
178
- echo " ${disko_script} and ${nixos_system} must be existing store-paths"
179
- exit 1
180
+ abort " ${disko_script} and ${nixos_system} must be existing store-paths"
180
181
fi
181
182
:
182
183
else
183
184
abort " flake must be set"
184
185
fi
185
186
187
+ if [[ -n ${SSH_PRIVATE_KEY-} ]]; then
188
+ sshPrivateKeyFile=$( mktemp)
189
+ trap ' rm "$sshPrivateKeyFile"' EXIT
190
+ (
191
+ umask 077
192
+ printf ' %s' " $SSH_PRIVATE_KEY " > " $sshPrivateKeyFile "
193
+ )
194
+ unset SSH_AUTH_SOCK # don't use system agent if key was supplied
195
+ ssh_copy_id_args+=(-o " IdentityFile=${sshPrivateKeyFile} " )
196
+ ssh_copy_id_args+=(-f)
197
+ fi
198
+
199
+ until
200
+ ssh-copy-id \
201
+ -i " $ssh_key_dir " /nixos-remote.pub \
202
+ -o ConnectTimeout=10 \
203
+ -o UserKnownHostsFile=/dev/null \
204
+ -o StrictHostKeyChecking=no \
205
+ " ${ssh_copy_id_args[@]} " \
206
+ " $ssh_connection "
207
+ do
208
+ sleep 3
209
+ done
210
+
186
211
import_facts () {
187
212
local facts filtered_facts
188
213
if ! facts=$(
@@ -205,7 +230,7 @@ has_curl=\$(has curl)
205
230
FACTS
206
231
SSH
207
232
) ; then
208
- return 1
233
+ exit 1
209
234
fi
210
235
filtered_facts=$( echo " $facts " | grep -E ' ^(has|is)_[a-z0-9_]+=\S+' )
211
236
if [[ -z $filtered_facts ]]; then
216
241
export $( echo " $filtered_facts " | xargs)
217
242
}
218
243
219
- # wait for machine to become reachable (possibly forever)
220
- until import_facts; do
221
- sleep 5
222
- done
244
+ import_facts
223
245
224
246
if [[ ${has_tar-n} == " n" ]]; then
225
247
abort " no tar command found, but required to unpack kexec tarball"
@@ -236,10 +258,6 @@ if [[ ${is_arch-n} != "x86_64" ]] && [[ $kexec_url == "$default_kexec_url" ]]; t
236
258
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
259
fi
238
260
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
261
if [[ ${is_kexec-n} == " n" ]] && [[ ${is_installer-n} == " n" ]]; then
244
262
ssh_ << SSH
245
263
set -efu ${enable_debug}
@@ -279,6 +297,9 @@ nix_copy --to "ssh://$ssh_connection" "$disko_script"
279
297
ssh_ " $disko_script "
280
298
281
299
if [[ ${stop_after_disko-n} == " y" ]]; then
300
+ # Should we also do this for `--no-reboot`?
301
+ echo " WARNING: leaving temporary ssh key at '$ssh_key_dir /nixos-remote' to login to the machine" >&2
302
+ trap - EXIT
282
303
exit 0
283
304
fi
284
305
0 commit comments