diff --git a/devices/xiaomi-lavender/default.nix b/devices/xiaomi-lavender/default.nix index d96cbb89..3845aa65 100644 --- a/devices/xiaomi-lavender/default.nix +++ b/devices/xiaomi-lavender/default.nix @@ -43,6 +43,9 @@ # This device adds skip_initramfs to cmdline for normal boots boot_as_recovery = true; + # Though this device has "boot_as_recovery", it still has a classic + # recovery partition for recovery. Go figure. + has_recovery_partition = true; vendor_partition = "/dev/disk/by-partlabel/vendor"; gadgetfs.functions = { diff --git a/modules/system-types/android.nix b/modules/system-types/android.nix index 42884bd3..fb7665a7 100644 --- a/modules/system-types/android.nix +++ b/modules/system-types/android.nix @@ -1,6 +1,8 @@ { config, pkgs, lib, modules, baseModules, ... }: let + inherit (lib) optionalString; + # In the future, this pattern should be extracted. # We're basically subclassing the main config, just like nesting does in # NixOS () @@ -15,6 +17,22 @@ let }]; }).config; + withRecovery = let + inherit (device_config) info; + withBootAsRecovery = if info ? boot_as_recovery then info.boot_as_recovery else false; + in + # As defined... Some devices will have a discrete recovery partition even + # if the system is "boot as recovery". + if info ? has_recovery_partition + then info.has_recovery_partition + # Defaults: with 'boot as recovery' → no recovery ; without 'boot as recovery' → with recovery + else !withBootAsRecovery + ; + + withAB = let inherit (device_config) info; in + if info ? ab_partitions then info.ab_partitions else false + ; + device_config = config.mobile.device; device_name = device_config.name; enabled = config.mobile.system.type == "android"; @@ -32,11 +50,30 @@ let initrd = config.system.build.initrd; }; + # Note: + # The flash scripts, by design, are not using nix-provided paths for + # either of fastboot or the outputs. + # This is because this output should have no refs. A simple tarball of this + # output should be usable even on systems without Nix. + # TODO: Embed device-specific fastboot instructions as `echo` in the script. android-device = pkgs.runCommandNoCC "android-device-${device_name}" {} '' mkdir -p $out - ln -s ${rootfs}/${rootfs.filename} $out/system.img - ln -s ${android-bootimg} $out/boot.img - ln -s ${android-recovery} $out/recovery.img + cp -v ${rootfs}/${rootfs.filename} $out/system.img + cp -v ${android-bootimg} $out/boot.img + ${optionalString withRecovery '' + cp -v ${android-recovery} $out/recovery.img + ''} + cat > $out/flash-critical.sh <<'EOF' + #!/usr/bin/env bash + dir="$(cd "$(dirname "''${BASH_SOURCE[0]}")"; echo "$PWD")" + PS4=" $ " + set -x + fastboot flash ${optionalString withAB "--slot=all"} boot "$dir"/boot.img + ${optionalString withRecovery '' + fastboot flash ${optionalString withAB "--slot=all"} recovery "$dir"/recovery.img + ''} + EOF + chmod +x $out/flash-critical.sh ''; in {