Difference between revisions of "NixOS:nixos-rebuild build-vm"

From NixOS Wiki
Jump to: navigation, search
m (update 22.05)
m (rollback unauthorized mass edits)
Tag: Rollback
 
(9 intermediate revisions by 5 users not shown)
Line 3: Line 3:
 
<code>
 
<code>
 
nixos-rebuild build-vm
 
nixos-rebuild build-vm
 +
 +
nixos-rebuild build-vm -I nixos-config=./configuration.nix -I nix_path='<nixpkgs/nixos>' --max-jobs 4  --show-trace # e.g. to specify the environment variables / cores used
 
</code>
 
</code>
 +
 +
You can also use <code>build-vm-with-bootloader</code>. From the man page: this boots using the regular boot loader of your configuration rather than booting directly into the kernel and initial ramdisk of the system.
  
 
You will not be able to login to this virtual machine, as the passwords are not carried over to the virtual machine you build.
 
You will not be able to login to this virtual machine, as the passwords are not carried over to the virtual machine you build.
Line 20: Line 24:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
to your /etc/nixos/configuration.nix
+
to your <code>/etc/nixos/configuration.nix</code>
  
 
you should now be able to login and test your system with this user and password.
 
you should now be able to login and test your system with this user and password.
Line 26: Line 30:
 
https://discourse.nixos.org/t/default-login-and-password-for-nixos/4683/2
 
https://discourse.nixos.org/t/default-login-and-password-for-nixos/4683/2
  
By default, the virtual machine is configured to have 1 CPU and 1024MiB memory. It may be too small for testing with desktop environment enabled inside. You can set options <code>virtualisation.cores</code> and <code>virtualisation.memorySize</code> to enlarge the CPU cores and memory size for the virtual machine. Note that due to [https://github.com/NixOS/nixpkgs/issues/59219 issue 59219], you need to import an extra module in order to use these options.
+
By default, the virtual machine is configured to have 1 CPU and 1024MiB memory. It may be too small for testing with desktop environment enabled inside. You can set options <code>virtualisation.vmVariant.virtualisation.cores</code> and <code>virtualisation.vmVariant.virtualisation.memorySize</code> to enlarge the CPU cores and memory size for the virtual machine.  
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
Line 36: Line 40:
 
     cores = 3;         
 
     cores = 3;         
 
   };
 
   };
};
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
or
+
Use <code>virtualisation.vmVariantWithBootLoader</code> option if you used `build-vm-with-bootloader` earlier.
 +
 
 +
== Troubleshooting ==
 +
=== Still can't login after updating configuration ===
 +
When running a virtual machine a file called <code>$hostname.qcow2</code> is created in your current working directory. After changing your <code>/etc/nixos/configuration.nix</code> delete this file, rebuild and then start the new virtual machine. Now you should be able to login.
 +
 
 +
 
 +
 
 +
== Alternatives ==
 +
 
 +
===  Bootable ISO ===
 +
Build it as a [https://nix.dev/tutorials/nixos/building-bootable-iso-image bootable ISO image]:
 +
 
 +
=== VM ===
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
{
+
{  
   imports = [ <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix> ];
+
...
  virtualisation = {
+
   imports = [  
    memorySize = 2048; # Use 2048MiB memory.
+
    <nixos/nixos/modules/virtualisation/virtualbox-image.nix> ]
    cores = 4;        # Simulate 4 cores.
+
...
  };
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
<code>
 +
nix build -f '<nixpkgs/nixos>' -I nixos-config=./configuration.nix config.system.build.virtualBoxOVA
 +
</code>
 +
[https://discourse.nixos.org/t/nixos-rebuild-build-vm-not-portable-across-linux-distributions/28564/4 Source]

Latest revision as of 11:03, 6 April 2024

A virtual machine can be created , it will use your /etc/nixos/configuration.nix to make a 'clone' of your system. Useful for testing new configurations.

nixos-rebuild build-vm

nixos-rebuild build-vm -I nixos-config=./configuration.nix -I nix_path='<nixpkgs/nixos>' --max-jobs 4 --show-trace # e.g. to specify the environment variables / cores used

You can also use build-vm-with-bootloader. From the man page: this boots using the regular boot loader of your configuration rather than booting directly into the kernel and initial ramdisk of the system.

You will not be able to login to this virtual machine, as the passwords are not carried over to the virtual machine you build.

You should have user nixosvmtest (isSystemUser or isNormalUser)

users.users.nixosvmtest.isSystemUser = true ;
users.users.nixosvmtest.initialPassword = "test";

If you have a user called nixosvmtest for example, you can add

users.users.nixosvmtest.group = "nixosvmtest";
users.groups.nixosvmtest = {};

to your /etc/nixos/configuration.nix

you should now be able to login and test your system with this user and password.

https://discourse.nixos.org/t/default-login-and-password-for-nixos/4683/2

By default, the virtual machine is configured to have 1 CPU and 1024MiB memory. It may be too small for testing with desktop environment enabled inside. You can set options virtualisation.vmVariant.virtualisation.cores and virtualisation.vmVariant.virtualisation.memorySize to enlarge the CPU cores and memory size for the virtual machine.

{
virtualisation.vmVariant = {
  # following configuration is added only when building VM with build-vm
  virtualisation = {
    memorySize =  2048; # Use 2048MiB memory.
    cores = 3;         
  };
}

Use virtualisation.vmVariantWithBootLoader option if you used `build-vm-with-bootloader` earlier.

Troubleshooting

Still can't login after updating configuration

When running a virtual machine a file called $hostname.qcow2 is created in your current working directory. After changing your /etc/nixos/configuration.nix delete this file, rebuild and then start the new virtual machine. Now you should be able to login.


Alternatives

Bootable ISO

Build it as a bootable ISO image:

VM

{ 
...
  imports = [ 
    <nixos/nixos/modules/virtualisation/virtualbox-image.nix> ]
...

nix build -f '<nixpkgs/nixos>' -I nixos-config=./configuration.nix config.system.build.virtualBoxOVA Source