about summary refs log tree commit diff
path: root/nix/modules
diff options
context:
space:
mode:
Diffstat (limited to 'nix/modules')
-rw-r--r--nix/modules/r2wars-web/default.nix1
-rw-r--r--nix/modules/vm/default.nix78
-rw-r--r--nix/modules/x86_64-linux.nix (renamed from nix/modules/default.nix)2
3 files changed, 78 insertions, 3 deletions
diff --git a/nix/modules/r2wars-web/default.nix b/nix/modules/r2wars-web/default.nix
index de3f105..509824e 100644
--- a/nix/modules/r2wars-web/default.nix
+++ b/nix/modules/r2wars-web/default.nix
@@ -3,7 +3,6 @@
 let
   cfg = config.services.emile.r2wars-web;
 in with lib; {
-
   options.services.emile.r2wars-web = {
     enable = mkEnableOption "Enable r2wars-web";
 
diff --git a/nix/modules/vm/default.nix b/nix/modules/vm/default.nix
new file mode 100644
index 0000000..0f65765
--- /dev/null
+++ b/nix/modules/vm/default.nix
@@ -0,0 +1,78 @@
+{ config, lib, pkgs, ... }: 
+
+let
+  cfg = config.services.emile.r2wars-web;
+in with lib; {
+  options.services.emile.r2wars-web = {
+    enable = mkEnableOption "Enable r2wars-web";
+
+    # ip and port to listen on
+    guest = mkOption {
+      type = types.str;
+      default = "vmnameone";
+      example = "vmnameone";
+      description = "The name of the vm";
+    };
+  };
+
+  config = mkIf cfg.enable {
+    systemd.services = lib.mapAttrs' (name: guest: lib.nameValuePair "libvirtd-guest-${name}" {
+      after = [ "libvirtd.service" ];
+      requires = [ "libvirtd.service" ];
+      wantedBy = [ "multi-user.target" ];
+      serviceConfig = {
+        Type = "oneshot";
+        RemainAfterExit = "yes";
+      };
+      script =
+        let
+          xml = pkgs.writeText "libvirt-guest-${name}.xml"
+            ''
+              <domain type="kvm">
+                <name>${name}</name>
+                <uuid>UUID</uuid>
+                <os>
+                  <type>hvm</type>
+                </os>
+                <memory unit="GiB">${guest.memory}</memory>
+                <devices>
+                  <disk type="volume">
+                    <source volume="guest-${name}"/>
+                    <target dev="vda" bus="virtio"/>
+                  </disk>
+                  <graphics type="spice" autoport="yes"/>
+                  <input type="keyboard" bus="usb"/>
+                  <interface type="direct">
+                    <source dev="${hostNic}" mode="bridge"/>
+                    <mac address="${guest.mac}"/>
+                    <model type="virtio"/>
+                  </interface>
+                </devices>
+                <features>
+                  <acpi/>
+                </features>
+              </domain>
+            '';
+        in
+          ''
+            uuid="$(${pkgs.libvirt}/bin/virsh domuuid '${name}' || true)"
+            ${pkgs.libvirt}/bin/virsh define <(sed "s/UUID/$uuid/" '${xml}')
+            ${pkgs.libvirt}/bin/virsh start '${name}'
+          '';
+      preStop =
+        ''
+          ${pkgs.libvirt}/bin/virsh shutdown '${name}'
+          let "timeout = $(date +%s) + 10"
+          while [ "$(${pkgs.libvirt}/bin/virsh list --name | grep --count '^${name}$')" -gt 0 ]; do
+            if [ "$(date +%s)" -ge "$timeout" ]; then
+              # Meh, we warned it...
+              ${pkgs.libvirt}/bin/virsh destroy '${name}'
+            else
+              # The machine is still running, let's give it some time to shut down
+              sleep 0.5
+            fi
+          done
+        '';
+    }) guests;
+  };
+}
diff --git a/nix/modules/default.nix b/nix/modules/x86_64-linux.nix
index 6e6faae..9f81e10 100644
--- a/nix/modules/default.nix
+++ b/nix/modules/x86_64-linux.nix
@@ -1,5 +1,3 @@
-{ ... }:
-
 {
   imports = [
     ./ports