nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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
41 inherit patches;
42 # The given patches are made for the entire SDK source tree.
43 prePatch = ''
44 chmod --recursive u+w "../.."
45 pushd "../.."
46 '';
47 postPatch = ''
48 popd
49 ''
50 # Use arm64 instead of arm64e.
51 + lib.optionalString stdenv.hostPlatform.isDarwin ''
52 substituteInPlace lib/src/ios/xcodeproj.dart \
53 --replace-fail arm64e arm64
54 ''
55 # need network
56 + lib.optionalString (lib.versionAtLeast version "3.35.0") ''
57 cp ${engine_stamp} ../../bin/cache/engine_stamp.json
58 substituteInPlace lib/src/flutter_cache.dart \
59 --replace-fail "registerArtifact(FlutterEngineStamp(this, logger));" ""
60 '';
61
62 # When the JIT snapshot is being built, the application needs to run.
63 # It attempts to generate configuration files, and relies on a few external
64 # tools.
65 nativeBuildInputs = [
66 git
67 which
68 ];
69 preConfigure = ''
70 export HOME=.
71 export FLUTTER_ROOT="$(realpath ../../)"
72 mkdir --parents "$FLUTTER_ROOT/bin/cache"
73 ln --symbolic '${dart}' "$FLUTTER_ROOT/bin/cache/dart-sdk"
74 '';
75
76 dartCompileFlags = [ "--define=NIX_FLUTTER_HOST_PLATFORM=${systemPlatform}" ];
77
78 # The Dart wrapper launchers are useless for the Flutter tool - it is designed
79 # to be launched from a snapshot by the SDK.
80 postInstall = ''
81 pushd "$out"
82 rm ${builtins.concatStringsSep " " (builtins.attrNames dartEntryPoints)}
83 popd
84 '';
85
86 sdkSourceBuilders = {
87 # https://github.com/dart-lang/pub/blob/e1fbda73d1ac597474b82882ee0bf6ecea5df108/lib/src/sdk/dart.dart#L80
88 "dart" =
89 name:
90 runCommand "dart-sdk-${name}" { passthru.packageRoot = "."; } ''
91 for path in '${dart}/pkg/${name}'; do
92 if [ -d "$path" ]; then
93 ln -s "$path" "$out"
94 break
95 fi
96 done
97
98 if [ ! -e "$out" ]; then
99 echo 1>&2 'The Dart SDK does not contain the requested package: ${name}!'
100 exit 1
101 fi
102 '';
103 };
104
105 inherit pubspecLock;
106}