summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-04 01:30:11 +0000
committertv <tv@krebsco.de>2026-12-04 02:13:09 +0000
commitda2fdb598f7980279a255b62994b95abb843b1f4 (patch)
tree0a7e1b94ada5cba282bd0cbe31854504b960c43a
parent9e10bd98d356db4b6ac3ea11cc9bcdb9aca5c2c9 (diff)
x11: X server and sessions
From the dirt. Boolean options are written as on/off. Sessions run for any user: each user's orchestrator only looks at X sockets owned by that user.
-rw-r--r--configs/x11/default.nix7
-rw-r--r--configs/x11/xserver.nix189
-rw-r--r--modules/x11/default.nix3
-rw-r--r--modules/x11/xsession.nix103
-rw-r--r--systems/leg/config.nix25
5 files changed, 325 insertions, 2 deletions
diff --git a/configs/x11/default.nix b/configs/x11/default.nix
new file mode 100644
index 00000000..8ee40c22
--- /dev/null
+++ b/configs/x11/default.nix
@@ -0,0 +1,7 @@
+{
+ imports = [
+ ./xserver.nix
+ ];
+
+ hrm.xsession.enable = true;
+}
diff --git a/configs/x11/xserver.nix b/configs/x11/xserver.nix
new file mode 100644
index 00000000..816ffbc8
--- /dev/null
+++ b/configs/x11/xserver.nix
@@ -0,0 +1,189 @@
+# 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
+
+ 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";
+ };
+ }
+ // {
+ "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;
+ };
+ };
+ };
+ };
+}
diff --git a/modules/x11/default.nix b/modules/x11/default.nix
new file mode 100644
index 00000000..40589798
--- /dev/null
+++ b/modules/x11/default.nix
@@ -0,0 +1,3 @@
+{ lib, mylib, ... }: {
+ imports = lib.attrValues (mylib.mapNixDir lib.id ./.);
+}
diff --git a/modules/x11/xsession.nix b/modules/x11/xsession.nix
new file mode 100644
index 00000000..91b59490
--- /dev/null
+++ b/modules/x11/xsession.nix
@@ -0,0 +1,103 @@
+# X sessions as systemd user services.
+#
+# When an X server of the user creates its socket /tmp/.X11-unix/XN, the
+# user's xsession-orchestrator starts xsession@N.target, which wants every
+# service named in hrm.xsession.services as NAME@N.service, with
+# DISPLAY=:N. When the socket goes away, the target is stopped.
+{ config, lib, pkgs, ... }: let
+ cfg = config.hrm.xsession;
+in {
+ options.hrm.xsession = {
+ enable = lib.mkEnableOption "X sessions as systemd user services";
+ services = lib.mkOption {
+ default = [];
+ type = lib.types.listOf lib.types.str;
+ description = ''
+ Names of the systemd user services NAME@ to run in each X session.
+ '';
+ };
+ };
+ config = lib.mkIf cfg.enable (lib.mkMerge [
+ {
+ systemd.user.targets."xsession@".wants =
+ map (name: "${name}@%i.service") cfg.services;
+ systemd.user.services = lib.genAttrs' cfg.services (name:
+ lib.nameValuePair "${name}@" {
+ bindsTo = [ "xsession@%i.target" ];
+ requires = lib.optional (name != "xserver-wait-ready") "xserver-wait-ready@%i.service";
+ after = lib.optional (name != "xserver-wait-ready") "xserver-wait-ready@%i.service";
+ serviceConfig.SyslogIdentifier = "${name}@%i";
+ environment.DISPLAY = ":%i";
+ });
+ }
+
+ {
+ systemd.user.paths.xsession-orchestrator = {
+ wantedBy = [ "paths.target" ];
+ pathConfig.PathChanged = "/tmp/.X11-unix";
+ };
+ systemd.user.services.xsession-orchestrator = {
+ serviceConfig = {
+ ExecStart = pkgs.writeDash "xsession-orchestrator" ''
+ set -efu
+
+ workdir=$(${pkgs.coreutils}/bin/mktemp --tmpdir -d xsession-orchestrator.XXXXXXXX)
+ readonly workdir
+ trap 'cd / && rm -R "$workdir"' EXIT
+ cd "$workdir"
+
+ ${pkgs.findutils}/bin/find /tmp/.X11-unix -type s -user "$USER" |
+ ${pkgs.gnused}/bin/sed -nr 's:.*/X([0-9]+)$:\1:p' |
+ ${pkgs.coreutils}/bin/sort -n > running_displays
+ if test -s running_displays; then
+ echo "running displays:$(printf ' :%s' $(cat running_displays))" >&2
+ else
+ echo 'no running displays' >&2
+ fi
+
+ ${pkgs.systemd}/bin/systemctl --user show 'xsession@[0-9]*.target' -p Id --state=active |
+ ${pkgs.gnused}/bin/sed -nr 's:^Id=xsession@([0-9]+)\.target$:\1:p' |
+ ${pkgs.coreutils}/bin/sort -n > running_sessions
+ if test -s running_sessions; then
+ echo "running sessions:$(printf ' xsession@%s.target' $(cat running_sessions))" >&2
+ else
+ echo 'no running sessions' >&2
+ fi
+
+ missing_sessions=$(${pkgs.coreutils}/bin/comm -23 running_displays running_sessions)
+ obsolete_sessions=$(${pkgs.coreutils}/bin/comm -13 running_displays running_sessions)
+
+ if test -n "$obsolete_sessions"; then
+ obsolete_sessions=$(printf ' xsession@%s.target' $obsolete_sessions)
+ echo "stopping obsolete sessions:$obsolete_sessions" >&2
+ ${pkgs.systemd}/bin/systemctl --user stop $obsolete_sessions
+ fi
+
+ if test -n "$missing_sessions"; then
+ missing_sessions=$(printf ' xsession@%s.target' $missing_sessions)
+ echo "starting missing sessions:$missing_sessions" >&2
+ ${pkgs.systemd}/bin/systemctl --user start $missing_sessions
+ fi
+ '';
+ SyslogIdentifier = "xsession-orchestrator";
+ Type = "oneshot";
+ };
+ };
+ }
+
+ # Wait for X to become ready.
+ {
+ hrm.xsession.services = [ "xserver-wait-ready" ];
+ systemd.user.services."xserver-wait-ready@".serviceConfig = {
+ ExecStart = pkgs.writeDash "xserver-wait-ready" ''
+ until ${pkgs.xdpyinfo}/bin/xdpyinfo >/dev/null 2>&1; do
+ ${pkgs.coreutils}/bin/sleep 0.1s
+ done
+ '';
+ TimeoutStartSec = "3s";
+ Type = "oneshot";
+ RemainAfterExit = true;
+ };
+ }
+ ]);
+}
diff --git a/systems/leg/config.nix b/systems/leg/config.nix
index 86709b0d..066aafe9 100644
--- a/systems/leg/config.nix
+++ b/systems/leg/config.nix
@@ -8,6 +8,7 @@
../../configs/earlyoom.nix
../../configs/networkd.nix
+ ../../configs/x11
{
### from https://github.com/gkovacs/udev-key-remapping
@@ -81,8 +82,6 @@
hardware.sensor.iio.enable = true;
- hardware.graphics.enable = true;
-
nix.settings.cores = 4;
users.users.root.packages = [
@@ -127,5 +126,27 @@
# modesetting doesn't show anything beyond the cursor.
services.xserver.modules = [ pkgs.xf86videoamdgpu ];
+ # The built-in screen is mounted in portrait orientation.
+ hrm.xserver.configs = {
+ "80-monitor.conf" = {
+ Section.Monitor.eDP = {
+ DisplaySize = [ 120 190 ];
+ Option.Rotate = "left";
+ Option.UseEdidDpi = "False";
+ Option.DPI = "337x342";
+ };
+ };
+ "99-rotate.conf" = {
+ Section.InputClass."libinput touchscreen catchall" = {
+ MatchProduct = "NVTK0603:00 0603:F001";
+ MatchIsTouchscreen = true;
+ Option.TransformationMatrix = "0 -1 1 1 0 0 0 0 1";
+ };
+ Section.Monitor."<default monitor>" = {
+ Option.Rotate = "left";
+ };
+ };
+ };
+
services.illum.enable = true;
}