1{ config
2, lib
3, pkgs
4, ...
5}:
6with lib; let
7 cfg = config.programs.gamescope;
8
9 gamescope =
10 let
11 wrapperArgs =
12 optional (cfg.args != [ ])
13 ''--add-flags "${toString cfg.args}"''
14 ++ builtins.attrValues (mapAttrs (var: val: "--set-default ${var} ${val}") cfg.env);
15 in
16 pkgs.runCommand "gamescope" { nativeBuildInputs = [ pkgs.makeBinaryWrapper ]; } ''
17 mkdir -p $out/bin
18 makeWrapper ${cfg.package}/bin/gamescope $out/bin/gamescope --inherit-argv0 \
19 ${toString wrapperArgs}
20 '';
21in
22{
23 options.programs.gamescope = {
24 enable = mkEnableOption (mdDoc "gamescope");
25
26 package = mkOption {
27 type = types.package;
28 default = pkgs.gamescope;
29 defaultText = literalExpression "pkgs.gamescope";
30 description = mdDoc ''
31 The GameScope package to use.
32 '';
33 };
34
35 capSysNice = mkOption {
36 type = types.bool;
37 default = false;
38 description = mdDoc ''
39 Add cap_sys_nice capability to the GameScope
40 binary so that it may renice itself.
41 '';
42 };
43
44 args = mkOption {
45 type = types.listOf types.string;
46 default = [ ];
47 example = [ "--rt" "--prefer-vk-device 8086:9bc4" ];
48 description = mdDoc ''
49 Arguments passed to GameScope on startup.
50 '';
51 };
52
53 env = mkOption {
54 type = types.attrsOf types.string;
55 default = { };
56 example = literalExpression ''
57 # for Prime render offload on Nvidia laptops.
58 # Also requires `hardware.nvidia.prime.offload.enable`.
59 {
60 __NV_PRIME_RENDER_OFFLOAD = "1";
61 __VK_LAYER_NV_optimus = "NVIDIA_only";
62 __GLX_VENDOR_LIBRARY_NAME = "nvidia";
63 }
64 '';
65 description = mdDoc ''
66 Default environment variables available to the GameScope process, overridable at runtime.
67 '';
68 };
69 };
70
71 config = mkIf cfg.enable {
72 security.wrappers = mkIf cfg.capSysNice {
73 gamescope = {
74 owner = "root";
75 group = "root";
76 source = "${gamescope}/bin/gamescope";
77 capabilities = "cap_sys_nice+pie";
78 };
79 };
80
81 environment.systemPackages = mkIf (!cfg.capSysNice) [ gamescope ];
82 };
83
84 meta.maintainers = with maintainers; [ nrdxp ];
85}