nix_build/lib.rs
1use std::path::PathBuf;
2
3mod config;
4pub use config::{Config, Derivation};
5
6/// Collection of Nix expressions useful for package configuration
7pub mod exprs;
8
9#[derive(Debug)]
10pub enum Error {
11 NixNotAvailable,
12 BuildFailed,
13 UnknownOutput,
14}
15
16type Result<T> = std::result::Result<T, Error>;
17
18const NIX_BIN_NAME: &str = "nix";
19
20/// Returns the path to the found `nix` program
21///
22/// Will prioritize the `NIX` environment variable if set
23pub fn is_nix_available() -> Option<PathBuf> {
24 std::env::var_os("NIX")
25 .map(PathBuf::from)
26 .or_else(|| which::which(NIX_BIN_NAME).ok())
27 .and_then(|nix| {
28 if nix.try_exists().ok().unwrap_or_default() {
29 Some(nix)
30 } else {
31 None
32 }
33 })
34}
35
36/// Builds the derivation found in `default.nix` with default options
37///
38/// Returns the resulting derivations
39///
40/// # Examples
41/// ```no_run
42/// use nix_build as nix;
43///
44/// # fn main() -> Result<(), nix::Error> {
45/// let derivations = nix::build()?; // will build ./default.nix
46/// let libfoo = derivations[0].output().expect("to have an 'out' derivation");
47///
48/// println!("cargo:rustc-link-search=native={}", libfoo.display());
49/// println!("cargo:rustc-link-lib=static=foo");
50/// # Ok(()) }
51/// ```
52pub fn build() -> Result<Vec<Derivation>> {
53 Config::new().build()
54}