1{ stdenv, lib, buildPackages, fetchFromGitHub, fetchpatch, perl, buildLinux, rpiVersion, ... } @ args:
2
3let
4 # NOTE: raspberrypifw & raspberryPiWirelessFirmware should be updated with this
5 modDirVersion = "6.6.31";
6 tag = "stable_20240529";
7in
8lib.overrideDerivation (buildLinux (args // {
9 version = "${modDirVersion}-${tag}";
10 inherit modDirVersion;
11 pname = "linux-rpi";
12
13 src = fetchFromGitHub {
14 owner = "raspberrypi";
15 repo = "linux";
16 rev = tag;
17 hash = "sha256-UWUTeCpEN7dlFSQjog6S3HyEWCCnaqiUqV5KxCjYink=";
18 };
19
20 defconfig = {
21 "1" = "bcmrpi_defconfig";
22 "2" = "bcm2709_defconfig";
23 "3" = if stdenv.hostPlatform.isAarch64 then "bcmrpi3_defconfig" else "bcm2709_defconfig";
24 "4" = "bcm2711_defconfig";
25 }.${toString rpiVersion};
26
27 structuredExtraConfig = (args.structuredExtraConfig or {}) // (with lib.kernel; {
28 # Workaround https://github.com/raspberrypi/linux/issues/6198
29 # Needed because NixOS 24.05+ sets DRM_SIMPLEDRM=y which pulls in
30 # DRM_KMS_HELPER=y.
31 BACKLIGHT_CLASS_DEVICE = yes;
32 });
33
34 features = {
35 efiBootStub = false;
36 } // (args.features or {});
37
38 kernelPatches = (args.kernelPatches or []) ++ [
39 # Fix compilation errors due to incomplete patch backport.
40 # https://github.com/raspberrypi/linux/pull/6223
41 {
42 name = "gpio-pwm_-_pwm_apply_might_sleep.patch";
43 patch = fetchpatch {
44 url = "https://github.com/peat-psuwit/rpi-linux/commit/879f34b88c60dd59765caa30576cb5bfb8e73c56.patch";
45 hash = "sha256-HlOkM9EFmlzOebCGoj7lNV5hc0wMjhaBFFZvaRCI0lI=";
46 };
47 }
48
49 {
50 name = "ir-rx51_-_pwm_apply_might_sleep.patch";
51 patch = fetchpatch {
52 url = "https://github.com/peat-psuwit/rpi-linux/commit/23431052d2dce8084b72e399fce82b05d86b847f.patch";
53 hash = "sha256-UDX/BJCJG0WVndP/6PbPK+AZsfU3vVxDCrpn1kb1kqE=";
54 };
55 }
56 ];
57
58 extraMeta = if (rpiVersion < 3) then {
59 platforms = with lib.platforms; arm;
60 hydraPlatforms = [];
61 } else {
62 platforms = with lib.platforms; arm ++ aarch64;
63 hydraPlatforms = [ "aarch64-linux" ];
64 };
65} // (args.argsOverride or {}))) (oldAttrs: {
66 postConfigure = ''
67 # The v7 defconfig has this set to '-v7' which screws up our modDirVersion.
68 sed -i $buildRoot/.config -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
69 sed -i $buildRoot/include/config/auto.conf -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
70 '';
71
72 # Make copies of the DTBs named after the upstream names so that U-Boot finds them.
73 # This is ugly as heck, but I don't know a better solution so far.
74 postFixup = ''
75 dtbDir=${if stdenv.isAarch64 then "$out/dtbs/broadcom" else "$out/dtbs"}
76 rm $dtbDir/bcm283*.dtb
77 copyDTB() {
78 cp -v "$dtbDir/$1" "$dtbDir/$2"
79 }
80 '' + lib.optionalString (lib.elem stdenv.hostPlatform.system ["armv6l-linux"]) ''
81 copyDTB bcm2708-rpi-zero-w.dtb bcm2835-rpi-zero.dtb
82 copyDTB bcm2708-rpi-zero-w.dtb bcm2835-rpi-zero-w.dtb
83 copyDTB bcm2708-rpi-b.dtb bcm2835-rpi-a.dtb
84 copyDTB bcm2708-rpi-b.dtb bcm2835-rpi-b.dtb
85 copyDTB bcm2708-rpi-b.dtb bcm2835-rpi-b-rev2.dtb
86 copyDTB bcm2708-rpi-b-plus.dtb bcm2835-rpi-a-plus.dtb
87 copyDTB bcm2708-rpi-b-plus.dtb bcm2835-rpi-b-plus.dtb
88 copyDTB bcm2708-rpi-b-plus.dtb bcm2835-rpi-zero.dtb
89 copyDTB bcm2708-rpi-cm.dtb bcm2835-rpi-cm.dtb
90 '' + lib.optionalString (lib.elem stdenv.hostPlatform.system ["armv7l-linux"]) ''
91 copyDTB bcm2709-rpi-2-b.dtb bcm2836-rpi-2-b.dtb
92 '' + lib.optionalString (lib.elem stdenv.hostPlatform.system ["armv7l-linux" "aarch64-linux"]) ''
93 copyDTB bcm2710-rpi-zero-2.dtb bcm2837-rpi-zero-2.dtb
94 copyDTB bcm2710-rpi-3-b.dtb bcm2837-rpi-3-b.dtb
95 copyDTB bcm2710-rpi-3-b-plus.dtb bcm2837-rpi-3-a-plus.dtb
96 copyDTB bcm2710-rpi-3-b-plus.dtb bcm2837-rpi-3-b-plus.dtb
97 copyDTB bcm2710-rpi-cm3.dtb bcm2837-rpi-cm3.dtb
98 copyDTB bcm2711-rpi-4-b.dtb bcm2838-rpi-4-b.dtb
99 '';
100})