Flutter development in NixOS

I have been trying to do android development in NixOS using the following flake.

{
  description = "A basic project in flutter";

  inputs = {
    flake-utils = {
      url = "github:numtide/flake-utils";
    };
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
    android-nixpkgs = {
      url = "github:tadfisher/android-nixpkgs";
    };
  };

  outputs = { self, nixpkgs, flake-utils, flake-compat, android-nixpkgs }:
    flake-utils.lib.eachDefaultSystem ( system:
      let
        pkgs = import nixpkgs {
          inherit system;
          config = {
            allowUnfree = true;
            android_sdk = {
              accept_license = true;
            };
          };
        };
        androidCustomPackage = android-nixpkgs.sdk.${system} (
          sdkPkgs: with sdkPkgs; [
            cmdline-tools-latest
            build-tools-30-0-3
            build-tools-33-0-2
            build-tools-34-0-0
            platform-tools
            emulator
            #patcher-v4
            platforms-android-28
            platforms-android-29
            platforms-android-30
            platforms-android-31
            platforms-android-32
            platforms-android-33
            platforms-android-34
          ]
        );
        pinnedJDK = pkgs.jdk17; # jdk11, jdk13
      in {
        devShells = {
          default = pkgs.mkShell {
            name = "My-flutter-dev-shell";
            buildInputs = with pkgs; [
              flutter
              android-studio
            ] ++ [
              pinnedJDK
              #androidEnvCustomPackage.androidsdk
              androidCustomPackage
            ];
            #shellHook = ''
            #  GRADLE_USER_HOME=$HOME/gradle-user-home
            #  GRADLE_HOME=$HOME/gradle-home
            #'';
            JAVA_HOME = pinnedJDK;
            ANDROID_HOME = "${androidCustomPackage}/share/android-sdk";
            ANDROID_SDK_HOME = "${androidCustomPackage}/share/android-sdk";
            GRADLE_USER_HOME = "/home/admin0101/.gradle";
            GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidCustomPackage}/share/android-sdk/build-tools/34.0.0/aapt2";
          };
        };
      }
    );
}

It works fine for flutter build linux but it fails with a Gradle error if i try to do flutter build apk. It seems like it is trying to write in /nix/store to configure the flutter plugin for Gradle.

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘/home/admin1010/MyProjects/fivexyz/android/app/build.gradle’ line: 4

  • What went wrong:
    Error resolving plugin [id: ‘dev.flutter.flutter-gradle-plugin’, version: ‘1.0.0’]

A problem occurred configuring project ‘:gradle’.
Could not create service of type OutputFilesRepository using ExecutionGradleServices.createOutputFilesRepository().
Failed to create parent directory ‘/nix/store/ap5y9j4v0d66bcw36fqj8nfqvwbn7bmq-flutter-wrapped-3.16.7-sdk-links/packages/flutter_tools/gradle/.gradle’ when creating directory ‘/nix/store/ap5y9j4v0d66bcw36fqj8nfqvwbn7bmq-flutter-wrapped-3.16.7-sdk-links/packages/flutter_tools/gradle/.gradle/buildOutputCleanup’

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

BUILD FAILED in 8s
Running Gradle task ‘assembleRelease’… 9.6s
Gradle task assembleRelease failed with exit code 1

I have tried setting the GRADLE_USER_HOME to my home directory but it did not fix the problem. It seems Gradle uses the new location for temperory files and cache but as soon as it gets to flutter plugins, It fails with the same error.

It seems to be a gradle + flutter issue with the more recent flutter builds, the issue here, as well as here on GitHub. So you might want to try some fixes mentioned here.

1 Like

I’ve tried on and off to get flutter working on my NixOS machine, and this is the issue I eventually run into every time. The posted solutions have not worked for me, although the issue may be that I’m using the latest flutter316 package on unstable. @Nomad have you had any luck with this? Is it just an issue with using a specific gradle build tools version?

I was able to make it work. It is not an issue with gradle. Instead of loading the flutter’s gradle plugin directly, change the way it is loaded in android/app/build.gradle .

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

I used https://github.com/babariviere/flutter-nix-hello-world/blob/fe3d783f177acfbc6c040373978db54c166d0f65/android/app/build.gradle it for reference. Replacing the android directory which was generated by flutter create with the one that is in this repo worked for me.

Hi, this was working very well in previous versions, but flutter updated how they write their gradle configs.
I described the issue here : Unable to build flutter app with flutter 3.19 new Gradle structure · Issue #289936 · NixOS/nixpkgs · GitHub
Did you find any method for the new gradle ?

There is not need to use the old version of Gradle just change the way flutter plugin is loaded by Gradle. (Use my answer above for reference). It should work.