1{ version
2, engineVersion
3, patches
4, dart
5, src
6, lib
7, stdenv
8, darwin
9, git
10, which
11}:
12
13let
14 unwrapped =
15 stdenv.mkDerivation {
16 name = "flutter-${version}-unwrapped";
17 inherit src patches version;
18
19 outputs = [ "out" "cache" ];
20
21 buildInputs = [ git ];
22 nativeBuildInputs = [ ]
23 ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.DarwinTools ];
24
25 preConfigure = ''
26 if [ "$(< bin/internal/engine.version)" != '${engineVersion}' ]; then
27 echo 1>&2 "The given engine version (${engineVersion}) does not match the version required by the Flutter SDK ($(< bin/internal/engine.version))."
28 exit 1
29 fi
30 '';
31
32 postPatch = ''
33 patchShebangs --build ./bin/
34 '';
35
36 buildPhase = ''
37 export FLUTTER_ROOT="$(pwd)"
38 export FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools"
39 export SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
40
41 export SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot"
42 export STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp"
43
44 export DART_SDK_PATH="${dart}"
45
46 # The Flutter tool compilation requires dependencies to be cached, as there is no Internet access.
47 # Dart expects package caches to be mutable, and does not support composing cache directories.
48 # The packages cached during the build therefore cannot be easily used. They are provided through
49 # the derivation's "cache" output, though, in case they are needed.
50 #
51 # Note that non-cached packages will normally be fetched from the Internet when they are needed, so Flutter
52 # will function without an existing package cache as long as it has an Internet connection.
53 export PUB_CACHE="$cache"
54
55 if [ -d .pub-preload-cache ]; then
56 ${dart}/bin/dart pub cache preload .pub-preload-cache/*
57 elif [ -d .pub-cache ]; then
58 mv .pub-cache "$PUB_CACHE"
59 else
60 echo 'ERROR: Failed to locate the Dart package cache required to build the Flutter tool.'
61 exit 1
62 fi
63
64 pushd "$FLUTTER_TOOLS_DIR"
65 ${dart}/bin/dart pub get --offline
66 popd
67
68 local revision="$(cd "$FLUTTER_ROOT"; git rev-parse HEAD)"
69 ${dart}/bin/dart --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.dart_tool/package_config.json" "$SCRIPT_PATH"
70 echo "$revision" > "$STAMP_PATH"
71 echo -n "${version}" > version
72
73 # Certain prebuilts should be replaced with Nix-built (or at least Nix-patched) equivalents.
74 rm -r \
75 $FLUTTER_ROOT/bin/cache/dart-sdk \
76 $FLUTTER_ROOT/bin/cache/artifacts/engine
77 '';
78
79 installPhase = ''
80 runHook preInstall
81
82 mkdir -p $out
83 cp -r . $out
84 ln -sf ${dart} $out/bin/cache/dart-sdk
85
86 runHook postInstall
87 '';
88
89 doInstallCheck = true;
90 nativeInstallCheckInputs = [ which ]
91 ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.DarwinTools ];
92 installCheckPhase = ''
93 runHook preInstallCheck
94
95 export HOME="$(mktemp -d)"
96 $out/bin/flutter config --android-studio-dir $HOME
97 $out/bin/flutter config --android-sdk $HOME
98 $out/bin/flutter --version | fgrep -q '${version}'
99
100 runHook postInstallCheck
101 '';
102
103 passthru = {
104 inherit dart engineVersion;
105 # The derivation containing the original Flutter SDK files.
106 # When other derivations wrap this one, any unmodified files
107 # found here should be included as-is, for tooling compatibility.
108 sdk = unwrapped;
109 };
110
111 meta = with lib; {
112 description = "Flutter is Google's SDK for building mobile, web and desktop with Dart";
113 longDescription = ''
114 Flutter is Google’s UI toolkit for building beautiful,
115 natively compiled applications for mobile, web, and desktop from a single codebase.
116 '';
117 homepage = "https://flutter.dev";
118 license = licenses.bsd3;
119 platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
120 maintainers = with maintainers; [ babariviere ericdallo FlafyDev hacker1024 ];
121 };
122 };
123in
124unwrapped