NixOS 101 Course / Section 3: Your own NixOS service modules
Premium

3.2 Run your NixOS configuration as a VM

Lesson 14 of 28 · 9:43

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Running a NixOS Module in a VM
  2. 1:32 Inspecting the Configuration and VM Modules
  3. 3:58 Port Forwarding, SSH, and VM Setup
  4. 6:41 Graphical vs Terminal VM Mode
  5. 8:14 Testing the Hello Service in the VM
Transcript

Running a NixOS Module in a VM

0:00 So after we just learned how to quickly write a module file like this and use it in your NixOS system directly, let's switch over to running this in a VM for playing around, which is also part of the next exercise. In the next video we'll actually do the exercise. Now let me show you how to write and run a module as a VM. So let's first kind of copy the exercise and go back to our terminal. We can just download everything and then unzip it and then go into the hello folder.

0:36 And what do we have here? So there's a bunch of files and in the default.nix, so if we just run nix-build, this is a non-flake exercise. The reason for that is that we're basically running packages.nixos as a function and then build attribute from that to run a VM. And if that was a flake, it would be like five times longer and wouldn't really help so much. But of course you can run it with flakes too by just removing those three lines and adding the 15 flake lines.

1:11 However, the point here is that we can run the packages.nixos function from nixpkgs and give it a list of modules. Here we have two modules and then those modules together evaluated give us a NixOS evaluation. This function is basically the same like nixpkgs.lib.nixosSystem in the flake.

The full transcript comes with the lesson.

About this lesson

Developing modules on the machine you work on is risky. This lesson builds a NixOS configuration into a VM with one command, using the Hello as a Service module from the previous lesson, and reaches it from the host via port forwarding and SSH.

In this video

  • pkgs.nixos and the vm attribute
  • Importing qemu-vm.nix, and the virtualisation options that configure the VM run script rather than the system inside
  • Forwarding ports from host to guest, based on the service's own option
  • Graphical window or plain terminal: switching with one setting
  • Splitting application specific from platform specific modules, and why that pays off for testing

After this lesson

You can try any NixOS configuration in a throwaway VM. Keep these files for the next exercises where we build on top!

Links: