Skip to content

Commit

Permalink
integrate new example into microvms-host.nix example
Browse files Browse the repository at this point in the history
  • Loading branch information
RobWalt committed Mar 21, 2024
1 parent 3012167 commit e76628c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 175 deletions.
145 changes: 0 additions & 145 deletions examples/host-static-guest.nix

This file was deleted.

72 changes: 43 additions & 29 deletions examples/microvms-host.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ nixpkgs.lib.nixosSystem {
# this runs as a MicroVM that nests MicroVMs
self.nixosModules.microvm

({ config, lib, pkgs, ... }: {
({ config, lib, pkgs, ... }:
let
hypervisors-with-mac = builtins.listToAttrs(
map (hypervisor:
let
hash = builtins.hashString "sha256" hypervisor;
c = off: builtins.substring off 2 hash;
mac = "${builtins.substring 0 1 hash}2:${c 2}:${c 4}:${c 6}:${c 8}:${c 10}";
in
{ name = hypervisor; value = mac; })
self.lib.hypervisors);
in
{
networking.hostName = "microvms-host";
system.stateVersion = config.system.nixos.version;
users.users.root.password = "";
Expand Down Expand Up @@ -38,38 +50,30 @@ nixpkgs.lib.nixosSystem {
};

# Nested MicroVMs (a *host* option)
microvm.vms = builtins.listToAttrs (
map (hypervisor: {
name = hypervisor;
value = {
config = {
system.stateVersion = config.system.nixos.version;
networking.hostName = "${hypervisor}-microvm";
microvm.vms = builtins.mapAttrs (hypervisor: mac:
{
config = {
system.stateVersion = config.system.nixos.version;
networking.hostName = "${hypervisor}-microvm";

microvm = {
inherit hypervisor;
interfaces = [ {
type = "tap";
id = "vm-${builtins.substring 0 12 hypervisor}";
mac =
let
hash = builtins.hashString "sha256" hypervisor;
c = off: builtins.substring off 2 hash;
in
"${builtins.substring 0 1 hash}2:${c 2}:${c 4}:${c 6}:${c 8}:${c 10}";
} ];
};
# Just use 99-ethernet-default-dhcp.network
systemd.network.enable = true;
microvm = {
inherit hypervisor;
interfaces = [ {
type = "tap";
id = "vm-${builtins.substring 0 12 hypervisor}";
inherit mac;
} ];
};
# Just use 99-ethernet-default-dhcp.network
systemd.network.enable = true;

users.users.root.password = "";
services.openssh = {
enable = true;
settings.PermitRootLogin = "yes";
};
users.users.root.password = "";
services.openssh = {
enable = true;
settings.PermitRootLogin = "yes";
};
};
}) self.lib.hypervisors);
}) hypervisors-with-mac;

systemd.network = {
enable = true;
Expand All @@ -85,6 +89,16 @@ nixpkgs.lib.nixosSystem {
DHCPServer = true;
IPv6SendRA = true;
};
dhcpServerStaticLeases =
[
# Let DHCP assign a statically known address to the qemu vm
{
dhcpServerStaticLeaseConfig = {
MACAddress = hypervisors-with-mac.qemu;
Address = "10.0.13.37";
};
}
];
addresses = [ {
addressConfig.Address = "10.0.0.1/24";
} {
Expand Down
1 change: 0 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
};
in {
vm = nixosToApp ./examples/microvms-host.nix;
host-static-guest = nixosToApp ./examples/host-static-guest.nix;
qemu-vnc = nixosToApp ./examples/qemu-vnc.nix;
graphics = {
type = "app";
Expand Down

0 comments on commit e76628c

Please sign in to comment.