Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nix.url = "github:DeterminateSystems/nix-src";
nix.url = "github:DeterminateSystems/nix-src/RossComputerGuy/capi-derived-path";
nix.inputs.nixpkgs.follows = "nixpkgs";
nix-cargo-integration.url = "github:yusdacra/nix-cargo-integration";
nix-cargo-integration.inputs.nixpkgs.follows = "nixpkgs";
Expand Down
71 changes: 71 additions & 0 deletions nix-bindings-store/src/derived_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::ptr::NonNull;

use super::path::StorePath;

use anyhow::Result;
use nix_bindings_bindgen_raw as raw;
use nix_bindings_util::context::Context;
use nix_bindings_util::{check_call, result_string_init};

pub struct DerivedPath {
raw: NonNull<raw::DerivedPath>,
}
impl DerivedPath {
pub fn get_store_path(&self) -> Result<StorePath> {
let mut context = Context::new();
unsafe {
let store_path = check_call!(raw::derived_path_get_store_path(
&mut context,
self.as_ptr()
))?;
let store_path =
NonNull::new(store_path).expect("nix_derived_path_get_store_path returned a null pointer");
Ok(StorePath::new_raw(store_path))
}
}

/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
///
/// Construct a new `DerivedPath` by first cloning the C derived path.
///
/// # Safety
///
/// This does not take ownership of the C derived path, so it should be a borrowed pointer, or you should free it.
pub unsafe fn new_raw_clone(raw: NonNull<raw::DerivedPath>) -> Self {
Self::new_raw(
NonNull::new(raw::derived_path_clone(raw.as_ptr()))
.or_else(|| panic!("nix_derived_path_clone returned a null pointer"))
.unwrap(),
)
}

/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
///
/// Takes ownership of a C `nix_derived_path`. It will be freed when the `DerivedPath` is dropped.
///
/// # Safety
///
/// The caller must ensure that the provided `NonNull<raw::DerivedPath>` is valid and that the ownership
/// semantics are correctly followed. The `raw` pointer must not be used after being passed to this function.
pub unsafe fn new_raw(raw: NonNull<raw::DerivedPath>) -> Self {
DerivedPath { raw }
}

/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
///
/// Get a pointer to the underlying Nix C API derived path.
///
/// # Safety
///
/// This function is unsafe because it returns a raw pointer. The caller must ensure that the pointer is not used beyond the lifetime of this `DerivedPath`.
pub unsafe fn as_ptr(&self) -> *mut raw::DerivedPath {
self.raw.as_ptr()
}
}
impl Drop for DerivedPath {
fn drop(&mut self) {
unsafe {
raw::derived_path_free(self.as_ptr());
}
}
}
1 change: 1 addition & 0 deletions nix-bindings-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod derivation;
pub mod derived_path;
pub mod path;
pub mod store;