Tools for compiling Flutter, Dart, and Flutter eLinux projects with Nix
github:FlafyDev/guifetch - Flutter project
github:FlafyDev/listen_blue - Flutter project
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [ dart-flutter.overlays.default ];
};
in {
devShell = pkgs.mkShell {
packages = [
pkgs.pubspec-nix
];
};
}
Once you have pubspec-nix
, run it at the root of your Flutter project and wait for it to download everything.
After pubspec-nix
is done, a file named pubspec-nix.lock
will be created at the root of your Flutter project.
This file will be used to generate Pub's cache directory inside the Nix derivation.
Now that you have the pubspec-nix.lock
. All that's left is to make a package with buildFlutterApp
:
{ buildFlutterApp }:
buildFlutterApp {
pname = "pname";
version = "version";
# Optional:
# pubspecNixLockFile = <path> (default = src/pubspec-nix.lock)
src = ./.;
}
pubspecNixLockFile
can be set to override the default location of pubspec-nix.lock
.
Setting configurePhase
, buildPhase
, or installPhase
will do nothing. Consider using pre<Phase>
and post<Phsae>
instead.
pkgs.callPackage ./package.nix {
buildFlutterApp = pkgs.buildFlutterApp.override {
flutter = pkgs.flutter2;
};
}
The process is similar to packaging a Flutter project.
{ buildDartApp }:
buildDartApp {
pname = "pname";
version = "version";
# Optional:
# pubspecNixLockFile = <path> (default = src/pubspec-nix.lock)
# dartRuntimeFlags = <list of strings> (default = [])
# jit = <bool> (default = false)
src = ./.;
}
You can create a devShell for Flutter projects with mkFlutterShell
similar to how you would with mkShell
.
You can get mkFlutterShell
through the default overlay (overlays.default
).
let
pkgs = import nixpkgs {
inherit system;
overlays = [ dart-flutter.overlays.default ];
};
in
{
devShell = pkgs.mkFlutterShell {
# Enable if you want to build you app for Android.
android = {
enable = true; # Default: false
sdkPackages = sdkPkgs:
with sdkPkgs; [
build-tools-29-0-2
platforms-android-32
];
androidStudio = false; # Default: false
emulator = false; # Default: false
};
# Enable if you want to build your app for the Linux desktop.
linux = {
enable = true; # Default: false
};
# Enable if you want to build your app with Flutter eLinux.
elinux = {
enable = true;
exposeAsFlutter = true; # Default: false - Changes the executable's name from "flutter-elinux" to "flutter"
};
# This function also acts like `mkShell`, so you can still do:
buildInputs = with pkgs; [
gst_all_1.gstreamer
gst_all_1.gst-libav
gst_all_1.gst-plugins-base
gst_all_1.gst-plugins-good
libunwind
elfutils
zstd
orc
];
};
}
Similar to how you can easily code and package Python and Shell with Nix, you can now do the same with Dart using buildDartScript (Can be found in the default overlay of this flake.)
{buildDartScript, hello}:
# 1st example
buildDartScript "my-script-1" {} ''
void main() {
print('Hello, World!');
}
''
# 2nd example
buildDartScript "my-script-2" {
isolated = true; # Clears PATH at runtime.
dependencies = [ hello ]; # Adds dependencies to PATH at runtime.
# Accepts any argument that buildDartApp accepts.
} ''
import 'dart:io';
void main() async {
final output = await Process.run("hello", []);
print("Output: " + output.stdout); // Prints "Output: Hello, world!"
}
''
# 3rd example
buildDartScript "my-script-3" {} ./main.dart
# 4th example
buildDartScript "my-script-4" {} ./src # Must contains main.dart
Adding Dart dependencies with Pub is still not possible. Different ways of implementing this are still being considered, but it is planned.
Rewriting pubspec-nix
from Python to Dart is also planned, but blocked on this feature.
nix flake check -L
-L
Logging
These sources helped me understand how Flutter downloads stuff.