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

Hello anyone. I come back using NixOS after a year with my latest laptop.
I have questions about Flutter setup on NixOS. I want to get it easy without using Flakes or Nix dev-shell, and it can done in my any IDE / text editor setup (I install Android Studio, VS Code, and Neovim). How must I do to solve it? I get this error on execute flutter doctor -v.

$ flutter doctor -v
[✓] Flutter (Channel stable, 3.13.8, on NixOS 23.11 (Tapir) 6.1.87, locale en_US.UTF-8)
    • Flutter version 3.13.8 on channel stable at /nix/store/xqnvplwa30md9503anpixix0zn57dv9k-flutter-3.13.8-unwrapped
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6c4930c4ac (7 months ago), 2023-10-18 10:57:55 -0500
    • Engine revision 767d8c75e8
    • Dart version 3.1.4
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/anifyuli/.android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /home/anifyuli/.android/sdk
    • Java binary at: /run/current-system/sw/bin/java
    • Java version OpenJDK Runtime Environment (build 19.0.2+7-nixos)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = google-chrome-stable

[✗] Linux toolchain - develop for Linux desktop
    ✗ clang++ is required for Linux development.
      It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
    ✗ CMake is required for Linux development.
      It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
    ✗ ninja is required for Linux development.
      It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from
      https://github.com/ninja-build/ninja/releases
    ✗ pkg-config is required for Linux development.
      It is likely available from your distribution (e.g.: apt install pkg-config), or can be downloaded from
      https://www.freedesktop.org/wiki/Software/pkg-config/

[!] Android Studio (version 2023.2)
    • Android Studio at /nix/store/pc4fn8zl4336bdlagji24hpn8qs4n1c2-android-studio-stable-2023.2.1.25-unwrapped
    • Flutter plugin version 79.0.1
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Failed to run Java: ProcessException: No such file or directory
        Command: /nix/store/pc4fn8zl4336bdlagji24hpn8qs4n1c2-android-studio-stable-2023.2.1.25-unwrapped/jbr/bin/java -version
    ✗ Unable to determine bundled Java version.
    • Try updating or re-installing Android Studio.

[☠] Connected device (the doctor check crashed)
    ✗ Due to an error, the doctor check did not complete. If the error message below is not helpful, please let us know about this
      issue at https://github.com/flutter/flutter/issues.
    ✗ Exception: Unable to run "adb", check your Android SDK installation and ANDROID_SDK_ROOT environment variable:
      /home/anifyuli/.android/sdk/platform-tools/adb
    • #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
      #1      AndroidDevices.pollingGetDevices (package:flutter_tools/src/android/android_device_discovery.dart:75:7)
      <asynchronous suspension>
      #2      PollingDeviceDiscovery._populateDevices (package:flutter_tools/src/device.dart:563:36)
      <asynchronous suspension>
      #3      Future.wait.<anonymous closure> (dart:async/future.dart:525:21)
      <asynchronous suspension>
      #4      DeviceManager.refreshAllDevices (package:flutter_tools/src/device.dart:231:40)
      <asynchronous suspension>
      #5      DeviceValidator.validate (package:flutter_tools/src/doctor.dart:689:34)
      <asynchronous suspension>
      #6      Future.any.onValue (dart:async/future.dart:615:5)
      <asynchronous suspension>


[✓] Network resources
    • All expected network resources are available.

I install Flutter on my Home Manager like this :

  # Packages that should be installed to the user profile.
  home.packages = with pkgs; [    
    flutter-unwrapped
  ];

I add Android SDK downloade from Android Studio and save it on $HOME/.android/sdk. I still confused how to solve it

Thanks for your help :hugs: and sorry My English

2 Likes

Here’s the shell.nix I use:

I’m not an expert on Nix or NixOS, so there may well be things to improve there.

One thing that’s important for me is that I install Flutter itself as a Git worktree with git clone, rather than try to do that part via Nix. The shell.nix is therefore only for dependencies of Flutter, and other tools, not for installing Flutter itself.

A Git worktree with git clone is exactly what Flutter’s upstream install instructions do, and it makes an extremely smooth workflow for experimenting with changes to the Flutter framework itself and for contributing those changes to Flutter upstream. And in my experience, that very smooth connection to upstream is one of the things that makes Flutter as great a platform as it is. So I’d recommend to any Flutter developer that you consider using the git clone approach, whether you’re on NixOS or any other operating system.

I have question again. How to integrate Android Studio to Flutter? I still haven’t idea how to do that. And I find Flutter from Nixpkgs have some error on emulator recognition

How to integrate Android Studio to Flutter? I still haven’t idea how to do that.

One of the dependencies in my shell.nix above is android-studio. Then I run nix-shell --run android-studio &, and that works.

Because the way I install Flutter itself (as described above) aligns with Flutter upstream’s install instructions, Android Studio can be configured for Flutter in the same way as on any other Linux distribution. Here’s Flutter’s instructions for that:

(I said above that Flutter upstream’s install instructions use git clone, and looking at that page I’m reminded that that’s not quite right. The upstream instructions are to download a tarball and unpack it. But when you do… what you get is a Git worktree, complete with a .git/ directory, exactly like one you could have gotten with git clone. So ignore the mention of git clone, but the core recommendation is the same: for installing Flutter, follow Flutter upstream’s instructions.)

And I find Flutter from Nixpkgs have some error on emulator recognition

If you share the exact error message you’re seeing (with copy and paste), it may be possible to help debug this. But as I’ve said, my own recommendation would be to install Flutter the upstream way, rather than from nixpkgs.

In my thread, I mention it problems

[!] Android Studio (version 2023.2)
    • Android Studio at /nix/store/pc4fn8zl4336bdlagji24hpn8qs4n1c2-android-studio-stable-2023.2.1.25-unwrapped
    • Flutter plugin version 79.0.1
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Failed to run Java: ProcessException: No such file or directory
        Command: /nix/store/pc4fn8zl4336bdlagji24hpn8qs4n1c2-android-studio-stable-2023.2.1.25-unwrapped/jbr/bin/java -version
    ✗ Unable to determine bundled Java version.
    • Try updating or re-installing Android Studio.

[☠] Connected device (the doctor check crashed)
    ✗ Due to an error, the doctor check did not complete. If the error message below is not helpful, please let us know about this
      issue at https://github.com/flutter/flutter/issues.
    ✗ Exception: Unable to run "adb", check your Android SDK installation and ANDROID_SDK_ROOT environment variable:
      /home/anifyuli/.android/sdk/platform-tools/adb
    • #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
      #1      AndroidDevices.pollingGetDevices (package:flutter_tools/src/android/android_device_discovery.dart:75:7)
      <asynchronous suspension>
      #2      PollingDeviceDiscovery._populateDevices (package:flutter_tools/src/device.dart:563:36)
      <asynchronous suspension>
      #3      Future.wait.<anonymous closure> (dart:async/future.dart:525:21)
      <asynchronous suspension>
      #4      DeviceManager.refreshAllDevices (package:flutter_tools/src/device.dart:231:40)
      <asynchronous suspension>
      #5      DeviceValidator.validate (package:flutter_tools/src/doctor.dart:689:34)
      <asynchronous suspension>
      #6      Future.any.onValue (dart:async/future.dart:615:5)
      <asynchronous suspension>

I see. It sounds like this is the error you mean:

[☠] Connected device (the doctor check crashed)
    ✗ Due to an error, the doctor check did not complete. If the error message below is not helpful, please let us know about this
      issue at https://github.com/flutter/flutter/issues.
    ✗ Exception: Unable to run "adb", check your Android SDK installation and ANDROID_SDK_ROOT environment variable:
      /home/anifyuli/.android/sdk/platform-tools/adb

What happens if you run adb devices yourself, in the same Nix environment where you’re running that flutter doctor -v command? If that gives an error, then that’s the thing to debug, and Flutter is no longer part of the picture.

I connect my phone to my computer with adb

anifyuli@ThinkPad-X280:~  
$ adb devices 
List of devices attached
82fdbbb4	device

But it still same error

[☠] Connected device (the doctor check crashed)
    ✗ Due to an error, the doctor check did not complete. If the error message below is not helpful, please let us know about this
      issue at https://github.com/flutter/flutter/issues.
    ✗ Exception: Unable to run "adb", check your Android SDK installation and ANDROID_SDK_ROOT environment variable:
      /home/anifyuli/.android/sdk/platform-tools/adb
    • #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
      #1      AndroidDevices.pollingGetDevices (package:flutter_tools/src/android/android_device_discovery.dart:75:7)
      <asynchronous suspension>
      #2      PollingDeviceDiscovery._populateDevices (package:flutter_tools/src/device.dart:563:36)
      <asynchronous suspension>
      #3      Future.wait.<anonymous closure> (dart:async/future.dart:525:21)
      <asynchronous suspension>
      #4      DeviceManager.refreshAllDevices (package:flutter_tools/src/device.dart:231:40)
      <asynchronous suspension>
      #5      DeviceValidator.validate (package:flutter_tools/src/doctor.dart:689:34)
      <asynchronous suspension>
      #6      Future.any.onValue (dart:async/future.dart:615:5)
      <asynchronous suspension>

I now using this repo for add Android SDK to my system because if I use Android SDK from Android Studio, I need to patchelf if I want to use it without Android Studio

Hmm, I see. I think at this point if you want to debug that Flutter installation further, the next step will involve getting your hands into the source code of the flutter tool itself. You’ll want to look at what’s happening at this stack frame:

      #1      AndroidDevices.pollingGetDevices (package:flutter_tools/src/android/android_device_discovery.dart:75:7)

i.e. in the file packages/flutter_tools/lib/src/android/android_device_discovery.dart in your Flutter tree. That code is running an adb devices command, and it’s failing for some reason; you’ll want to find out more information about why.

The good news is that, like the Flutter framework, the flutter command-line tool is all Dart code, just like you write for a Flutter app. You can freely edit the file (*) and rerun the command, just as if you were editing a file in your own app. And because your Flutter tree is a Git worktree, you can use git diff and git stash and the rest of the Git CLI to track your changes, undo them, re-apply them, and so on.

Because you passed -v to flutter doctor, I think this code really ought to give you more information to help debug this. In particular, it should be printing the stderr output from that command, given that it failed. To debug this, I’d suggest editing the code to make it do that.

Once you’ve written that change, if you’d like to send it as a PR upstream, I think that’d be a nice contribution to Flutter. If you do, ping this thread, and if I see it I’d be glad to give it a review to help it get merged. (I’m a Flutter committer, one of many.)

Circling back, though, I’d again suggest that you try installing Flutter following the upstream instructions, instead of via Nix. It’s possible that will fix this issue; and for the reasons I gave above, I think it’s a better workflow anyway. If it doesn’t, then you can carry on debugging from there.

(*) In fact it just occurred to me that your Flutter installation may be in /nix/store/ and therefore impossible to edit. If so, then this is an example of why it’s helpful to install it the upstream way instead :slight_smile:, and again I recommend doing that.