From 89ef5c979b0f13f8642376ddcacd1182fbd715c2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 11 Aug 2009 01:35:56 +0000 Subject: [PATCH] * New nixos-rebuild action: "nixos-rebuild build-vm" builds a virtual machine containing a replica (minus the state) of the system configuration. This is mostly useful for testing configuration changes prior to doing an actual "nixos-rebuild switch" (or even "nixos-rebuild test"). The VM can be started as follows: $ nixos-rebuild build-vm $ ./result/bin/run-*-vm which starts a KVM/QEMU instance. Additional QEMU options can be passed through the QEMU_OPTS environment variable (e.g. QEMU_OPTS="-redir tcp:8080::80" to forward a host port to the guest). The fileSystem attribute of the regular system configuration is ignored (using mkOverride), because obviously we can't allow the VM to access the host's block devices. Instead, at startup the VM creates an empty disk image in ./.qcow2 to store the VM's root filesystem. Building a VM in this way is efficient because the VM shares its Nix store with the host (through a CIFS mount). However, because the Nix store of the host is mounted read-only in the guest, you cannot run Nix build actions inside the VM. Therefore the VM can only be reconfigured by re-running "nixos-rebuild build-vm" on the host and restarting the VM. svn path=/nixos/trunk/; revision=16662 --- default.nix | 7 ++++++ lib/eval-config.nix | 11 +++++---- modules/installer/tools/nixos-rebuild.sh | 30 +++++++++++++++++------- modules/services/networking/ssh/sshd.nix | 4 +--- modules/virtualisation/qemu-vm.nix | 8 +++++-- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/default.nix b/default.nix index 3c174ca13e5..f0020c5a1a6 100644 --- a/default.nix +++ b/default.nix @@ -7,6 +7,11 @@ let (import ./lib/eval-config.nix {inherit configuration;}) config optionDeclarations pkgs; + vmConfig = (import ./lib/eval-config.nix { + inherit configuration; + extraModules = [./modules/virtualisation/qemu-vm.nix]; + }).config; + in { @@ -14,6 +19,8 @@ in system = config.system.build.system; + vm = vmConfig.system.build.vm; + # The following are used by nixos-rebuild. nixFallback = pkgs.nixUnstable; manifests = config.installer.manifests; diff --git a/lib/eval-config.nix b/lib/eval-config.nix index 9dccc62e1ec..9d6c5794df1 100644 --- a/lib/eval-config.nix +++ b/lib/eval-config.nix @@ -7,6 +7,7 @@ , nixpkgs ? import ./from-env.nix "NIXPKGS" /etc/nixos/nixpkgs , pkgs ? import nixpkgs {inherit system;} , extraArgs ? {} +, extraModules ? [] }: let extraArgs_ = extraArgs; in @@ -14,10 +15,12 @@ let extraArgs_ = extraArgs; in rec { inherit nixpkgs pkgs; - configComponents = [ - configuration - ./check-config.nix - ] ++ (import ../modules/module-list.nix); + configComponents = + [ configuration + ./check-config.nix + ] + ++ extraModules + ++ (import ../modules/module-list.nix); extraArgs = extraArgs_ // { inherit pkgs; diff --git a/modules/installer/tools/nixos-rebuild.sh b/modules/installer/tools/nixos-rebuild.sh index cd2329416ab..86b7337beac 100644 --- a/modules/installer/tools/nixos-rebuild.sh +++ b/modules/installer/tools/nixos-rebuild.sh @@ -15,12 +15,14 @@ Usage: $0 [OPTIONS...] OPERATION The operation is one of the following: - switch: make the configuration the boot default and activate now - boot: make the configuration the boot default - test: activate the configuration, but don't make it the boot default - build: build the configuration, but don't make it the default or - activate it - dry-run: just show what store paths would be built/downloaded + switch: make the configuration the boot default and activate now + boot: make the configuration the boot default + test: activate the configuration, but don't make it the boot default + build: build the configuration, but don't make it the default or + activate it + build-vm: build a virtual machine containing the configuration + (useful for testing) + dry-run: just show what store paths would be built/downloaded Options: @@ -54,7 +56,8 @@ while test "$#" -gt 0; do i="$1"; shift 1 if test "$i" = "--help"; then showSyntax - elif test "$i" = switch -o "$i" = boot -o "$i" = test -o "$i" = build -o "$i" = dry-run; then + elif test "$i" = switch -o "$i" = boot -o "$i" = test -o "$i" = build \ + -o "$i" = dry-run -o "$i" = build-vm; then action="$i" elif test "$i" = --install-grub; then export NIXOS_INSTALL_GRUB=1 @@ -129,6 +132,9 @@ if test "$action" = switch -o "$action" = boot; then elif test "$action" = test -o "$action" = build -o "$action" = dry-run; then nix-build $NIXOS -A system -K -k $extraBuildFlags pathToConfig=./result +elif test "$action" = build-vm; then + nix-build $NIXOS -A vm -K -k $extraBuildFlags + pathToConfig=./result else showSyntax fi @@ -141,7 +147,7 @@ if test "$action" = switch -o "$action" = boot -o "$action" = test; then fi -if test "$action" = "test"; then +if test "$action" = test; then cat >&2 <&2 <