add --vm-test mode for local vm tests

Update src/nixos-anywhere.sh
This commit is contained in:
lassulus 2023-07-20 21:48:27 +02:00 committed by Jörg Thalheim
parent 11d3803cc6
commit 581053b2c4
2 changed files with 42 additions and 12 deletions

View File

@ -83,7 +83,23 @@ example uses a local directory on the source machine.
5. On the target machine, make sure you have access as root via ssh by adding 5. On the target machine, make sure you have access as root via ssh by adding
your SSH key to the file `authorized_keys` in the directory `/root/.ssh` your SSH key to the file `authorized_keys` in the directory `/root/.ssh`
6. You can now run **nixos-anywhere** from the command line as shown below, 6. (Optional) Test your nixos and disko configuration:
The following command will automatically test your nixos configuration and run
disko inside a virtual machine, where
- `<path to configuration>` is the path to the directory or repository
containing `flake.nix` and `disk-config.nix`
- `<configuration name>` must match the name that immediately follows the text
`nixosConfigurations.` in the flake, as indicated by the comment in the
[example](https://github.com/numtide/nixos-anywhere-examples/blob/main/flake.nix)).
```
nix run github:numtide/nixos-anywhere -- --flake <path to configuration>#<configuration name> --vm-test
```
7. You can now run **nixos-anywhere** from the command line as shown below,
where: where:
- `<path to configuration>` is the path to the directory or repository - `<path to configuration>` is the path to the directory or repository

View File

@ -46,6 +46,8 @@ Options:
URL of the source Nix store to copy the nixos and disko closure from URL of the source Nix store to copy the nixos and disko closure from
* --build-on-remote * --build-on-remote
build the closure on the remote machine instead of locally and copy-closuring it build the closure on the remote machine instead of locally and copy-closuring it
* --vm-test
build the system and test the disk configuration inside a VM without installing it to the target.
USAGE USAGE
} }
@ -155,7 +157,9 @@ while [[ $# -gt 0 ]]; do
--build-on-remote) --build-on-remote)
build_on_remote=y build_on_remote=y
;; ;;
--vm-test)
vm_test=y
;;
*) *)
if [[ -z ${ssh_connection-} ]]; then if [[ -z ${ssh_connection-} ]]; then
ssh_connection="$1" ssh_connection="$1"
@ -198,17 +202,19 @@ nix_build() {
"$@" "$@"
} }
if [[ -z ${ssh_connection-} ]]; then if [[ -z ${vm_test-} ]]; then
abort "ssh-host must be set" if [[ -z ${ssh_connection-} ]]; then
fi abort "ssh-host must be set"
fi
# we generate a temporary ssh keypair that we can use during nixos-anywhere # we generate a temporary ssh keypair that we can use during nixos-anywhere
ssh_key_dir=$(mktemp -d) ssh_key_dir=$(mktemp -d)
trap 'rm -rf "$ssh_key_dir"' EXIT trap 'rm -rf "$ssh_key_dir"' EXIT
mkdir -p "$ssh_key_dir" mkdir -p "$ssh_key_dir"
# ssh-copy-id requires this directory # ssh-copy-id requires this directory
mkdir -p "$HOME/.ssh/" mkdir -p "$HOME/.ssh/"
ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
fi
# parse flake nixos-install style syntax, get the system attr # parse flake nixos-install style syntax, get the system attr
if [[ -n ${flake-} ]]; then if [[ -n ${flake-} ]]; then
@ -222,6 +228,14 @@ if [[ -n ${flake-} ]]; then
exit 1 exit 1
fi fi
if [[ ${build_on_remote-n} == "n" ]]; then if [[ ${build_on_remote-n} == "n" ]]; then
if [[ -n ${vm_test-} ]]; then
exec nix build \
--print-out-paths \
--no-link \
-L \
"${nix_options[@]}" \
"${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.installTest"
fi
disko_script=$(nix_build "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript") disko_script=$(nix_build "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript")
nixos_system=$(nix_build "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel") nixos_system=$(nix_build "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
fi fi