Skip to content

Commit

Permalink
Adding Nix support for adding packages to the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Jul 10, 2024
1 parent e4888f9 commit b3d734e
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use queue::Queue;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error;
use std::{fs, thread};
use std::process::{Command, Stdio};
use std::{env, fs, thread};

use error::*;
use job::*;
Expand All @@ -23,6 +24,7 @@ mod worker;


const DOCS_URL: &str = "https://jzbor.de/zinn/zinn";
const NIX_ENV_MARKER: &str = "ZINN_NIX_ENV";


#[derive(Parser)]
Expand Down Expand Up @@ -100,6 +102,16 @@ struct Zinnfile {
///
/// See also [`JobDescription`].
jobs: HashMap<String, JobDescription>,

/// Nix configuration
///
/// See also [`NixConfig`]
nix: Option<NixConfig>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct NixConfig {
packages: Vec<String>,
}

impl Args {
Expand Down Expand Up @@ -128,6 +140,34 @@ where
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}

fn check_nix_flakes() -> bool {
Command::new("nix")
.arg("shell")
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok()
}

fn wrap_nix(nix_config: NixConfig) -> ZinnResult<()>{
let packages = nix_config.packages.iter()
.map(|p| format!("nixpkgs#{}", p));
Command::new("nix")
.arg("shell")
.args(packages)
.arg("--command")
.args(env::args())
.env(NIX_ENV_MARKER, "1")
.status()?;

Ok(())
}

fn inside_nix_wrap() -> bool {
env::var(NIX_ENV_MARKER).is_ok()
}

fn main_with_barkeeper<T: StateTracker>(barkeeper: T, args: Args)
where
<T as StateTracker>::ThreadStateTracker: 'static
Expand Down Expand Up @@ -166,6 +206,14 @@ where
return;
}

// enter nix wrap if desired
if let Some(nix_config) = zinnfile.nix {
if !inside_nix_wrap() && check_nix_flakes() {
resolve(wrap_nix(nix_config));
return;
}
}

// init template engine
let mut handlebars = Handlebars::new();
handlebars.set_strict_mode(true);
Expand Down

0 comments on commit b3d734e

Please sign in to comment.