flutter: Modularize unwrapped, wrapped, and FHS components

This commit is contained in:
hacker1024 2022-11-14 13:11:49 +11:00
parent c6b044c101
commit ea48c7c9b6
5 changed files with 232 additions and 229 deletions

View file

@ -1,11 +1,13 @@
{ callPackage, fetchurl, dart }:
let
mkFlutter = opts: callPackage (import ./flutter.nix opts) { };
wrapFlutter = flutter: callPackage (import ./wrapper.nix) { flutter = flutter; };
mkFlutterFHS = flutter: callPackage (import ./fhs.nix) { flutter = flutter; };
getPatches = dir:
let files = builtins.attrNames (builtins.readDir dir);
in map (f: dir + ("/" + f)) files;
flutterDrv = { version, pname, dartVersion, hash, dartHash, patches }: mkFlutter {
inherit version pname patches;
flutterDrv = { version, dartVersion, hash, dartHash, patches }: mkFlutter {
inherit version patches;
dart = dart.override {
version = dartVersion;
sources = {
@ -26,9 +28,8 @@ let
};
in
{
inherit mkFlutter;
inherit mkFlutter wrapFlutter mkFlutterFHS flutterDrv;
stable = flutterDrv {
pname = "flutter";
version = "3.3.3";
dartVersion = "2.18.2";
hash = "sha256-MTZeWQUp4/TcPzYIT6eqIKSPUPvn2Mp/thOQzNgpTXg=";
@ -40,7 +41,6 @@ in
};
v2 = flutterDrv {
pname = "flutter";
version = "2.10.5";
dartVersion = "2.16.2";
hash = "sha256-DTZwxlMUYk8NS1SaWUJolXjD+JnRW73Ps5CdRHDGnt0=";

View file

@ -0,0 +1,71 @@
{ lib
, stdenv
, flutter
, buildFHSUserEnv
, runCommandLocal
, supportsAndroidEmulator ? stdenv.isLinux
}:
# Wrap flutter inside an fhs user env to allow execution of binary,
# like adb from $ANDROID_HOME or java from android-studio.
buildFHSUserEnv {
name = flutter.name;
multiPkgs = pkgs: with pkgs; ([
# Flutter only use these certificates
(runCommandLocal "fedoracert" { } ''
mkdir -p $out/etc/pki/tls/
ln -s ${cacert}/etc/ssl/certs $out/etc/pki/tls/certs
'')
zlib
]);
targetPkgs = pkgs: with pkgs; ([
flutter
# General ecosystem dependencies
bash
curl
git
unzip
which
xz
# flutter test requires this lib
libGLU
] ++ lib.optional supportsAndroidEmulator [
# for android emulator
alsa-lib
dbus
expat
libpulseaudio
libuuid
xorg.libX11
xorg.libxcb
xorg.libXcomposite
xorg.libXcursor
xorg.libXdamage
xorg.libXext
xorg.libXfixes
xorg.libXi
xorg.libXrender
xorg.libXtst
libGL
nspr
nss
systemd
]);
runScript = "flutter";
passthru = {
inherit flutter;
};
meta = flutter.meta // {
longDescription = ''
${flutter.meta.longDescription}
Wrapped in a FHS environment to improve compatibility with internal tools and tools in the ecosystem.
'';
};
}

View file

@ -1,202 +1,80 @@
{ pname
, version
{ version
, patches
, dart
, src
, supportsLinuxDesktop ? true
}:
{ bash
, buildFHSUserEnv
, cacert
{ lib
, git
, runCommandLocal
, writeShellScript
, makeWrapper
, stdenv
, lib
, alsa-lib
, atk
, cairo
, clang
, cmake
, dbus
, expat
, gdk-pixbuf
, glib
, gtk3
, harfbuzz
, libepoxy
, libGL
, libpulseaudio
, libuuid
, libX11
, libxcb
, libXcomposite
, libXcursor
, libXdamage
, libXext
, libXfixes
, libXi
, libXrender
, libXtst
, ninja
, nspr
, nss
, pango
, pkg-config
, systemd
, which
, xorgproto
, callPackage
}:
let
drvName = "flutter-${version}";
flutter = stdenv.mkDerivation {
name = "${drvName}-unwrapped";
buildInputs = [ git ];
stdenv.mkDerivation {
name = "$flutter-${version}-unwrapped";
inherit src patches version;
buildInputs = [ git ];
postPatch = ''
patchShebangs --build ./bin/
'';
inherit src patches version;
buildPhase = ''
export FLUTTER_ROOT="$(pwd)"
export FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools"
export SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
export SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot"
export STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp"
export DART_SDK_PATH="${dart}"
HOME=../.. # required for pub upgrade --offline, ~/.pub-cache
# path is relative otherwise it's replaced by /build/flutter
pushd "$FLUTTER_TOOLS_DIR"
${dart}/bin/dart pub get --offline
popd
local revision="$(cd "$FLUTTER_ROOT"; git rev-parse HEAD)"
${dart}/bin/dart --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.dart_tool/package_config.json" "$SCRIPT_PATH"
echo "$revision" > "$STAMP_PATH"
echo -n "${version}" > version
rm -r bin/cache/{artifacts,dart-sdk,downloads}
rm bin/cache/*.stamp
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r . $out
mkdir -p $out/bin/cache/
ln -sf ${dart} $out/bin/cache/dart-sdk
runHook postInstall
'';
doInstallCheck = true;
installCheckInputs = [ which ];
installCheckPhase = ''
runHook preInstallCheck
export HOME="$(mktemp -d)"
$out/bin/flutter config --android-studio-dir $HOME
$out/bin/flutter config --android-sdk $HOME
$out/bin/flutter --version | fgrep -q '${version}'
runHook postInstallCheck
'';
};
# Wrap flutter inside an fhs user env to allow execution of binary,
# like adb from $ANDROID_HOME or java from android-studio.
fhsEnv = buildFHSUserEnv {
name = "${drvName}-fhs-env";
multiPkgs = pkgs:
with pkgs; ([
# Flutter only use these certificates
(runCommandLocal "fedoracert" { } ''
mkdir -p $out/etc/pki/tls/
ln -s ${cacert}/etc/ssl/certs $out/etc/pki/tls/certs
'')
zlib
]);
targetPkgs = pkgs:
with pkgs; ([
bash
curl
dart
git
unzip
which
xz
# flutter test requires this lib
libGLU
# for android emulator
alsa-lib
dbus
expat
libpulseaudio
libuuid
libX11
libxcb
libXcomposite
libXcursor
libXdamage
libXext
libXfixes
libXi
libXrender
libXtst
libGL
nspr
nss
systemd
]);
};
in
let
self = (self:
runCommandLocal drvName
{
flutterWithCorrectedCache = writeShellScript "flutter_corrected_cache" ''
export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
${flutter}/bin/flutter "$@"
postPatch = ''
patchShebangs --build ./bin/
'';
buildInputs = [
makeWrapper
pkg-config
] ++ lib.lists.optionals supportsLinuxDesktop (let
# https://discourse.nixos.org/t/handling-transitive-c-dependencies/5942/3
deps = pkg: (pkg.buildInputs or []) ++ (pkg.propagatedBuildInputs or []);
collect = pkg: lib.unique ([ pkg ] ++ deps pkg ++ lib.concatMap collect (deps pkg));
in
collect atk.dev ++
collect cairo.dev ++
collect gdk-pixbuf.dev ++
collect glib.dev ++
collect gtk3.dev ++
collect harfbuzz.dev ++
collect libepoxy.dev ++
collect pango.dev ++
collect libX11.dev ++
collect xorgproto);
buildPhase = ''
export FLUTTER_ROOT="$(pwd)"
export FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools"
export SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
export SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot"
export STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp"
export DART_SDK_PATH="${dart}"
HOME=../.. # required for pub upgrade --offline, ~/.pub-cache
# path is relative otherwise it's replaced by /build/flutter
pushd "$FLUTTER_TOOLS_DIR"
${dart}/bin/dart pub get --offline
popd
local revision="$(cd "$FLUTTER_ROOT"; git rev-parse HEAD)"
${dart}/bin/dart --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.dart_tool/package_config.json" "$SCRIPT_PATH"
echo "$revision" > "$STAMP_PATH"
echo -n "${version}" > version
rm -r bin/cache/{artifacts,dart-sdk,downloads}
rm bin/cache/*.stamp
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r . $out
mkdir -p $out/bin/cache/
ln -sf ${dart} $out/bin/cache/dart-sdk
runHook postInstall
'';
doInstallCheck = true;
installCheckInputs = [ which ];
installCheckPhase = ''
runHook preInstallCheck
export HOME="$(mktemp -d)"
$out/bin/flutter config --android-studio-dir $HOME
$out/bin/flutter config --android-sdk $HOME
$out/bin/flutter --version | fgrep -q '${version}'
runHook postInstallCheck
'';
passthru = {
unwrapped = flutter;
inherit dart;
mkFlutterApp = callPackage ../../../build-support/flutter {
flutter = self;
};
};
meta = with lib; {
description = "Flutter is Google's SDK for building mobile, web and desktop with Dart";
longDescription = ''
@ -208,43 +86,4 @@ runCommandLocal drvName
platforms = [ "x86_64-linux" "aarch64-linux" ];
maintainers = with maintainers; [ babariviere ericdallo ];
};
} ''
mkdir -p $out/bin
mkdir -p $out/bin/cache/
ln -sf ${dart} $out/bin/cache/dart-sdk
mkdir -p $out/libexec
makeWrapper "$flutterWithCorrectedCache" $out/libexec/flutter_launcher \
--set-default ANDROID_EMULATOR_USE_SYSTEM_LIBS 1 \
--prefix PATH : ${lib.makeBinPath (lib.lists.optionals supportsLinuxDesktop [
pkg-config
cmake
ninja
clang
])} \
--prefix PKG_CONFIG_PATH : "$PKG_CONFIG_PATH_FOR_TARGET" \
--prefix CXXFLAGS "''\t" '${lib.optionalString supportsLinuxDesktop "-isystem ${libX11.dev}/include -isystem ${xorgproto}/include"}' \
--prefix LDFLAGS "''\t" '${lib.optionalString supportsLinuxDesktop "-rpath ${lib.makeLibraryPath [
atk
cairo
gdk-pixbuf
glib
gtk3
harfbuzz
libepoxy
pango
libX11
]}"}' \
--suffix LD_LIBRARY_PATH : '${lib.optionalString supportsLinuxDesktop (lib.makeLibraryPath [
# The prebuilt flutter_linux_gtk library shipped in the Flutter SDK does not have an appropriate RUNPATH.
# Its dependencies must be added here.
libepoxy
])}' \
--add-flags --no-version-check
makeWrapper ${fhsEnv}/bin/${drvName}-fhs-env $out/bin/${pname} \
--add-flags $out/libexec/flutter_launcher
'') self;
in
self
}

View file

@ -0,0 +1,89 @@
{ lib
, flutter
, supportsLinuxDesktop ? true
, makeWrapper
, runCommandLocal
, writeShellScript
, pkg-config
, atk
, cairo
, gdk-pixbuf
, glib
, gtk3
, harfbuzz
, libepoxy
, pango
, libX11
, xorgproto
, cmake
, ninja
, clang
}:
runCommandLocal "flutter"
{
flutterWithCorrectedCache = writeShellScript "flutter_corrected_cache" ''
export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
${flutter}/bin/flutter "$@"
'';
buildInputs = [
makeWrapper
pkg-config
] ++ lib.lists.optionals supportsLinuxDesktop (
let
# https://discourse.nixos.org/t/handling-transitive-c-dependencies/5942/3
deps = pkg: (pkg.buildInputs or [ ]) ++ (pkg.propagatedBuildInputs or [ ]);
collect = pkg: lib.unique ([ pkg ] ++ deps pkg ++ lib.concatMap collect (deps pkg));
in
collect atk.dev ++
collect cairo.dev ++
collect gdk-pixbuf.dev ++
collect glib.dev ++
collect gtk3.dev ++
collect harfbuzz.dev ++
collect libepoxy.dev ++
collect pango.dev ++
collect libX11.dev ++
collect xorgproto
);
passthru = flutter.passthru // {
unwrapped = flutter;
};
inherit (flutter) meta;
} ''
mkdir -p $out/bin
mkdir -p $out/bin/cache/
ln -sf ${flutter.dart} $out/bin/cache/dart-sdk
makeWrapper "$flutterWithCorrectedCache" $out/bin/flutter \
--set-default ANDROID_EMULATOR_USE_SYSTEM_LIBS 1 \
--prefix PATH : ${lib.makeBinPath (lib.lists.optionals supportsLinuxDesktop [
pkg-config
cmake
ninja
clang
])} \
--prefix PKG_CONFIG_PATH : "$PKG_CONFIG_PATH_FOR_TARGET" \
--prefix CXXFLAGS "''\t" '${lib.optionalString supportsLinuxDesktop "-isystem ${libX11.dev}/include -isystem ${xorgproto}/include"}' \
--prefix LDFLAGS "''\t" '${lib.optionalString supportsLinuxDesktop "-rpath ${lib.makeLibraryPath [
atk
cairo
gdk-pixbuf
glib
gtk3
harfbuzz
libepoxy
pango
libX11
]}"}' \
--suffix LD_LIBRARY_PATH : '${lib.optionalString supportsLinuxDesktop (lib.makeLibraryPath [
# The prebuilt flutter_linux_gtk library shipped in the Flutter SDK does not have an appropriate RUNPATH.
# Its dependencies must be added here.
libepoxy
])}' \
--add-flags --no-version-check
''

View file

@ -13699,8 +13699,12 @@ with pkgs;
flutterPackages =
recurseIntoAttrs (callPackage ../development/compilers/flutter { });
flutter = flutterPackages.stable;
flutter2 = flutterPackages.v2;
flutter-unwrapped = flutterPackages.stable;
flutter2-unwrapped = flutterPackages.v2;
flutter-wrapped = flutterPackages.wrapFlutter flutter-unwrapped;
flutter2-wrapped = flutterPackages.wrapFlutter flutter2-unwrapped;
flutter = flutterPackages.mkFlutterFHS flutter-wrapped;
flutter2 = flutterPackages.mkFlutterFHS flutter2-wrapped;
fnm = callPackage ../development/tools/fnm {
inherit (darwin.apple_sdk.frameworks) DiskArbitration Foundation Security;