Skip to content

Commit 9fb92e0

Browse files
Mic92mergify[bot]
authored andcommitted
factor out a runVmTest function
1 parent bc77bd1 commit 9fb92e0

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

src/nixos-anywhere.sh

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ else
9090
sshTtyParam="-T"
9191
fi
9292
postKexecSshPort=22
93+
buildOnRemote=n
9394

9495
declare -A diskEncryptionKeys
9596
declare -a nixCopyOptions
@@ -252,20 +253,46 @@ nixBuild() {
252253
"$@"
253254
}
254255

255-
if [[ -z ${vmTest-} ]]; then
256-
if [[ -z ${sshConnection-} ]]; then
257-
abort "ssh-host must be set"
256+
runVmTest() {
257+
if [[ -n ${diskoScript-} ]] && [[ -n ${nixosSystem-} ]]; then
258+
echo "--vm-test is not supported with --store-paths" >&2
259+
echo "Please use --flake instead or build config.system.build.installTest of your nixos configuration manually" >&2
260+
exit 1
258261
fi
259262

260-
# we generate a temporary ssh keypair that we can use during nixos-anywhere
261-
ssh_key_dir=$(mktemp -d)
262-
trap 'rm -rf "$ssh_key_dir"' EXIT
263-
mkdir -p "$ssh_key_dir"
264-
# ssh-copy-id requires this directory
265-
mkdir -p "$HOME/.ssh/"
266-
ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
263+
if [[ ${buildOnRemote} == "y" ]]; then
264+
echo "--vm-test is not supported with --build-on-remote" >&2
265+
exit 1
266+
fi
267+
if [[ -n ${extraFiles-} ]]; then
268+
echo "--vm-test is not supported with --extra-files" >&2
269+
exit 1
270+
fi
271+
if [[ -n ${diskEncryptionKeys-} ]]; then
272+
echo "--vm-test is not supported with --disk-encryption-keys" >&2
273+
exit 1
274+
fi
275+
exec nix build \
276+
--print-out-paths \
277+
--no-link \
278+
-L \
279+
"${nixOptions[@]}" \
280+
"${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.installTest"
281+
}
282+
283+
284+
if [[ -z ${sshConnection-} ]]; then
285+
abort "ssh-host must be set"
267286
fi
268287

288+
# we generate a temporary ssh keypair that we can use during nixos-anywhere
289+
ssh_key_dir=$(mktemp -d)
290+
trap 'rm -rf "$ssh_key_dir"' EXIT
291+
mkdir -p "$ssh_key_dir"
292+
# ssh-copy-id requires this directory
293+
mkdir -p "$HOME/.ssh/"
294+
ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
295+
269296
# parse flake nixos-install style syntax, get the system attr
270297
if [[ -n ${flake-} ]]; then
271298
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
@@ -277,32 +304,14 @@ if [[ -n ${flake-} ]]; then
277304
echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
278305
exit 1
279306
fi
280-
if [[ ${buildOnRemote-n} == "n" ]]; then
281-
if [[ -n ${vmTest-} ]]; then
282-
if [[ -n ${extraFiles-} ]]; then
283-
echo "--vm-test is not supported with --extra-files" >&2
284-
exit 1
285-
fi
286-
if [[ -n ${diskEncryptionKeys-} ]]; then
287-
echo "--vm-test is not supported with --disk-encryption-keys" >&2
288-
exit 1
289-
fi
290-
exec nix build \
291-
--print-out-paths \
292-
--no-link \
293-
-L \
294-
"${nixOptions[@]}" \
295-
"${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.installTest"
296-
fi
307+
if [[ -n $vmTest ]]; then
308+
runVmTest
309+
fi
310+
if [[ ${buildOnRemote} == "n" ]]; then
297311
diskoScript=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript")
298312
nixosSystem=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
299313
fi
300314
elif [[ -n ${diskoScript-} ]] && [[ -n ${nixosSystem-} ]]; then
301-
if [[ -n ${vmTest-} ]]; then
302-
echo "vm-test is not supported with --store-paths" >&2
303-
echo "Please use --flake instead or build config.system.build.installTest of your nixos configuration manually" >&2
304-
exit 1
305-
fi
306315
if [[ ! -e ${diskoScript} ]] || [[ ! -e ${nixosSystem} ]]; then
307316
abort "${diskoScript} and ${nixosSystem} must be existing store-paths"
308317
fi
@@ -446,7 +455,7 @@ runDisko() {
446455
done
447456
if [[ -n ${diskoScript-} ]]; then
448457
nixCopy --to "ssh://$sshConnection" "$diskoScript"
449-
elif [[ ${buildOnRemote-n} == "y" ]]; then
458+
elif [[ ${buildOnRemote} == "y" ]]; then
450459
step Building disko script
451460
# We need to do a nix copy first because nix build doesn't have --no-check-sigs
452461
nixCopy --to "ssh-ng://$sshConnection" "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript" \
@@ -465,7 +474,7 @@ nixosInstall() {
465474
if [[ -n ${nixosSystem-} ]]; then
466475
step Uploading the system closure
467476
nixCopy --to "ssh://$sshConnection?remote-store=local?root=/mnt" "$nixosSystem"
468-
elif [[ ${buildOnRemote-n} == "y" ]]; then
477+
elif [[ ${buildOnRemote} == "y" ]]; then
469478
step Building the system closure
470479
# We need to do a nix copy first because nix build doesn't have --no-check-sigs
471480
nixCopy --to "ssh-ng://$sshConnection?remote-store=local?root=/mnt" "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel" \

0 commit comments

Comments
 (0)