pmos-work-a71/dev/default.nix

103 lines
4.1 KiB
Nix

{
perSystem = {
config,
pkgs,
...
}: {
devShells.default =
let
lineageBuildZipUrl = "https://mirrorbits.lineageos.org/full/a71/20241225/lineage-21.0-20241225-nightly-a71-signed.zip";
lineageBuildZip = pkgs.fetchurl {
url = lineageBuildZipUrl;
hash = "sha256-G+ibXe1crfhFnB2sfSKtI4XDx1qysMj75H7aTJcUUm0=";
};
lineageBuildZipContent = pkgs.fetchzip {
url = lineageBuildZipUrl;
hash = "sha256-nBn26btXmW5u1oYCqucAbk6ixzMxGNapuMofBisv56o=";
stripRoot = false;
};
magiskApk = pkgs.fetchurl {
url = "https://github.com/topjohnwu/Magisk/releases/download/v28.1/Magisk-v28.1.apk";
hash = "sha256-i/0zRrPaWBT4Lv9vGxtf7dCtWF85olcJsj61SqxFaR0=";
};
emptyDtbo = pkgs.runCommand "empty-dtbo" {} ''
mkdir $out
dd if=/dev/zero of=$out/empty_dtbo.img count=1
${pkgs.android-tools}/bin/avbtool add_hash_footer --partition_name dtbo --partition_size 2621440 --image $out/empty_dtbo.img
'';
# rootDir = builtins.toString ./..;
rootDir = "."; # Assume PWD as root for now
in
pkgs.mkShell {
packages = with pkgs; [
(python3.withPackages (pypi: with pypi; [
libfdt
]))
(writeShellScriptBin "flash-lineage-magisk" ''
rootDir="${rootDir}"
lineageBootImg="$rootDir/.cache/lineageos-magisk-boot.img"
if [[ ! -e "$lineageBootImg" ]]; then
echo "Wait for Android to have fully booted and ensure USB debugging is enabled."
read -p "Press enter to continue..."
mkdir -p "$rootDir/.cache"
installedMagisk=$(adb shell pm list packages com.topjohnwu.magisk)
if [[ -z "$installedMagisk" ]]; then
echo "Magisk not found on device. Installing now..."
adb sideload "${magiskApk}"
fi
adb push "${lineageBuildZipContent}/boot.img" "/storage/emulated/0/Download/lineageos-boot.img"
echo "Launching Magisk. Patch the boot image located in the downloads directory."
adb shell monkey -p com.topjohnwu.magisk 1
read -p "Press enter to continue..."
adb shell 'cp /storage/emulated/0/Download/magisk_patched*.img /storage/emulated/0/Download/magisk-boot.img'
sleep 1
adb shell 'rm /storage/emulated/0/Download/magisk_patched*.img'
adb pull "/storage/emulated/0/Download/magisk-boot.img" "$lineageBootImg"
fi
echo "Boot into download mode."
read -p "Press enter to continue..."
heimdall flash --BOOT "$lineageBootImg"
'')
(writeShellScriptBin "flash-lineage" ''
echo "Installing LineageOS..."
adb sideload ${lineageBuildZip}
flash-lineage-magisk
'')
(writeShellScriptBin "flash-postmarketos" ''
echo "Installing postmarketOS..."
heimdall flash \
--no-reboot \
--DTBO ${emptyDtbo}/empty_dtbo.img
# does not work for some reason
# pmbootstrap flasher --method heimdall-bootimg --resume flash_kernel
'')
(writeShellScriptBin "init-repositories" ''
rootDir="${rootDir}"
mkdir -p "$rootDir/.repos"
[[ -d "$rootDir/.repos/mainline-kernel" ]] || git clone git@github.com:WiiPlayer2/linux-sm7150.git "$rootDir/.repos/mainline-kernel"
'')
(writeScriptBin "build-and-flash-kernel" ''
#!/usr/bin/env bash
rootDir="${rootDir}"
pushd $rootDir/.repos/mainline-kernel
source $(pmbootstrap-envkernel) && \
( [[ ! -e ".output/.config" ]] || make defconfig sm7150.config ) && \
make -j$(nproc) && \
pmbootstrap build --envkernel linux-postmarketos-qcom-sm7150 && \
pmbootstrap flasher --method fastboot flash_kernel && \
fastboot reboot
popd
'')
];
};
};
}