use crate::app::finish_tui_outcome;
use crate::app::install_in_tui;
use crate::app::open_tui;
use crate::install::install;
use crate::output::delete_file;
use crate::output::note_orphan;
use crate::output::print_doc;
use crate::output::print_migration_notes;
use crate::output::print_plan;
use crate::output::report_skew;
use crate::output::report_taint;
use crate::output::report_validation;
use crate::output::report_warnings;
use crate::output::write_file;
use crate::output::write_generated;
use crate::output::write_lock;
use crate::resolve::build_pending_oracle_sets;
use crate::resolve::flake_input_module_pins;
use crate::resolve::module_source_pins;
use crate::resolve::options_hash_for_rev;
use crate::resolve::patch_inputs_for_pending;
use crate::resolve::print_resolved_module_sources;
use crate::resolve::resolve_pending_baselines;
use crate::resolve::resolve_pending_flake_inputs;
use crate::resolve::resolve_pending_host_modules;
use crate::resolve::resolve_pending_module_sources;
use crate::resolve::resolve_pending_project_modules;
use crate::resolve::resolve_pending_project_release;
use crate::resolve::write_module_source_caches;
use crate::resolve::PendingModuleSource;
use crate::updates::cmd_updates;
use crate::updates::updates_report;
mod app;
mod install;
mod output;
mod resolve;
mod tui;
mod updates;
use std::collections::{BTreeMap, BTreeSet};
use std::io::IsTerminal;
use clap::{Parser, Subcommand};
use knixl_lock::model::{FlakeInputPin, HostBaseline, OracleModulePin};
use knixl_lock::{FileState, Plan};
#[derive(Parser)]
#[command(name = "knixl", version)]
struct Cli {
#[command(subcommand)]
cmd: Cmd,
#[arg(long, global = true)]
json: bool,
}
#[derive(Subcommand)]
enum Cmd {
Plan {
#[arg(long)]
detailed_exitcode: bool,
},
Check,
Generate {
#[arg(long)]
accept_drift: bool,
#[arg(long)]
prune: bool,
},
Upgrade {
#[arg(long)]
yes: bool,
#[arg(long)]
refresh: bool,
#[arg(long, requires = "refresh")]
if_security: bool,
#[arg(long, requires = "if_security", value_parser = ["low", "medium", "high", "critical"])]
min_severity: Option<String>,
#[arg(long, requires = "refresh", value_name = "PACKAGE=PATTERN")]
keep: Vec<String>,
},
Doc {
node: String,
},
Install {
pkg: String,
#[arg(long)]
host: Option<String>,
#[arg(long)]
yes: bool,
#[arg(long)]
strict: bool,
#[arg(long)]
build: bool,
#[arg(long)]
no_abi_check: bool,
},
Tui,
Updates,
}
#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Code {
Clean = 0,
Internal = 1,
#[allow(dead_code)]
Usage = 2,
Drift = 3,
NeedsAck = 4,
Validation = 5,
RegenPending = 6,
}
fn verdict(plan: &Plan) -> Code {
if plan.has_validation_errors() {
return Code::Validation;
}
if plan.any(FileState::is_drifted) {
return Code::Drift;
}
if plan.requires_ack() {
return Code::NeedsAck;
}
if plan.any(FileState::is_dirty) {
return Code::RegenPending;
}
Code::Clean
}
fn run(cli: Cli, ctx: &Ctx) -> Code {
let refresh = matches!(cli.cmd, Cmd::Upgrade { refresh: true, .. });
let keeps: Vec<(String, String)> = match &cli.cmd {
Cmd::Upgrade { keep, .. } => {
let mut out = Vec::new();
for k in keep {
match k.split_once('=') {
Some((p, v)) if !p.is_empty() && !v.is_empty() => {
out.push((p.to_string(), v.to_string()))
}
_ => {
eprintln!("knixl: --keep {k}: expected PACKAGE=PATTERN, e.g. curl=8.19.*");
return Code::Usage;
}
}
}
out
}
_ => Vec::new(),
};
if let Cmd::Upgrade {
if_security: true,
min_severity,
..
} = &cli.cmd
{
let floor = min_severity.as_deref().unwrap_or("low");
match updates_report(&ctx.root) {
Ok(reports) => {
let worst = reports
.iter()
.filter_map(|r| r.security.as_ref())
.flat_map(|s| s.fixed.iter().flat_map(|f| f.cves.iter()))
.map(|(_, score)| knixl_pipeline::updates::severity_rank(*score))
.max();
if reports.iter().any(|r| r.security.is_none()) {
eprintln!("knixl: --if-security needs vulnix to tell, so nothing is refreshed");
return Code::Validation;
}
if worst.is_none_or(|w| w < knixl_pipeline::updates::severity_floor(floor)) {
println!("no security fixes at {floor} or above in the update, so nothing is refreshed");
return Code::Clean;
}
}
Err(e) => {
eprintln!("knixl: {e}");
return Code::Internal;
}
}
}
let pending: BTreeMap<String, HostBaseline> =
if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
match resolve_pending_baselines(ctx, refresh, &keeps) {
Ok(p) => p,
Err(code) => return code,
}
} else {
BTreeMap::new()
};
let pending_flake_inputs: Option<Vec<FlakeInputPin>> = if matches!(cli.cmd, Cmd::Upgrade { .. })
{
match resolve_pending_flake_inputs(ctx) {
Ok(p) => p,
Err(code) => return code,
}
} else {
None
};
let pending_project_release: Option<(String, String)> =
if matches!(cli.cmd, Cmd::Upgrade { .. }) {
match resolve_pending_project_release(ctx, refresh, &keeps) {
Ok(p) => p,
Err(code) => return code,
}
} else {
None
};
let input_pins = flake_input_module_pins(
&ctx.root,
pending_flake_inputs
.as_deref()
.unwrap_or(&ctx.lock.flake_inputs),
);
let pending_modules: Option<Vec<OracleModulePin>> =
if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
match resolve_pending_project_modules(ctx, &input_pins) {
Ok(p) => p,
Err(code) => return code,
}
} else {
None
};
let pending_host_modules: BTreeMap<String, Vec<OracleModulePin>> =
if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
match resolve_pending_host_modules(ctx, &input_pins) {
Ok(p) => p,
Err(code) => return code,
}
} else {
BTreeMap::new()
};
let pending_module_sources: Option<Vec<PendingModuleSource>> =
if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
match resolve_pending_module_sources(ctx) {
Ok(p) => p,
Err(code) => return code,
}
} else {
None
};
let patched_lock = (!pending.is_empty()
|| !pending_host_modules.is_empty()
|| pending_module_sources.is_some()
|| pending_flake_inputs.is_some()
|| pending_project_release.is_some())
.then(|| {
let mut lock = ctx.lock.clone();
if let Some((release, rev)) = &pending_project_release {
lock.oracle.release = release.clone();
lock.oracle.nixpkgs_rev = rev.clone();
lock.oracle.options_hash = options_hash_for_rev(rev);
}
if let Some(pins) = &pending_flake_inputs {
lock.flake_inputs = pins.clone();
}
lock.baselines
.extend(pending.iter().map(|(host, b)| (host.clone(), b.clone())));
for (host, pins) in &pending_host_modules {
lock.baselines
.entry(host.clone())
.or_insert_with(|| HostBaseline {
release: String::new(),
nixpkgs_rev: String::new(),
options_hash: String::new(),
modules: Vec::new(),
})
.modules = pins.clone();
}
if let Some(pending) = &pending_module_sources {
lock.module_sources = module_source_pins(pending);
}
lock
});
let patched_inputs = (!pending.is_empty() || pending_flake_inputs.is_some())
.then(|| patch_inputs_for_pending(&ctx.inputs, &pending, pending_flake_inputs.is_some()));
let lock_for_plan = patched_lock.as_ref().unwrap_or(&ctx.lock);
let regather =
pending_flake_inputs.is_some() || !pending.is_empty() || pending_project_release.is_some();
let regathered = match (regather, &patched_lock) {
(true, Some(lock)) => match knixl_pipeline::gather::gather_with_lock(
&ctx.root,
&default_formatter(),
ctx.running.tool.clone(),
Some(lock.clone()),
) {
Ok(p) => Some((
patch_inputs_for_pending(&p.inputs, &pending, pending_flake_inputs.is_some()),
p.generated,
)),
Err(e) => {
eprintln!("knixl: {e}");
return Code::Internal;
}
},
_ => None,
};
let inputs_for_plan = regathered
.as_ref()
.map(|(inputs, _)| inputs)
.or(patched_inputs.as_ref())
.unwrap_or(&ctx.inputs);
let generated_for_plan = regathered
.as_ref()
.map(|(_, generated)| generated)
.unwrap_or(&ctx.generated);
let patched_running =
(pending_modules.is_some() || pending_project_release.is_some()).then(|| {
let mut oracle = ctx.running.oracle.clone();
if let Some(pins) = &pending_modules {
oracle.modules = pins.clone();
}
if let Some((release, rev)) = &pending_project_release {
oracle.release = release.clone();
oracle.nixpkgs_rev = rev.clone();
oracle.options_hash = options_hash_for_rev(rev);
}
knixl_lock::reconcile::Versions {
tool: ctx.running.tool.clone(),
formatter: ctx.running.formatter.clone(),
oracle,
modules: ctx.running.modules.clone(),
}
});
let running_for_plan = patched_running.as_ref().unwrap_or(&ctx.running);
let plan = Plan::compute(inputs_for_plan, &ctx.disk, lock_for_plan, running_for_plan);
if plan.has_validation_errors() {
report_validation(&plan.validation_errors, cli.json);
return Code::Validation;
}
report_warnings(&ctx.warnings);
match cli.cmd {
Cmd::Plan { detailed_exitcode } => {
print_plan(&plan, cli.json, &ctx.warnings);
if detailed_exitcode {
verdict(&plan)
} else {
Code::Clean
}
}
Cmd::Check => {
print_plan(&plan, cli.json, &ctx.warnings);
if !ctx.flake_lock_problems.is_empty() {
report_validation(&ctx.flake_lock_problems, cli.json);
return Code::Validation;
}
verdict(&plan)
}
Cmd::Generate {
accept_drift,
prune,
} => {
if plan.skew_needs_ack() {
report_skew(&plan, cli.json);
eprintln!("version skew present: run `knixl upgrade` to review and apply");
return Code::NeedsAck;
}
let (mut drifted, mut orphaned) = (false, false);
for f in &plan.files {
match &f.state {
FileState::Clean => {}
FileState::Stale { .. } | FileState::Missing { .. } => write_file(ctx, f),
FileState::Drifted { .. } if accept_drift => write_file(ctx, f),
FileState::Drifted { .. } => {
report_taint(f, cli.json);
drifted = true;
}
FileState::Orphaned if prune => delete_file(ctx, f),
FileState::Orphaned => {
note_orphan(f, cli.json);
orphaned = true;
}
}
}
let worst = if drifted {
Code::Drift
} else if orphaned {
Code::RegenPending
} else {
Code::Clean
};
if worst == Code::Clean {
write_lock(ctx, &plan.lock_next);
}
worst
}
Cmd::Upgrade { yes, .. } => {
if !plan.requires_ack()
&& !plan.any(FileState::is_dirty)
&& pending.is_empty()
&& pending_modules.is_none()
&& pending_host_modules.is_empty()
&& pending_module_sources.is_none()
&& pending_flake_inputs.is_none()
&& pending_project_release.is_none()
&& ctx.unvalidated.is_empty()
{
println!("already up to date");
return Code::Clean;
}
print_migration_notes(&plan, &ctx.registry);
print_plan(&plan, cli.json, &ctx.warnings);
if !yes {
for (host, b) in &pending {
println!(
"would resolve nixpkgs release \"{}\" for host \"{host}\" -> {}",
b.release, b.nixpkgs_rev,
);
}
for m in pending_modules.iter().flatten() {
println!(
"would resolve oracle module \"{}\" ({}) -> {}",
m.name, m.url, m.rev,
);
}
for (host, pins) in &pending_host_modules {
for m in pins {
println!(
"would resolve oracle module \"{}\" ({}) for host \"{host}\" -> {}",
m.name, m.url, m.rev,
);
}
}
if let Some(pending) = &pending_module_sources {
print_resolved_module_sources(pending, "would resolve");
}
for i in pending_flake_inputs.iter().flatten() {
println!(
"would resolve flake input \"{}\" ({}) -> {}",
i.name, i.url, i.rev,
);
}
if let Some((release, rev)) = &pending_project_release {
match release.is_empty() {
true => println!("would clear the project nixpkgs release"),
false => println!(
"would resolve the project nixpkgs release \"{release}\" -> {rev}"
),
}
}
let missing: BTreeSet<&str> = ctx
.unvalidated
.iter()
.map(|(_, rev)| rev.as_str())
.collect();
for rev in missing {
println!("would build the NixOS option set for nixpkgs {rev}");
}
eprintln!("re-run with --yes to apply");
return Code::NeedsAck;
}
for f in &plan.files {
if !matches!(f.state, FileState::Clean) {
write_generated(&ctx.root, generated_for_plan, f);
}
}
for (host, b) in &pending {
println!(
"resolved nixpkgs release \"{}\" for host \"{host}\" -> {}",
b.release, b.nixpkgs_rev,
);
}
for m in pending_modules.iter().flatten() {
println!(
"resolved oracle module \"{}\" ({}) -> {}",
m.name, m.url, m.rev,
);
}
for (host, pins) in &pending_host_modules {
for m in pins {
println!(
"resolved oracle module \"{}\" ({}) for host \"{host}\" -> {}",
m.name, m.url, m.rev,
);
}
}
if let Some(pending) = &pending_module_sources {
print_resolved_module_sources(pending, "resolved");
}
for i in pending_flake_inputs.iter().flatten() {
println!(
"resolved flake input \"{}\" ({}) -> {}",
i.name, i.url, i.rev,
);
}
if let Some((release, rev)) = &pending_project_release {
match release.is_empty() {
true => println!("cleared the project nixpkgs release"),
false => {
println!("resolved the project nixpkgs release \"{release}\" -> {rev}")
}
}
}
let mut lock_next = plan.lock_next.clone();
let mut changed_hosts: BTreeSet<String> = pending.keys().cloned().collect();
changed_hosts.extend(pending_host_modules.keys().cloned());
if let Err(code) = build_pending_oracle_sets(
&ctx.root,
&mut lock_next,
pending_modules.is_some(),
&changed_hosts,
false,
true,
) {
return code;
}
if let Some(pending) = &pending_module_sources {
if let Err(code) = write_module_source_caches(pending) {
return code;
}
}
write_lock(ctx, &lock_next); Code::Clean
}
Cmd::Doc { .. } => unreachable!("doc is dispatched before Ctx::load"),
Cmd::Install {
pkg,
host,
yes,
strict,
build,
no_abi_check,
} if !yes && std::io::stdin().is_terminal() && std::io::stdout().is_terminal() => {
install_in_tui(
ctx,
&pkg,
host.as_deref(),
strict,
build,
no_abi_check,
&pending,
)
}
Cmd::Install {
pkg,
host,
yes,
strict,
build,
no_abi_check,
} => install(
ctx,
&pkg,
host.as_deref(),
yes,
strict,
build,
no_abi_check,
&pending,
pending_modules.as_deref(),
&pending_host_modules,
pending_module_sources.as_deref(),
),
Cmd::Tui => unreachable!("tui is dispatched before Ctx::load"),
Cmd::Updates => cmd_updates(ctx, cli.json),
}
}
fn confirm(prompt: &str) -> bool {
use std::io::Write;
print!("{prompt} [y/N] ");
let _ = std::io::stdout().flush();
let mut line = String::new();
std::io::stdin().read_line(&mut line).is_ok()
&& matches!(line.trim().chars().next(), Some('y') | Some('Y'))
}
fn main() {
let code = std::panic::catch_unwind(dispatch).unwrap_or(Code::Internal);
std::process::exit(code as i32);
}
fn dispatch() -> Code {
let cli = Cli::parse();
if matches!(cli.cmd, Cmd::Tui) {
if !std::io::stdin().is_terminal() || !std::io::stdout().is_terminal() {
eprintln!("knixl: tui needs an interactive terminal");
return Code::Usage;
}
return match open_tui(tui::Entry::Hub, Default::default()) {
Ok(outcome) => finish_tui_outcome(outcome),
Err(e) => {
eprintln!("knixl: {e}");
Code::Internal
}
};
}
if let Cmd::Doc { node } = &cli.cmd {
return match knixl_pipeline::gather::doc_registry(&discover_root()) {
Ok(registry) => print_doc(®istry, node),
Err(e) => {
eprintln!("knixl: {e}");
gather_error_code(&e)
}
};
}
run(cli, &Ctx::load())
}
struct Ctx {
inputs: knixl_lock::reconcile::Inputs,
disk: knixl_lock::reconcile::DiskState,
lock: knixl_lock::Lock,
running: knixl_lock::reconcile::Versions,
registry: knixl_modules::Registry,
root: std::path::PathBuf,
generated: std::collections::BTreeMap<std::path::PathBuf, String>,
warnings: Vec<String>,
flake_lock_problems: Vec<String>,
unfree_hosts: BTreeSet<std::path::PathBuf>,
unvalidated: Vec<(String, String)>,
}
fn gather_error_code(e: &knixl_pipeline::gather::GatherError) -> Code {
use knixl_pipeline::gather::GatherError;
use knixl_pipeline::GenerateError;
match e {
GatherError::Module(_)
| GatherError::Generate(
GenerateError::Parse(_) | GenerateError::Validation(_) | GenerateError::Lower(_),
) => Code::Validation,
GatherError::Io(_) | GatherError::Lock(_) | GatherError::Generate(_) => Code::Internal,
}
}
impl Ctx {
fn load() -> Ctx {
let root = discover_root();
let tool = env!("CARGO_PKG_VERSION")
.parse()
.expect("tool version parses");
let project = knixl_pipeline::gather::gather(&root, &default_formatter(), tool)
.unwrap_or_else(|e| {
eprintln!("knixl: {e}");
std::process::exit(gather_error_code(&e) as i32);
});
Ctx {
inputs: project.inputs,
disk: project.disk,
lock: project.lock,
running: project.versions,
registry: project.registry,
root: project.root,
generated: project.generated,
warnings: project.warnings,
flake_lock_problems: project.flake_lock_problems,
unvalidated: project.unvalidated,
unfree_hosts: project.unfree_hosts,
}
}
}
fn discover_root() -> std::path::PathBuf {
let start = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
let mut dir = start.as_path();
loop {
if dir.join("knixl.lock.kdl").exists() || dir.join("hosts").is_dir() {
return dir.to_path_buf();
}
match dir.parent() {
Some(parent) => dir = parent,
None => return start,
}
}
}
fn default_formatter() -> knixl_nix::Formatter {
let bin = choose_formatter_bin(std::env::var("KNIXL_FORMATTER").ok(), formatter_runs);
knixl_nix::Formatter::detect("nixfmt-rfc-style", bin.into(), "0.6.0")
}
fn choose_formatter_bin(env_override: Option<String>, runs: impl Fn(&str) -> bool) -> String {
if let Some(bin) = env_override {
return bin;
}
for cand in ["nixfmt", "nixfmt-rfc-style"] {
if runs(cand) {
return cand.to_string();
}
}
"nixfmt".to_string()
}
fn formatter_runs(bin: &str) -> bool {
std::process::Command::new(bin)
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::choose_formatter_bin;
#[test]
fn env_override_wins() {
assert_eq!(choose_formatter_bin(Some("cat".into()), |_| true), "cat");
}
#[test]
fn prefers_nixfmt_when_present() {
assert_eq!(choose_formatter_bin(None, |_| true), "nixfmt");
}
#[test]
fn falls_back_to_alternative_name() {
assert_eq!(
choose_formatter_bin(None, |b| b == "nixfmt-rfc-style"),
"nixfmt-rfc-style"
);
}
#[test]
fn defaults_to_nixfmt_when_none_run() {
assert_eq!(choose_formatter_bin(None, |_| false), "nixfmt");
}
}