Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Setup flutter flake env #1

Open
hatch01 opened this issue Nov 29, 2023 · 6 comments
Open

Setup flutter flake env #1

hatch01 opened this issue Nov 29, 2023 · 6 comments

Comments

@hatch01
Copy link

hatch01 commented Nov 29, 2023

Hey, I'm new to the nixos community.
I am trying to write a flake.nix to enable me to develop flutter apps and build them to linux, android,web.
However, I have many difficulties to setup the android SDK.
So I tried your project, but it does not seem to setup the android SDKs.
Is it normal?

Do you have any help, template, example that you could show me to help me to achieve a simple flutter dev environment.

@maximoffua
Copy link
Owner

I'm using devenv in all my projects. It simplifies the work with flakes. Here is my "template" for Flutter Android projects:

devenv.nix

# devenv.nix
{
  inputs,
  pkgs,
  config,
  ...
}: let
  inherit (inputs) flutter-nix android-nixpkgs;
  flutter-sdk = flutter-nix.packages.${pkgs.stdenv.system};
  sdk = (import android-nixpkgs {}).sdk (sdkPkgs:
    with sdkPkgs; [
      build-tools-30-0-3
      build-tools-34-0-0
      cmdline-tools-latest
      emulator
      platform-tools
      platforms-android-34
      platforms-android-33
      platforms-android-31
      platforms-android-28
      system-images-android-34-google-apis-playstore-x86-64
    ]);
in {
  # https://devenv.sh/basics/
  # dotenv.enable = true;
  env.ANDROID_AVD_HOME = "${config.env.DEVENV_ROOT}/.android/avd";
  env.ANDROID_SDK_ROOT = "${sdk}/share/android-sdk";
  env.ANDROID_HOME = config.env.ANDROID_SDK_ROOT;
  env.CHROME_EXECUTABLE = "chromium";
  env.FLUTTER_SDK = "${pkgs.flutter}";
  env.GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${sdk}/share/android-sdk/build-tools/34.0.0/aapt2";

  # https://devenv.sh/packages/
  packages = [
    flutter-sdk.flutter
    pkgs.git
    pkgs.lazygit
    pkgs.chromium
    pkgs.cmake
  ];

  # https://devenv.sh/scripts/
  # Create the initial AVD that's needed by the emulator
  scripts.create-avd.exec = "avdmanager create avd --force --name phone --package 'system-images;android-33;google_apis;x86_64'";

  # https://devenv.sh/processes/
  # These processes will all run whenever we run `devenv run`
  processes.emulator.exec = "emulator -avd phone -skin 720x1280";
  processes.generate.exec = "dart run build_runner watch || true";
  # processes.grovero-app.exec = "flutter run lib/main.dart";

  enterShell = ''
    mkdir -p $ANDROID_AVD_HOME
    export PATH="${sdk}/bin:$PATH"
    export FLUTTER_GRADLE_PLUGIN_BUILDDIR="''${XDG_CACHE_HOME:-$HOME/.cache}/flutter/gradle-plugin";
  '';

  # https://devenv.sh/languages/
  languages.dart = {
    enable = true;
    package = flutter-sdk.dart;
  };
  languages.java = {
    enable = true;
    gradle.enable = false;
    jdk.package = pkgs.jdk;
  };

  # See full reference at https://devenv.sh/reference/options/
}

devenv.yaml

allowUnfree: true
inputs:
  nixpkgs:
    url: github:NixOS/nixpkgs/nixpkgs-unstable
  android-nixpkgs:
    url: github:maximoffua/android-nixpkgs/stable
  flutter-nix:
    url: github:maximoffua/flutter.nix

maximoffua referenced this issue Dec 4, 2023
Add usage example for devenv.sh
@contrun
Copy link

contrun commented Dec 5, 2023

I put all the things @maximoffua wrote together in https://github.com/contrun/nix-flutter-project-template . This now works marvelously. Thank you @hatch01 and @maximoffua . I finally get it working. It was a big headache.

@hatch01
Copy link
Author

hatch01 commented Dec 5, 2023

Thanks for the repo !

@Prn-Ice
Copy link

Prn-Ice commented Dec 16, 2023

Here's a flake version:

{
  description = "Flutter 3.13.x";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    android.url = "github:tadfisher/android-nixpkgs";
    flutter-nix.url = "github:maximoffua/flutter.nix";
  };
  outputs = { self, nixpkgs, flake-utils, android, flutter-nix }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
        };
        flutter-sdk = flutter-nix.packages.${system};
        android-sdk = android.sdk.${system} (sdkPkgs:
          with sdkPkgs; [
            build-tools-30-0-3
            build-tools-34-0-0
            cmdline-tools-latest
            emulator
            platform-tools
            platforms-android-33
            platforms-android-34
            sources-android-34
            system-images-android-34-google-apis-playstore-x86-64
          ]
        );
      in
      {
        devShell = with pkgs; mkShell rec {
          buildInputs = [
            flutter-sdk.flutter
            jdk
            android-sdk
          ];

          JAVA_HOME = jdk.home;
          CHROME_EXECUTABLE = "google-chrome-stable";

          # Fix an issue with Flutter using an older version of aapt2, which does not know
          # an used parameter.
          GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${android-sdk}/share/android-sdk/build-tools/34.0.0/aapt2";
          FLUTTER_GRADLE_PLUGIN_BUILDDIR = "~/.cache/flutter/gradle-plugin";
        };
      });
}

@hatch01
Copy link
Author

hatch01 commented Dec 16, 2023

Here is mine if it could help.
Without the use of flake-utils.
I have many more deps because my project (onyx-lyon1) uses a lot of packages.

{
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";

    flake-parts = {
      url = "github:hercules-ci/flake-parts";
      inputs.nixpkgs-lib.follows = "nixpkgs";
    };
  };

  outputs = {
    self,
    nixpkgs,
    flake-parts,
    ...
  } @ inputs:
    flake-parts.lib.mkFlake {inherit inputs;} {
      systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
      perSystem = {
        pkgs,
        system,
        lib,
        ...
      }: {
        devShells.default = let
          pkgs = import nixpkgs {
            inherit system;
            config = {
              allowUnfree = true;
              android_sdk.accept_license = true;
            };
          };
          buildToolsVersionForAapt2 = "34.0.0-rc4";
          androidComposition = pkgs.androidenv.composeAndroidPackages {
            # Installing both version for aapt2 and version that flutter wants
            buildToolsVersions = [buildToolsVersionForAapt2 "30.0.3"];
            platformVersions = ["34" "33" "31" "30"];
            abiVersions = ["armeabi-v7a" "arm64-v8a" "x86" "x86_64"];
            includeEmulator = true;
            emulatorVersion = "34.1.9";
            toolsVersion = "26.1.1";
            platformToolsVersion = "33.0.3";
            includeSources = false;
            includeSystemImages = false;
            systemImageTypes = ["google_apis_playstore"];
            # cmakeVersions = [ "3.10.2" ];
            includeNDK = true;
            # ndkVersions = [ "22.0.7026061" ];
            useGoogleAPIs = false;
            useGoogleTVAddOns = false;
            extraLicenses = [
              "android-googletv-license"
              "android-sdk-arm-dbt-license"
              "android-sdk-license"
              "android-sdk-preview-license"
              "google-gdk-license"
              "intel-android-extra-license"
              "intel-android-sysimage-license"
              "mips-android-sysimage-license"
            ];
          };
          androidSdk = androidComposition.androidsdk;
          PWD = builtins.getEnv "PWD";
        in
          pkgs.mkShell {
            CHROME_EXECUTABLE = lib.getExe pkgs.chromium;
            ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
            ANDROID_NDK_ROOT = "${androidSdk}/libexec/android-sdk/ndk-bundle";
            ANDROID_AVD_HOME = "${PWD}/.android/avd";
            GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/${buildToolsVersionForAapt2}/aapt2";
            LD_LIBRARY_PATH = "${PWD}/apps/onyx/build/linux/x64/debug/bundle/lib/:${PWD}/apps/onyx/build/linux/x64/release/bundle/lib/:${PWD}/apps/onyx/build/linux/x64/profile/bundle/lib/";
            buildInputs = with pkgs; [
              chromium
              flutter
              jdk17
              androidSdk
              at-spi2-core
              clang
              dart
              dbus
              util-linux
              cmake
              ninja
              libsecret
              android-tools
              pkg-config
              gtk3
              glib
              pcre2
              pcre
              libselinux
              libsepol
              libthai
              libdatrie
              xorg.libXdmcp
              libxkbcommon
              xorg.libXtst
              libepoxy
              libgcrypt
              libgpg-error
            ];
          };
        formatter = pkgs.alejandra;
      };
    };
}

@wexder
Copy link

wexder commented Jan 7, 2024

For anyone struggling to get it working, make sure you don't have any config set in flutter. Clear it with flutter config --android-sdk= and flutter config --android-studio-dir=

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants