···11+diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart
22+index e4e474ab6e..5548599802 100644
33+--- a/packages/flutter_tools/lib/src/runner/flutter_command.dart
44++++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart
55+@@ -1693,7 +1693,7 @@ Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and
66+77+ // Populate the cache. We call this before pub get below so that the
88+ // sky_engine package is available in the flutter cache for pub to find.
99+- if (shouldUpdateCache) {
1010++ if (false) {
1111+ // First always update universal artifacts, as some of these (e.g.
1212+ // ios-deploy on macOS) are required to determine `requiredArtifacts`.
1313+ final bool offline;
1414+diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
1515+index a1104da..1749d65 100644
1616+--- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
1717++++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
1818+@@ -444,12 +444,8 @@ class FlutterCommandRunner extends CommandRunner<void> {
1919+ globals.analytics.suppressTelemetry();
2020+ }
2121+2222+- globals.flutterVersion.ensureVersionFile();
2323+ final bool machineFlag =
2424+ topLevelResults[FlutterGlobalOptions.kMachineFlag] as bool? ?? false;
2525+- if (await _shouldCheckForUpdates(topLevelResults, topLevelMachineFlag: machineFlag)) {
2626+- await globals.flutterVersion.checkFlutterVersionFreshness();
2727+- }
2828+2929+ // See if the user specified a specific device.
3030+ final String? specifiedDeviceId =
···11+From 6df275df3b8694daf16302b407520e3b1dee6724 Mon Sep 17 00:00:00 2001
22+From: Philip Hayes <philiphayes9@gmail.com>
33+Date: Thu, 12 Sep 2024 13:23:00 -0700
44+Subject: [PATCH] fix: cleanup xcode_backend.sh to fix iOS build w/
55+ `NixOS/nixpkgs` flutter
66+77+This patch cleans up `xcode_backend.sh`. It now effectively just runs
88+`exec $FLUTTER_ROOT/bin/dart ./xcode_backend.dart`.
99+1010+The previous `xcode_backend.sh` tries to discover `$FLUTTER_ROOT` from
1111+argv[0], even though its presence is already guaranteed (the wrapped
1212+`xcode_backend.dart` also relies on this env).
1313+1414+When using nixpkgs flutter, the flutter SDK directory is composed of several
1515+layers, joined together using symlinks (called a `symlinkJoin`). Without this
1616+patch, the auto-discover traverses the symlinks into the wrong layer, and so it
1717+uses an "unwrapped" `dart` command instead of a "wrapped" dart that sets some
1818+important envs/flags (like `$FLUTTER_ROOT`).
1919+2020+Using the "unwrapped" dart then manifests in this error when compiling, since
2121+it doesn't see the ios build-support artifacts:
2222+2323+```
2424+$ flutter run -d iphone
2525+Running Xcode build...
2626+Xcode build done. 6.4s
2727+Failed to build iOS app
2828+Error (Xcode): Target debug_unpack_ios failed: Error: Flutter failed to create a directory at "/<nix-store>/XXXX-flutter-3.24.1-unwrapped/bin/cache/artifacts".
2929+```
3030+---
3131+ packages/flutter_tools/bin/xcode_backend.sh | 25 ++++-----------------
3232+ 1 file changed, 4 insertions(+), 21 deletions(-)
3333+3434+diff --git a/packages/flutter_tools/bin/xcode_backend.sh b/packages/flutter_tools/bin/xcode_backend.sh
3535+index 2889d7c8e4..48b9d06c6e 100755
3636+--- a/packages/flutter_tools/bin/xcode_backend.sh
3737++++ b/packages/flutter_tools/bin/xcode_backend.sh
3838+@@ -6,24 +6,7 @@
3939+ # exit on error, or usage of unset var
4040+ set -euo pipefail
4141+4242+-# Needed because if it is set, cd may print the path it changed to.
4343+-unset CDPATH
4444+-
4545+-function follow_links() (
4646+- cd -P "$(dirname -- "$1")"
4747+- file="$PWD/$(basename -- "$1")"
4848+- while [[ -h "$file" ]]; do
4949+- cd -P "$(dirname -- "$file")"
5050+- file="$(readlink -- "$file")"
5151+- cd -P "$(dirname -- "$file")"
5252+- file="$PWD/$(basename -- "$file")"
5353+- done
5454+- echo "$file"
5555+-)
5656+-
5757+-PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")"
5858+-BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
5959+-FLUTTER_ROOT="$BIN_DIR/../../.."
6060+-DART="$FLUTTER_ROOT/bin/dart"
6161+-
6262+-"$DART" "$BIN_DIR/xcode_backend.dart" "$@"
6363++# Run `dart ./xcode_backend.dart` with the dart from $FLUTTER_ROOT.
6464++dart="${FLUTTER_ROOT}/bin/dart"
6565++xcode_backend_dart="${BASH_SOURCE[0]%.sh}.dart"
6666++exec "${dart}" "${xcode_backend_dart}" "$@"
6767+--
6868+2.46.0
6969+
···11+This patch introduces an intermediate Gradle build step to alter the behavior
22+of flutter_tools' Gradle project, specifically moving the creation of `build`
33+and `.gradle` directories from within the Nix Store to somewhere in `$HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev`.
44+55+Without this patch, flutter_tools' Gradle project tries to generate `build` and `.gradle`
66+directories within the Nix Store. Resulting in read-only errors when trying to build a
77+Flutter Android app at runtime.
88+99+This patch takes advantage of the fact settings.gradle takes priority over settings.gradle.kts to build the intermediate Gradle project
1010+when a Flutter app runs `includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")`
1111+1212+`rootProject.buildFileName = "/dev/null"` so that the intermediate project doesn't use `build.gradle.kts` that's in the same directory.
1313+1414+The intermediate project makes a `settings.gradle` file in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` and `includeBuild`s it.
1515+This Gradle project will build the actual `packages/flutter_tools/gradle` project by setting
1616+`rootProject.projectDir = new File("$settingsDir")` and `apply from: new File("$settingsDir/settings.gradle.kts")`.
1717+1818+Now the `.gradle` will be built in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/`, but `build` doesn't.
1919+To move `build` to `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` as well, we need to set `buildDirectory`.
2020+diff --git a/packages/flutter_tools/gradle/settings.gradle b/packages/flutter_tools/gradle/settings.gradle
2121+new file mode 100644
2222+index 0000000000..b2485c94b4
2323+--- /dev/null
2424++++ b/packages/flutter_tools/gradle/settings.gradle
2525+@@ -0,0 +1,19 @@
2626++rootProject.buildFileName = "/dev/null"
2727++
2828++def engineShortRev = (new File("$settingsDir/../../../bin/internal/engine.version")).text.take(10)
2929++def dir = new File("$System.env.HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev")
3030++dir.mkdirs()
3131++def file = new File(dir, "settings.gradle")
3232++
3333++file.text = """
3434++rootProject.projectDir = new File("$settingsDir")
3535++apply from: new File("$settingsDir/settings.gradle.kts")
3636++
3737++gradle.allprojects { project ->
3838++ project.beforeEvaluate {
3939++ project.layout.buildDirectory = new File("$dir/build")
4040++ }
4141++}
4242++"""
4343++
4444++includeBuild(dir)