Flutter

From NixOS Wiki
Jump to: navigation, search

Flutter is an open-source mobile application development framework created by Google. It allows developers to build high-performance, cross-platform apps for iOS and Android using a single codebase.

In order to understand the sections below more for Android development on NixOS, check out the Android wiki page.

Development

The easiest way is to install Android Studio by adding pkgs.android-studio to your list of packages in configuration.nix.

If you prefer VSCode, you can create a dev-shell with the packages "jdk", "flutter", and a preferred android sdk such as the preconfigured one "androidenv.androidPkgs_9_0.androidsdk" (mentioned in the Android wiki page). Add other packages if missing any. Or you can install Android Studio to get all the Android packages, and install Flutter.

Below is an example flake.nix for creating a dev shell. Create following flake.nix in a new project directory

Breeze-text-x-plain.png
flake.nix
{
description = "Flutter 3.13.x";
inputs = {
  nixpkgs.url = "github:NixOS/nixpkgs/23.11";
  flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
  flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs {
        inherit system;
        config = {
          android_sdk.accept_license = true;
          allowUnfree = true;
        };
      };
      buildToolsVersion = "34.0.0";
      androidComposition = pkgs.androidenv.composeAndroidPackages {
        buildToolsVersions = [ buildToolsVersion "28.0.3" ];
        platformVersions = [ "34" "28" ];
        abiVersions = [ "armeabi-v7a" "arm64-v8a" ];
      };
      androidSdk = androidComposition.androidsdk;
    in
    {
      devShell =
        with pkgs; mkShell rec {
          ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
          buildInputs = [
            flutter
            androidSdk # The customized SDK that we've made above
            jdk17
          ];
        };
    });
}


If you don't want to customize the android sdk, you can instead use the predefined packages, as mentioned in this section on the manual, such as androidenv.androidPkgs_9_0.androidsdk:

Breeze-text-x-plain.png
flake.nix
{
description = "Flutter 3.13.x";
inputs = {
  nixpkgs.url = "github:NixOS/nixpkgs/23.11";
  flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
  flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs {
        inherit system;
        config = {
          android_sdk.accept_license = true;
          allowUnfree = true;
        };
      };
      androidSdk = pkgs.androidenv.androidPkgs_9_0.androidsdk;
    in
    {
      devShell =
        with pkgs; mkShell rec {
          ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
          buildInputs = [
            flutter
            androidSdk
            jdk17
          ];
        };
    });
}


Run following commands to start a new demo project and run the "hello world" application

# nix develop
# flutter create my_app
# cd my_app
# flutter run

Emulators

View the Android wiki page for more info, but you can set up emulators in Android Studio, run them from there, then target the emulator in VSCode when running your flutter code. Otherwise, you can Nixify or even manually add your emulators as stated in the Android wiki page

Flutter && Firebase Web

If you are just Flutter for web with Google's Firebase, then I found these snippets work.

Please note that someone has wonderfully packaged multiple version of Flutter, so you can pick the version you want (thanks to however did that! awesome!!). https://search.nixos.org/packages?channel=unstable&query=flutter

Breeze-text-x-plain.png
home.nix
...
  home.sessionVariables = {
    JAVA_HOME = "${pkgs.jdk17}/lib/openjdk";
    CHROME_EXECUTABLE = "${pkgs.google-chrome}/bin/google-chrome-stable";
    GOOGLE_APPLICATION_CREDENTIALS="~/myGoogleCreds.json";
  };
  home.packages = with pkgs; [
    ...
    #flutter   # latest
    flutter329 # specific version of Flutter
    firebase-tools
    android-studio
    android-tools
    android-udev-rules
    # Java for Android development
    jdk17
    ...
];
  programs.vscode = {
    enable = true;
    package = pkgs.vscode;
    profiles.default.extensions = with pkgs.vscode-extensions; [
      dart-code.dart-code
      dart-code.flutter
    ];
  };
...


Packaging

Use buildFlutterApplication from nixpkgs.

See also

  • The team working on flutter in nixpkgs maintains several pieces of infrastructure related to the cause. The documentation is lacking as of now, but there are plans to improve it.