From d098b095003946adf5714cc8edee501d8cf08e74 Mon Sep 17 00:00:00 2001 From: Cassie Cheung Date: Thu, 13 Feb 2025 15:31:24 +0800 Subject: [PATCH] apple/t2: refactor kernel package, add stable kernel Refactors the kernel package to a generic one and adds support for the lts kernel. Since nixpkgs' kernel packages provide the `kernelPatches` override, utilizing that ensures that kernel updates are not delayed too much due to having to PR them to nixos-hardware separately. This comes at the expense of possible breakage when upstream updates something that breaks the patches. Because the T2 Linux project has a lot of patches which changes periodically, including them in the repository directly is a maintenance burden. Instead, this patch comes with an update script and JSON files containing all of the patches to be downloaded (by fetchurl) and applied. The NixOS option to enable changing release versions will be added in the next commit. --- apple/t2/default.nix | 4 +- apple/t2/pkgs/linux-t2.nix | 56 ----------- apple/t2/pkgs/linux-t2/default.nix | 6 ++ apple/t2/pkgs/linux-t2/generic.nix | 59 ++++++++++++ apple/t2/pkgs/linux-t2/latest.json | 117 +++++++++++++++++++++++ apple/t2/pkgs/linux-t2/latest.nix | 6 ++ apple/t2/pkgs/linux-t2/stable.json | 113 ++++++++++++++++++++++ apple/t2/pkgs/linux-t2/update-patches.py | 93 ++++++++++++++++++ 8 files changed, 396 insertions(+), 58 deletions(-) delete mode 100644 apple/t2/pkgs/linux-t2.nix create mode 100644 apple/t2/pkgs/linux-t2/default.nix create mode 100644 apple/t2/pkgs/linux-t2/generic.nix create mode 100644 apple/t2/pkgs/linux-t2/latest.json create mode 100644 apple/t2/pkgs/linux-t2/latest.nix create mode 100644 apple/t2/pkgs/linux-t2/stable.json create mode 100755 apple/t2/pkgs/linux-t2/update-patches.py diff --git a/apple/t2/default.nix b/apple/t2/default.nix index 9eb8ff3c..10793762 100644 --- a/apple/t2/default.nix +++ b/apple/t2/default.nix @@ -47,8 +47,8 @@ in config = lib.mkMerge [ { - # For keyboard and touchbar - boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.callPackage ./pkgs/linux-t2.nix { }); + # For keyboard, touchpad, touchbar and audio. + boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.callPackage ./pkgs/linux-t2 { }); boot.initrd.kernelModules = [ "apple-bce" ]; services.udev.packages = [ audioFilesUdevRules ]; diff --git a/apple/t2/pkgs/linux-t2.nix b/apple/t2/pkgs/linux-t2.nix deleted file mode 100644 index 6b0696f3..00000000 --- a/apple/t2/pkgs/linux-t2.nix +++ /dev/null @@ -1,56 +0,0 @@ -{ lib, buildLinux, fetchFromGitHub, fetchzip, runCommand -, ... } @ args: - -let - version = "6.13"; - majorVersion = lib.elemAt (lib.take 1 (lib.splitVersion version)) 0; - - patchRepo = fetchFromGitHub { - owner = "t2linux"; - repo = "linux-t2-patches"; - rev = "07cba1a25b43834f24f8be302bd025f2f89493c5"; - hash = "sha256-kRSzqOApTmTTv3rfNmP9/RQ7zV4jptlEOLWixtgwTLk="; - }; - - kernel = fetchzip { - url = "mirror://kernel/linux/kernel/v${majorVersion}.x/linux-${version}.tar.xz"; - hash = "sha256-FD22KmTFrIhED5X3rcjPTot1UOq1ir1zouEpRWZkRC0="; - }; -in -buildLinux (args // { - inherit version; - - pname = "linux-t2"; - # Snippet from nixpkgs - modDirVersion = "${lib.concatStringsSep "." (lib.take 3 (lib.splitVersion "${version}.0"))}"; - - src = runCommand "patched-source" {} '' - cp -r ${kernel} $out - chmod -R u+w $out - cd $out - while read -r patch; do - echo "Applying patch $patch"; - patch -p1 < $patch; - done < <(find ${patchRepo} -type f -name "*.patch" | sort) - ''; - - structuredExtraConfig = with lib.kernel; { - APPLE_BCE = module; - APPLE_GMUX = module; - APFS_FS = module; - BRCMFMAC = module; - BT_BCM = module; - BT_HCIBCM4377 = module; - BT_HCIUART_BCM = yes; - BT_HCIUART = module; - HID_APPLETB_BL = module; - HID_APPLETB_KBD = module; - HID_APPLE = module; - DRM_APPLETBDRM = module; - HID_SENSOR_ALS = module; - SND_PCM = module; - STAGING = yes; - }; - - kernelPatches = []; -} // (args.argsOverride or {})) diff --git a/apple/t2/pkgs/linux-t2/default.nix b/apple/t2/pkgs/linux-t2/default.nix new file mode 100644 index 00000000..76247b8b --- /dev/null +++ b/apple/t2/pkgs/linux-t2/default.nix @@ -0,0 +1,6 @@ +{ callPackage, linux_6_12, ... }@args: + +callPackage ./generic.nix args { + kernel = linux_6_12; + patchesFile = ./stable.json; +} diff --git a/apple/t2/pkgs/linux-t2/generic.nix b/apple/t2/pkgs/linux-t2/generic.nix new file mode 100644 index 00000000..297433c3 --- /dev/null +++ b/apple/t2/pkgs/linux-t2/generic.nix @@ -0,0 +1,59 @@ +{ + lib, + fetchurl, # fetchpatch does unnecessary normalization + ... +}@args: + +{ + kernel, + patchesFile, +}: +let + inherit (builtins) readFile fromJSON; + + patchset = fromJSON (readFile patchesFile); + t2-patches = map ( + { name, hash }: + { + inherit name; + patch = fetchurl { + inherit name hash; + url = patchset.base_url + name; + }; + } + ) patchset.patches; +in +kernel.override ( + args + // { + pname = "linux-t2"; + + structuredExtraConfig = with lib.kernel; { + APPLE_BCE = module; + APPLE_GMUX = module; + APFS_FS = module; + BRCMFMAC = module; + BT_BCM = module; + BT_HCIBCM4377 = module; + BT_HCIUART_BCM = yes; + BT_HCIUART = module; + HID_APPLETB_BL = module; + HID_APPLETB_KBD = module; + HID_APPLE = module; + DRM_APPLETBDRM = module; + HID_SENSOR_ALS = module; + SND_PCM = module; + STAGING = yes; + }; + + kernelPatches = t2-patches ++ (args.kernelPatches or [ ]); + + argsOverride.extraMeta = { + description = "The Linux kernel (with patches from the T2 Linux project)"; + + # take responsibility for the downstream kernel + maintainers = with lib.maintainers; [ soopyc ]; + }; + } + // (args.argsOverride or { }) +) diff --git a/apple/t2/pkgs/linux-t2/latest.json b/apple/t2/pkgs/linux-t2/latest.json new file mode 100644 index 00000000..03c9714b --- /dev/null +++ b/apple/t2/pkgs/linux-t2/latest.json @@ -0,0 +1,117 @@ +{ + "base_url": "https://raw.githubusercontent.com/t2linux/linux-t2-patches/8f138bc16772fdeb0fc68b631fb6f66ddae1a0a1/", + "patches": [ + { + "name": "1001-Add-apple-bce-driver.patch", + "hash": "sha256-e3RPvWPj2QASGOS4kieO8YFekrXsBCJtfF82OPlOn2E=" + }, + { + "name": "1002-Put-apple-bce-in-drivers-staging.patch", + "hash": "sha256-wkveNo1qwAwXWDGTFed4ZDbuBnJbwKgWLmTHK9qq0oM=" + }, + { + "name": "1005-HID-hid-appletb-bl-add-driver-for-the-backlight-of-A.patch", + "hash": "sha256-kEscS1FAyDxLZPOPInYTXFEf4fouBEa4zGctX14hSu8=" + }, + { + "name": "1006-HID-hid-appletb-kbd-add-driver-for-the-keyboard-mode.patch", + "hash": "sha256-eVEuvnoRItaDjwWu41nn9bTkgHgY+1SL/hFAvUR2IlY=" + }, + { + "name": "1007-HID-multitouch-support-getting-the-contact-ID-from.patch", + "hash": "sha256-A4tExJafroVAv/Hbdt7farJM6RYl/DU5KwSbzqcdEVY=" + }, + { + "name": "1008-HID-multitouch-support-getting-the-tip-state-from-HI.patch", + "hash": "sha256-GwK+d87p3UMy66037dzq6/Zearj8gnRHhqSdZczRadY=" + }, + { + "name": "1009-HID-multitouch-take-cls-maxcontacts-into-account-for.patch", + "hash": "sha256-fywzHnCQ657UFye1ckiufral3pBwBGtKdxWqWqaFHM0=" + }, + { + "name": "1010-HID-multitouch-allow-specifying-if-a-device-is-direc.patch", + "hash": "sha256-87SOnLgeG60Svu2Z9QUyP9P6cZ1Gznt6ZYLFImfx+vY=" + }, + { + "name": "1011-HID-multitouch-add-device-ID-for-Apple-Touch-Bars.patch", + "hash": "sha256-TJjXAlQ4WEROnq/xd6/4JHTuEJjqT7dLLcE4etKdzc0=" + }, + { + "name": "1012-lib-vsprintf-Add-support-for-generic-FOURCCs-by-exte.patch", + "hash": "sha256-cMl4WJcT/nM/z12LEsnLxssQgnRqKE+d/P2uMr5PUDQ=" + }, + { + "name": "1014-drm-format-helper-add-helper-for-BGR888-to-XRGB8888-.patch", + "hash": "sha256-iBM2z+/jZb/3+c9wHjC7W359oRl+DzFDZ1KL5eUc3qo=" + }, + { + "name": "1015-drm-tiny-add-driver-for-Apple-Touch-Bars-in-x86-Macs.patch", + "hash": "sha256-sTb+ruY6W18wz9XRJzZKlfl4a8fGo1nrtCoOYNaPAjI=" + }, + { + "name": "2008-i915-4-lane-quirk-for-mbp15-1.patch", + "hash": "sha256-f/PTDwRGEyVkGHFyypTKi+gIni3R4TmcsOneGc7Ay5A=" + }, + { + "name": "2009-apple-gmux-allow-switching-to-igpu-at-probe.patch", + "hash": "sha256-XKwlyJZjJLQz39mc0/S7sPnRnwrqMsq9OKy+QCO+oho=" + }, + { + "name": "3001-applesmc-convert-static-structures-to-drvdata.patch", + "hash": "sha256-7KGC0pCos351wKNhwJI/Qoc7w+orOPpiTkdCN50G8nI=" + }, + { + "name": "3002-applesmc-make-io-port-base-addr-dynamic.patch", + "hash": "sha256-1fnmMP/3WSy2JqjdCvxu8tWoshCPr8eiG6ttYK/N9WA=" + }, + { + "name": "3003-applesmc-switch-to-acpi_device-from-platform.patch", + "hash": "sha256-3S6BzyoPLoirfyAu4yDyE9aYjUYr/7etmAvN3uKSDLE=" + }, + { + "name": "3004-applesmc-key-interface-wrappers.patch", + "hash": "sha256-9Bq28WcqiOnef0br6bATOudr1ViSpajQs7fv7o8Tg0I=" + }, + { + "name": "3005-applesmc-basic-mmio-interface-implementation.patch", + "hash": "sha256-Hh9DymQ+Y74oSs1zv9F0lTU3jfAenKTM+cmFH9tmN9Q=" + }, + { + "name": "3006-applesmc-fan-support-on-T2-Macs.patch", + "hash": "sha256-D5IQHE8QFo49mnUawSau7xUsMFkHKayyhnHW4hi+fWE=" + }, + { + "name": "3007-applesmc-Add-iMacPro-to-applesmc_whitelist.patch", + "hash": "sha256-rLDuyQYzAaWuu/u6nRRVvGdwyfpLvkGPu1OoD8KS0os=" + }, + { + "name": "3008-applesmc-make-applesmc_remove-void.patch", + "hash": "sha256-j2Rj1PW/bFEYh9IkGkkG9b+G27IAPCInyd828z4WE1s=" + }, + { + "name": "3009-applesmc-battery-charge-limiter.patch", + "hash": "sha256-skYKmLsJ6O42/wINLccZWoKS0XhVlC1Nfxi1Ekloxns=" + }, + { + "name": "4001-Input-bcm5974-Add-support-for-the-T2-Macs.patch", + "hash": "sha256-CaviinY3rYqQh+/DGyeBTzLL/ZfIvguOQlWCs3KN4zc=" + }, + { + "name": "5001-wifi-brcmfmac-use-random-seed-flag-for-BCM4355-and-B.patch", + "hash": "sha256-tlKhUNmDfsKat6O8eK1h84qikUj1Kiv9bbBQaZuDQK4=" + }, + { + "name": "7001-drm-i915-fbdev-Discard-BIOS-framebuffers-exceeding-h.patch", + "hash": "sha256-O6RHFxmKZn7aCq1D+r5z2T3jLt0r5+01EABD9rs0E5M=" + }, + { + "name": "8001-Add-APFS-driver.patch", + "hash": "sha256-40Gl8eR9RB6qEoU+vY7+sVWms8e566OUmAb9LZWZK3w=" + }, + { + "name": "8002-Necessary-modifications-to-build-APFS-with-the-kerne.patch", + "hash": "sha256-n1SH5vC//LRJdWOavH9/BHqBTjkUj5f+dFGitSeHksw=" + } + ] +} diff --git a/apple/t2/pkgs/linux-t2/latest.nix b/apple/t2/pkgs/linux-t2/latest.nix new file mode 100644 index 00000000..7ab2e41b --- /dev/null +++ b/apple/t2/pkgs/linux-t2/latest.nix @@ -0,0 +1,6 @@ +{ callPackage, linux_6_13, ... }@args: + +callPackage ./generic.nix args { + kernel = linux_6_13; + patchesFile = ./latest.json; +} diff --git a/apple/t2/pkgs/linux-t2/stable.json b/apple/t2/pkgs/linux-t2/stable.json new file mode 100644 index 00000000..ce38004c --- /dev/null +++ b/apple/t2/pkgs/linux-t2/stable.json @@ -0,0 +1,113 @@ +{ + "base_url": "https://raw.githubusercontent.com/t2linux/linux-t2-patches/19c29ff4bbda9f512b7b69d63d249de269dc66ab/", + "patches": [ + { + "name": "1001-Add-apple-bce-driver.patch", + "hash": "sha256-e3RPvWPj2QASGOS4kieO8YFekrXsBCJtfF82OPlOn2E=" + }, + { + "name": "1002-Put-apple-bce-in-drivers-staging.patch", + "hash": "sha256-DXj4CmE7TKMGrAhQDOR5RVw9YHmyQMiXZsmxYfkKAEA=" + }, + { + "name": "1005-HID-hid-appletb-bl-add-driver-for-the-backlight-of-A.patch", + "hash": "sha256-kEscS1FAyDxLZPOPInYTXFEf4fouBEa4zGctX14hSu8=" + }, + { + "name": "1006-HID-hid-appletb-kbd-add-driver-for-the-keyboard-mode.patch", + "hash": "sha256-eVEuvnoRItaDjwWu41nn9bTkgHgY+1SL/hFAvUR2IlY=" + }, + { + "name": "1007-HID-multitouch-support-getting-the-contact-ID-from.patch", + "hash": "sha256-A4tExJafroVAv/Hbdt7farJM6RYl/DU5KwSbzqcdEVY=" + }, + { + "name": "1008-HID-multitouch-support-getting-the-tip-state-from-HI.patch", + "hash": "sha256-GwK+d87p3UMy66037dzq6/Zearj8gnRHhqSdZczRadY=" + }, + { + "name": "1009-HID-multitouch-take-cls-maxcontacts-into-account-for.patch", + "hash": "sha256-fywzHnCQ657UFye1ckiufral3pBwBGtKdxWqWqaFHM0=" + }, + { + "name": "1010-HID-multitouch-allow-specifying-if-a-device-is-direc.patch", + "hash": "sha256-87SOnLgeG60Svu2Z9QUyP9P6cZ1Gznt6ZYLFImfx+vY=" + }, + { + "name": "1011-HID-multitouch-add-device-ID-for-Apple-Touch-Bars.patch", + "hash": "sha256-TJjXAlQ4WEROnq/xd6/4JHTuEJjqT7dLLcE4etKdzc0=" + }, + { + "name": "1012-lib-vsprintf-Add-support-for-generic-FOURCCs-by-exte.patch", + "hash": "sha256-cMl4WJcT/nM/z12LEsnLxssQgnRqKE+d/P2uMr5PUDQ=" + }, + { + "name": "1014-drm-format-helper-add-helper-for-BGR888-to-XRGB8888-.patch", + "hash": "sha256-iBM2z+/jZb/3+c9wHjC7W359oRl+DzFDZ1KL5eUc3qo=" + }, + { + "name": "1015-drm-tiny-add-driver-for-Apple-Touch-Bars-in-x86-Macs.patch", + "hash": "sha256-sTb+ruY6W18wz9XRJzZKlfl4a8fGo1nrtCoOYNaPAjI=" + }, + { + "name": "2008-i915-4-lane-quirk-for-mbp15-1.patch", + "hash": "sha256-lZ7MWXZubmAlJCqBmuzueg7agENJbikxP1SE46SmwNw=" + }, + { + "name": "2009-apple-gmux-allow-switching-to-igpu-at-probe.patch", + "hash": "sha256-XKwlyJZjJLQz39mc0/S7sPnRnwrqMsq9OKy+QCO+oho=" + }, + { + "name": "3001-applesmc-convert-static-structures-to-drvdata.patch", + "hash": "sha256-7KGC0pCos351wKNhwJI/Qoc7w+orOPpiTkdCN50G8nI=" + }, + { + "name": "3002-applesmc-make-io-port-base-addr-dynamic.patch", + "hash": "sha256-1fnmMP/3WSy2JqjdCvxu8tWoshCPr8eiG6ttYK/N9WA=" + }, + { + "name": "3003-applesmc-switch-to-acpi_device-from-platform.patch", + "hash": "sha256-3S6BzyoPLoirfyAu4yDyE9aYjUYr/7etmAvN3uKSDLE=" + }, + { + "name": "3004-applesmc-key-interface-wrappers.patch", + "hash": "sha256-9Bq28WcqiOnef0br6bATOudr1ViSpajQs7fv7o8Tg0I=" + }, + { + "name": "3005-applesmc-basic-mmio-interface-implementation.patch", + "hash": "sha256-Hh9DymQ+Y74oSs1zv9F0lTU3jfAenKTM+cmFH9tmN9Q=" + }, + { + "name": "3006-applesmc-fan-support-on-T2-Macs.patch", + "hash": "sha256-D5IQHE8QFo49mnUawSau7xUsMFkHKayyhnHW4hi+fWE=" + }, + { + "name": "3007-applesmc-Add-iMacPro-to-applesmc_whitelist.patch", + "hash": "sha256-rLDuyQYzAaWuu/u6nRRVvGdwyfpLvkGPu1OoD8KS0os=" + }, + { + "name": "3008-applesmc-make-applesmc_remove-void.patch", + "hash": "sha256-j2Rj1PW/bFEYh9IkGkkG9b+G27IAPCInyd828z4WE1s=" + }, + { + "name": "3009-applesmc-battery-charge-limiter.patch", + "hash": "sha256-skYKmLsJ6O42/wINLccZWoKS0XhVlC1Nfxi1Ekloxns=" + }, + { + "name": "4001-Input-bcm5974-Add-support-for-the-T2-Macs.patch", + "hash": "sha256-CaviinY3rYqQh+/DGyeBTzLL/ZfIvguOQlWCs3KN4zc=" + }, + { + "name": "7001-drm-i915-fbdev-Discard-BIOS-framebuffers-exceeding-h.patch", + "hash": "sha256-O6RHFxmKZn7aCq1D+r5z2T3jLt0r5+01EABD9rs0E5M=" + }, + { + "name": "8001-Add-APFS-driver.patch", + "hash": "sha256-RjEhzCqGvwRSGFjMgg1Z1kFwbxB9VT3SkVKoOdtDvP0=" + }, + { + "name": "8002-Necessary-modifications-to-build-APFS-with-the-kerne.patch", + "hash": "sha256-n1SH5vC//LRJdWOavH9/BHqBTjkUj5f+dFGitSeHksw=" + } + ] +} diff --git a/apple/t2/pkgs/linux-t2/update-patches.py b/apple/t2/pkgs/linux-t2/update-patches.py new file mode 100755 index 00000000..28d46b13 --- /dev/null +++ b/apple/t2/pkgs/linux-t2/update-patches.py @@ -0,0 +1,93 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python -p nix -p "python3.withPackages (p: [p.requests])" + +import argparse +import json +import re +import subprocess +import sys +from tempfile import NamedTemporaryFile + +import requests + +API_BASE = "https://api.github.com" +PATCH_PATTERN = re.compile(r"^\d{4}-.*\.patch$") + +parser = argparse.ArgumentParser( + description="Update linux-t2 patches from a GitHub repository." +) +parser.add_argument("filename", help="the output filename") +parser.add_argument( + "--repository", + help="the source github repository", + default="t2linux/linux-t2-patches", + nargs="?", +) +ref_group = parser.add_mutually_exclusive_group() +ref_group.add_argument( + "--reference", help="the git reference for the patches", default=None, nargs="?" +) +ref_group.add_argument( + "--branch", help="the git branch to fetch", default=None, nargs="?" +) + + +def get_api(endpoint, *args, **kwargs): + kwargs["headers"] = {"X-GitHub-Api-Version": "2022-11-28"} + response = requests.get(API_BASE + endpoint, *args, **kwargs) + response.raise_for_status() + return response.json() + + +def get_sri_hash(data: bytes): + with NamedTemporaryFile() as tmpfile: + tmpfile.write(data) + tmpfile.flush() + proc = subprocess.run( + ["nix-hash", "--sri", "--flat", "--type", "sha256", tmpfile.name], + check=True, + capture_output=True, + ) + return proc.stdout.decode("utf8").strip() + + +def main(): + args = parser.parse_args() + reference = args.reference + branch = args.branch + + if reference is None: + if branch is None: + print("Branch and reference not provided, fetching default branch") + branch = get_api(f"/repos/{args.repository}")["default_branch"] + + print(f"Reference not provided, fetching from branch {branch}") + branch_data = get_api(f"/repos/{args.repository}/branches/{branch}") + reference = branch_data["commit"]["sha"] + + print(f"Repository: {args.repository}") + print(f" Reference: {reference}") + + base_url = f"https://raw.githubusercontent.com/{args.repository}/{reference}/" + contents = get_api(f"/repos/{args.repository}/contents", {"ref": reference}) + patches = filter(lambda e: PATCH_PATTERN.match(e.get("name")), contents) + + patches_with_hash = [] + for patch in patches: + patch_content = requests.get(patch["download_url"]) + patch_hash = get_sri_hash(patch_content.content) + print(f"{patch['name']}: {patch_hash}") + patches_with_hash.append({"name": patch["name"], "hash": patch_hash}) + + result = {"base_url": base_url, "patches": patches_with_hash} + + with open(args.filename, "w+") as f: + json.dump(result, f, indent=2) + f.write("\n") # write final newline + print(f"Wrote to {args.filename}") + + return 0 + + +if __name__ == "__main__": + sys.exit(main())