1{
2 lib,
3 stdenv,
4 systemPlatform,
5 buildDartApplication,
6 runCommand,
7 writeTextFile,
8 git,
9 which,
10 dart,
11 version,
12 flutterSrc,
13 patches ? [ ],
14 pubspecLock,
15 engineVersion,
16}:
17
18let
19 # https://github.com/flutter/flutter/blob/17c92b7ba68ea609f4eb3405211d019c9dbc4d27/engine/src/flutter/tools/engine_tool/test/commands/stamp_command_test.dart#L125
20 engine_stamp = writeTextFile {
21 name = "engine_stamp";
22 text = builtins.toJSON {
23 build_date = "2025-06-27T12:30:00.000Z";
24 build_time_ms = 1751027400000;
25 git_revision = engineVersion;
26 git_revision_date = "2025-06-27T17:11:53-07:00";
27 content_hash = "1111111111111111111111111111111111111111";
28 };
29 };
30
31 dartEntryPoints."flutter_tools.snapshot" = "bin/flutter_tools.dart";
32in
33buildDartApplication.override { inherit dart; } {
34 pname = "flutter-tools";
35 inherit version dartEntryPoints;
36 dartOutputType = "jit-snapshot";
37
38 src = flutterSrc;
39 sourceRoot = "${flutterSrc.name}/packages/flutter_tools";
40 postUnpack = ''chmod -R u+w "$NIX_BUILD_TOP/source"'';
41
42 inherit patches;
43 # The given patches are made for the entire SDK source tree.
44 prePatch = ''pushd "$NIX_BUILD_TOP/source"'';
45 postPatch = ''
46 popd
47 ''
48 # Use arm64 instead of arm64e.
49 + lib.optionalString stdenv.hostPlatform.isDarwin ''
50 substituteInPlace lib/src/ios/xcodeproj.dart \
51 --replace-fail arm64e arm64
52 ''
53 # need network
54 + lib.optionalString (lib.versionAtLeast version "3.35.0") ''
55 cp ${engine_stamp} ../../bin/cache/engine_stamp.json
56 substituteInPlace lib/src/flutter_cache.dart \
57 --replace-fail "registerArtifact(FlutterEngineStamp(this, logger));" ""
58 '';
59
60 # When the JIT snapshot is being built, the application needs to run.
61 # It attempts to generate configuration files, and relies on a few external
62 # tools.
63 nativeBuildInputs = [
64 git
65 which
66 ];
67 preConfigure = ''
68 export HOME=.
69 export FLUTTER_ROOT="$NIX_BUILD_TOP/source"
70 mkdir -p "$FLUTTER_ROOT/bin/cache"
71 ln -s '${dart}' "$FLUTTER_ROOT/bin/cache/dart-sdk"
72 '';
73
74 dartCompileFlags = [ "--define=NIX_FLUTTER_HOST_PLATFORM=${systemPlatform}" ];
75
76 # The Dart wrapper launchers are useless for the Flutter tool - it is designed
77 # to be launched from a snapshot by the SDK.
78 postInstall = ''
79 pushd "$out"
80 rm ${builtins.concatStringsSep " " (builtins.attrNames dartEntryPoints)}
81 popd
82 '';
83
84 sdkSourceBuilders = {
85 # https://github.com/dart-lang/pub/blob/e1fbda73d1ac597474b82882ee0bf6ecea5df108/lib/src/sdk/dart.dart#L80
86 "dart" =
87 name:
88 runCommand "dart-sdk-${name}" { passthru.packageRoot = "."; } ''
89 for path in '${dart}/pkg/${name}'; do
90 if [ -d "$path" ]; then
91 ln -s "$path" "$out"
92 break
93 fi
94 done
95
96 if [ ! -e "$out" ]; then
97 echo 1>&2 'The Dart SDK does not contain the requested package: ${name}!'
98 exit 1
99 fi
100 '';
101 };
102
103 inherit pubspecLock;
104}