1{
2 stdenv,
3 lib,
4 checkbashisms,
5 coreutils,
6 ethtool,
7 fetchFromGitHub,
8 gawk,
9 gnugrep,
10 gnused,
11 hdparm,
12 iw,
13 kmod,
14 makeWrapper,
15 pciutils,
16 perl,
17 perlcritic,
18 shellcheck,
19 smartmontools,
20 systemd,
21 udevCheckHook,
22 util-linux,
23 x86_energy_perf_policy,
24 # RDW only works with NetworkManager, and thus is optional with default off
25 enableRDW ? false,
26 networkmanager,
27}:
28stdenv.mkDerivation rec {
29 pname = "tlp";
30 version = "1.8.0";
31
32 src = fetchFromGitHub {
33 owner = "linrunner";
34 repo = "TLP";
35 rev = version;
36 hash = "sha256-Bqg0IwLh3XIVJd2VkPQFDCZ/hVrzRFrRLlSHJXlJGWU=";
37 };
38
39 # XXX: See patch files for relevant explanations.
40 patches = [
41 ./patches/0001-makefile-correctly-sed-paths.patch
42 ./patches/0002-reintroduce-tlp-sleep-service.patch
43 ];
44
45 postPatch = ''
46 substituteInPlace Makefile --replace-fail ' ?= /usr/' ' ?= /'
47 '';
48
49 buildInputs = [ perl ];
50 nativeBuildInputs = [
51 makeWrapper
52 udevCheckHook
53 ];
54
55 # XXX: While [1] states that DESTDIR should not be used, and that the correct
56 # variable to set is, in fact, PREFIX, tlp thinks otherwise. The Makefile for
57 # tlp concerns itself only with DESTDIR [2] (possibly incorrectly) and so we set
58 # that as opposed to PREFIX, despite what [1] says.
59 #
60 # [1]: https://github.com/NixOS/nixpkgs/issues/65718
61 # [2]: https://github.com/linrunner/TLP/blob/ab788abf4936dfb44fbb408afc34af834230a64d/Makefile#L4-L46
62 makeFlags = [
63 "TLP_NO_INIT=1"
64 "TLP_WITH_ELOGIND=0"
65 "TLP_WITH_SYSTEMD=1"
66
67 "DESTDIR=${placeholder "out"}"
68 ];
69
70 installTargets = [
71 "install-tlp"
72 "install-man"
73 ]
74 ++ lib.optionals enableRDW [
75 "install-rdw"
76 "install-man-rdw"
77 ];
78
79 doCheck = true;
80 nativeCheckInputs = [
81 checkbashisms
82 perlcritic
83 shellcheck
84 ];
85 checkTarget = [ "checkall" ];
86
87 doInstallCheck = true;
88
89 # TODO: Consider using resholve here
90 postInstall =
91 let
92 paths = lib.makeBinPath (
93 [
94 coreutils
95 ethtool
96 gawk
97 gnugrep
98 gnused
99 hdparm
100 iw
101 kmod
102 pciutils
103 perl
104 smartmontools
105 systemd
106 util-linux
107 ]
108 ++ lib.optional enableRDW networkmanager
109 ++ lib.optional (lib.meta.availableOn stdenv.hostPlatform x86_energy_perf_policy) x86_energy_perf_policy
110 );
111 in
112 ''
113 fixup_perl=(
114 $out/share/tlp/tlp-pcilist
115 $out/share/tlp/tlp-readconfs
116 $out/share/tlp/tlp-usblist
117 )
118 for f in "''${fixup_perl[@]}"; do
119 wrapProgram "$f" --prefix PATH : "${paths}"
120 done
121
122 fixup_bash=(
123 $out/bin/*
124 $out/etc/NetworkManager/dispatcher.d/*
125 $out/lib/udev/tlp-*
126 $out/sbin/*
127 $out/share/tlp/bat.d/*
128 $out/share/tlp/func.d/*
129 $out/share/tlp/tlp-func-base
130 )
131 for f in "''${fixup_bash[@]}"; do
132 sed -i '2iexport PATH=${paths}:$PATH' "$f"
133 done
134
135 rm -rf $out/var
136 rm -rf $out/share/metainfo
137 '';
138
139 meta = with lib; {
140 description = "Advanced Power Management for Linux";
141 homepage = "https://linrunner.de/en/tlp/docs/tlp-linux-advanced-power-management.html";
142 changelog = "https://github.com/linrunner/TLP/releases/tag/${version}";
143 platforms = platforms.linux;
144 mainProgram = "tlp";
145 maintainers = with maintainers; [
146 abbradar
147 lovesegfault
148 ];
149 license = licenses.gpl2Plus;
150 };
151}