Library Examples
Practical examples of how to use the Zoi library in your Rust code.
This page provides practical, copy-paste-ready examples for using Zoi's library features.
Building a Package
This example demonstrates how to build a package from a .pkg.lua
file. This will create a distributable package archive (.pkg.tar.zst
).
Code
use zoi::{build, Scope};
use std::path::Path;
use anyhow::Result;
fn main() -> Result<()> {
// Path to your package definition file.
let package_file = Path::new("path/to/your/package.pkg.lua");
// Specify the build type, e.g. "source" or "pre-compiled".
let build_type = "source";
// Specify the target platforms.
let platforms = vec!["linux-amd64".to_string()];
// Optionally, provide a PGP key to sign the package.
let sign_key = Some("YourPgpKeyNameOrFingerprint".to_string());
println!("Building package: {}", package_file.display());
// Call the build function.
build(package_file, build_type, &platforms, sign_key)?;
println!("Package built successfully!");
Ok(())
}
Installing a Package
This example shows how to install a package from a local archive file that you have either built or downloaded.
Code
use zoi::{install_package, Scope};
use std::path::Path;
use anyhow::Result;
fn main() -> Result<()> {
// Path to the package archive.
let archive_path = Path::new("path/to/your/package-1.0.0-linux-amd64.pkg.tar.zst");
// Optionally override the installation scope.
let scope = Some(Scope::User);
// Specify the registry handle. Use "local" if it's not from a registry.
let registry_handle = "local";
println!("Installing from archive: {}", archive_path.display());
// Call the install function.
let installed_files = install_package(archive_path, scope, registry_handle)?;
println!("Package installed successfully. {} files were installed.", installed_files.len());
Ok(())
}
Uninstalling a Package
This example demonstrates how to programmatically uninstall a package.
Code
use zoi::{uninstall_package, Scope};
use anyhow::Result;
fn main() -> Result<()> {
let package_name = "my-package-name";
// Optionally specify the scope from which to uninstall.
let scope = Some(Scope::User);
println!("Uninstalling package: {}", package_name);
// Call the uninstall function.
uninstall_package(package_name, scope)?;
println!("Package '{}' uninstalled successfully.", package_name);
Ok(())
}
Last updated on