From 98eaa5ffe24539bd8aef3211b4379e30d6e6cc03 Mon Sep 17 00:00:00 2001 From: kekrby Date: Thu, 25 Aug 2022 18:17:01 +0300 Subject: [PATCH] apple/t2: init Co-authored-by: networkException --- README.md | 1 + apple/t2/README.md | 9 ++++++ apple/t2/default.nix | 59 ++++++++++++++++++++++++++++++++++++++ apple/t2/pkgs/linux-t2.nix | 46 +++++++++++++++++++++++++++++ flake.nix | 1 + 5 files changed, 116 insertions(+) create mode 100644 apple/t2/README.md create mode 100644 apple/t2/default.nix create mode 100644 apple/t2/pkgs/linux-t2.nix diff --git a/README.md b/README.md index 2bbfd45dc..393686cf7 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ See code for all available configurations. | [Apple MacBook Pro 11,5](apple/macbook-pro/11-5) | `` | | [Apple MacBook Pro 12,1](apple/macbook-pro/12-1) | `` | | [Apple MacBook Pro 14,1](apple/macbook-pro/14-1) | `` | +| [Apple Macs with a T2 Chip](apple/t2) | `` | | [Asus ROG Strix G733QS](asus/rog-strix/g733qs) | `` | | [Asus ROG Zephyrus G14 GA401](asus/zephyrus/ga401) | `` | | [Asus ROG Zephyrus G14 GA402](asus/zephyrus/ga402) | `` | diff --git a/apple/t2/README.md b/apple/t2/README.md new file mode 100644 index 000000000..7f3a842e6 --- /dev/null +++ b/apple/t2/README.md @@ -0,0 +1,9 @@ +## NixOS on T2 Macs + +This is the `nixos-hardware` module of the [T2 Linux Project](https://t2linux.org). + +Overall, most features (WiFi, bluetooth, audio, touchbar, ...) of Macs are supported, [see this page](https://wiki.t2linux.org/state/) for a detailed list of things that work and things that don't/partially work. + +Following [this guide](https://wiki.t2linux.org/distributions/nixos/installation/) is the recommended way to install, as it incudes the extra things you have to do on a T2 Mac. + +You can consult the [wiki](https://wiki.t2linux.org/) for information specific to T2 Macs. diff --git a/apple/t2/default.nix b/apple/t2/default.nix new file mode 100644 index 000000000..f8da1ce33 --- /dev/null +++ b/apple/t2/default.nix @@ -0,0 +1,59 @@ +{ pkgs, ... }: + +let + audioFiles = pkgs.fetchFromGitHub { + owner = "kekrby"; + repo = "t2-better-audio"; + rev = "e46839a28963e2f7d364020518b9dac98236bcae"; + hash = "sha256-x7K0qa++P1e1vuCGxnsFxL1d9+nwMtZUJ6Kd9e27TFs="; + }; + + overrideAudioFiles = package: pluginsPath: + package.overrideAttrs (new: old: { + preConfigurePhases = old.preConfigurePhases or [ ] ++ [ "postPatchPhase" ]; + + postPatchPhase = '' + cp -r ${audioFiles}/files/{profile-sets,paths} ${pluginsPath}/alsa/mixer/ + ''; + }); +in +{ + # For keyboard and touchbar + boot.kernelPackages = with pkgs; recurseIntoAttrs (linuxPackagesFor (callPackage ./pkgs/linux-t2.nix { })); + boot.initrd.kernelModules = [ "apple-bce" ]; + + # For audio + boot.kernelParams = [ "pcie_ports=compat" "intel_iommu=on" "iommu=pt" ]; + services.udev.extraRules = builtins.readFile (pkgs.substitute { + src = "${audioFiles}/files/91-audio-custom.rules"; + replacements = [ "--replace" "/usr/bin/sed" "${pkgs.gnused}/bin/sed" ]; + }); + + hardware.pulseaudio.package = overrideAudioFiles pkgs.pulseaudio "src/modules/"; + + services.pipewire = rec { + package = overrideAudioFiles pkgs.pipewire "spa/plugins/"; + + wireplumber.package = pkgs.wireplumber.override { + pipewire = package; + }; + }; + + powerManagement = { + powerUpCommands = "${pkgs.kmod}/bin/modprobe apple_touchbar"; + powerDownCommands = "${pkgs.kmod}/bin/rmmod apple_touchbar"; + }; + + # After suspend, the inode for the backlight device has changed. This service + # simply restarts upower to inform it of that change. + systemd.services.fix-keyboard-backlight-after-suspend = { + after = [ "suspend.target" ]; + wantedBy = [ "suspend.target" ]; + + serviceConfig = { + Type = "simple"; + ExecStartPre = "${pkgs.coreutils}/bin/sleep 30"; + ExecStart = "${pkgs.systemd}/bin/systemctl --no-block restart upower.service"; + }; + }; +} diff --git a/apple/t2/pkgs/linux-t2.nix b/apple/t2/pkgs/linux-t2.nix new file mode 100644 index 000000000..c8636a6ab --- /dev/null +++ b/apple/t2/pkgs/linux-t2.nix @@ -0,0 +1,46 @@ +{ lib, buildLinux, fetchFromGitHub, fetchurl, ... } @ args: + +let + patchRepo = fetchFromGitHub { + owner = "t2linux"; + repo = "linux-t2-patches"; + rev = "13dee3659d1ef17c5ea588c8be629fe693045496"; + hash = "sha256-jUmFruM3K65nfxu9uztkieZDGEiUfJavzQqwrjSPzLc="; + }; + + version = "6.3.4"; + majorVersion = with lib; (elemAt (take 1 (splitVersion version)) 0); +in +buildLinux (args // { + inherit version; + + pname = "linux-t2"; + # Snippet from nixpkgs + modDirVersion = with lib; "${concatStringsSep "." (take 3 (splitVersion "${version}.0"))}"; + + src = fetchurl { + url = "mirror://kernel/linux/kernel/v${majorVersion}.x/linux-${version}.tar.xz"; + hash = "sha256-2GJ1KO1rOuYH0Ase9aRuDnBRrkCyhf1OgvT/C7craOg="; + }; + + structuredExtraConfig = with lib.kernel; { + APPLE_BCE = module; + APPLE_GMUX = module; + BRCMFMAC = module; + BT_BCM = module; + BT_HCIBCM4377 = module; + BT_HCIUART_BCM = yes; + BT_HCIUART = module; + HID_APPLE_IBRIDGE = module; + HID_APPLE = module; + HID_APPLE_MAGIC_BACKLIGHT = module; + HID_APPLE_TOUCHBAR = module; + HID_SENSOR_ALS = module; + SND_PCM = module; + STAGING = yes; + }; + + kernelPatches = lib.attrsets.mapAttrsToList (file: type: { name = file; patch = "${patchRepo}/${file}"; }) + (lib.attrsets.filterAttrs (file: type: type == "regular" && lib.strings.hasSuffix ".patch" file) + (builtins.readDir patchRepo)); +} // (args.argsOverride or {})) diff --git a/flake.nix b/flake.nix index 43a2143cb..79b599729 100644 --- a/flake.nix +++ b/flake.nix @@ -13,6 +13,7 @@ apple-macbook-pro-11-5 = import ./apple/macbook-pro/11-5; apple-macbook-pro-12-1 = import ./apple/macbook-pro/12-1; apple-macbook-pro-14-1 = import ./apple/macbook-pro/14-1; + apple-t2 = import ./apple/t2; asus-battery = import ./asus/battery.nix; asus-fx504gd = import ./asus/fx504gd; asus-rog-strix-g733qs = import ./asus/rog-strix/g733qs;