about summary refs log tree commit diff
path: root/pkgs/development/compilers/flutter/wrapper.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/development/compilers/flutter/wrapper.nix')
-rw-r--r--pkgs/development/compilers/flutter/wrapper.nix95
1 files changed, 75 insertions, 20 deletions
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"