feat(tvix-store): Improve tvix-store copy.

This change contains 2 improvements to the tvix-store copy command:

1. Allows reading the reference graph from stdin, using `-` argument
2. Supports json representation produced by `nix path-info --json`
   command.

In general it makes is easier and faster to import arbitrary closures
from an existing nix store with e.g the following command:

```
nix path-info ./result --json --closure-size --recursive | \
  jq -s '{closure: add}' | \
  tvix-store copy -
```

Change-Id: Id6eea2993da233ecfbdc186f1a8c37735b686264
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12765
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Vova Kryachko 2024-11-12 09:54:05 -05:00 committed by Vladimir Kryachko
parent b1764e1109
commit 6aada91062
3 changed files with 117 additions and 12 deletions

View file

@ -85,10 +85,18 @@ enum Commands {
#[clap(flatten)]
service_addrs: ServiceUrlsGrpc,
/// A path pointing to a JSON file produced by the Nix
/// A path pointing to a JSON file(or '-' for stdin) produced by the Nix
/// `__structuredAttrs` containing reference graph information provided
/// by the `exportReferencesGraph` feature.
///
/// Additionally supports the output from the following nix command:
///
/// ```notrust
/// nix path-info --json --closure-size --recursive <some-path> | \
/// jq -s '{closure: add}' | \
/// tvix-store copy -
/// ```
///
/// This can be used to invoke tvix-store inside a Nix derivation
/// copying to a Tvix store (or outside, if the JSON file is copied
/// out).
@ -348,9 +356,14 @@ async fn run_cli(
} => {
let (blob_service, directory_service, path_info_service, _nar_calculation_service) =
tvix_store::utils::construct_services(service_addrs).await?;
// Parse the file at reference_graph_path.
let reference_graph_json = tokio::fs::read(&reference_graph_path).await?;
let reference_graph_json = if reference_graph_path == PathBuf::from("-") {
let mut writer: Vec<u8> = vec![];
tokio::io::copy(&mut tokio::io::stdin(), &mut writer).await?;
writer
} else {
tokio::fs::read(&reference_graph_path).await?
};
#[derive(Deserialize, Serialize)]
struct ReferenceGraph<'a> {
@ -430,8 +443,8 @@ async fn run_cli(
references: elem.references.iter().map(StorePath::to_owned).collect(),
nar_size: elem.nar_size,
nar_sha256: elem.nar_sha256,
signatures: vec![],
deriver: None,
signatures: elem.signatures.iter().map(|s| s.to_owned()).collect(),
deriver: elem.deriver.map(|p| p.to_owned()),
ca: None,
};