NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.38k stars 14.33k forks source link

Issue: dart run build_runner Cannot Find Flutter SDK in Nixpkgs #350726

Open Steffen70 opened 1 month ago

Steffen70 commented 1 month ago

Issue: dart run build_runner Cannot Find Flutter SDK in Nixpkgs

Environment:

Description:

I am encountering an issue where running dart run build_runner does not work with the Flutter SDK from nixpkgs. It fails to locate the Flutter SDK, even though the app builds without any issues. However, when I switch to using Flutter installed via Homebrew, the command works as expected and the freezed models are generated successfully.

Specifically:

Steps to Reproduce:

  1. Use latest Flutter Version from nixpkgs (github:NixOS/nixpkgs/d4f247e89f6e10120f911e2e2d2254a050d0f732)
  2. Run dart run build_runner build --delete-conflicting-outputs in a project that uses freezed models (no source code provided, but this should reproduce the issue).
  3. Observe the error indicating that the Flutter SDK cannot be found.
  4. Alternatively, run flutter pub run and observe that the Color type is set to InvalidType, which is likely caused by dart run not finding the Flutter SDK.

Expected Behavior:

Actual Behavior:

Affected Platforms:

Additional Information:

image

image

{
  description = "A Nix Flake for Flutter mobile development and fastlane CI/CD";

  inputs = {
    # Use the unstable channel for the latest packages
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

    flake-utils.url = "github:numtide/flake-utils";

    # Custom powershell modules
    powershell_modules.url = "github:seventymx/powershell_modules";

    # Specific nixpkgs reference for Flutter
    nixpkgs-flutter.url = "github:NixOS/nixpkgs/d4f247e89f6e10120f911e2e2d2254a050d0f732";
  };

  outputs =
    { self, ... }@inputs:
    inputs.flake-utils.lib.eachDefaultSystem (
      system:
      let
        unstable = import inputs.nixpkgs {
          inherit system;
          config = {
            android_sdk.accept_license = true;
            allowUnfree = true;
          };
        };

        flutterPkgs = import inputs.nixpkgs-flutter { inherit system; };

        androidComposition = unstable.androidenv.composeAndroidPackages {
          # API levels for Android 13 and 14
          platformVersions = [
            "31"
            "32"
            "33"
            "34"
          ];
          buildToolsVersions = [
            "30.0.3"
            "33.0.0"
            "34.0.0"
          ];
          abiVersions = [ "x86_64" ];
          includeEmulator = true;
          includeSystemImages = true;
          systemImageTypes = [ "google_apis_playstore" ];
          includeExtras = [ "extras;google;gcm" ];
        };

        # TODO: Make it work on Apple Silicon - Currently only works on x86_64-linux
        androidSdk = androidComposition.androidsdk;

        hostPlatform = unstable.stdenv.hostPlatform.system;

        appleInputs =
          if
            builtins.elem hostPlatform [
              "aarch64-darwin"
              "x86_64-darwin"
            ]
          then
            [ unstable.cocoapods ]
          else
            [ ];
      in
      {
        devShell = unstable.mkShellNoCC {
          buildInputs = [
            unstable.nixfmt-rfc-style
            unstable.git
            androidSdk
            unstable.jdk17
            unstable.fastlane
            unstable.ruby
            unstable.powershell
            unstable.scrcpy
            flutterPkgs.flutter
          ] ++ appleInputs;

          shellHook = ''
            export SHELL="${unstable.powershell}/bin/pwsh"

            export PSModulePath=${inputs.powershell_modules}

            # Call pwsh with inline commands
            pwsh -NoExit -Command "& {
              Import-Module ShellUtils

              # Restore the environment variables from the .secrets file - not included in the repository (do not use in ci/cd, use environment variables instead)
              Restore-EnvVariables -FilePath "../flutter_secrets/.secrets"

              Set-AndroidEnvironment -AndroidSdkPath "${androidSdk}"

              Initialize-Fastlane -FastlaneAppIdentifier "mx.seventy.demo" -FastlaneAppName "SPMobile"

              Import-Module FlutterBuildUtils
            }"

            exit 0
          '';
        };
      }
    );
}
Steffen70 commented 1 month ago

I found a seemingly related issue here: #322871.

After reviewing it, I tried adding FLUTTER_ROOT in the shell hook, and now both dart run and flutter pub run work. However, I still get the same InvalidType error when running the build (screenshot attached).

Could you please help investigate this?

Notify maintainers: @babariviere
@ericdallo
@mkg20001
@RossComputerGuy
@FlafyDev
@hacker1024

image

jeanlucthumm commented 1 month ago

What are you setting FLUTTER_ROOT to?

Steffen70 commented 1 month ago

image

I’m getting the original error again: Because spmobile depends on flutter_test from sdk which doesn't exist (the Flutter SDK is not available), version solving failed. This is the same error as when I don’t set FLUTTER_ROOT at all. It looks like I need to set FLUTTER_ROOT to the main Flutter directory (not bin) for dart run to work.