Skip to content

Commit

Permalink
feat(qemu): added auto-generate hooks for win10 vm
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Jun 26, 2024
1 parent 1797047 commit cce2aa8
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 105 deletions.
5 changes: 5 additions & 0 deletions modules/desktop/gaming/minecraft.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ in
prismlauncher
];

modules.hardware.virtualization.stopServices = mkIf (cfg.enableMapRender) [{
name = "win10";
services = [ "render-bluemap-maps.service" ];
}];

# Minecraft development kit
modules.dev.lang.ides = [ "jvm" ];

Expand Down
6 changes: 6 additions & 0 deletions modules/desktop/submodules/bluetooth.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
enable = true;
package = unstable.bluez;
};

modules.hardware.virtualization.stopServices = [{
name = "win10";
services = [ "bluetooth.service" ];
}];

services.blueman.enable = true;
hardware.enableAllFirmware = true;
};
Expand Down
34 changes: 0 additions & 34 deletions modules/hardware/virtualization/hooks/qemu

This file was deleted.

This file was deleted.

This file was deleted.

22 changes: 22 additions & 0 deletions modules/hardware/virtualization/qemu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -e

GUEST_NAME="$1"
HOOK_NAME="$2"
STATE_NAME="$3"

BASEDIR=$(dirname "$0")

HOOKPATH="$BASEDIR/qemu.d/$GUEST_NAME/$HOOK_NAME/$STATE_NAME"

if [ ! -d "$HOOKPATH" ]; then
echo "Hooks directory $HOOKPATH wasn't found"
exit 0
fi

find "$HOOKPATH" -maxdepth 1 -type f -executable | while IFS= read -r file; do
if [ -n "$file" ]; then
bash -c "$file" "$@"
fi
done
File renamed without changes.
61 changes: 0 additions & 61 deletions modules/hardware/virtualization/select-vagrant-vm.sh

This file was deleted.

File renamed without changes.
74 changes: 71 additions & 3 deletions modules/hardware/virutalization.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,50 @@ with lib.my;
let
cfg = config.modules.hardware.virtualization;
virutalizationDir = mapper.mapDirToAttrs ./virtualization;

vmNames = trivial.pipe cfg.stopServices [
(builtins.map (x: x.name))
lists.unique
];

mkQemuHook = vm: scripts:
let
installQemuScript = strings.optionalString cfg.enableWindowsVM ''
mkdir -p $out/release/end
cp start.sh $out/prepare/begin/start.sh
cp revert.sh $out/release/end/revert.sh
'';

name = "${vm}-stop-services";
scriptFile = pkgs.writeShellScript name (builtins.concatStringsSep "\n" scripts);
in
pkgs.stdenv.mkDerivation {
inherit name;

src = ./virtualization;

installPhase = ''
mkdir -p $out/prepare/begin
cp ${scriptFile} $out/prepare/begin/${name}.sh
'' + installQemuScript;
};

qemuHooks = builtins.listToAttrs
(builtins.map
(name:
{
inherit name;
value = trivial.pipe cfg.stopServices [
(builtins.filter (x: x.name == name))
(builtins.map (x: x.services or [ ]))
lists.flatten
lists.unique
(builtins.map (x: "systemctl stop ${x} || echo 'Failed disable ${x} service' &"))
(mkQemuHook name)
];
})
vmNames);
in
{
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
Expand All @@ -12,6 +56,26 @@ in
modules.hardware.virtualization = {
enable = mkEnableOption "Enable virutalization tools";
enableWindowsVM = mkEnableOption "Enable Windows Gaming VM";
stopServices = mkOption {
description = "Set of services, that should be stopped by KVM before start VM";
type = with types; listOf (submodule {
options = {
name = mkOption {
type = str;
description = "VM name, which should be stop service";
};
services = mkOption {
type = listOf str;
description = "List of services, that should be stopped";
default = [ ];
};
};
});
default = [{
name = "win10";
services = [ ];
}];
};
};
};

Expand All @@ -27,7 +91,7 @@ in
ovmf.enable = true;
runAsRoot = true;
};
hooks.qemu.win10 = mkIf (cfg.enableWindowsVM) ./virtualization/hooks/win10;
hooks.qemu = qemuHooks;
};
};

Expand Down Expand Up @@ -58,15 +122,19 @@ in
};

qemuHookScript = link.mkLink {
src = virutalizationDir.hooks.qemu.source;
src = virutalizationDir.qemu.source;
dest = "/var/lib/libvirt/hooks/qemu";
active = cfg.enableWindowsVM;
};
in vibiosLink + qemuHookScript;
in
vibiosLink + qemuHookScript;

boot = {
kernelParams = [ "intel_iommu=on" "iommu=pt" ];
kernelModules = [ "kvm-intel" "vifo-pci" ];
};

services.openssh.openFirewall = mkForce false;
modules.services.boinc.enable = true;
};
}
5 changes: 5 additions & 0 deletions modules/services/boinc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ in
config = mkIf cfg.enable {
users.users.wittano.extraGroups = [ "boinc" ];

modules.hardware.virtualization.stopServices = [{
name = "win10";
services = [ "boinc.service" ];
}];

services.boinc = {
inherit extraEnvPackages;

Expand Down
5 changes: 5 additions & 0 deletions modules/services/filebot.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ in
config = {
modules.dev.lang.ides = [ "go" ];

modules.hardware.virtualization.stopServices = [{
name = "win10";
services = [ "fielbot.service" ];
}];

services.filebot = {
enable = cfg.enable;
user = "wittano";
Expand Down

0 comments on commit cce2aa8

Please sign in to comment.