Skip to content

Commit

Permalink
fix: Use separate sources for crates.io & Git deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk27 committed Aug 16, 2023
1 parent 2593eab commit 9bbe32c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
56 changes: 32 additions & 24 deletions build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
, runCommandLocal
, remarshal
, formats
, crateDependencies
, cratesIoDependencies
, zstd
, fetchurl
, lndir
Expand All @@ -84,15 +84,18 @@ let
postInstall
;

crate_sources = unpackedDependencies;
cratesio_sources = unpackedCratesIoDependencies;
git_sources = unpackedGitDependencies;

# The cargo config with source replacement. Replaces both crates.io crates
# and git dependencies.
cargoconfig = builtinz.writeTOML "config" {
source = {
crates-io = { replace-with = "nix-sources"; };
nix-sources = {
directory = unpackedDependencies;
crates-io = {
directory = unpackedCratesIoDependencies;
};
git = {
directory = unpackedGitDependencies;
};
} // lib.listToAttrs (
map
Expand All @@ -108,7 +111,7 @@ let
name = "${e.url}${key}";
value = lib.filterAttrs (n: _: n == "rev" || n == "tag" || n == "branch") e // {
git = e.url;
replace-with = "nix-sources";
replace-with = "git";
};
}
)
Expand Down Expand Up @@ -188,29 +191,33 @@ let
log "RUST_TEST_THREADS: $RUST_TEST_THREADS"
log "cargo_bins_jq_filter: $cargo_bins_jq_filter"
log "cargo_build_output_json (created): $cargo_build_output_json"
log "crate_sources: $crate_sources"
log "RUSTFLAGS: $RUSTFLAGS"
log "CARGO_BUILD_RUSTFLAGS: $CARGO_BUILD_RUSTFLAGS"
${lib.optionalString remapPathPrefix ''
# Remove the source path(s) in Rust
if [ -n "$RUSTFLAGS" ]; then
RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $crate_sources=/sources"
RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $cratesio_sources=/sources"
RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $git_sources=/sources"
log "RUSTFLAGS (updated): $RUSTFLAGS"
elif [ -n "$CARGO_BUILD_RUSTFLAGS" ]; then
CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS --remap-path-prefix $crate_sources=/sources"
log "CARGO_BUILD_RUSTFLAGS (updated): $CARGO_BUILD_RUSTFLAGS"
else
export CARGO_BUILD_RUSTFLAGS="--remap-path-prefix $crate_sources=/sources"
if [ -z "$CARGO_BUILD_RUSTFLAGS" ]; then
export CARGO_BUILD_RUSTFLAGS=""
fi
CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS --remap-path-prefix $cratesio_sources=/sources"
CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS --remap-path-prefix $git_sources=/sources"
log "CARGO_BUILD_RUSTFLAGS (updated): $CARGO_BUILD_RUSTFLAGS"
fi
''}
mkdir -p target
# make sure that all source files are tagged as "recent" (since we write
# Make sure that all source files are tagged as "recent" (since we write
# some stubs here and there)
find . -type f -exec touch {} +
Expand Down Expand Up @@ -365,7 +372,7 @@ let
};
};

# Unpacks all dependencies required to compile user's crate.
# Crates.io dependencies required to compile user's crate.
#
# As an output, for each dependency, this derivation produces a subdirectory
# containing `.cargo-checksum.json` (required for Cargo to process the crate)
Expand All @@ -380,18 +387,19 @@ let
# something-else-1.2.3/src (-> /nix/store/...)
# ...
# ```
#
# (note that the actual crate format is not document, but in practice it's a
# gzipped tar.)
unpackedDependencies = symlinkJoinPassViaFile {
name = "dependencies";

paths =
(map unpackCrateDependency crateDependencies) ++
(map unpackGitDependency gitDependencies);
unpackedCratesIoDependencies = symlinkJoinPassViaFile {
name = "crates-io-dependencies";
paths = (map unpackCratesIoDependency cratesIoDependencies);
};

# Git dependencies required to compile user's crate; follows same format as
# the crates.io dependencies above.
unpackedGitDependencies = symlinkJoinPassViaFile {
name = "git-dependencies";
paths = (map unpackGitDependency gitDependencies);
};

unpackCrateDependency = { name, version, sha256 }:
unpackCratesIoDependency = { name, version, sha256 }:
let
crate = fetchurl {
inherit sha256;
Expand Down
2 changes: 1 addition & 1 deletion config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ let
# version and sha256 of the crate
# Example:
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
crateDependencies = libb.mkVersions buildPlanConfig.cargolock;
cratesIoDependencies = libb.mkVersions buildPlanConfig.cargolock;
};

# config used when planning the builds
Expand Down

0 comments on commit 9bbe32c

Please sign in to comment.