1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.programs.gamemode;
5 settingsFormat = pkgs.formats.ini { };
6 configFile = settingsFormat.generate "gamemode.ini" cfg.settings;
7in
8{
9 options = {
10 programs.gamemode = {
11 enable = lib.mkEnableOption "GameMode to optimise system performance on demand";
12
13 enableRenice = lib.mkEnableOption "CAP_SYS_NICE on gamemoded to support lowering process niceness" // {
14 default = true;
15 };
16
17 settings = lib.mkOption {
18 type = settingsFormat.type;
19 default = { };
20 description = ''
21 System-wide configuration for GameMode (/etc/gamemode.ini).
22 See gamemoded(8) man page for available settings.
23 '';
24 example = lib.literalExpression ''
25 {
26 general = {
27 renice = 10;
28 };
29
30 # Warning: GPU optimisations have the potential to damage hardware
31 gpu = {
32 apply_gpu_optimisations = "accept-responsibility";
33 gpu_device = 0;
34 amd_performance_level = "high";
35 };
36
37 custom = {
38 start = "''${pkgs.libnotify}/bin/notify-send 'GameMode started'";
39 end = "''${pkgs.libnotify}/bin/notify-send 'GameMode ended'";
40 };
41 }
42 '';
43 };
44 };
45 };
46
47 config = lib.mkIf cfg.enable {
48 environment = {
49 systemPackages = [ pkgs.gamemode ];
50 etc."gamemode.ini".source = configFile;
51 };
52
53 security = {
54 polkit.enable = true;
55 wrappers = lib.mkIf cfg.enableRenice {
56 gamemoded = {
57 owner = "root";
58 group = "root";
59 source = "${pkgs.gamemode}/bin/gamemoded";
60 capabilities = "cap_sys_nice+ep";
61 };
62 };
63 };
64
65 systemd = {
66 packages = [ pkgs.gamemode ];
67 user.services.gamemoded = {
68 # The upstream service already defines this, but doesn't get applied.
69 # See https://github.com/NixOS/nixpkgs/issues/81138
70 wantedBy = [ "default.target" ];
71
72 # Use pkexec from the security wrappers to allow users to
73 # run libexec/cpugovctl & libexec/gpuclockctl as root with
74 # the the actions defined in share/polkit-1/actions.
75 #
76 # This uses a link farm to make sure other wrapped executables
77 # aren't included in PATH.
78 environment.PATH = lib.mkForce (pkgs.linkFarm "pkexec" [
79 {
80 name = "pkexec";
81 path = "${config.security.wrapperDir}/pkexec";
82 }
83 ]);
84
85 serviceConfig.ExecStart = lib.mkIf cfg.enableRenice [
86 "" # Tell systemd to clear the existing ExecStart list, to prevent appending to it.
87 "${config.security.wrapperDir}/gamemoded"
88 ];
89 };
90 };
91
92 users.groups.gamemode = { };
93 };
94
95 meta = {
96 maintainers = with lib.maintainers; [ kira-bruneau ];
97 };
98}