Logo

dev-resources.site

for different kinds of informations.

Easy way to setup Flutter Development Environment on NixOS without Flakes or dev-shell

Published at
12/11/2024
Categories
nix
flutter
tutorial
development
Author
shanu-kumawat
Categories
4 categories in total
nix
open
flutter
open
tutorial
open
development
open
Author
13 person written this
shanu-kumawat
open
Easy way to setup Flutter Development Environment on NixOS without Flakes or dev-shell

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!

nix Article's
30 articles in total
Favicon
Easy development environments with Nix and Nix flakes!
Favicon
A Conversation with Docker CTO Justin Cormack and Flux CEO Ron Efrani: The Future of Developer Environments
Favicon
Nice one
Favicon
NixOS - A Unique Linux Distribution
Favicon
Getting started with Nix and Nix Flakes
Favicon
My new Nix series!
Favicon
gRPC, Haskell, Nix, love, hate
Favicon
Easy way to setup Flutter Development Environment on NixOS without Flakes or dev-shell
Favicon
Dotfiles, the nix way
Favicon
Why Use Nix package manager, Even on macOS?
Favicon
Easy GitHub CLI Extensions with Nix
Favicon
Abusing Haskell: Executable Blog Posts
Favicon
Nix first steps
Favicon
Cross-Posting to Dev.to with API
Favicon
An Introduction to Nix for Ruby Developers
Favicon
Using niv to Manage Haskell Dependencies
Favicon
Don't Rebuild Yourself - an Intro to Nix Package Caches
Favicon
Packing Custom Fonts for NixOS
Favicon
Embrace the Power of Nix for Your Python + Rust Workflow
Favicon
How to Deploy Flutter on Upsun
Favicon
Declarative and reproducible environments with colima, nix and k8s
Favicon
Combining Nix with Terraform for better DevOps
Favicon
The Perfect System Configuration
Favicon
Azure Function app that runs Haskell Nix package
Favicon
A Journey to Find an Ultimate Development Environment
Favicon
The one thing I do not like about the Nix package manager (and a fix for it)
Favicon
Creating Repeatable Builds
Favicon
Develop R Packages under Nix Shell
Favicon
How I use Nix in my Elm projects
Favicon
Nix Quick Tips - Flake for OCaml

Featured ones: