From 459ed33bdd278ba31d3a225710faed082f11662e Mon Sep 17 00:00:00 2001 From: bad Date: Fri, 19 Aug 2022 23:33:27 +0200 Subject: [PATCH] Add integration for libnixstore This adds a conditional feature flag for loading data from libnixstore exposed behind a feature flag dependency --- Cargo.toml | 7 +++++++ shell.nix | 10 ++++++++++ src/lib.rs | 7 +++++-- src/libnixstore.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 shell.nix create mode 100644 src/libnixstore.rs diff --git a/Cargo.toml b/Cargo.toml index 89f7c85..e189386 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,12 @@ repository = "https://im.badat.dev/bad/narinfo-rs" license = "MIT OR Apache-2.0" keywords = ["nix", "nixos"] +[features] +libnixstore = ["dep:libnixstore"] + [dependencies] derive_builder = { version = "0.11.2", default-features = false } +libnixstore = { version = "0.2.0", optional = true } + +[package.metadata.docs.rs] +all-features = true diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..a5df06c --- /dev/null +++ b/shell.nix @@ -0,0 +1,10 @@ +with import { }; +mkShell { + nativeBuildInputs = [ + nix + nlohmann_json + libsodium + boost + pkg-config + ]; +} diff --git a/src/lib.rs b/src/lib.rs index 852a60e..83dc6b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,10 +6,13 @@ extern crate alloc; pub mod error; -mod narinfo; -mod nix_cache_info; +pub mod narinfo; +pub mod nix_cache_info; mod sig; +#[cfg(feature = "libnixstore")] +mod libnixstore; + pub use crate::narinfo::NarInfo; pub use nix_cache_info::NixCacheInfo; pub use sig::Sig; diff --git a/src/libnixstore.rs b/src/libnixstore.rs new file mode 100644 index 0000000..084bc73 --- /dev/null +++ b/src/libnixstore.rs @@ -0,0 +1,27 @@ +use crate::narinfo::NarInfoBuilder; +use crate::Sig; +use alloc::borrow::Cow; + +impl<'a> NarInfoBuilder<'a> { + /// Read and set the store dir from [`libnixstore`]. + /// + /// This function assumes that [`libnixstore::init`] has already been called + pub fn store_dir_from_libnixstore(&mut self) -> &Self { + let store_dir = libnixstore::get_store_dir(); + self.store_path(Cow::Owned(store_dir)); + self + } + + /// Read and set `path`, `references` and `sigs` from a [`libnixstore::PathInfo`] + pub fn attr_from_libnixstore_path(&mut self, path: &'a libnixstore::PathInfo) -> &Self { + self.file_size(Some(path.size)); + self.references(path.refs.iter().map(Cow::from).collect()); + self.sigs( + path.sigs + .iter() + .map(|v| Sig::parse(v).expect("libnixstore provided signature should be in a valid format")) + .collect()); + + self + } +}