Skip to content

Commit aaa7722

Browse files
committed
[nix] use a single vendor dir for all deps
This fixes some annoying edge cases where the guest .cargo/config.toml vendor file were not picked up. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 3d4010b commit aaa7722

1 file changed

Lines changed: 27 additions & 34 deletions

File tree

flake.nix

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@
109109
rustc = toolchains.stable.rust;
110110
};
111111

112+
manifests = [
113+
"Cargo.toml"
114+
"src/tests/rust_guests/dummyguest/Cargo.toml"
115+
"src/tests/rust_guests/simpleguest/Cargo.toml"
116+
"src/tests/rust_guests/witguest/Cargo.toml"
117+
];
118+
manifestDeps = builtins.map (manifest:
119+
let lockPath = builtins.replaceStrings [ "toml" ] [ "lock" ] manifest; in
120+
let lockFile = ./${lockPath}; in
121+
rust-platform.importCargoLock { inherit lockFile; }) manifests;
112122
# when building a guest with cargo-hyperlight, or when
113123
# building a miri sysroot for the main workspace, we need to
114124
# include any crates.io dependencies of the standard library
@@ -118,20 +128,9 @@
118128
) toolchains;
119129
stdlibDeps = builtins.map (lockFile:
120130
rust-platform.importCargoLock { inherit lockFile; }) stdlibLocks;
121-
withStdlibLock = lockFile:
122-
pkgs.symlinkJoin {
123-
name = "cargo-deps";
124-
paths = stdlibDeps ++ [
125-
(rust-platform.importCargoLock {
126-
inherit lockFile;
127-
})
128-
];
129-
};
130-
deps = {
131-
"Cargo.toml" = withStdlibLock ./Cargo.lock;
132-
"src/tests/rust_guests/dummyguest/Cargo.toml" = withStdlibLock ./src/tests/rust_guests/dummyguest/Cargo.lock;
133-
"src/tests/rust_guests/simpleguest/Cargo.toml" = withStdlibLock ./src/tests/rust_guests/simpleguest/Cargo.lock;
134-
"src/tests/rust_guests/witguest/Cargo.toml" = withStdlibLock ./src/tests/rust_guests/witguest/Cargo.lock;
131+
deps = pkgs.symlinkJoin {
132+
name = "cargo-deps";
133+
paths = stdlibDeps ++ manifestDeps;
135134
};
136135

137136
# Script snippet, used in the cargo/rustc wrappers below,
@@ -144,26 +143,10 @@
144143
# like `cargo clippy` and `cargo hyperlight` (see
145144
# https://github.com/rust-lang/cargo/issues/11031).
146145
materialiseDeps = let
147-
sortedNames = lib.lists.reverseList (builtins.attrNames deps);
146+
sortedManifests = lib.lists.sort (p: q: p > q) manifests;
148147
matchClause = path: '' */${path}) root="''${manifest%${path}}" ;;'';
149148
matchClauses = lib.strings.concatStringsSep "\n"
150-
(builtins.map matchClause sortedNames);
151-
makeClause = manifest: vendor: let
152-
dir = builtins.dirOf manifest;
153-
gitExclude = builtins.toString (/. + "${dir}/.cargo");
154-
in ''
155-
mkdir -p $root/${dir}/.cargo
156-
cat >$root/${dir}/.cargo/config.toml <<EOF
157-
[source.crates-io]
158-
replace-with = "vendored-sources"
159-
160-
[source.vendored-sources]
161-
directory = "${vendor}"
162-
EOF
163-
printf "# vendor dependency configuration generated by nix\n%s\n" "${gitExclude}" >> $root/.git/info/exclude
164-
'';
165-
makeClauses = lib.strings.concatStringsSep "\n"
166-
(lib.mapAttrsToList makeClause deps);
149+
(builtins.map matchClause sortedManifests);
167150
in ''
168151
base_cargo() {
169152
PATH="$base/bin:$PATH" "$base/bin/cargo" "$@"
@@ -174,8 +157,18 @@
174157
${matchClauses}
175158
esac
176159
if [ -f ''${root}/flake.nix ]; then
160+
161+
mkdir -p $root/$.cargo
162+
cat >$root/.cargo/config.toml <<EOF
163+
[source.crates-io]
164+
replace-with = "vendored-sources"
165+
166+
[source.vendored-sources]
167+
directory = "${deps}"
168+
EOF
169+
177170
sed -i '/# vendor dependency configuration generated by nix/{N;d;}' $root/.git/info/exclude
178-
${makeClauses}
171+
printf "# vendor dependency configuration generated by nix\n%s\n" "/.cargo" >> $root/.git/info/exclude
179172
fi
180173
181174
# libgit2-sys copies a vendored git2 into the target/
@@ -238,7 +231,7 @@
238231
pname = "hyperlight";
239232
version = "0.0.0";
240233
src = lib.cleanSource ./.;
241-
cargoDeps = deps."Cargo.toml";
234+
cargoDeps = deps;
242235

243236
nativeBuildInputs = [
244237
azure-cli

0 commit comments

Comments
 (0)