modules: Add a gaming virtualisation module

This commit is contained in:
PapaTutuWawa 2021-08-24 12:21:49 +02:00
parent 0f6542e5a5
commit de389523d7
4 changed files with 177 additions and 130 deletions

View File

@ -7,6 +7,7 @@
../modules/fonts.nix ../modules/fonts.nix
../modules/users/alexander.nix ../modules/users/alexander.nix
../modules/programs/zsh ../modules/programs/git ../modules/programs/mpv ../modules/programs/tmux ../modules/programs/emacs ../modules/programs/firefox ../modules/programs/gnome-terminal ../modules/programs/gnome ../modules/programs/i18n.nix ../modules/programs/gamemode ../modules/programs/nonvm ../modules/programs/zsh ../modules/programs/git ../modules/programs/mpv ../modules/programs/tmux ../modules/programs/emacs ../modules/programs/firefox ../modules/programs/gnome-terminal ../modules/programs/gnome ../modules/programs/i18n.nix ../modules/programs/gamemode ../modules/programs/nonvm
../modules/virtualisation/gaming.nix
../modules/programs/distributed-build/builder.nix ../modules/programs/distributed-build/builder.nix
../modules/programs/music ../modules/programs/music
]; ];
@ -19,7 +20,10 @@
})) }))
# Steam overlay # Steam overlay
(import ../overlays/steam.nix pkgs) (import ../overlays/steam.nix {
pkgs = pkgs;
lib = lib;
})
# Simple overrides # Simple overrides
(final: prev: { (final: prev: {
@ -64,7 +68,7 @@
emacsPgtkGcc emacsPgtkGcc
unstable.firmwareLinuxNonfree unstable.firmwareLinuxNonfree
gajim gajim
libvirt virt-manager qemu scream virt-manager unstable.qemu scream
gamemode # Custom package gamemode # Custom package
key-mapper # Custom package key-mapper # Custom package
replaysorcery # Custom package replaysorcery # Custom package
@ -77,72 +81,6 @@
# Prevent us from having to always type it out # Prevent us from having to always type it out
NIXOS_CONFIG = "/home/${config.system.singleUser}/Development/Personal/nixos-config/hosts/miku.nix"; NIXOS_CONFIG = "/home/${config.system.singleUser}/Development/Personal/nixos-config/hosts/miku.nix";
}; };
etc = {
"evdev-proxy/config.toml".source = pkgs.writeText "config.toml" ''
log_level = "INFO"
[[device]]
[device.Simple]
name = "EvdevProxyMouse"
vendor = 0x1337
model = 0x1337
class = "Mouse"
[[device.Simple.selector]]
USBIDClass = {vendor=0x046d, model=0xc531, class="Mouse"}
[[device.Simple.selector]]
USBIDClass = {vendor=0x046d, model=0xc07c, class="Mouse"}
[[device]]
[device.Simple]
name = "EvdevProxyTartarus"
vendor = 0x1337
model = 0x1338
class = "Keyboard"
# This is to give a deterministic /dev/input/by-id/ path to
# the mapped version of the Tartarus evdev node.
# (Useful for qemu)
[[device.Simple.selector]]
EVDEVClass = {phys="\"key-mapper\""}
'';
"libvirt/hooks/qemu".source = let
vfio-isolate-state = "/tmp/vfio-isolate-state";
in pkgs.writeScript "qemu" ''
#!${pkgs.stdenv.shell}
guest=$1
action=$2
phase=$3
extra=$4
if [[ "$guest" = "win10" ]]; then
case "$action" in
prepare)
# Only do this while in preparation
[[ ! "$phase" = "prepare" ]] && exit 0
sudo -u alexander systemctl --user start evdev-proxy.service
sudo -u alexander systemctl --user start scream.service
sleep 2
${pkgs.vfio-isolate}/bin/vfio-isolate \
-u ${vfio-isolate-state} \
cpu-governor performance "$GUEST_CORES" \
cpuset-create --cpus "$GUEST_CORES" /guest.slice \
cpuset-create --cpus C0,4 /host.slice \
move-tasks / /host.slice \
irq-affinity mask "$GUEST_CORES"
;;
stopped)
# Only run when the VM is fully stopped
[[ ! "$phase" = "end" ]] && exit 0
sudo -u alexander systemctl --user stop evdev-proxy.service
sudo -u alexander systemctl --user stop scream.service
${pkgs.vfio-isolate}/bin/vfio-isolate \
restore ${vfio-isolate-state}
esac
fi
'';
};
}; };
networking = { networking = {
@ -209,36 +147,9 @@
sshd.enable = true; sshd.enable = true;
}; };
virtualisation.libvirtd = {
enable = true;
qemuOvmf = true;
#qemuRunAsRoot = false;
qemuPackage = pkgs.unstable.qemu;
qemuVerbatimConfig = ''
seccomp_sandbox = 0
cgroup_device_acl = [
"/dev/null", "/dev/full", "/dev/zero",
"/dev/random", "/dev/urandom",
"/dev/ptmx", "/dev/kvm", "/dev/kqemu",
"/dev/rtc","/dev/hpet",
"/dev/input/by-id/usb-Logitech_USB_Receiver-if02-event-mouse",
"/dev/input/by-id/usb-Razer_Razer_Tartarus_V2-event-kbd",
"/dev/input/by-id/virtual-event-EvdevProxyMouse",
"/dev/input/by-id/virtual-event-EvdevProxyTartarus",
"/dev/input/by-id/usb-Razer_Razer_BlackWidow_Ultimate-event-kbd"
]
'';
};
# NOTE: Workaround for libvirt's SYSCONFDIR being set to /var/lib
# (See https://github.com/NixOS/nixpkgs/issues/51152#issuecomment-899374407)
system.activationScripts.libvirt-hooks.text = ''
ln -Tfs /etc/libvirt/hooks /var/lib/libvirt/hooks
'';
systemd = { systemd = {
services = { services = {
NetworkManager-wait-online.enable = false; NetworkManager-wait-online.enable = false;
libvirtd.path = with pkgs; [ vfio-isolate systemd ];
}; };
user.services = { user.services = {
@ -281,15 +192,6 @@
Restart = "always"; Restart = "always";
}; };
}; };
evdev-proxy = {
description = "Creates virtual device to proxy evdev devices events";
#wantedBy = [ "default.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.evdev-proxy}/bin/evdev-proxy";
Restart = "always";
};
};
# TODO: Remove once in stable or I decide to use unstable # TODO: Remove once in stable or I decide to use unstable
gamemode = { gamemode = {
description = "A daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS."; description = "A daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS.";
@ -302,7 +204,6 @@
]); ]);
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
#ExecStart = "${pkgs.gamemode}/bin/gamemoded";
ExecStart = "${config.security.wrapperDir}/gamemoded"; ExecStart = "${config.security.wrapperDir}/gamemoded";
Restart = "always"; Restart = "always";
}; };

View File

@ -0,0 +1,36 @@
[general]
reaper_freq=5
desiredgov=performance
; TODO: Maybe set this
;defaultgov=powersave
; The iGPU desired governor is used when the integrated GPU is under heavy load
igpu_desiredgov=powersave
igpu_power_threshold=0.3
softrealtime=on;
renice=0
ioprio=0
inhibit_screensaver=1
[filter]
; If "whitelist" entry has a value(s)
; gamemode will reject anything not in the whitelist
;whitelist=RiseOfTheTombRaide
; Gamemode will always reject anything in the blacklist
;blacklist=HalfLife3
; glxgears
[gpu]
[supervisor]
[custom]
start=systemctl --user start replaysorcery-kms
systemctl --user start replaysorcery
end=systemctl --user stop replaysorcery-kms
systemctl --user stop replaysorcery
script_timeout=10

View File

@ -0,0 +1,109 @@
{ pkgs, ... }:
{
environment.etc = {
"evdev-proxy/config.toml".source = pkgs.writeText "config.toml" ''
log_level = "INFO"
[[device]]
[device.Simple]
name = "EvdevProxyMouse"
vendor = 0x1337
model = 0x1337
class = "Mouse"
[[device.Simple.selector]]
USBIDClass = {vendor=0x046d, model=0xc531, class="Mouse"}
[[device.Simple.selector]]
USBIDClass = {vendor=0x046d, model=0xc07c, class="Mouse"}
[[device]]
[device.Simple]
name = "EvdevProxyTartarus"
vendor = 0x1337
model = 0x1338
class = "Keyboard"
# This is to give a deterministic /dev/input/by-id/ path to
# the mapped version of the Tartarus evdev node.
# (Useful for qemu)
[[device.Simple.selector]]
EVDEVClass = {phys="\"key-mapper\""}
'';
"libvirt/hooks/qemu".source = let
vfio-isolate-state = "/tmp/vfio-isolate-state";
in pkgs.writeScript "qemu" ''
#!${pkgs.stdenv.shell}
guest=$1
action=$2
phase=$3
extra=$4
if [[ "$guest" = "win10" ]]; then
case "$action" in
prepare)
# Only do this while in preparation
[[ ! "$phase" = "prepare" ]] && exit 0
sudo -u alexander systemctl --user start evdev-proxy.service
sudo -u alexander systemctl --user start scream.service
sleep 2
${pkgs.vfio-isolate}/bin/vfio-isolate \
-u ${vfio-isolate-state} \
cpu-governor performance "$GUEST_CORES" \
cpuset-create --cpus "$GUEST_CORES" /guest.slice \
cpuset-create --cpus C0,4 /host.slice \
move-tasks / /host.slice \
irq-affinity mask "$GUEST_CORES"
;;
stopped)
# Only run when the VM is fully stopped
[[ ! "$phase" = "end" ]] && exit 0
sudo -u alexander systemctl --user stop evdev-proxy.service
sudo -u alexander systemctl --user stop scream.service
${pkgs.vfio-isolate}/bin/vfio-isolate \
restore ${vfio-isolate-state}
esac
fi
'';
};
virtualisation.libvirtd = {
enable = true;
#qemuRunAsRoot = false;
qemuOvmf = true;
qemuPackage = pkgs.unstable.qemu;
qemuVerbatimConfig = ''
seccomp_sandbox = 0
cgroup_device_acl = [
"/dev/null", "/dev/full", "/dev/zero",
"/dev/random", "/dev/urandom",
"/dev/ptmx", "/dev/kvm", "/dev/kqemu",
"/dev/rtc","/dev/hpet",
"/dev/input/by-id/usb-Logitech_USB_Receiver-if02-event-mouse",
"/dev/input/by-id/usb-Razer_Razer_Tartarus_V2-event-kbd",
"/dev/input/by-id/virtual-event-EvdevProxyMouse",
"/dev/input/by-id/virtual-event-EvdevProxyTartarus",
"/dev/input/by-id/usb-Razer_Razer_BlackWidow_Ultimate-event-kbd"
]
'';
};
# NOTE: Workaround for libvirt's SYSCONFDIR being set to /var/lib
# (See https://github.com/NixOS/nixpkgs/issues/51152#issuecomment-899374407)
system.activationScripts.libvirt-hooks.text = ''
ln -Tfs /etc/libvirt/hooks /var/lib/libvirt/hooks
'';
systemd = {
services.libvirtd.path = with pkgs; [ vfio-isolate systemd bash ];
user.services.evdev-proxy = {
description = "Creates virtual device to proxy evdev devices events";
#wantedBy = [ "default.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.evdev-proxy}/bin/evdev-proxy";
Restart = "always";
};
};
};
}

View File

@ -1,4 +1,5 @@
pkgs: final: prev: { pkgs, lib }:
final: prev:
# TODO: Replace with the commented out version below # TODO: Replace with the commented out version below
let let
@ -12,28 +13,28 @@ in {
steam = steamPackages.steam-fhsenv.override { steam = steamPackages.steam-fhsenv.override {
extraPkgs = pkgsx: with pkgsx; [ gamemode ]; extraPkgs = pkgsx: with pkgsx; [ gamemode ];
}; };
}
#(final: prev: #let
# let # # NOTE: This is a really messy solution
# # NOTE: This is a really messy solution # buildFHSUserEnvBubblewrap = prev.callPackage ../packages/build-support/build-fhs-userenv-bubblewrap {};
# buildFHSUserEnvBubblewrap = prev.callPackage ../packages/build-support/build-fhs-userenv-bubblewrap {}; #in {
# in { # steamPackages = prev.steamPackages.override {
# steamPackages = prev.steamPackages.override { # buildFHSUserEnv = buildFHSUserEnvBubblewrap;
# buildFHSUserEnv = buildFHSUserEnvBubblewrap; # # Include gamemode in the FHS
# # Include gamemode in the FHS # #extraPkgs = (pkgs: with pkgs; [ gamemode ]);
# #extraPkgs = (pkgs: with pkgs; [ gamemode ]); # };
# }; #
# steam = prev.steamPackages.steam-fhsenv.overrideAttrs (old: {
# steam = prev.steamPackages.steam-fhsenv.overrideAttrs (old: { # specifyHomeDirs = true;
# specifyHomeDirs = true; # chdirTo = "/home/$USER";
# chdirTo = "/home/$USER"; # });
# }); # #steam = final.steamPackages.steam-fhsenv.override {
# #steam = final.steamPackages.steam-fhsenv.override { # # extraPkgs = pkgs: with pkgs; [ gamemode ];
# # extraPkgs = pkgs: with pkgs; [ gamemode ]; # #};
# #}; # steam-run = final.steam.run;
# steam-run = final.steam.run; # steam-run-native = (final.steam.override {
# steam-run-native = (final.steam.override { # nativeOnly = true;
# nativeOnly = true; # });
# }); # steamcmd = final.steamPackages.steamcmd;
# steamcmd = final.steamPackages.steamcmd; #}
# })