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