Flutter: Difference between revisions

From NixOS Wiki
imported>ANicon111
m added android dependencies for building
imported from old wiki (The second flake example doesn't work because it uses a variable androidSdk which is defined in the flake before it, but not the second one)
 
(18 intermediate revisions by 8 users not shown)
Line 1: Line 1:
== Developement enviroment ==
[https://flutter.dev 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.


To be able to build flutter apps, you need to add the platform-specific dependencies to your environment. This may be done by running <code>nix-shell</code> using a shell.nix configuration:
In order to understand the sections below more for Android development on NixOS, [[Android|check out the Android wiki page]].


<syntaxhighlight lang=nix>
== Development ==
{ pkgs ? import <nixpkgs> {} }:
let android-nixpkgs = (pkgs.callPackage (import (builtins.fetchGit {url = "https://github.com/tadfisher/android-nixpkgs.git";})) {channel = "stable";});


in pkgs.mkShell {
=== Linux desktop/Web ===
  buildInputs = with pkgs; [
 
    #basics
To build Flutter apps to Linux desktop or Web you only need the <code>flutter</code> package from Nixpkgs.
    flutter git
Then run <code>flutter build linux</code> or <code>flutter build web</code>.
    #linux build
 
    at-spi2-core.dev clang cmake dbus.dev flutter gtk3 libdatrie libepoxy.dev util-linux.dev libselinux libsepol libthai libxkbcommon ninja pcre pcre2.dev pkg-config xorg.libXdmcp xorg.libXtst
=== Android ===
    #android build
 
    (android-nixpkgs.sdk (sdkPkgs: with sdkPkgs; [cmdline-tools-latest build-tools-32-0-0 tools patcher-v4 platform-tools platforms-android-31 system-images-android-31-default-x86-64 emulator])) jdk8 unzip
The easiest way is to install Android Studio by adding <code>pkgs.android-studio</code> to your list of packages in configuration.nix.
    #web build
 
    chromium
If you prefer [[Vscode|VSCode]], you can create a [[Development environment with nix-shell|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.
  ];
 
  #declaring FLUTTER_ROOT
Below is an example [[Flakes|flake.nix]] for creating a dev shell. Create following <code>flake.nix</code> in a new project directory
  FLUTTER_ROOT = pkgs.flutter;
 
  #libepoxy workaround
{{file|flake.nix|nix|<nowiki>
   LD_LIBRARY_PATH = "${pkgs.libepoxy}/lib";
{
   #web chrome and dart-sdk workaround
description = "Flutter 3.13.x";
  CHROME_EXECUTABLE = "chromium";
inputs = {
  shellHook = ''
  nixpkgs.url = "github:NixOS/nixpkgs/23.11";
    if ! [ -d $HOME/.cache/flutter/ ]
  flake-utils.url = "github:numtide/flake-utils";
     then
};
    mkdir $HOME/.cache/flutter/
outputs = { self, nixpkgs, flake-utils }:
     fi
  flake-utils.lib.eachDefaultSystem (system:
     ln -f -s ${pkgs.flutter}/bin/cache/dart-sdk $HOME/.cache/flutter/
    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
          ];
        };
    });
}
</nowiki>}}
 
If you don't want to customize the android sdk, you can instead use the predefined packages, as mentioned [https://nixos.org/manual/nixpkgs/unstable/#using-predefined-android-package-compositions in this section on the manual], such as <code>androidenv.androidPkgs_9_0.androidsdk</code>:
 
{{file|flake.nix|nix|<nowiki>
{
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
          ];
        };
    });
}
}
</nowiki>}}
Run following commands to start a new demo project and run the "hello world" application
<syntaxhighlight lang="console">
# nix develop
# flutter create my_app
# cd my_app
# flutter run
</syntaxhighlight>
</syntaxhighlight>


This file uses the tool android-nixpkgs made by tadfisher to get all the android dependencies (be sure to change the package versions/branch to your needs; they can be found here: https://github.com/tadfisher/android-nixpkgs/tree/main/channels )
== Emulators ==
[[Android|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
 
== Packaging ==
 
Use [https://github.com/NixOS/nixpkgs/blob/cfe96dbfce8bd62dcd4a8ad62cb79dec140b1a62/pkgs/development/compilers/flutter/flutter.nix#L168 buildFlutterApplication from nixpkgs].
 
== See also ==
 
* The [https://github.com/orgs/NixOS/teams/flutter 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.


Also be sure to run <code>avdmanager create avd -n "Android" -k "system-images;android-31;default;x86_64"</code> to create an android emulator
[[Category: Development]]

Latest revision as of 10:10, 16 August 2024

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

Linux desktop/Web

To build Flutter apps to Linux desktop or Web you only need the flutter package from Nixpkgs. Then run flutter build linux or flutter build web.

Android

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

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:

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

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.