astro / microvm.nix

NixOS MicroVMs
https://astro.github.io/microvm.nix/
MIT License
1.45k stars 103 forks source link

Basic example for flake based NixOS configurations #68

Closed seandheath closed 1 year ago

seandheath commented 1 year ago

I can't seem to figure out the right order of operations to declaratively launch VMs from a flake based NixOS configuration. My configuration is here:

https://github.com/seandheath/nixos/blob/main/flake.nix

I know I'm missing something fundamental about how flakes work or how this project works, but my goal is to be able to put a full "configuration.nix" style file on it's own (currently ./modules/microvm.nix) that has the full system config for just the VM inside it:

https://github.com/seandheath/nixos/blob/main/modules/microvm.nix

I don't understand the delineation between where to apply the "host" configuration options and where the "vm" configuration file goes. Any help is appreciated.

astro commented 1 year ago

Which of the hosts in your flake.nix is going to be a microvm? So far it looks like physical workstations/servers. Each microvm gets their own entry in nixosConfigurations together with a nixpkgs.lib.nixosSystem invocation.

You don't have to put all your systems into one single flake.

seandheath commented 1 year ago

None of the hosts in the flake.nix are VMs, they would be the hosts. I apologize, this might be basic flake usage but I'm still trying to figure it out.

My goal is to have a bunch of VMs that wrap services like Nextcloud and are run on my "hydrogen" server. They would just boot up and run the various services in VMs and use port forwarding for access.

What is the "right" way to build that architecture?

astro commented 1 year ago

None of the hosts in the flake.nix are VMs, they would be the hosts.

In hosts import the microvm.nixosModules.host module. It provides systemd service templates for the microvms and the microvm command. Use that to create and update your MicroVM independent of nixos-rebuild of the host. With microvm -f you can specify which flake to use.

My goal is to have a bunch of VMs that wrap services like Nextcloud and are run on my "hydrogen" server. They would just boot up and run the various services in VMs and use port forwarding for access.

That's the main use-case :-)

What is the "right" way to build that architecture?

The right way is going to be what works well for you.

Unless names clash you may well run MicroVMs from different flakes.

seandheath commented 1 year ago

Am I on the right track with this basic flake:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    microvm.url = "github:astro/microvm.nix";
  };
  outputs = { self, nixpkgs, microvm, ... }: {
    nixosConfigurations.host = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        microvm.nixosModules.host
        {
          # Host configuration goes here
          networking.hostName = "host";
          users.users.root.password = "";
          microvm.vms.guest-microvm = {
            flake = self;
          };
          microvm.autostart = [
            "guest"
          ];
        }
      ];
    };
    nixosConfigurations.guest = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        microvm.nixosModules.microvm
        {
          # Guest configuration goes here
          networking.hostName = "guest";
          microvm.forwardPorts = [{
            host.port = 2222;
            guest.port = 22;
          }]
          networking.firewall.allowedTCPPorts = [ 22 ];
          services.openssh = {
            enable = true;
            permitRootLogin = "yes";
          };
        }
      ];
    };
  };
}
astro commented 1 year ago

Yes