Home Explore Blog Models CI



nixpkgs

2nd chunk of `doc/languages-frameworks/dart.section.md`
0dc60b138a9dba3fc2a2439715a67c64e40baa47e3a2e9cb000000010000097f
Dart supports multiple [outputs types](https://dart.dev/tools/dart-compile#types-of-output); you can choose between them using `dartOutputType` (defaults to `exe`). If you want to override the binaries path or the source path they come from, you can use `dartEntryPoints`. Outputs that require a runtime will automatically be wrapped with the relevant runtime (`dartaotruntime` for `aot-snapshot`, `dart run` for `jit-snapshot` and `kernel`, `node` for `js`); this can be overridden through `dartRuntimeCommand`.

```nix
{
  lib,
  buildDartApplication,
  fetchFromGitHub,
}:

buildDartApplication rec {
  pname = "dart-sass";
  version = "1.62.1";

  src = fetchFromGitHub {
    owner = "sass";
    repo = "dart-sass";
    tag = version;
    hash = "sha256-U6enz8yJcc4Wf8m54eYIAnVg/jsGi247Wy8lp1r1wg4=";
  };

  pubspecLock = lib.importJSON ./pubspec.lock.json;
}
```

### Patching dependencies {#ssec-dart-applications-patching-dependencies}

Some Dart packages require patches or build environment changes. Package derivations can be customised with the `customSourceBuilders` argument.

A collection of such customisations can be found in Nixpkgs, in the `development/compilers/dart/package-source-builders` directory.

This allows fixes for packages to be shared between all applications that use them. It is strongly recommended to add to this collection instead of including fixes in your application derivation itself.

### Running executables from dev_dependencies {#ssec-dart-applications-build-tools}

Many Dart applications require executables from the `dev_dependencies` section in `pubspec.yaml` to be run before building them.

This can be done in `preBuild`, in one of two ways:

1. Packaging the tool with `buildDartApplication`, adding it to Nixpkgs, and running it like any other application
2. Running the tool from the package cache

Of these methods, the first is recommended when using a tool that does not need
to be of a specific version.

For the second method, the `packageRun` function from the `dartConfigHook` can be used.
This is an alternative to `dart run` that does not rely on Pub.

e.g., for `build_runner`:

```bash
packageRun build_runner build
```

Do _not_ use `dart run <package_name>`, as this will attempt to download dependencies with Pub.

### Usage with nix-shell {#ssec-dart-applications-nix-shell}

#### Using dependencies from the Nix store {#ssec-dart-applications-nix-shell-deps}

Title: Dart Application Customization, Dependency Patching, and `dev_dependencies` Management
Summary
This chunk details advanced configurations for building Dart applications, including selecting different output types using `dartOutputType` (e.g., `exe`, `aot-snapshot`, `jit-snapshot`, `kernel`, `js`), overriding binary and source paths with `dartEntryPoints`, and customizing runtime commands via `dartRuntimeCommand`. It provides a Nix example for `dart-sass`. The text then explains how to patch Dart package dependencies using the `customSourceBuilders` argument, recommending that such customizations be added to a shared Nixpkgs directory for reusability. Finally, it outlines two methods for running executables from `dev_dependencies`: packaging them as separate applications or using the `packageRun` function from `dartConfigHook` (preferred over `dart run` to avoid re-downloading dependencies). The chunk concludes by introducing the topic of using `nix-shell` for Dart applications, specifically concerning dependencies from the Nix store.