From 1c7d2e04e6973e935f6ce314db30af174609cc0f Mon Sep 17 00:00:00 2001 From: Kira Bruneau Date: Mon, 17 May 2021 20:31:05 -0400 Subject: [PATCH 1/2] gamemode: init at 1.6.1 --- pkgs/tools/games/gamemode/default.nix | 105 ++++++++++++++++++ .../gamemode/preload-nix-workaround.patch | 12 ++ pkgs/top-level/all-packages.nix | 4 + 3 files changed, 121 insertions(+) create mode 100644 pkgs/tools/games/gamemode/default.nix create mode 100644 pkgs/tools/games/gamemode/preload-nix-workaround.patch diff --git a/pkgs/tools/games/gamemode/default.nix b/pkgs/tools/games/gamemode/default.nix new file mode 100644 index 000000000000..d5e0ce2be78f --- /dev/null +++ b/pkgs/tools/games/gamemode/default.nix @@ -0,0 +1,105 @@ +{ lib +, stdenv +, fetchFromGitHub +, fetchpatch +, libgamemode32 +, meson +, ninja +, pkg-config +, dbus +, inih +, systemd +, appstream +}: + +stdenv.mkDerivation rec { + pname = "gamemode"; + version = "1.6.1"; + + src = fetchFromGitHub { + owner = "FeralInteractive"; + repo = pname; + rev = version; + fetchSubmodules = true; + sha256 = "sha256-P00OnZiPZyxBu9zuG+3JNorXHBhJZy+cKPjX+duZrJ0="; + }; + + outputs = [ "out" "dev" "lib" "man" "static" ]; + + patches = [ + # Run executables from PATH instead of /usr/bin + # See https://github.com/FeralInteractive/gamemode/pull/323 + (fetchpatch { + url = "https://github.com/FeralInteractive/gamemode/commit/be44b7091baa33be6dda60392e4c06c2f398ee72.patch"; + sha256 = "TlDUETs4+N3pvrVd0FQGlGmC+6ByhJ2E7gKXa7suBtE="; + }) + + # Fix loading shipped config when using a prefix other than /usr + # See https://github.com/FeralInteractive/gamemode/pull/324 + (fetchpatch { + url = "https://github.com/FeralInteractive/gamemode/commit/b29aa903ce5acc9141cfd3960c98ccb047eca872.patch"; + sha256 = "LwBzBJQ7dfm2mFVSOSPjJP+skgV5N6h77i66L1Sq+ZM="; + }) + + # Add @libraryPath@ template variable to fix loading the PRELOAD library + ./preload-nix-workaround.patch + ]; + + postPatch = '' + substituteInPlace data/gamemoderun \ + --subst-var-by libraryPath ${lib.makeLibraryPath ([ + (placeholder "lib") + ] ++ lib.optionals (stdenv.hostPlatform.system == "x86_64-linux") [ + # Support wrapping 32bit applications on a 64bit linux system + libgamemode32 + ])} + ''; + + nativeBuildInputs = [ + meson + ninja + pkg-config + ]; + + buildInputs = [ + dbus + inih + systemd + ]; + + mesonFlags = [ + # libexec is just a way to package binaries without including them + # in PATH. It doesn't make sense to install them to $lib + # (the default behaviour in the meson hook). + "--libexecdir=${placeholder "out"}/libexec" + + "-Dwith-systemd-user-unit-dir=lib/systemd/user" + ]; + + doCheck = true; + checkInputs = [ + appstream + ]; + + # Move static libraries to $static so $lib only contains dynamic libraries. + postInstall = '' + moveToOutput lib/*.a "$static" + ''; + + # Add $lib/lib to gamemoded & gamemode-simulate-game's rpath since + # they use dlopen to load libgamemode. Can't use makeWrapper since + # it would break the security wrapper in the NixOS module. + postFixup = '' + for bin in "$out/bin/gamemoded" "$out/bin/gamemode-simulate-game"; do + patchelf --set-rpath "$lib/lib:$(patchelf --print-rpath "$bin")" "$bin" + done + ''; + + meta = with lib; { + description = "Optimise Linux system performance on demand"; + homepage = "https://github.com/FeralInteractive/GameMode"; + license = licenses.bsd3; + maintainers = with maintainers; [ kira-bruneau ]; + platforms = platforms.linux; + }; +} diff --git a/pkgs/tools/games/gamemode/preload-nix-workaround.patch b/pkgs/tools/games/gamemode/preload-nix-workaround.patch new file mode 100644 index 000000000000..06989ff984ab --- /dev/null +++ b/pkgs/tools/games/gamemode/preload-nix-workaround.patch @@ -0,0 +1,12 @@ +diff --git a/data/gamemoderun b/data/gamemoderun +index 573b3e4..6f2799e 100755 +--- a/data/gamemoderun ++++ b/data/gamemoderun +@@ -5,5 +5,6 @@ GAMEMODEAUTO_NAME="libgamemodeauto.so.0" + + # ld will find the right path to load the library, including for 32-bit apps. + LD_PRELOAD="${GAMEMODEAUTO_NAME}${LD_PRELOAD:+:$LD_PRELOAD}" ++LD_LIBRARY_PATH="@libraryPath@${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" + +-exec env LD_PRELOAD="${LD_PRELOAD}" $GAMEMODERUNEXEC "$@" ++exec env LD_PRELOAD="${LD_PRELOAD}" LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" $GAMEMODERUNEXEC "$@" diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 6f20edc53ce8..1410aae7e9ef 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -854,6 +854,10 @@ in amidst = callPackage ../tools/games/amidst { }; + gamemode = callPackage ../tools/games/gamemode { + libgamemode32 = pkgsi686Linux.gamemode.lib; + }; + gfshare = callPackage ../tools/security/gfshare { }; gobgp = callPackage ../tools/networking/gobgp { }; From caac437b9b486e4e5271066c9eec049e3b7cb3ce Mon Sep 17 00:00:00 2001 From: Kira Bruneau Date: Mon, 17 May 2021 20:31:45 -0400 Subject: [PATCH 2/2] nixos/gamemode: add module --- nixos/modules/module-list.nix | 1 + nixos/modules/programs/gamemode.nix | 96 +++++++++++++++++++++++++++ pkgs/tools/games/gamemode/default.nix | 1 - 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 nixos/modules/programs/gamemode.nix diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index aa4e2ccc46bc..78554b39730e 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -139,6 +139,7 @@ ./programs/flexoptix-app.nix ./programs/freetds.nix ./programs/fuse.nix + ./programs/gamemode.nix ./programs/geary.nix ./programs/gnome-disks.nix ./programs/gnome-documents.nix diff --git a/nixos/modules/programs/gamemode.nix b/nixos/modules/programs/gamemode.nix new file mode 100644 index 000000000000..03949bf98df6 --- /dev/null +++ b/nixos/modules/programs/gamemode.nix @@ -0,0 +1,96 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.programs.gamemode; + settingsFormat = pkgs.formats.ini { }; + configFile = settingsFormat.generate "gamemode.ini" cfg.settings; +in +{ + options = { + programs.gamemode = { + enable = mkEnableOption "GameMode to optimise system performance on demand"; + + enableRenice = mkEnableOption "CAP_SYS_NICE on gamemoded to support lowering process niceness" // { + default = true; + }; + + settings = mkOption { + type = settingsFormat.type; + default = {}; + description = '' + System-wide configuration for GameMode (/etc/gamemode.ini). + See gamemoded(8) man page for available settings. + ''; + example = literalExample '' + { + general = { + renice = 10; + }; + + # Warning: GPU optimisations have the potential to damage hardware + gpu = { + apply_gpu_optimisations = "accept-responsibility"; + gpu_device = 0; + amd_performance_level = "high"; + }; + + custom = { + start = "''${pkgs.libnotify}/bin/notify-send 'GameMode started'"; + end = "''${pkgs.libnotify}/bin/notify-send 'GameMode ended'"; + }; + } + ''; + }; + }; + }; + + config = mkIf cfg.enable { + environment = { + systemPackages = [ pkgs.gamemode ]; + etc."gamemode.ini".source = configFile; + }; + + security = { + polkit.enable = true; + wrappers = mkIf cfg.enableRenice { + gamemoded = { + source = "${pkgs.gamemode}/bin/gamemoded"; + capabilities = "cap_sys_nice+ep"; + }; + }; + }; + + systemd = { + packages = [ pkgs.gamemode ]; + user.services.gamemoded = { + # The upstream service already defines this, but doesn't get applied. + # See https://github.com/NixOS/nixpkgs/issues/81138 + wantedBy = [ "default.target" ]; + + # Use pkexec from the security wrappers to allow users to + # run libexec/cpugovctl & libexec/gpuclockctl as root with + # the the actions defined in share/polkit-1/actions. + # + # This uses a link farm to make sure other wrapped executables + # aren't included in PATH. + environment.PATH = mkForce (pkgs.linkFarm "pkexec" [ + { + name = "pkexec"; + path = "${config.security.wrapperDir}/pkexec"; + } + ]); + + serviceConfig.ExecStart = mkIf cfg.enableRenice [ + "" # Tell systemd to clear the existing ExecStart list, to prevent appending to it. + "${config.security.wrapperDir}/gamemoded" + ]; + }; + }; + }; + + meta = { + maintainers = with maintainers; [ kira-bruneau ]; + }; +} diff --git a/pkgs/tools/games/gamemode/default.nix b/pkgs/tools/games/gamemode/default.nix index d5e0ce2be78f..e9fdec592204 100644 --- a/pkgs/tools/games/gamemode/default.nix +++ b/pkgs/tools/games/gamemode/default.nix @@ -20,7 +20,6 @@ stdenv.mkDerivation rec { owner = "FeralInteractive"; repo = pname; rev = version; - fetchSubmodules = true; sha256 = "sha256-P00OnZiPZyxBu9zuG+3JNorXHBhJZy+cKPjX+duZrJ0="; };