summaryrefslogtreecommitdiff
path: root/modules/nixos/disable-screen/default.nix
blob: b70d80d51145e3b1041c6acf7ffeb3ec2cd1aa06 (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
# This file defines some configuration options which disable the screen. This
# is only relevant because this host is an old laptop running as a server.

{ lib, config, ... }:

let
  inherit (lib) mkEnableOption mkOption types;

  cfg = config.disable-screen;
in
{
  options.disable-screen = {
    enable = mkEnableOption "disable screen";

    device-path = mkOption {
      description = "Path to the device in the `/sys` file system.";
      type = types.str;
      example = "/sys/class/backlight/intel_backlight";
    };

    device-unit = mkOption {
      description = "The systemd device unit that corresponds to the device speciefied in `device-path`.";
      type = types.str;
      example = "sys-devices-pci...-intel_backligt.device";
    };
  };

  config = {
    # Disable sleep on lid close.
    services.logind =
      let
        lidSwitchAction = "ignore";
      in
      {
        lidSwitchExternalPower = lidSwitchAction;
        lidSwitchDocked = lidSwitchAction;
        lidSwitch = lidSwitchAction;
      };

    # Don't store screen brightness between boots. We always want to turn off the
    # screen.
    #
    # See: https://wiki.archlinux.org/title/backlight#Save_and_restore_functionality
    # See: https://github.com/NixOS/nixpkgs/blob/990398921f677615c0732d704857484b84c6c888/nixos/modules/system/boot/systemd.nix#L97-L101
    systemd.suppressedSystemUnits = [ "[email protected]" ];

    # FIXME: Figure out how to enable screen when on-device debugging is necessary.
    # Create a new service which turns off the display on boot.
    #
    # See: https://nixos.wiki/wiki/Backlight#.2Fsys.2Fclass.2Fbacklight.2F...
    # See: https://superuser.com/questions/851846/how-to-write-a-systemd-service-that-depends-on-a-device-being-present
    systemd.services.disable-screen =
      {
        requires = [ cfg.device-unit ];
        after = [ cfg.device-unit ];
        wantedBy = [ cfg.device-unit ];

        serviceConfig.Type = "oneshot";
        script = ''
          tee ${cfg.device-path}/brightness <<<0
        '';
      };
  };
}