crane/lib/vendorCargoDeps.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.9 KiB
Nix
Raw Normal View History

{ findCargoFiles
, lib
, runCommandLocal
, vendorCargoRegistries
2022-02-08 07:03:12 +03:00
, vendorGitDeps
2021-12-27 01:05:23 +03:00
}:
args:
2021-12-27 01:05:23 +03:00
let
inherit (builtins)
attrNames
pathExists
readFile;
inherit (lib)
concatMapStrings
escapeShellArg;
src = args.src or (throw ''
unable to find `src` attribute. consider one of the following:
- set `cargoVendorDir = vendorCargoDeps { cargoLock = ./some/path/to/Cargo.lock; }`
- set `cargoVendorDir = vendorCargoDeps { src = ./src/containing/cargo/lock/file; }`
- set `cargoVendorDir = null` to skip vendoring altogether
'');
cargoLock = args.cargoLock or (src + "/Cargo.lock");
cargoLockContents = args.cargoLockContents or (
if pathExists cargoLock
then readFile cargoLock
else
throw ''
unable to find Cargo.lock at ${src}. please ensure one of the following:
- a Cargo.lock exists at the root of the source directory of the derivation
- set `cargoVendorDir = vendorCargoDeps { cargoLock = ./some/path/to/Cargo.lock; }`
- set `cargoVendorDir = vendorCargoDeps { src = ./src/containing/cargo/lock/file; }`
- set `cargoVendorDir = null` to skip vendoring altogether
''
);
2022-02-08 07:03:12 +03:00
linkSources = sources: concatMapStrings
(name: ''
ln -s ${escapeShellArg sources.${name}} $out/${escapeShellArg name}
'')
(attrNames sources);
lock = args.cargoLockParsed or (builtins.fromTOML cargoLockContents);
2022-02-08 07:03:12 +03:00
lockPackages = lock.package or (throw "Cargo.lock missing [[package]] definitions");
vendoredRegistries = vendorCargoRegistries {
2022-02-08 07:03:12 +03:00
inherit lockPackages;
cargoConfigs = (findCargoFiles src).cargoConfigs;
};
2022-02-08 07:03:12 +03:00
vendoredGit = vendorGitDeps {
inherit lockPackages;
};
2021-12-27 01:05:23 +03:00
in
runCommandLocal "vendor-cargo-deps" { } ''
mkdir -p $out
cat >>$out/config.toml <<EOF
${vendoredRegistries.config}
2022-02-08 07:03:12 +03:00
${vendoredGit.config}
EOF
2022-02-08 07:03:12 +03:00
${linkSources vendoredRegistries.sources}
${linkSources vendoredGit.sources}
''