Does anyone have a tested way to build android apks from flutter projects on NixOS?

I am a week in trying to build a flutter project using NixOS. I have tried devenv with half a dozen configurations. I have tried oven a dozen flakes. I tried installing flutter through my configuration.nix file. I’ve tried half a dozen ways to bring in this PR:

If you have reproducible instructions that work, maybe even just a repo with a sample project and associated flake, I would greatly appreciate it. The only next step I can think of at this point is to switch OS and that seems excessive.

1 Like

I really am not sure what the big fuss is about. A bog-standard Java + Android SDK + Flutter installation should work just fine.

{ pkgs ? import (builtins.fetchTarball "https://github.com/hacker1024/nixpkgs/archive/flutter/gradle-8.9-fix.tar.gz") {
    config = {
      allowUnfree = true;
      android_sdk.accept_license = true;
    };
  }
}:

with pkgs;

let
  androidComposition = androidenv.composeAndroidPackages {
    buildToolsVersions = [ "34.0.0" ];
    platformVersions = [ "35" ];
    includeNDK = true;
    ndkVersions = [ "26.3.11579264" ];
    cmakeVersions = [ "3.22.1" ];
  };
in
mkShell {
  packages = [
    jre
    flutter
  ];

  env = rec {
    ANDROID_HOME = "${androidComposition.androidsdk}/libexec/android-sdk";
    GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${ANDROID_HOME}/build-tools/${lib.getVersion (builtins.elemAt androidComposition.build-tools 0)}/aapt2";
  };
}

Hey, thanks for responding. I’m confused. The use of mkShell makes me think this is intended to be used as a flake? But when I try to use it as such, I get:

error:
       … while evaluating the file '/nix/store/j68hhwlkj9xg5vadvdksl9f0cgixrj9d-source/flake.nix':

       error: file '/nix/store/j68hhwlkj9xg5vadvdksl9f0cgixrj9d-source/flake.nix' must be an attribute set

Bit of a shameless plug, but I’m building a repository with templates for tools and frameworks that I use often. I’ve created one for flutter here: nix-registry/templates/flutter/flake.nix at e60c03afa75634044286dff678fec3547e8cda65 · limwa/nix-registry · GitHub

You can use this template by copying the files in the flutter folder to your project or by running nix flake init -t github:limwa/nix-registry#flutter. For this command, you might need to use nix --extra-experimental-features "nix-command flakes" flake init -t github:limwa/nix-registry#flutter instead, if you don’t have those experimental features enabled in your Nix config.

Finally, the template is compatible with nix-shell and is tested daily to ensure it still works with the latest nixpkgs version (Flutter Template · Workflow runs · limwa/nix-registry · GitHub).

Depending on the flutter project you’re working on, you might need to change the versions of the components in the Android SDK in flake.nix, but the versions in the template work out of the box for a newly created Flutter project using flutter create.

Mkshell works in a shell.nix which you can use via nix-shell

You can have a mkshell in flake.nix like devShells.<somename> = mkshell ... which you can use via nix develop

Thanks for self-promoting. That worked for me! Had to fiddle with it for my pre-existing project, but it worked!

1 Like