1{
2 useNixpkgsEngine ? false,
3 version,
4 engineVersion,
5 engineHashes ? { },
6 engineUrl ? "https://github.com/flutter/engine.git@${engineVersion}",
7 enginePatches ? [ ],
8 engineRuntimeModes ? [
9 "release"
10 "debug"
11 ],
12 engineSwiftShaderHash,
13 engineSwiftShaderRev,
14 patches,
15 channel,
16 dart,
17 src,
18 pubspecLock,
19 artifactHashes ? null,
20 lib,
21 stdenv,
22 callPackage,
23 makeWrapper,
24 darwin,
25 git,
26 which,
27 jq,
28 flutterTools ? null,
29}@args:
30
31let
32 engine =
33 if args.useNixpkgsEngine or false then
34 callPackage ./engine/default.nix {
35 inherit (args) dart;
36 dartSdkVersion = args.dart.version;
37 flutterVersion = version;
38 swiftshaderRev = engineSwiftShaderRev;
39 swiftshaderHash = engineSwiftShaderHash;
40 version = engineVersion;
41 hashes = engineHashes;
42 url = engineUrl;
43 patches = enginePatches;
44 runtimeModes = engineRuntimeModes;
45 }
46 else
47 null;
48
49 dart = if args.useNixpkgsEngine or false then engine.dart else args.dart;
50
51 flutterTools =
52 args.flutterTools or (callPackage ./flutter-tools.nix {
53 inherit dart version;
54 flutterSrc = src;
55 inherit patches;
56 inherit pubspecLock;
57 systemPlatform = stdenv.hostPlatform.system;
58 });
59
60 unwrapped = stdenv.mkDerivation {
61 name = "flutter-${version}-unwrapped";
62 inherit src patches version;
63
64 buildInputs = [ git ];
65 nativeBuildInputs = [
66 makeWrapper
67 jq
68 ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.DarwinTools ];
69
70 preConfigure = ''
71 if [ "$(< bin/internal/engine.version)" != '${engineVersion}' ]; then
72 echo 1>&2 "The given engine version (${engineVersion}) does not match the version required by the Flutter SDK ($(< bin/internal/engine.version))."
73 exit 1
74 fi
75 '';
76
77 postPatch = ''
78 patchShebangs --build ./bin/
79 '';
80
81 buildPhase = ''
82 # The flutter_tools package tries to run many Git commands. In most
83 # cases, unexpected output is handled gracefully, but commands are never
84 # expected to fail completely. A blank repository needs to be created.
85 rm -rf .git # Remove any existing Git directory
86 git init -b nixpkgs
87 GIT_AUTHOR_NAME=Nixpkgs GIT_COMMITTER_NAME=Nixpkgs \
88 GIT_AUTHOR_EMAIL= GIT_COMMITTER_EMAIL= \
89 GIT_AUTHOR_DATE='1/1/1970 00:00:00 +0000' GIT_COMMITTER_DATE='1/1/1970 00:00:00 +0000' \
90 git commit --allow-empty -m "Initial commit"
91 (. '${../../../build-support/fetchgit/deterministic-git}'; make_deterministic_repo .)
92
93 mkdir -p bin/cache
94
95 # Add a flutter_tools artifact stamp, and build a snapshot.
96 # This is the Flutter CLI application.
97 echo "$(git rev-parse HEAD)" > bin/cache/flutter_tools.stamp
98 ln -s '${flutterTools}/share/flutter_tools.snapshot' bin/cache/flutter_tools.snapshot
99
100 # Some of flutter_tools's dependencies contain static assets. The
101 # application attempts to read its own package_config.json to find these
102 # assets at runtime.
103 # TODO: Remove this once Flutter 3.24 is the lowest version in Nixpkgs.
104 # https://github.com/flutter/flutter/pull/150340 makes it redundant.
105 mkdir -p packages/flutter_tools/.dart_tool
106 ln -s '${flutterTools.pubcache}/package_config.json' packages/flutter_tools/.dart_tool/package_config.json
107
108 echo -n "${version}" > version
109 cat <<EOF > bin/cache/flutter.version.json
110 {
111 "devToolsVersion": "$(cat "${dart}/bin/resources/devtools/version.json" | jq -r .version)",
112 "flutterVersion": "${version}",
113 "frameworkVersion": "${version}",
114 "channel": "${channel}",
115 "repositoryUrl": "https://github.com/flutter/flutter.git",
116 "frameworkRevision": "nixpkgs000000000000000000000000000000000",
117 "frameworkCommitDate": "1970-01-01 00:00:00",
118 "engineRevision": "${engineVersion}",
119 "dartSdkVersion": "${dart.version}"
120 }
121 EOF
122
123 # Suppress a small error now that `.gradle`'s location changed.
124 # Location changed because of the patch "gradle-flutter-tools-wrapper.patch".
125 mkdir -p "$out/packages/flutter_tools/gradle/.gradle"
126 '';
127
128 installPhase = ''
129 runHook preInstall
130
131 mkdir -p $out
132 cp -r . $out
133 rm -rf $out/bin/cache/dart-sdk
134 ln -sf ${dart} $out/bin/cache/dart-sdk
135
136 # The regular launchers are designed to download/build/update SDK
137 # components, and are not very useful in Nix.
138 # Replace them with simple links and wrappers.
139 rm "$out/bin"/{dart,flutter}
140 ln -s "$out/bin/cache/dart-sdk/bin/dart" "$out/bin/dart"
141 makeShellWrapper "$out/bin/dart" "$out/bin/flutter" \
142 --set-default FLUTTER_ROOT "$out" \
143 --set FLUTTER_ALREADY_LOCKED true \
144 --add-flags "--disable-dart-dev --packages='${flutterTools.pubcache}/package_config.json' \$NIX_FLUTTER_TOOLS_VM_OPTIONS $out/bin/cache/flutter_tools.snapshot"
145
146 runHook postInstall
147 '';
148
149 doInstallCheck = true;
150 nativeInstallCheckInputs = [
151 which
152 ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.DarwinTools ];
153 installCheckPhase = ''
154 runHook preInstallCheck
155
156 export HOME="$(mktemp -d)"
157 $out/bin/flutter config --android-studio-dir $HOME
158 $out/bin/flutter config --android-sdk $HOME
159 $out/bin/flutter --version | fgrep -q '${builtins.substring 0 10 engineVersion}'
160
161 runHook postInstallCheck
162 '';
163
164 passthru =
165 {
166 # TODO: rely on engine.version instead of engineVersion
167 inherit
168 dart
169 engineVersion
170 artifactHashes
171 channel
172 ;
173 tools = flutterTools;
174 # The derivation containing the original Flutter SDK files.
175 # When other derivations wrap this one, any unmodified files
176 # found here should be included as-is, for tooling compatibility.
177 sdk = unwrapped;
178 }
179 // lib.optionalAttrs (engine != null) {
180 inherit engine;
181 };
182
183 meta = with lib; {
184 description = "Flutter is Google's SDK for building mobile, web and desktop with Dart";
185 longDescription = ''
186 Flutter is Google’s UI toolkit for building beautiful,
187 natively compiled applications for mobile, web, and desktop from a single codebase.
188 '';
189 homepage = "https://flutter.dev";
190 license = licenses.bsd3;
191 platforms = [
192 "x86_64-linux"
193 "aarch64-linux"
194 "x86_64-darwin"
195 "aarch64-darwin"
196 ];
197 maintainers =
198 teams.flutter.members
199 ++ (with maintainers; [
200 ericdallo
201 ]);
202 };
203 };
204in
205unwrapped