summaryrefslogtreecommitdiffstats
path: root/nixos/modules/hardware/tuxedo-drivers.nix
blob: 07b9f2b7e6d58d0c0363db1cd36fa7b1be6e8aa1 (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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.hardware.tuxedo-drivers;
  tuxedo-drivers = config.boot.kernelPackages.tuxedo-drivers;
  udevRule =
    attr: val:
    let
      # Workaround to evaluate true to "1" and false to "0".
      # Otherwise, true evaluates to "1" and false to "".
      newVal = if lib.isBool val then (if val then "1" else "0") else val;
    in
    lib.concatStringsSep ", " [
      ''SUBSYSTEM=="platform"''
      ''DRIVER=="tuxedo_keyboard"''
      ''ATTR{${attr}}="${newVal}"''
    ];
  optUdevRule = attr: val: lib.optional (val != null) (udevRule attr val);
in
{
  imports = [
    (lib.mkRenamedOptionModule
      [
        "hardware"
        "tuxedo-keyboard"
      ]
      [
        "hardware"
        "tuxedo-drivers"
      ]
    )
  ];

  options.hardware.tuxedo-drivers = {
    enable = lib.mkEnableOption ''
      The tuxedo-drivers driver enables access to the following on TUXEDO notebooks:
      - Driver for Fn-keys
      - SysFS control of brightness/color/mode for most TUXEDO keyboards
      - Hardware I/O driver for TUXEDO Control Center

      For more inforation it is best to check at the source code description: <https://gitlab.com/tuxedocomputers/development/packages/tuxedo-drivers>
    '';
    settings = {
      charging-profile = lib.mkOption {
        type = lib.types.nullOr (
          lib.types.enum [
            "high_capacity"
            "balanced"
            "stationary"
          ]
        );
        default = null;
        description = ''
          The maximum charge level to help reduce battery wear:
          - `high_capacity` charges to 100% (driver default)
          - `balanced` charges to 90%
          - `stationary` charges to 80% (maximum lifespan)

          **Note:** Regardless of the configured charging profile, the operating system will always report the battery as being charged to 100%.
        '';
      };
      charging-priority = lib.mkOption {
        type = lib.types.nullOr (
          lib.types.enum [
            "charge_battery"
            "performance"
          ]
        );
        default = null;
        description = ''
          These options manage the trade-off between battery charging and CPU performance when the USB-C power supply cannot provide sufficient power for both simultaneously:
          - `charge_battery` prioritizes battery charging (driver default)
          - `performance` prioritizes maximum CPU performance
        '';
      };
      fn-lock = lib.mkOption {
        type = lib.types.nullOr lib.types.bool;
        default = null;
        description = ''
          Enables or disables the laptop keyboard's Function (Fn) lock at boot.

          When set to `true`, the Fn lock is enabled, allowing the function keys (F1–F12) to control brightness, volume etc.
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    boot.kernelModules = [ "tuxedo_keyboard" ];
    boot.extraModulePackages = [ tuxedo-drivers ];
    services.udev.packages = [
      tuxedo-drivers
    ]
    ++ lib.optional (lib.any (v: v != null) (lib.attrValues cfg.settings)) (
      pkgs.writeTextDir "etc/udev/rules.d/90-tuxedo.rules" (
        lib.concatLines (
          [ "# Custom rules for TUXEDO laptops" ]
          ++ (optUdevRule "charging_profile/charging_profile" cfg.settings.charging-profile)
          ++ (optUdevRule "charging_priority/charging_prio" cfg.settings.charging-priority)
          ++ (optUdevRule "fn_lock" cfg.settings.fn-lock)
        )
      )
    );
  };
}