summaryrefslogtreecommitdiffstats
path: root/tv/2configs/xserver/default.nix
blob: 9e20bda18444b4d43a18ddc48a6aa8bfedb165e9 (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
{ config, lib, pkgs, ... }@args:

with lib;

let
  # TODO krebs.build.user
  user = config.users.users.tv;

  out = {
    services.xserver.display = 11;
    services.xserver.tty = 11;

    services.xserver.synaptics = {
      enable = true;
      twoFingerScroll = true;
      accelFactor = "0.035";
    };

    fonts.fonts = [
      pkgs.xlibs.fontschumachermisc
    ];

    systemd.services.urxvtd = {
      wantedBy = [ "multi-user.target" ];
      reloadIfChanged = true;
      serviceConfig = {
        ExecReload = need-reload "urxvtd.service";
        ExecStart = "${pkgs.rxvt_unicode}/bin/urxvtd";
        Restart = "always";
        RestartSec = "2s";
        StartLimitBurst = 0;
        User = user.name;
      };
    };

    environment.systemPackages = [
      pkgs.slock
    ];

    security.setuidPrograms = [
      "slock"
    ];

    systemd.services.display-manager = mkForce {};

    services.xserver.enable = true;

    systemd.services.xmonad = {
      wantedBy = [ "multi-user.target" ];
      requires = [ "xserver.service" ];
      environment = xmonad-environment;
      serviceConfig = {
        ExecStart = "${xmonad-start}/bin/xmonad";
        ExecStop = "${xmonad-stop}/bin/xmonad-stop";
        User = user.name;
        WorkingDirectory = user.home;
      };
    };

    systemd.services.xserver = {
      after = [
        "systemd-udev-settle.service"
        "local-fs.target"
        "acpid.service"
      ];
      reloadIfChanged = true;
      environment = xserver-environment;
      serviceConfig = {
        ExecReload = need-reload "xserver.service";
        ExecStart = "${xserver}/bin/xserver";
      };
    };
  };

  xmonad-pkg = pkgs.haskellPackages.callPackage xmonad-src {};
  xmonad-src = pkgs.runCommand "xmonad-package" {} ''
    ${pkgs.cabal2nix}/bin/cabal2nix ${./xmonad} > $out
  '';

  xmonad-environment = {
    DISPLAY = ":${toString config.services.xserver.display}";
    XMONAD_STATE = "/tmp/xmonad.state";
  };

  xmonad-start = pkgs.writeScriptBin "xmonad" ''
    #! ${pkgs.bash}/bin/bash
    set -efu
    export PATH; PATH=${makeSearchPath "bin" [
      pkgs.rxvt_unicode
    ]}:/var/setuid-wrappers
    settle() {(
      # Use PATH for a clean journal
      command=''${1##*/}
      PATH=''${1%/*}; export PATH
      shift
      until "$command" "$@"; do
        ${pkgs.coreutils}/bin/sleep 1
      done
    )&}
    settle ${pkgs.xorg.xhost}/bin/xhost +LOCAL:
    settle ${pkgs.xorg.xrdb}/bin/xrdb -merge ${import ./Xresources.nix args}
    settle ${pkgs.xorg.xsetroot}/bin/xsetroot -solid '#1c1c1c'
    if test -e "$XMONAD_STATE"; then
      IFS=''$'\n'
      exec ${xmonad-pkg}/bin/xmonad --resume $(< "$XMONAD_STATE")
    else
      exec ${xmonad-pkg}/bin/xmonad
    fi
  '';

  xmonad-stop = pkgs.writeScriptBin "xmonad-stop" ''
    #! /bin/sh
    exec ${xmonad-pkg}/bin/xmonad --shutdown
  '';

  xserver-environment = {
    XKB_BINDIR = "${pkgs.xorg.xkbcomp}/bin"; # Needed for the Xkb extension.
    XORG_DRI_DRIVER_PATH = "/run/opengl-driver/lib/dri"; # !!! Depends on the driver selected at runtime.
    LD_LIBRARY_PATH = concatStringsSep ":" (
      [ "${pkgs.xorg.libX11}/lib" "${pkgs.xorg.libXext}/lib" ]
      ++ concatLists (catAttrs "libPath" config.services.xserver.drivers));
  };

  xserver = pkgs.writeScriptBin "xserver" ''
    #! /bin/sh
    set -efu
    exec ${pkgs.xorg.xorgserver}/bin/X \
        :${toString config.services.xserver.display} \
        vt${toString config.services.xserver.tty} \
        -config ${import ./xserver.conf.nix args} \
        -logfile /var/log/X.${toString config.services.xserver.display}.log \
        -nolisten tcp \
        -xkbdir ${pkgs.xkeyboard_config}/etc/X11/xkb \
  '';

  need-reload = s: let
    pkg = pkgs.writeScriptBin "need-reload" ''
      #! /bin/sh
      echo "$*"
    '';
  in "${pkg}/bin/need-reload ${s}";

in out