Skip to content

Commit 2716126

Browse files
authored
Merge pull request #162 from numtide/vm-test
add --vm-test mode for local vm tests
2 parents 11d3803 + 581053b commit 2716126

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

docs/quickstart.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,23 @@ example uses a local directory on the source machine.
8383
5. On the target machine, make sure you have access as root via ssh by adding
8484
your SSH key to the file `authorized_keys` in the directory `/root/.ssh`
8585

86-
6. You can now run **nixos-anywhere** from the command line as shown below,
86+
6. (Optional) Test your nixos and disko configuration:
87+
88+
The following command will automatically test your nixos configuration and run
89+
disko inside a virtual machine, where
90+
91+
- `<path to configuration>` is the path to the directory or repository
92+
containing `flake.nix` and `disk-config.nix`
93+
94+
- `<configuration name>` must match the name that immediately follows the text
95+
`nixosConfigurations.` in the flake, as indicated by the comment in the
96+
[example](https://github.com/numtide/nixos-anywhere-examples/blob/main/flake.nix)).
97+
98+
```
99+
nix run github:numtide/nixos-anywhere -- --flake <path to configuration>#<configuration name> --vm-test
100+
```
101+
102+
7. You can now run **nixos-anywhere** from the command line as shown below,
87103
where:
88104

89105
- `<path to configuration>` is the path to the directory or repository

src/nixos-anywhere.sh

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Options:
4646
URL of the source Nix store to copy the nixos and disko closure from
4747
* --build-on-remote
4848
build the closure on the remote machine instead of locally and copy-closuring it
49+
* --vm-test
50+
build the system and test the disk configuration inside a VM without installing it to the target.
4951
USAGE
5052
}
5153

@@ -155,7 +157,9 @@ while [[ $# -gt 0 ]]; do
155157
--build-on-remote)
156158
build_on_remote=y
157159
;;
158-
160+
--vm-test)
161+
vm_test=y
162+
;;
159163
*)
160164
if [[ -z ${ssh_connection-} ]]; then
161165
ssh_connection="$1"
@@ -198,17 +202,19 @@ nix_build() {
198202
"$@"
199203
}
200204

201-
if [[ -z ${ssh_connection-} ]]; then
202-
abort "ssh-host must be set"
203-
fi
205+
if [[ -z ${vm_test-} ]]; then
206+
if [[ -z ${ssh_connection-} ]]; then
207+
abort "ssh-host must be set"
208+
fi
204209

205-
# we generate a temporary ssh keypair that we can use during nixos-anywhere
206-
ssh_key_dir=$(mktemp -d)
207-
trap 'rm -rf "$ssh_key_dir"' EXIT
208-
mkdir -p "$ssh_key_dir"
209-
# ssh-copy-id requires this directory
210-
mkdir -p "$HOME/.ssh/"
211-
ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
210+
# we generate a temporary ssh keypair that we can use during nixos-anywhere
211+
ssh_key_dir=$(mktemp -d)
212+
trap 'rm -rf "$ssh_key_dir"' EXIT
213+
mkdir -p "$ssh_key_dir"
214+
# ssh-copy-id requires this directory
215+
mkdir -p "$HOME/.ssh/"
216+
ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
217+
fi
212218

213219
# parse flake nixos-install style syntax, get the system attr
214220
if [[ -n ${flake-} ]]; then
@@ -222,6 +228,14 @@ if [[ -n ${flake-} ]]; then
222228
exit 1
223229
fi
224230
if [[ ${build_on_remote-n} == "n" ]]; then
231+
if [[ -n ${vm_test-} ]]; then
232+
exec nix build \
233+
--print-out-paths \
234+
--no-link \
235+
-L \
236+
"${nix_options[@]}" \
237+
"${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.installTest"
238+
fi
225239
disko_script=$(nix_build "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.diskoScript")
226240
nixos_system=$(nix_build "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
227241
fi

0 commit comments

Comments
 (0)