ALPHA: wire is a tool to deploy nixos systems wire.althaea.zone/
1--- 2comment: true 3title: Creating a Virtual Machine 4description: Creating a NixOS virtual machine to use as a deployment target. 5--- 6 7# Creating a Virtual Machine 8 9{{ $frontmatter.description }} 10 11## Creating a `vm.nix` 12 13For this step, you'll need your ssh public key, which you can obtain from 14`ssh-add -L`. 15 16Open a text editor and edit `vm.nix`. Place in it this basic NixOS 17virtual machine configuration, which enables openssh and forwards it's 22 port: 18 19```nix:line-numbers [vm.nix] 20let 21 sources = import ./npins; 22in 23{ 24 imports = [ "${sources.nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix" ]; 25 26 networking.hostName = "wire-tutorial"; 27 28 users.users.root = { 29 initialPassword = "root"; 30 openssh.authorizedKeys.keys = [ 31 # I made this a nix syntax error so you're forced to deal with it! 32 <your ssh public-key as a string> 33 ]; 34 }; 35 36 boot = { 37 loader = { 38 systemd-boot.enable = true; 39 efi.canTouchEfiVariables = true; 40 }; 41 42 kernelParams = [ "console=ttyS0" ]; 43 44 boot.growPartition = true; 45 }; 46 47 # enable openssh 48 services = { 49 openssh = { 50 enable = true; 51 settings.PermitRootLogin = "yes"; 52 }; 53 54 getty.autologinUser = "root"; 55 }; 56 57 virtualisation = { 58 graphics = false; 59 useBootLoader = true; 60 61 # use a 5gb disk 62 diskSize = 5 * 1024; 63 64 # grow the filesystem to fit the 5 gb we reserved 65 fileSystems."/".autoResize = true; 66 67 # forward `openssh` port 22 to localhost:2222. 68 forwardPorts = [ 69 { 70 from = "host"; 71 host.port = 2222; 72 guest.port = 22; 73 } 74 ]; 75 }; 76 77 system.stateVersion = "23.11"; 78} 79``` 80 81If you like, you may take a moment to understand each line of this 82configuration. 83 84## Building & Running the virtual machine 85 86Open a separate Terminal tab/window/instance, ensuring you enter the development 87shell with `nix-shell`. 88Then, build the virtual machine with a bootloader, 89taking our `vm.nix` as the nixos configuration. 90 91```sh 92$ nix-shell 93[nix-shell]$ nix-build '<nixpkgs/nixos>' -A vmWithBootLoader -I nixos-config=./vm.nix 94``` 95 96::: tip HELP 97 98If you got an error such as 99 100``` 101error: The option `...' in `...' is already declared in `...'. 102``` 103 104make sure you ran the above command in the `nix-shell`! 105 106::: 107 108Building the virtual machine can take some time, but once it completes, start it 109by running: 110 111```sh 112[nix-shell]$ ./result/bin/run-wire-tutorial-vm 113``` 114 115You will see boot-up logs fly across the screen and eventually you will be placed 116into shell inside the virtual machine. 117 118```sh [Virtual Machine] 119running activation script... 120setting up /etc... 121 122Welcome to NixOS 25.11 (Xantusia)! 123 124[ OK ] Created slice Slice /system/getty. 125[ OK ] Created slice Slice /system/modprobe. 126... 127<<< Welcome to NixOS 25.11pre861972.88cef159e47c (x86_64) - hvc0 >>> 128 129Run 'nixos-help' for the NixOS manual. 130 131wire-tutorial login: root (automatic login) 132 133[root@wire-tutorial:~]# 134 135``` 136 137::: details 138Further details on how the above commands work can be found at 139[nix.dev](https://nix.dev/tutorials/nixos/nixos-configuration-on-vm.html#creating-a-qemu-based-virtual-machine-from-a-nixos-configuration) 140::: 141 142## Summary 143 144Congratulations, you created a virtual machine in your terminal. 145We'll be deploying to this virtual machine, so keep the 146terminal instance open. 147 148::: info 149From now on, commands ran inside the virtual machine will be lead with the 150following prompt: 151 152```sh [Virtual Machine] 153[root@wire-tutorial:~]# 154 155``` 156 157::: 158 159::: tip 160If you ever want to quit the virtual machine, run the command `poweroff`. 161:::