DEV Community

Shanu Kumawat
Shanu Kumawat

Posted on

Setting Up Flutter Development Environment on Nix

Why I Wrote This Article

As a developer, I've often found the lack of beginner-friendly documentation and updated guides for setting up Flutter on Nix to be a significant hurdle. This article addresses that gap by providing a detailed guide on how to configure Flutter for development on Nix. I use this setup personally, and I'll keep this article updated as I refine my configuration.

You can find the repository for my Nix configuration here:

My Nix Config Repository


Overview

This guide demonstrates how to configure Flutter as a Nix module. If you'd prefer, you can also adapt it into a devshell setup. We'll begin by creating a flutter.nix file and importing it into your configuration.nix.

Creating the flutter.nix File

Start by creating a flutter.nix file. Here's the configuration I use:

{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.programs.flutter;
  androidComposition = pkgs.androidenv.composeAndroidPackages {
    toolsVersion = "26.1.1";
    platformToolsVersion = "35.0.1";
    buildToolsVersions = [
      "30.0.3"
      "33.0.1"
      "34.0.0"
    ];
    platformVersions = [
      "31"
      "33"
      "34"
    ];
    abiVersions = [ "x86_64" ];
    includeEmulator = true;
    emulatorVersion = "35.1.4";
    includeSystemImages = true;
    systemImageTypes = [ "google_apis_playstore" ];
    includeSources = 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;
  buildToolsVersion = "33.0.1";
in {
  options.programs.flutter = {
    enable = mkEnableOption "Flutter development environment";
    addToKvmGroup = mkEnableOption "Add user to KVM group for hardware acceleration";
    enableAdb = mkEnableOption "Enable ADB and add user to adbusers group";
    user = mkOption {
      type = types.str;
      description = "Username for Flutter development";
    };
  };

  config = mkIf cfg.enable {
    environment.systemPackages = with pkgs; [
      flutter
      androidSdk
      # android-studio
      jdk17
      firebase-tools
    ];

    environment.variables = {
      ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
      ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
      JAVA_HOME = "${pkgs.jdk17}";
      GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/34.0.0/aapt2";
    };

    nixpkgs.config = {
      android_sdk.accept_license = true;
      allowUnfree = true;
    };

    environment.shellInit = ''
      export PATH=$PATH:${androidSdk}/libexec/android-sdk/platform-tools
      export PATH=$PATH:${androidSdk}/libexec/android-sdk/cmdline-tools/latest/bin
      export PATH=$PATH:${androidSdk}/libexec/android-sdk/emulator
      export PATH="$PATH":"$HOME/.pub-cache/bin"
    '';

    programs.adb.enable = cfg.enableAdb;

    users.users.${cfg.user}.extraGroups =
      (optional cfg.addToKvmGroup "kvm") ++ (optional cfg.enableAdb "adbusers");
  };
}
Enter fullscreen mode Exit fullscreen mode

Feel free to modify it according to your needs.

Importing into configuration.nix

Once you have the flutter.nix file, import it into your configuration.nix like so:

imports = [
  ./path/to/flutter.nix
];
Enter fullscreen mode Exit fullscreen mode

Replace ./path/to/flutter.nix with the actual path to your flutter.nix file.

Additional Configuration

# Enable the Flutter module in your `configuration.nix`:
programs.flutter = {
  enable = true;
  user = "your-username"; # Replace with your actual username
  enableAdb = true;       # Enable ADB for Android debugging
  addToKvmGroup = true;   # Add to KVM group for hardware acceleration
};
Enter fullscreen mode Exit fullscreen mode

Things to Note

  • This configuration assumes you're using flakes and the unstable branch of Nixpkgs. If you're facing issues, ensure that flakes are enabled in your setup.
  • If you're unsure about any step or encounter errors, feel free to reach out or ask in the comments.

Need More Tutorials?

If you'd like more tutorials on Nix, Flutter, or anything else, let me know! I’m happy to explore and write on topics that help the community.

About the Author

Hi, I'm Shanu Kumawat, a Flutter developer passionate about crafting seamless and beautiful mobile applications. Currently, I'm diving deeper into expanding my tech stack by learning Elixir and Phoenix, embracing the world of functional programming and scalable web development.

When I'm not coding, I enjoy exploring innovative technologies and sharing knowledge with the community. Connect with me on GitHub, Twitter, or LinkedIn to collaborate and grow together!

Top comments (0)