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.

@Nomad I have not been able to get the hello world project you mentioned to work. I tried just copying over the app/build.gradle file like you mentioned and still no luck.

Reproduce:

  1. flutter create temp && cd temp
  2. Edit android/app/build.gradle to your posted changes (apply plugin instead of plugin {} block)
  3. flutter run
    Gives the error you posted.

I am running latest flutter:

Flutter 3.19.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision nixpkgs000 () • 1970-01-01 00:00:00
Engine • revision 04817c99c9
Tools • Dart 3.3.0 • DevTools 2.31.1

Your post is about a month old, but even then I was unable to get flutter build working. Could you kindly post your flake.nix if it is different than the one you started this thread with?

I looked at @hatch01 's github issue and it seems like we may be a minute before this gets fixed…
Sorry, also no pressure to respond if you are busy. I’m a little past my toes in NixOS, but its slow going for me to figure out this stuff :sweat_smile:

mybe the easyly in ubuntu on docker

docker run -itd -v /home/jcleng/work/:/home/jcleng/work/ \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=$DISPLAY \
--ipc=host \
--privileged \
-u $(id -u):$(id -g) \
--name=flutter ubuntu:latest

Sorry for the delay, I must have dismissed the notification.
I just uploaded the version that worked for me on GitHub.
https://github.com/SHIVAYspec/flutter-on-nixos-demo-app

1 Like