Skip to content

Commit

Permalink
rustPlatform: Allow impure cache.
Browse files Browse the repository at this point in the history
I hope this isn't too controversial. Right now if you make any change to your rust source (including any non-source files) a subsequent Nix build will recompile all of your dependencies and do a full compile of your crate. This change allows using a directory of your choice to cache cargo's artifacts. This allows incremental rebuilds.

The intent of this is that it can be used in CI, and possibly local development to reduce full rebuilds. This is especially useful if you depend on a large number of crates.

This is not intended to be used for production releases and other cases where complete correctness is required.

Concerns:

- I think executables that have been removed from the source will be grabbed by the installPhase so might not be removed from the package output.
  • Loading branch information
kevincox committed Sep 25, 2020
1 parent 1e501a4 commit f7745fd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkgs/build-support/rust/default.nix
Expand Up @@ -32,6 +32,11 @@
, depsExtraArgs ? {}
, cargoParallelTestThreads ? true

# If set the CARGO_TARGET_DIR will be set to this directory. This will allow
# you to avoid rebuilding dependencies.
# This setting is *incompatible* with sandboxing.
, impureCacheDir ? null

# Needed to `pushd`/`popd` into a subdir of a tarball if this subdir
# contains a Cargo.toml, but isn't part of a workspace (which is e.g. the
# case for `rustfmt`/etc from the `rust-sources).
Expand Down Expand Up @@ -75,8 +80,17 @@ let
cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
releaseDir = "target/${rustTarget}/${buildType}";
tmpDir = "${releaseDir}-tmp";

releaseDirPrefix = if impureCacheDir == null
then "target"
else
assert
stdenv.lib.assertMsg (stdenv.lib.hasPrefix "/" impureCacheDir)
"impureCacheDir must be an absolute path.";
impureCacheDir;

releaseDir = "${releaseDirPrefix}/${rustTarget}/${buildType}";
tmpDir = "target/${rustTarget}/${buildType}-tmp";

# Specify the stdenv's `diff` by abspath to ensure that the user's build
# inputs do not cause us to find the wrong `diff`.
Expand All @@ -98,6 +112,8 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // {
PKG_CONFIG_ALLOW_CROSS =
if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;

CARGO_TARGET_DIR = impureCacheDir;

postUnpack = ''
eval "$cargoDepsHook"
Expand Down

0 comments on commit f7745fd

Please sign in to comment.