{
  inputs = {
    nixpkgs.url = "nixpkgs/nixpkgs-unstable";

    flake-parts = {
      url = "github:hercules-ci/flake-parts";
      inputs.nixpkgs-lib.follows = "nixpkgs";
    };

    android-nixpkgs.url = "github:tadfisher/android-nixpkgs";

    cursor.url = "github:omarcresp/cursor-flake/main";
  };


  outputs = {
    self,
    nixpkgs,
    flake-parts,
    cursor,
    ...
  } @ inputs:
    flake-parts.lib.mkFlake {inherit inputs;} {
      systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
      perSystem = {
        pkgs,
        system,
        lib,
        ...
      }: {
        devShells.default = let
          pkgs = import nixpkgs {
            inherit system;
            config = {
              allowUnfree = true;
              android_sdk.accept_license = true;
            };
            overlays = [];
          };
          android-nixpkgs = pkgs.callPackage inputs.android-nixpkgs {};
          androidSdk = android-nixpkgs.sdk (sdkPkgs:
            with sdkPkgs; [
              cmdline-tools-latest
              build-tools-34-0-0
              build-tools-30-0-3
              platform-tools
              platforms-android-35
              platforms-android-34
              platforms-android-33
              platforms-android-31
              platforms-android-30
              ndk-26-3-11579264
              cmake-3-22-1
            ]);
          PWD = builtins.getEnv "PWD";
          patchedFlutter = pkgs.flutter.override (prev: rec {
            flutter = prev.flutter.overrideAttrs (prevAttrs: {
              patches = prevAttrs.patches ++ [
                # This patch is needed to avoid the Kotlin Gradle plugin writing to the store.
                (pkgs.writeText "kotlin-fix.patch" ''
                  --- a/packages/flutter_tools/gradle/build.gradle.kts
                  +++ b/packages/flutter_tools/gradle/build.gradle.kts
                  @@ -4,6 +4,8 @@

                   import org.jetbrains.kotlin.gradle.dsl.JvmTarget

                  +gradle.startParameter.projectCacheDir = layout.buildDirectory.dir("cache").get().asFile
                  +
                   plugins {
                       `java-gradle-plugin`
                       groovy
                '')
              ];
              passthru = prevAttrs.passthru // {
                sdk = flutter;
              };
              # postInstall = (prevAttrs.postInstall or "") + ''
              #   mkdir -p $out/bin/cache
              #   touch $out/bin/cache/engine.realm
              # '';
            });
          });
        in
          pkgs.mkShell {
            ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
            ANDROID_NDK_ROOT = "${androidSdk}/libexec/android-sdk/ndk-bundle";
            ANDROID_AVD_HOME = "${PWD}/.android/avd";
            ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
            FLUTTER_SDK = "${patchedFlutter}";
            GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/share/android-sdk/build-tools/34.0.0/aapt2";
            buildInputs = with pkgs; [
              patchedFlutter
              jdk17
              androidSdk
              clang
              dart
              cmake
              android-tools
              cursor.packages.${pkgs.system}.default
            ];
              shellHook = ''
                zsh
                exit
              '';
          };
        # formatter = pkgs.alejandra;
      };
    };
}
