about summary refs log tree commit diff
path: root/pkgs/build-support/dart/build-dart-application/default.nix
blob: be1fd727767115d924c15e304c04bbcd4a029055 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{ lib, stdenv, fetchDartDeps, runCommand, writeText, dartHooks, makeWrapper, dart, cacert, nodejs, darwin }:

{ pubGetScript ? "dart pub get"

  # Output type to produce. Can be any kind supported by dart
  # https://dart.dev/tools/dart-compile#types-of-output
  # If using jit, you might want to pass some arguments to `dartJitFlags`
, dartOutputType ? "exe"
, dartCompileCommand ? "dart compile"
, dartCompileFlags ? [ ]
  # These come at the end of the command, useful to pass flags to the jit run
, dartJitFlags ? [ ]

  # Attrset of entry point files to build and install.
  # Where key is the final binary path and value is the source file path
  # e.g. { "bin/foo" = "bin/main.dart";  }
  # Set to null to read executables from pubspec.yaml
, dartEntryPoints ? null
  # Used when wrapping aot, jit, kernel, and js builds.
  # Set to null to disable wrapping.
, dartRuntimeCommand ?
    if dartOutputType == "aot-snapshot" then "${dart}/bin/dartaotruntime"
    else if (dartOutputType == "jit-snapshot" || dartOutputType == "kernel") then "${dart}/bin/dart"
    else if dartOutputType == "js" then "${nodejs}/bin/node"
    else null

, pubspecLockFile ? null
, vendorHash ? ""
, ...
}@args:

let
  dartDeps = (fetchDartDeps.override {
    dart = runCommand "dart-fod" { nativeBuildInputs = [ makeWrapper ]; } ''
      mkdir -p "$out/bin"
      makeWrapper "${dart}/bin/dart" "$out/bin/dart" \
        --add-flags "--root-certs-file=${cacert}/etc/ssl/certs/ca-bundle.crt"
    '';
  }) {
    buildDrvArgs = args;
    inherit pubGetScript vendorHash pubspecLockFile;
  };
  inherit (dartHooks.override { inherit dart; }) dartConfigHook dartBuildHook dartInstallHook;
in
assert !(builtins.isString dartOutputType && dartOutputType != "") ->
  throw "dartOutputType must be a non-empty string";
stdenv.mkDerivation (args // {
  inherit pubGetScript dartCompileCommand dartOutputType dartRuntimeCommand
    dartCompileFlags dartJitFlags;

    dartEntryPoints =
      if (dartEntryPoints != null)
      then writeText "entrypoints.json" (builtins.toJSON dartEntryPoints)
      else null;

  nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
    dart
    dartDeps
    dartConfigHook
    dartBuildHook
    dartInstallHook
    makeWrapper
  ] ++ lib.optionals stdenv.isDarwin [
    darwin.sigtool
  ];

  # When stripping, it seems some ELF information is lost and the dart VM cli
  # runs instead of the expected program. Don't strip if it's an exe output.
  dontStrip = args.dontStrip or (dartOutputType == "exe");

  passthru = { inherit dartDeps; } // (args.passthru or { });

  meta = (args.meta or { }) // { platforms = args.meta.platforms or dart.meta.platforms; };
})