1{ lib
2, stdenv
3, darwin
4, callPackage
5, flutter
6, supportedTargetFlutterPlatforms ? [
7 "universal"
8 "web"
9 ]
10 ++ lib.optional (stdenv.hostPlatform.isLinux && !(flutter ? engine)) "linux"
11 ++ lib.optional (stdenv.hostPlatform.isx86_64 || stdenv.hostPlatform.isDarwin) "android"
12 ++ lib.optionals stdenv.hostPlatform.isDarwin [ "macos" "ios" ]
13, artifactHashes ? flutter.artifactHashes
14, extraPkgConfigPackages ? [ ]
15, extraLibraries ? [ ]
16, extraIncludes ? [ ]
17, extraCxxFlags ? [ ]
18, extraCFlags ? [ ]
19, extraLinkerFlags ? [ ]
20, makeWrapper
21, runCommandLocal
22, writeShellScript
23, wrapGAppsHook3
24, git
25, which
26, pkg-config
27, atk
28, cairo
29, gdk-pixbuf
30, glib
31, gtk3
32, harfbuzz
33, libepoxy
34, pango
35, libX11
36, xorgproto
37, libdeflate
38, zlib
39, cmake
40, ninja
41, clang
42, lndir
43, symlinkJoin
44}:
45
46let
47 supportsLinuxDesktopTarget = builtins.elem "linux" supportedTargetFlutterPlatforms;
48
49 flutterPlatformArtifacts = lib.genAttrs supportedTargetFlutterPlatforms (flutterPlatform:
50 (callPackage ./artifacts/prepare-artifacts.nix {
51 src = callPackage ./artifacts/fetch-artifacts.nix {
52 inherit flutterPlatform;
53 systemPlatform = stdenv.hostPlatform.system;
54 flutter = callPackage ./wrapper.nix { inherit flutter; };
55 hash = artifactHashes.${flutterPlatform}.${stdenv.hostPlatform.system} or "";
56 };
57 }));
58
59 cacheDir = symlinkJoin rec {
60 name = "flutter-cache-dir";
61 paths = builtins.attrValues flutterPlatformArtifacts;
62 postBuild = ''
63 mkdir -p "$out/bin/cache"
64 ln -s '${flutter}/bin/cache/dart-sdk' "$out/bin/cache"
65 '';
66 passthru.flutterPlatform = flutterPlatformArtifacts;
67 };
68
69 # By default, Flutter stores downloaded files (such as the Pub cache) in the SDK directory.
70 # Wrap it to ensure that it does not do that, preferring home directories instead.
71 immutableFlutter = writeShellScript "flutter_immutable" ''
72 export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
73 ${flutter}/bin/flutter "$@"
74 '';
75
76 # Tools that the Flutter tool depends on.
77 tools = [ git which ];
78
79 # Libraries that Flutter apps depend on at runtime.
80 appRuntimeDeps = lib.optionals supportsLinuxDesktopTarget [
81 atk
82 cairo
83 gdk-pixbuf
84 glib
85 gtk3
86 harfbuzz
87 libepoxy
88 pango
89 libX11
90 libdeflate
91 ];
92
93 # Development packages required for compilation.
94 appBuildDeps =
95 let
96 # https://discourse.nixos.org/t/handling-transitive-c-dependencies/5942/3
97 deps = pkg: builtins.filter lib.isDerivation ((pkg.buildInputs or [ ]) ++ (pkg.propagatedBuildInputs or [ ]));
98 collect = pkg: lib.unique ([ pkg ] ++ deps pkg ++ builtins.concatMap collect (deps pkg));
99 in
100 builtins.concatMap collect appRuntimeDeps;
101
102 # Some header files and libraries are not properly located by the Flutter SDK.
103 # They must be manually included.
104 appStaticBuildDeps = (lib.optionals supportsLinuxDesktopTarget [ libX11 xorgproto zlib ]) ++ extraLibraries;
105
106 # Tools used by the Flutter SDK to compile applications.
107 buildTools = lib.optionals supportsLinuxDesktopTarget [
108 pkg-config
109 cmake
110 ninja
111 clang
112 ];
113
114 # Nix-specific compiler configuration.
115 pkgConfigPackages = map (lib.getOutput "dev") (appBuildDeps ++ extraPkgConfigPackages);
116 includeFlags = map (pkg: "-isystem ${lib.getOutput "dev" pkg}/include") (appStaticBuildDeps ++ extraIncludes);
117 linkerFlags = (map (pkg: "-rpath,${lib.getOutput "lib" pkg}/lib") appRuntimeDeps) ++ extraLinkerFlags;
118in
119(callPackage ./sdk-symlink.nix { }) (stdenv.mkDerivation
120{
121 pname = "flutter-wrapped";
122 inherit (flutter) version;
123
124 nativeBuildInputs = [ makeWrapper ]
125 ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.DarwinTools ]
126 ++ lib.optionals supportsLinuxDesktopTarget [ glib wrapGAppsHook3 ];
127
128 passthru = flutter.passthru // {
129 inherit (flutter) version;
130 unwrapped = flutter;
131 updateScript = ./update/update.py;
132 inherit cacheDir;
133 };
134
135 dontUnpack = true;
136 dontWrapGApps = true;
137
138 installPhase = ''
139 runHook preInstall
140
141 for path in ${builtins.concatStringsSep " " (builtins.foldl' (paths: pkg: paths ++ (map (directory: "'${pkg}/${directory}/pkgconfig'") ["lib" "share"])) [ ] pkgConfigPackages)}; do
142 addToSearchPath FLUTTER_PKG_CONFIG_PATH "$path"
143 done
144
145 mkdir -p $out/bin
146 makeWrapper '${immutableFlutter}' $out/bin/flutter \
147 --set-default ANDROID_EMULATOR_USE_SYSTEM_LIBS 1 \
148 '' + lib.optionalString (flutter ? engine && flutter.engine.meta.available) ''
149 --set-default FLUTTER_ENGINE "${flutter.engine}" \
150 --add-flags "--local-engine-host ${flutter.engine.outName}" \
151 '' + '' --suffix PATH : '${lib.makeBinPath (tools ++ buildTools)}' \
152 --suffix PKG_CONFIG_PATH : "$FLUTTER_PKG_CONFIG_PATH" \
153 --suffix LIBRARY_PATH : '${lib.makeLibraryPath appStaticBuildDeps}' \
154 --prefix CXXFLAGS "''\t" '${builtins.concatStringsSep " " (includeFlags ++ extraCxxFlags)}' \
155 --prefix CFLAGS "''\t" '${builtins.concatStringsSep " " (includeFlags ++ extraCFlags)}' \
156 --prefix LDFLAGS "''\t" '${builtins.concatStringsSep " " (map (flag: "-Wl,${flag}") linkerFlags)}' \
157 ''${gappsWrapperArgs[@]}
158
159 runHook postInstall
160 '';
161
162 inherit (flutter) meta;
163})