1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 kernel,
7 kernelModuleMakeFlags,
8 runtimeShell,
9}:
10
11let
12 baseName = "bbswitch";
13 version = "unstable-2021-11-29";
14 name = "${baseName}-${version}-${kernel.version}";
15
16in
17
18stdenv.mkDerivation {
19 inherit name;
20
21 src = fetchFromGitHub {
22 owner = "Bumblebee-Project";
23 repo = "bbswitch";
24 # https://github.com/Bumblebee-Project/bbswitch/tree/develop
25 rev = "23891174a80ea79c7720bcc7048a5c2bfcde5cd9";
26 hash = "sha256-50v1Jxem5kaI1dHOKmgBbPLxI82QeYxiaRHhrHpWRzU=";
27 };
28
29 patches = [
30 (fetchpatch {
31 url = "https://raw.githubusercontent.com/archlinux/svntogit-community/0bd986055ba52887b81048de5c61e618eec06eb0/trunk/0003-kernel-5.18.patch";
32 sha256 = "sha256-va62/bR1qyBBMPg0lUwCH7slGG0XijxVCsFa4FCoHEQ=";
33 })
34 ];
35
36 nativeBuildInputs = kernel.moduleBuildDependencies;
37
38 hardeningDisable = [ "pic" ];
39
40 preBuild = ''
41 substituteInPlace Makefile \
42 --replace "/lib/modules" "${kernel.dev}/lib/modules"
43 '';
44
45 makeFlags = kernelModuleMakeFlags;
46
47 installPhase = ''
48 mkdir -p $out/lib/modules/${kernel.modDirVersion}/misc
49 cp bbswitch.ko $out/lib/modules/${kernel.modDirVersion}/misc
50
51 mkdir -p $out/bin
52 tee $out/bin/discrete_vga_poweroff << EOF
53 #!${runtimeShell}
54
55 echo -n OFF > /proc/acpi/bbswitch
56 EOF
57 tee $out/bin/discrete_vga_poweron << EOF
58 #!${runtimeShell}
59
60 echo -n ON > /proc/acpi/bbswitch
61 EOF
62 chmod +x $out/bin/discrete_vga_poweroff $out/bin/discrete_vga_poweron
63 '';
64
65 meta = with lib; {
66 description = "Module for powering off hybrid GPUs";
67 platforms = [
68 "x86_64-linux"
69 "i686-linux"
70 ];
71 homepage = "https://github.com/Bumblebee-Project/bbswitch";
72 maintainers = with maintainers; [ abbradar ];
73 license = licenses.gpl2Plus;
74 };
75}