summaryrefslogtreecommitdiffstats
path: root/configs/x11/xserver.nix
blob: dcc0821a40abbe02003d23a250f376e57c366ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# The X server, started by the user on a virtual terminal: xinit on ttyN
# starts display :N.  hrm.xsession then runs the session.
{ config, lib, pkgs, ... }: let
  cfg = config.hrm.xserver;

  # Writes xorg.conf sections from attribute sets:
  #   Section.<name>.<Identifier> = { <entry> = <value>; Option.<name> = <value>; }
  to.xorg = rec {
    conf = content:
      lib.concatStringsSep "\n"
        (lib.concatLists
          (lib.mapAttrsToList
            (sectionName: lib.mapAttrsToList (toSection sectionName))
            content.Section));
    toSection = sectionName: Identifier: sectionEntries: let
      requiresIdentifier = lib.elem sectionName [
        "Device"
        "InputClass"
        "Monitor"
        "Screen"
        "ServerLayout"
      ];
    in /* xorg */ ''
      Section ${toValue sectionName}
      ${lib.concatMapStringsSep "\n"
        (line: "  " + line)
        (lib.mapAttrsToList
          (name: value: "${name} ${toValue value}")
          (lib.filterAttrs (_: value: value != null) (
            # Sections that require an identifier get the attribute name as
            # one, unless sectionEntries has an explicit Identifier.
            lib.optionalAttrs requiresIdentifier {
              inherit Identifier;
            }
            //
            builtins.removeAttrs sectionEntries [ "Option" ]
            //
            lib.optionalAttrs (sectionEntries?Option)
              (lib.mapAttrs'
                (name: value: lib.nameValuePair "Option ${toValue name}" (
                  if builtins.isBool value
                    then (if value then "on" else "off")
                    else toString value
                ))
                sectionEntries.Option)
          )))
      }
      EndSection
    '';
    toValue = value: {
      bool = builtins.toJSON (if value then "on" else "off");
      float = builtins.toJSON value;
      int = builtins.toJSON value;
      list = lib.concatMapStringsSep " " toValue value;
      string = builtins.toJSON value;
    }.${builtins.typeOf value};
  };

  configdir = pkgs.runCommand "xorg.conf.d" {
    fontPkgs = config.fonts.packages ++ [
      pkgs.fontadobe100dpi
      pkgs.fontadobe75dpi
    ];
    modulePkgs = config.services.xserver.modules ++ [
      pkgs.xorgserver.out
      (pkgs.write "xorg-config"
        (lib.mapAttrs'
          (name: content:
            lib.nameValuePair "/share/X11/xorg.conf.d/${name}" {
              text = to.xorg.conf content;
            })
          cfg.configs))
    ];
  } /* sh */ ''
    mkdir $out

    (
      exec > $out/00-nix-module-paths.conf
      echo 'Section "Files"'
      for pkg in $fontPkgs; do
        if test "''${pkg:0:''${#NIX_STORE}}" = "$NIX_STORE"; then
          find "$pkg" \
              -name fonts.dir \
              -printf '  FontPath "%h"\n'
        fi
      done
      for pkg in $modulePkgs; do
        path=$pkg/lib/xorg/modules
        if test -d "$path"; then
          printf '  ModulePath "%s"\n' "$path"
        fi
      done
      echo 'EndSection'
    )

    for pkg in $modulePkgs; do
      for conf in "$pkg"/share/X11/xorg.conf.d/*; do
        ln -sf "$conf" $out
      done
    done
  '';
in {
  options.hrm.xserver.configs = lib.mkOption {
    default = {};
    type = lib.types.attrsOf (lib.types.attrsOf lib.types.anything);
    description = ''
      Files in the X server's xorg.conf.d, by name, as attribute sets:
        Section.<name>.<Identifier> = { <entry> = <value>; Option.<name> = <value>; }
    '';
  };
  config = {
    # The X server's OpenGL drivers, in /run/opengl-driver.
    hardware.graphics.enable = true;

    environment.systemPackages = [
      (pkgs.writeDashBin "xinit" ''
        set -efu

        # For the nvidia driver's libraries.
        export LD_LIBRARY_PATH=/run/opengl-driver/lib:/run/opengl-driver-32/lib

        tty=$(${pkgs.coreutils}/bin/tty)
        i=''${tty#/dev/tty}

        logfile=$XDG_RUNTIME_DIR/Xorg.$i.log

        exec ${pkgs.xorgserver}/bin/X \
            :$i \
            vt$i \
            -configdir ${configdir} \
            -xkbdir ${config.services.xserver.xkb.dir} \
            -nolisten tcp \
            -nolisten local \
            -verbose 0 \
            -logverbose 1 \
            -logfile "$logfile" \
            -maxclients 512 \
          >/dev/null 2>&1
      '')
    ];

    services.xserver.modules = [
      pkgs.xf86inputlibinput
    ];
    services.udev.packages = [
      pkgs.libinput
    ];

    hrm.xserver.configs = lib.optionalAttrs
      (lib.any (pkg: pkg.pname == "xf86-video-amdgpu") config.services.xserver.modules)
      {
        # Naming the driver keeps the X server from probing all of them.
        "10-gpu.conf" = {
          Section.Device.GPU.Driver = "amdgpu";
        };
      }
    // lib.optionalAttrs
      (lib.any (pkg: pkg.pname == "nvidia-x11") config.services.xserver.modules)
      {
        "10-nvidia-x11.conf" = {
          Section.Device.GPU = {
            Driver = "nvidia";
            Option.SidebandSocketPath = "/run/nvidia-xdriver/";
          };
        };
      }
    // {
      "10-base.conf" = {
        Section.ServerFlags.default.Option.DontZap = true;
      };

      "80-libinput-config.conf" = {
        Section.InputClass.libinputConfiguration = {
          MatchIsTouchpad = true;
          Driver = "libinput";
          Option.AccelProfile = "adaptive";
          Option.AccelSpeed = 0.35;
          Option.LeftHanded = true;
          Option.MiddleEmulation = true;
          Option.NaturalScrolling = true;
          Option.ScrollButton = 1;
          Option.HorizontalScrolling = true;
          Option.SendEventsMode = "enabled";
          Option.Tapping = true;
          # TappingDrag or TappingDragLock breaks secondary buffer selection
          # in alacritty.
          Option.DisableWhileTyping = false;
        };
      };

      "80-keyboard.conf" = {
        Section.InputClass."keyboard catchall" = {
          MatchIsKeyboard = true;
          Option.XkbModel = config.services.xserver.xkb.model;
          Option.XkbLayout = config.services.xserver.xkb.layout;
          Option.XkbOptions = config.services.xserver.xkb.options;
          Option.XkbVariant = config.services.xserver.xkb.variant;
        };
      };
    };
  };
}