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
{ lib, config, pkgs, ... }:
let
  cfg = config.profpatsch.hardware.thinkpad;

in
{
  options.profpatsch.hardware.thinkpad = {
    enable = lib.mkEnableOption "thinkpad support";

    cpuType = lib.mkOption {
      type = lib.types.enum [ "intel" "amd" ];
      default = "intel";
      description = "The CPU type of the ThinkPad";
    };

    powerManagement = lib.mkOption {
      type = lib.types.enum [ "tlp" "auto-cpufreq" ];
      default = "tlp";
      description = ''
        The power management tool to use for the ThinkPad.
        - tlp: TLP Linux Advanced Power Management
        - [auto-cpufreq](https://github.com/AdnanHodzic/auto-cpufreq)

        Only enables the respective services, you might have to adjust settings manually.
      '';
    };
  };

  config = lib.mkIf cfg.enable (lib.mkMerge [
    (lib.mkIf (cfg.cpuType == "intel") {
      # We need to update the Intel microcode on every update,
      # otherwise there can be problems with newer kernels.
      hardware.cpu.intel.updateMicrocode = lib.mkDefault true;

    })
    (lib.mkIf (cfg.cpuType == "amd") {
      # We need to update the AMD microcode on every update,
      # otherwise there can be problems with newer kernels.
      hardware.cpu.amd.updateMicrocode = lib.mkDefault true;
    })
    (lib.mkIf (cfg.powerManagement == "tlp") {
      # TLP Linux Advanced Power Management
      services.tlp.enable = lib.mkDefault true;
      # ensure auto-cpufreq is always disabled (some hardware configs set it)
      services.auto-cpufreq.enable = lib.mkForce false;
    })
    (lib.mkIf (cfg.powerManagement == "auto-cpufreq") {
      services.auto-cpufreq.enable = lib.mkDefault true;
      # ensure tlp is always disabled (some hardware configs set it)
      services.tlp.enable = lib.mkForce false;
    })
    {
      # read acpi stats (e.g. battery)
      environment.systemPackages = [ pkgs.acpi ];

      # for wifi & cpu microcode (amd)
      hardware.enableRedistributableFirmware = lib.mkDefault true;

      hardware.trackpoint = lib.mkDefault {
        enable = true;
        emulateWheel = true;
        speed = 250;
        sensitivity = 140;
      };

      boot = {
        # acpi_call is required for some tlp features, e.g. discharge/recalibrate
        kernelModules = [
          "acpi_call"
        ];

        extraModulePackages = [
          config.boot.kernelPackages.acpi_call
        ];
      };
    }
  ]);
}