summary refs log tree commit diff
path: root/pkgs/build-support/rust/default.nix
blob: 864e42c476196015fe027124524d8d58bb6d1647 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{ stdenv, cacert, git, rust, cargo-vendor, python3 }:
let
  fetchcargo = import ./fetchcargo.nix {
    inherit stdenv cacert git rust cargo-vendor python3;
  };
in
{ name, cargoSha256 ? "unset"
, src ? null
, srcs ? null
, cargoPatches ? []
, patches ? []
, sourceRoot ? null
, logLevel ? ""
, buildInputs ? []
, cargoUpdateHook ? ""
, cargoDepsHook ? ""
, cargoBuildFlags ? []

, cargoVendorDir ? null
# This tells cargo-vendor to include a Cargo config file in the fixed-output
# derivation. This is desirable in every case, so please set it to true.
# Eventually this will default to true and even later this option and the old
# behaviour will be removed.
, useRealVendorConfig ? false
, ... } @ args:

assert cargoVendorDir == null -> cargoSha256 != "unset";
assert cargoVendorDir != null -> !useRealVendorConfig;

let
  cargoDeps = if cargoVendorDir == null
    then fetchcargo {
        inherit name src srcs sourceRoot cargoUpdateHook;
        patches = cargoPatches;
        sha256 = cargoSha256;
        writeVendorConfig = useRealVendorConfig;
      }
    else null;

  setupVendorDir = if cargoVendorDir == null
    then ''
      unpackFile "$cargoDeps"
      cargoDepsCopy=$(stripHash $(basename $cargoDeps))
      chmod -R +w "$cargoDepsCopy"
    ''
    else ''
      cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
    '';

in stdenv.mkDerivation (args // {
  inherit cargoDeps;

  patchRegistryDeps = ./patch-registry-deps;

  buildInputs = [ cacert git rust.cargo rust.rustc ] ++ buildInputs;

  patches = cargoPatches ++ patches;

  configurePhase = args.configurePhase or ''
    runHook preConfigure
    # noop
    runHook postConfigure
  '';

  postUnpack = ''
    eval "$cargoDepsHook"

    ${setupVendorDir}

    mkdir .cargo
  '' + (if useRealVendorConfig then ''
      substitute "$(pwd)/$cargoDepsCopy/.cargo/config" .cargo/config \
      --subst-var-by vendor "$(pwd)/$cargoDepsCopy"
    '' else ''
      cat >.cargo/config <<-EOF
        [source.crates-io]
        registry = 'https://github.com/rust-lang/crates.io-index'
        replace-with = 'vendored-sources'

        [source.vendored-sources]
        directory = '$(pwd)/$cargoDepsCopy'
      EOF
    '') + ''

    unset cargoDepsCopy

    export RUST_LOG=${logLevel}
  '' + (args.postUnpack or "");

  buildPhase = with builtins; args.buildPhase or ''
    runHook preBuild
    echo "Running cargo build --release ${concatStringsSep " " cargoBuildFlags}"
    cargo build --release --frozen ${concatStringsSep " " cargoBuildFlags}
    runHook postBuild
  '';

  checkPhase = args.checkPhase or ''
    runHook preCheck
    echo "Running cargo test"
    cargo test
    runHook postCheck
  '';

  doCheck = args.doCheck or true;

  installPhase = args.installPhase or ''
    runHook preInstall
    mkdir -p $out/bin
    find target/release -maxdepth 1 -executable -type f -exec cp "{}" $out/bin \;
    runHook postInstall
  '';

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