about summary refs log tree commit diff
path: root/pkgs/development/compilers/flutter
diff options
context:
space:
mode:
authorIlan Joselevich <personal@ilanjoselevich.com>2023-07-05 03:03:27 +0300
committerGitHub <noreply@github.com>2023-07-05 03:03:27 +0300
commitd625c365634b495154feb4a67806ca569a9d8e9c (patch)
treeb7d8ddc40a157361a39d14df73a28745bc47e827 /pkgs/development/compilers/flutter
parent94b586b8535190fb43d23774f3cef8b1360805be (diff)
parent570f3efd1d447597fd1d468188d785dfe1207b44 (diff)
Merge pull request #240715 from FlafyDev/flutter-cache-drv-2
flutter: Separate cache and unwrapped derivations #2
Diffstat (limited to 'pkgs/development/compilers/flutter')
-rw-r--r--pkgs/development/compilers/flutter/flutter.nix58
-rw-r--r--pkgs/development/compilers/flutter/patches/flutter3/move-cache.patch16
-rw-r--r--pkgs/development/compilers/flutter/wrapper.nix95
3 files changed, 92 insertions, 77 deletions
diff --git a/pkgs/development/compilers/flutter/flutter.nix b/pkgs/development/compilers/flutter/flutter.nix
index 3dbab3529e77f..59492f81231c5 100644
--- a/pkgs/development/compilers/flutter/flutter.nix
+++ b/pkgs/development/compilers/flutter/flutter.nix
@@ -3,68 +3,13 @@
 , patches
 , dart
 , src
-, includedEngineArtifacts ? {
-    common = [
-      "flutter_patched_sdk"
-      "flutter_patched_sdk_product"
-    ];
-    platform = { };
-  }
-
 , lib
-, callPackage
 , stdenv
-, runCommandLocal
-, symlinkJoin
-, lndir
 , git
 , which
 }:
 
 let
-  engineArtifactDirectory =
-    let
-      engineArtifacts = callPackage ./engine-artifacts { inherit engineVersion; };
-    in
-    runCommandLocal "flutter-engine-artifacts-${version}" { }
-      (
-        let
-          mkCommonArtifactLinkCommand = { artifact }:
-            ''
-              mkdir -p $out/common
-              ${lndir}/bin/lndir -silent ${artifact} $out/common
-            '';
-          mkPlatformArtifactLinkCommand = { artifact, os, architecture, variant ? null }:
-            let
-              artifactDirectory = "${os}-${architecture}${lib.optionalString (variant != null) "-${variant}"}";
-            in
-            ''
-              mkdir -p $out/${artifactDirectory}
-                ${lndir}/bin/lndir -silent ${artifact} $out/${artifactDirectory}
-            '';
-        in
-        ''
-          ${
-            builtins.concatStringsSep "\n"
-              ((map (name: mkCommonArtifactLinkCommand {
-                artifact = engineArtifacts.common.${name};
-              }) (if includedEngineArtifacts ? common then includedEngineArtifacts.common else [ ])) ++
-              (builtins.foldl' (commands: os: commands ++
-                (builtins.foldl' (commands: architecture: commands ++
-                  (builtins.foldl' (commands: variant: commands ++
-                    (map (artifact: mkPlatformArtifactLinkCommand {
-                      inherit artifact os architecture variant;
-                    }) engineArtifacts.platform.${os}.${architecture}.variants.${variant}))
-                  (map (artifact: mkPlatformArtifactLinkCommand {
-                    inherit artifact os architecture;
-                  }) engineArtifacts.platform.${os}.${architecture}.base)
-                  includedEngineArtifacts.platform.${os}.${architecture}))
-                [] (builtins.attrNames includedEngineArtifacts.platform.${os})))
-              [] (builtins.attrNames (if includedEngineArtifacts ? platform then includedEngineArtifacts.platform else { }))))
-          }
-        ''
-      );
-
   unwrapped =
     stdenv.mkDerivation {
       name = "flutter-${version}-unwrapped";
@@ -134,7 +79,6 @@ let
         mkdir -p $out
         cp -r . $out
         ln -sf ${dart} $out/bin/cache/dart-sdk
-        ln -sf ${engineArtifactDirectory} $out/bin/cache/artifacts/engine
 
         runHook postInstall
       '';
@@ -153,7 +97,7 @@ let
       '';
 
       passthru = {
-        inherit dart;
+        inherit dart engineVersion;
         # The derivation containing the original Flutter SDK files.
         # When other derivations wrap this one, any unmodified files
         # found here should be included as-is, for tooling compatibility.
diff --git a/pkgs/development/compilers/flutter/patches/flutter3/move-cache.patch b/pkgs/development/compilers/flutter/patches/flutter3/move-cache.patch
index efab81d2fc0ed..cc6e5e8f49e01 100644
--- a/pkgs/development/compilers/flutter/patches/flutter3/move-cache.patch
+++ b/pkgs/development/compilers/flutter/patches/flutter3/move-cache.patch
@@ -35,3 +35,19 @@ index dd80b1e46e..8e54517765 100644
        if (!devToolsDir.existsSync()) {
          throw Exception('Could not find directory at ${devToolsDir.path}');
        }
+diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart
+index 1c31c1b5db..76c7210d3b 100644
+--- a/packages/flutter_tools/lib/src/cache.dart
++++ b/packages/flutter_tools/lib/src/cache.dart
+@@ -529,6 +529,11 @@ class Cache {
+ 
+   /// Return the top-level directory in the cache; this is `bin/cache`.
+   Directory getRoot() {
++    const Platform platform = LocalPlatform();
++    if (platform.environment.containsKey('FLUTTER_CACHE_DIR')) {
++      return _fileSystem.directory(platform.environment['FLUTTER_CACHE_DIR']);
++    }
++
+     if (_rootOverride != null) {
+       return _fileSystem.directory(_fileSystem.path.join(_rootOverride!.path, 'bin', 'cache'));
+     } else {
diff --git a/pkgs/development/compilers/flutter/wrapper.nix b/pkgs/development/compilers/flutter/wrapper.nix
index 81e5e6e4fdcdb..50c522fd0e047 100644
--- a/pkgs/development/compilers/flutter/wrapper.nix
+++ b/pkgs/development/compilers/flutter/wrapper.nix
@@ -4,7 +4,19 @@
 , flutter
 , supportsLinuxDesktop ? stdenv.hostPlatform.isLinux
 , supportsAndroid ? stdenv.hostPlatform.isx86_64
-, includedEngineArtifacts ? null
+, includedEngineArtifacts ? {
+    common = [
+      "flutter_patched_sdk"
+      "flutter_patched_sdk_product"
+    ];
+    platform = {
+      android = lib.optionalAttrs supportsAndroid
+        ((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
+      linux = lib.optionalAttrs supportsLinuxDesktop
+        (lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
+          (architecture: [ "debug" "profile" "release" ]));
+    };
+  }
 , extraPkgConfigPackages ? [ ]
 , extraLibraries ? [ ]
 , extraIncludes ? [ ]
@@ -32,30 +44,73 @@
 , cmake
 , ninja
 , clang
+, lndir
+, symlinkJoin
 }:
 
 let
-  flutterWithArtifacts = flutter.override {
-    includedEngineArtifacts = if includedEngineArtifacts != null then includedEngineArtifacts else {
-      common = [
-        "flutter_patched_sdk"
-        "flutter_patched_sdk_product"
-      ];
-      platform = {
-        android = lib.optionalAttrs supportsAndroid
-          ((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
-        linux = lib.optionalAttrs supportsLinuxDesktop
-          (lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
-            (architecture: [ "debug" "profile" "release" ]));
-      };
-    };
+  engineArtifacts = callPackage ./engine-artifacts { inherit (flutter) engineVersion; };
+  mkCommonArtifactLinkCommand = { artifact }:
+    ''
+      mkdir -p $out/artifacts/engine/common
+      lndir -silent ${artifact} $out/artifacts/engine/common
+    '';
+  mkPlatformArtifactLinkCommand = { artifact, os, architecture, variant ? null }:
+    let
+      artifactDirectory = "${os}-${architecture}${lib.optionalString (variant != null) "-${variant}"}";
+    in
+    ''
+      mkdir -p $out/artifacts/engine/${artifactDirectory}
+      lndir -silent ${artifact} $out/artifacts/engine/${artifactDirectory}
+    '';
+  engineArtifactDirectory =
+    runCommandLocal "flutter-engine-artifacts-${flutter.version}" { nativeBuildInputs = [ lndir ]; }
+      (
+        builtins.concatStringsSep "\n"
+          ((map
+            (name: mkCommonArtifactLinkCommand {
+              artifact = engineArtifacts.common.${name};
+            })
+            (includedEngineArtifacts.common or [ ])) ++
+          (builtins.foldl'
+            (commands: os: commands ++
+              (builtins.foldl'
+                (commands: architecture: commands ++
+                  (builtins.foldl'
+                    (commands: variant: commands ++
+                      (map
+                        (artifact: mkPlatformArtifactLinkCommand {
+                          inherit artifact os architecture variant;
+                        })
+                        engineArtifacts.platform.${os}.${architecture}.variants.${variant}))
+                    (map
+                      (artifact: mkPlatformArtifactLinkCommand {
+                        inherit artifact os architecture;
+                      })
+                      engineArtifacts.platform.${os}.${architecture}.base)
+                    includedEngineArtifacts.platform.${os}.${architecture}))
+                [ ]
+                (builtins.attrNames includedEngineArtifacts.platform.${os})))
+            [ ]
+            (builtins.attrNames (includedEngineArtifacts.platform or { }))))
+      );
+
+  cacheDir = symlinkJoin {
+    name = "flutter-cache-dir";
+    paths = [
+      engineArtifactDirectory
+      "${flutter}/bin/cache"
+    ];
   };
 
   # By default, Flutter stores downloaded files (such as the Pub cache) in the SDK directory.
   # Wrap it to ensure that it does not do that, preferring home directories instead.
+  # The sh file `$out/bin/internal/shared.sh` runs when launching Flutter and calls `"$FLUTTER_ROOT/bin/cache/` instead of our environment variable `FLUTTER_CACHE_DIR`.
+  # We do not patch it since the script doesn't require engine artifacts(which are the only thing not added by the unwrapped derivation), so it shouldn't fail, and patching it will just be harder to maintain.
   immutableFlutter = writeShellScript "flutter_immutable" ''
     export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
-    ${flutterWithArtifacts}/bin/flutter "$@"
+    export FLUTTER_CACHE_DIR=${cacheDir}
+    ${flutter}/bin/flutter "$@"
   '';
 
   # Tools that the Flutter tool depends on.
@@ -105,12 +160,12 @@ in
 {
   nativeBuildInputs = [ makeWrapper ];
 
-  passthru = flutterWithArtifacts.passthru // {
-    inherit (flutterWithArtifacts) version;
-    unwrapped = flutterWithArtifacts;
+  passthru = flutter.passthru // {
+    inherit (flutter) version;
+    unwrapped = flutter;
   };
 
-  inherit (flutterWithArtifacts) meta;
+  inherit (flutter) meta;
 } ''
   for path in ${builtins.concatStringsSep " " (builtins.foldl' (paths: pkg: paths ++ (map (directory: "'${pkg}/${directory}/pkgconfig'") ["lib" "share"])) [ ] pkgConfigPackages)}; do
     addToSearchPath FLUTTER_PKG_CONFIG_PATH "$path"