Skip to content

Commit c12811b

Browse files
Mic92mergify[bot]
authored andcommitted
move more code into main function
1 parent 1c048c7 commit c12811b

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

src/nixos-anywhere.sh

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,20 @@ fi
234234

235235
# ssh wrapper
236236
runSshTimeout() {
237-
timeout 10 ssh -i "$ssh_key_dir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
237+
timeout 10 ssh -i "$sshKeyDir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
238238
}
239239
runSsh() {
240-
ssh "$sshTtyParam" -i "$ssh_key_dir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
240+
ssh "$sshTtyParam" -i "$sshKeyDir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
241241
}
242242

243243
nixCopy() {
244-
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $ssh_key_dir/nixos-anywhere ${sshArgs[*]}" nix copy \
244+
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $sshKeyDir/nixos-anywhere ${sshArgs[*]}" nix copy \
245245
"${nixOptions[@]}" \
246246
"${nixCopyOptions[@]}" \
247247
"$@"
248248
}
249249
nixBuild() {
250-
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $ssh_key_dir/nixos-anywhere ${sshArgs[*]}" nix build \
250+
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $sshKeyDir/nixos-anywhere ${sshArgs[*]}" nix build \
251251
--print-out-paths \
252252
--no-link \
253253
"${nixOptions[@]}" \
@@ -281,58 +281,10 @@ runVmTest() {
281281
"${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.installTest"
282282
}
283283

284-
285-
if [[ -z ${sshConnection-} ]]; then
286-
abort "ssh-host must be set"
287-
fi
288-
289-
if [[ -n ${flake-} ]]; then
290-
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
291-
flake="${BASH_REMATCH[1]}"
292-
flakeAttr="${BASH_REMATCH[2]}"
293-
fi
294-
if [[ -z ${flakeAttr-} ]]; then
295-
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." >&2
296-
echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
297-
exit 1
298-
fi
299-
fi
300-
301-
if [[ -n ${vmTest-} ]]; then
302-
runVmTest
303-
fi
304-
305-
# parse flake nixos-install style syntax, get the system attr
306-
if [[ -n ${flake-} ]]; then
307-
if [[ ${buildOnRemote} == "n" ]]; then
308-
diskoScript=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript")
309-
nixosSystem=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
310-
fi
311-
elif [[ -n ${diskoScript-} ]] && [[ -n ${nixosSystem-} ]]; then
312-
if [[ ! -e ${diskoScript} ]] || [[ ! -e ${nixosSystem} ]]; then
313-
abort "${diskoScript} and ${nixosSystem} must be existing store-paths"
314-
fi
315-
else
316-
abort "--flake or --store-paths must be set"
317-
fi
318-
319-
if [[ -n ${SSH_PRIVATE_KEY} ]] && [[ -z ${sshPrivateKeyFile-} ]]; then
320-
# $ssh_key_dir is getting deleted on trap EXIT
321-
sshPrivateKeyFile="$ssh_key_dir/from-env"
322-
(
323-
umask 077
324-
printf '%s\n' "$SSH_PRIVATE_KEY" >"$sshPrivateKeyFile"
325-
)
326-
fi
327-
328-
sshSettings=$(ssh "${sshArgs[@]}" -G "${sshConnection}")
329-
sshUser=$(echo "$sshSettings" | awk '/^user / { print $2 }')
330-
sshHost=$(echo "$sshSettings" | awk '/^hostname / { print $2 }')
331-
332284
uploadSshKey() {
333285
# we generate a temporary ssh keypair that we can use during nixos-anywhere
334286
sshKeyDir=$(mktemp -d)
335-
trap 'rm -rf "$ssh_key_dir"' EXIT
287+
trap 'rm -rf "$sshKeyDir"' EXIT
336288
mkdir -p "$sshKeyDir"
337289
# ssh-copy-id requires this directory
338290
mkdir -p "$HOME/.ssh/"
@@ -532,6 +484,53 @@ SSH
532484
}
533485

534486
main() {
487+
if [[ -z ${sshConnection-} ]]; then
488+
abort "ssh-host must be set"
489+
fi
490+
491+
if [[ -n ${flake-} ]]; then
492+
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
493+
flake="${BASH_REMATCH[1]}"
494+
flakeAttr="${BASH_REMATCH[2]}"
495+
fi
496+
if [[ -z ${flakeAttr-} ]]; then
497+
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." >&2
498+
echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
499+
exit 1
500+
fi
501+
fi
502+
503+
if [[ -n ${vmTest-} ]]; then
504+
runVmTest
505+
fi
506+
507+
# parse flake nixos-install style syntax, get the system attr
508+
if [[ -n ${flake-} ]]; then
509+
if [[ ${buildOnRemote} == "n" ]]; then
510+
diskoScript=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript")
511+
nixosSystem=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
512+
fi
513+
elif [[ -n ${diskoScript-} ]] && [[ -n ${nixosSystem-} ]]; then
514+
if [[ ! -e ${diskoScript} ]] || [[ ! -e ${nixosSystem} ]]; then
515+
abort "${diskoScript} and ${nixosSystem} must be existing store-paths"
516+
fi
517+
else
518+
abort "--flake or --store-paths must be set"
519+
fi
520+
521+
if [[ -n ${SSH_PRIVATE_KEY} ]] && [[ -z ${sshPrivateKeyFile-} ]]; then
522+
# $sshKeyDir is getting deleted on trap EXIT
523+
sshPrivateKeyFile="$sshKeyDir/from-env"
524+
(
525+
umask 077
526+
printf '%s\n' "$SSH_PRIVATE_KEY" >"$sshPrivateKeyFile"
527+
)
528+
fi
529+
530+
sshSettings=$(ssh "${sshArgs[@]}" -G "${sshConnection}")
531+
sshUser=$(echo "$sshSettings" | awk '/^user / { print $2 }')
532+
sshHost=$(echo "$sshSettings" | awk '/^hostname / { print $2 }')
533+
535534
uploadSshKey
536535

537536
importFacts

0 commit comments

Comments
 (0)