Merge pull request #43 from nixops4/use-harmonia-store-core-new
Add conversions for Harmonia types to nix-bindings-store
This commit is contained in:
commit
39e6939288
7 changed files with 1086 additions and 10 deletions
898
Cargo.lock
generated
898
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
19
nci.nix
19
nci.nix
|
|
@ -49,5 +49,24 @@
|
|||
};
|
||||
};
|
||||
};
|
||||
nci.crates.nix-bindings-store =
|
||||
let
|
||||
addHarmoniaProfile = ''
|
||||
cat >> Cargo.toml <<'EOF'
|
||||
|
||||
[profile.harmonia]
|
||||
inherits = "release"
|
||||
EOF
|
||||
'';
|
||||
in
|
||||
{
|
||||
profiles.harmonia = {
|
||||
features = [ "harmonia" ];
|
||||
runTests = true;
|
||||
# Add harmonia profile to Cargo.toml for both deps and main builds
|
||||
depsDrvConfig.mkDerivation.postPatch = addHarmoniaProfile;
|
||||
drvConfig.mkDerivation.postPatch = addHarmoniaProfile;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ nix-bindings-util-sys = { path = "../nix-bindings-util-sys", version = "0.2.1" }
|
|||
nix-bindings-store-sys = { path = "../nix-bindings-store-sys", version = "0.2.1" }
|
||||
lazy_static = "1.4"
|
||||
zerocopy = "0.8"
|
||||
harmonia-store-core = { version = "0.0.0-alpha.0", optional = true }
|
||||
serde_json = { version = "1.0", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
ctor = "0.2"
|
||||
|
|
@ -30,6 +32,9 @@ pkg-config = "0.3"
|
|||
# Needed for version parsing in build.rs
|
||||
nix-bindings-util = { path = "../nix-bindings-util", version = "0.2.1" }
|
||||
|
||||
[features]
|
||||
harmonia = [ "dep:harmonia-store-core", "dep:serde_json" ]
|
||||
|
||||
[lints.rust]
|
||||
warnings = "deny"
|
||||
dead-code = "allow"
|
||||
|
|
|
|||
100
nix-bindings-store/src/derivation/harmonia.rs
Normal file
100
nix-bindings-store/src/derivation/harmonia.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
use anyhow::Context as _;
|
||||
|
||||
use super::Derivation;
|
||||
|
||||
impl Derivation {
|
||||
/// Convert harmonia Derivation to nix-bindings Derivation.
|
||||
///
|
||||
/// This requires a Store instance because the Nix C API needs it for internal validation.
|
||||
pub fn from_harmonia(
|
||||
store: &mut crate::store::Store,
|
||||
harmonia_drv: &harmonia_store_core::derivation::Derivation,
|
||||
) -> anyhow::Result<Self> {
|
||||
let json = serde_json::to_string(harmonia_drv)
|
||||
.context("Failed to serialize harmonia Derivation to JSON")?;
|
||||
|
||||
store.derivation_from_json(&json)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Derivation> for harmonia_store_core::derivation::Derivation {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(nix_drv: &Derivation) -> anyhow::Result<Self> {
|
||||
let json = nix_drv
|
||||
.to_json_string()
|
||||
.context("Failed to convert nix Derivation to JSON")?;
|
||||
|
||||
serde_json::from_str(&json).context("Failed to parse JSON as harmonia Derivation")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn create_harmonia_derivation() -> harmonia_store_core::derivation::Derivation {
|
||||
use harmonia_store_core::derivation::{Derivation, DerivationOutput};
|
||||
use harmonia_store_core::derived_path::OutputName;
|
||||
use harmonia_store_core::store_path::StorePath;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::str::FromStr;
|
||||
|
||||
let system = format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS);
|
||||
let out_path = "8bs8sd27bzzy6w94fznjd2j8ldmdg7x6-myname";
|
||||
|
||||
let env = BTreeMap::from([
|
||||
("builder".into(), "/bin/sh".into()),
|
||||
("name".into(), "myname".into()),
|
||||
("out".into(), format!("/{out_path}").into()),
|
||||
("system".into(), system.clone().into()),
|
||||
]);
|
||||
let mut outputs = BTreeMap::new();
|
||||
outputs.insert(
|
||||
OutputName::from_str("out").unwrap(),
|
||||
DerivationOutput::InputAddressed(StorePath::from_base_path(out_path).unwrap()),
|
||||
);
|
||||
|
||||
Derivation {
|
||||
args: vec!["-c".into(), "echo $name foo > $out".into()],
|
||||
builder: "/bin/sh".into(),
|
||||
env,
|
||||
inputs: BTreeSet::new(),
|
||||
name: b"myname".as_slice().try_into().unwrap(),
|
||||
outputs,
|
||||
platform: system.into(),
|
||||
structured_attrs: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derivation_round_trip_harmonia() {
|
||||
let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap();
|
||||
let harmonia_drv = create_harmonia_derivation();
|
||||
|
||||
// Convert to nix-bindings Derivation
|
||||
let nix_drv = Derivation::from_harmonia(&mut store, &harmonia_drv).unwrap();
|
||||
|
||||
// Convert back to harmonia Derivation
|
||||
let harmonia_round_trip: harmonia_store_core::derivation::Derivation =
|
||||
(&nix_drv).try_into().unwrap();
|
||||
|
||||
assert_eq!(harmonia_drv, harmonia_round_trip);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derivation_clone() {
|
||||
let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap();
|
||||
let harmonia_drv = create_harmonia_derivation();
|
||||
|
||||
let derivation = Derivation::from_harmonia(&mut store, &harmonia_drv).unwrap();
|
||||
let cloned_derivation = derivation.clone();
|
||||
|
||||
let original_harmonia: harmonia_store_core::derivation::Derivation =
|
||||
(&derivation).try_into().unwrap();
|
||||
let cloned_harmonia: harmonia_store_core::derivation::Derivation =
|
||||
(&cloned_derivation).try_into().unwrap();
|
||||
|
||||
assert_eq!(original_harmonia, cloned_harmonia);
|
||||
}
|
||||
}
|
||||
|
|
@ -84,3 +84,9 @@ impl Drop for Derivation {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "harmonia")]
|
||||
mod harmonia;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
||||
65
nix-bindings-store/src/path/harmonia.rs
Normal file
65
nix-bindings-store/src/path/harmonia.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
use anyhow::{Context as _, Result};
|
||||
|
||||
use super::{StorePath, STORE_PATH_HASH_SIZE};
|
||||
|
||||
impl TryFrom<&harmonia_store_core::store_path::StorePath> for StorePath {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(harmonia_path: &harmonia_store_core::store_path::StorePath) -> Result<Self> {
|
||||
let hash: &[u8; STORE_PATH_HASH_SIZE] = harmonia_path.hash().as_ref();
|
||||
StorePath::from_parts(hash, harmonia_path.name().as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&StorePath> for harmonia_store_core::store_path::StorePath {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(nix_path: &StorePath) -> Result<Self> {
|
||||
let hash = nix_path
|
||||
.hash()
|
||||
.context("Failed to get hash from nix StorePath")?;
|
||||
let harmonia_hash = harmonia_store_core::store_path::StorePathHash::new(hash);
|
||||
|
||||
let name = nix_path
|
||||
.name()
|
||||
.context("Failed to get name from nix StorePath")?;
|
||||
|
||||
let harmonia_name: harmonia_store_core::store_path::StorePathName = name
|
||||
.parse()
|
||||
.context("Failed to parse name as StorePathName")?;
|
||||
|
||||
Ok(harmonia_store_core::store_path::StorePath::from((
|
||||
harmonia_hash,
|
||||
harmonia_name,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
fn store_path_round_trip_harmonia() {
|
||||
let harmonia_path: harmonia_store_core::store_path::StorePath =
|
||||
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv".parse().unwrap();
|
||||
|
||||
let nix_path: crate::path::StorePath = (&harmonia_path).try_into().unwrap();
|
||||
|
||||
let harmonia_round_trip: harmonia_store_core::store_path::StorePath =
|
||||
(&nix_path).try_into().unwrap();
|
||||
|
||||
assert_eq!(harmonia_path, harmonia_round_trip);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_path_harmonia_clone() {
|
||||
let harmonia_path: harmonia_store_core::store_path::StorePath =
|
||||
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv".parse().unwrap();
|
||||
|
||||
let nix_path: crate::path::StorePath = (&harmonia_path).try_into().unwrap();
|
||||
let cloned_path = nix_path.clone();
|
||||
|
||||
assert_eq!(nix_path.name().unwrap(), cloned_path.name().unwrap());
|
||||
assert_eq!(nix_path.hash().unwrap(), cloned_path.hash().unwrap());
|
||||
}
|
||||
}
|
||||
|
|
@ -125,6 +125,9 @@ impl Drop for StorePath {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "harmonia", nix_at_least = "2.33"))]
|
||||
mod harmonia;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
Loading…
Add table
Add a link
Reference in a new issue