pub mod ast;
pub(crate) mod interop;
pub(crate) mod provenance;
pub(crate) mod render;
#[cfg(all(test, feature = "evaluation"))]
mod backend_audit;
#[cfg(all(test, feature = "evaluation"))]
pub(crate) mod context_audit;
#[cfg(all(test, feature = "evaluation"))]
mod render_audit;
use crate::Diagnostic;
#[cfg(all(test, feature = "evaluation"))]
use crate::NixSession;
use crate::{Config, IntoConfig, ir::Node, ir::ValueKind};
#[cfg(all(test, feature = "evaluation"))]
use crate::{DiagnosticKind, Provenance};
use ast::{BinaryOp, Builtin, NixExpr, NixKind};
pub use render::{Generated, RenderOptions, SourceSpan, render, render_with_options};
use std::{cell::Cell, rc::Rc};
#[track_caller]
pub fn compile(config: impl IntoConfig) -> Result<Generated, Box<Diagnostic>> {
compile_with_options(config, RenderOptions::default())
}
#[track_caller]
pub fn compile_with_options(
config: impl IntoConfig,
options: RenderOptions,
) -> Result<Generated, Box<Diagnostic>> {
let config = config.into_config();
config
.validate()
.map_err(|error| Box::new(Diagnostic::validation(error.origin, error.message)))?;
for assignment in &config.assignments {
if let Some(reference) = option_reference(&assignment.value) {
return Err(Diagnostic::validation(
reference.origin.clone(),
"symbolic NixOS option references require NixosModule lowering".into(),
)
.into());
}
}
let mut generated = render_with_options(&lower(&config), options);
generated.backend_metadata = provenance::collect(&config);
Ok(generated)
}
pub(crate) fn option_reference(node: &Node) -> Option<&Node> {
scoped_reference(node, false)
}
pub(crate) fn module_package_reference(node: &Node) -> Option<&Node> {
scoped_reference(node, true)
}
fn scoped_reference(node: &Node, packages_only: bool) -> Option<&Node> {
let option_reference = |node| scoped_reference(node, packages_only);
match &node.kind {
ValueKind::OptionReference(_) => {
if packages_only {
None
} else {
Some(node)
}
}
ValueKind::List(items) => items.iter().find_map(option_reference),
ValueKind::AttrSet(fields) | ValueKind::OpaqueRecord(fields) => {
fields.iter().find_map(|(_, node)| option_reference(node))
}
ValueKind::Apply(left, right)
| ValueKind::AttrMerge(left, right)
| ValueKind::Assert(left, right)
| ValueKind::Divide(left, right)
| ValueKind::Equal(left, right) => {
option_reference(left).or_else(|| option_reference(right))
}
ValueKind::Select(value, _)
| ValueKind::ToText(value)
| ValueKind::StringPrefix { value, .. }
| ValueKind::InRange { value, .. } => option_reference(value),
ValueKind::Function { body, .. } => option_reference(body),
ValueKind::FunctionAttrs { defaults, body, .. } => defaults
.iter()
.find_map(|(_, v)| option_reference(v))
.or_else(|| option_reference(body)),
ValueKind::If(condition, yes, no) => option_reference(condition)
.or_else(|| option_reference(yes))
.or_else(|| option_reference(no)),
ValueKind::Reference(reference) => match &reference.source {
rusix::ir::Source::NixosPackages { .. } => Some(node),
rusix::ir::Source::Packages { overlays } => overlays.iter().find_map(option_reference),
_ => None,
},
ValueKind::Parameter(_) => None,
ValueKind::Bool(_)
| ValueKind::Int(_)
| ValueKind::Float(_)
| ValueKind::Null
| ValueKind::String(_) => None,
}
}
fn lower(config: &Config) -> NixExpr {
let assignments = config
.assignments
.iter()
.map(|assignment| {
let value = lower_value(&assignment.value);
let annotated =
NixExpr::attributed(NixKind::Group(Box::new(value)), assignment.origin.clone());
(assignment.path_segments().to_vec(), annotated)
})
.collect();
NixExpr::attributed(NixKind::AttrSet(assignments), config.origin.clone())
}
fn variable(name: &str) -> NixExpr {
NixExpr::plain(NixKind::Variable(name.into()))
}
fn integer(value: i64) -> NixExpr {
NixExpr::plain(NixKind::Int(value))
}
fn binary(op: BinaryOp, left: NixExpr, right: NixExpr) -> NixExpr {
NixExpr::plain(NixKind::Binary(op, Box::new(left), Box::new(right)))
}
pub(crate) fn lower_value(node: &Node) -> NixExpr {
lower_scoped(node, &[])
}
#[derive(Clone, Copy)]
enum DiagnosticBoundary {
OpaqueCall,
Validation,
Coercion,
FinalOption,
RecordUnion,
}
fn runtime_boundary(kind: &ValueKind) -> Option<DiagnosticBoundary> {
match kind {
ValueKind::Apply(..) => Some(DiagnosticBoundary::OpaqueCall),
ValueKind::AttrMerge(..) => Some(DiagnosticBoundary::RecordUnion),
ValueKind::InRange { .. } => Some(DiagnosticBoundary::Validation),
ValueKind::StringPrefix { .. } => Some(DiagnosticBoundary::Coercion),
ValueKind::OptionReference(_) => Some(DiagnosticBoundary::FinalOption),
_ => None,
}
}
#[derive(Clone)]
struct ParameterScope<'a> {
binding: u64,
arguments: Option<&'a [String]>,
record_used: Rc<Cell<bool>>,
}
fn lower_argument_region(
node: &Node,
binding: u64,
arguments: &[String],
scope: &[ParameterScope<'_>],
) -> NixExpr {
let record_used = Rc::new(Cell::new(false));
let mut nested = scope.to_vec();
nested.push(ParameterScope {
binding,
arguments: Some(arguments),
record_used: record_used.clone(),
});
let value = lower_scoped(node, &nested);
if !record_used.get() {
return value;
}
let record = NixExpr::plain(NixKind::AttrSet(
arguments
.iter()
.map(|argument| (vec![argument.clone()], variable(argument)))
.collect(),
));
NixExpr::plain(NixKind::Let(
format!("__rusix_arg_{}", scope.len()),
Box::new(record),
Box::new(value),
))
}
fn argument_scope(node: &Node, scope: &[ParameterScope<'_>]) -> Option<usize> {
match &node.kind {
ValueKind::Parameter(binding) => scope
.iter()
.position(|entry| entry.binding == *binding && entry.arguments.is_some()),
ValueKind::Select(value, _) => argument_scope(value, scope),
_ => None,
}
}
fn lower_scoped(node: &Node, scope: &[ParameterScope<'_>]) -> NixExpr {
let lower_value = |node| lower_scoped(node, scope);
let kind = match &node.kind {
ValueKind::Function { binding, body } => {
let name = format!("__rusix_arg_{}", scope.len());
let mut scope = scope.to_vec();
scope.push(ParameterScope {
binding: *binding,
arguments: None,
record_used: Rc::new(Cell::new(false)),
});
NixKind::Lambda(name, Box::new(lower_scoped(body, &scope)))
}
ValueKind::FunctionAttrs {
binding,
arguments,
defaults,
body,
} => NixKind::ArgumentFunction(
arguments
.iter()
.map(|argument| {
(
argument.clone(),
defaults
.iter()
.find(|(n, _)| n == argument)
.map(|(_, value)| {
lower_argument_region(value, *binding, arguments, scope)
}),
)
})
.collect(),
Box::new(lower_argument_region(body, *binding, arguments, scope)),
),
ValueKind::Parameter(binding) => {
let index = scope
.iter()
.position(|entry| entry.binding == *binding)
.expect("validated callback scope");
if scope[index].arguments.is_some() {
scope[index].record_used.set(true);
}
NixKind::Variable(format!("__rusix_arg_{index}"))
}
ValueKind::If(condition, yes, no) => NixKind::If(
Box::new(lower_value(condition)),
Box::new(lower_value(yes)),
Box::new(lower_value(no)),
),
ValueKind::AttrMerge(left, right) => NixKind::Binary(
BinaryOp::AttrMerge,
Box::new(lower_value(left)),
Box::new(lower_value(right)),
),
ValueKind::Assert(condition, value) => NixKind::Assert(
Box::new(lower_value(condition)),
Box::new(lower_value(value)),
),
ValueKind::Equal(left, right) => NixKind::Binary(
crate::compiler::ast::BinaryOp::Equal,
Box::new(lower_value(left)),
Box::new(lower_value(right)),
),
ValueKind::Bool(v) => NixKind::Bool(*v),
ValueKind::Int(v) => NixKind::Int(*v),
ValueKind::Float(v) => NixKind::Float(*v),
ValueKind::Null => NixKind::Null,
ValueKind::String(v) => NixKind::String(v.clone()),
ValueKind::List(items) => NixKind::List(items.iter().map(lower_value).collect()),
ValueKind::AttrSet(fields) | ValueKind::OpaqueRecord(fields) => NixKind::AttrSet(
fields
.iter()
.map(|(name, node)| (vec![name.clone()], lower_value(node)))
.collect(),
),
ValueKind::Reference(reference) => {
return interop::lower_reference_scoped(reference, &lower_value);
}
ValueKind::OptionReference(path) => {
NixKind::Select(Box::new(variable("config")), path.parts().to_vec())
}
ValueKind::ToText(value) => NixKind::Call(Builtin::ToString, vec![lower_value(value)]),
ValueKind::StringPrefix { prefix, value } => NixKind::Binary(
BinaryOp::Add,
Box::new(NixExpr::plain(NixKind::String(prefix.clone()))),
Box::new(lower_value(value)),
),
ValueKind::Apply(function, argument) => {
let mut function = lower_value(function);
if matches!(function.kind, NixKind::Apply(..))
&& function
.origin
.as_ref()
.is_some_and(|o| o.id == node.origin.id)
{
function.error_context = false;
}
NixKind::Apply(Box::new(function), Box::new(lower_value(argument)))
}
ValueKind::Select(value, path) => {
let kind = if let Some(index) = argument_scope(value, scope) {
let first = &path.parts()[0];
let known = scope[index].arguments.unwrap().contains(first);
let shadowed = scope[index + 1..]
.iter()
.any(|entry| entry.arguments.is_some_and(|names| names.contains(first)));
if matches!(value.kind, ValueKind::Parameter(_)) && known && !shadowed {
let root =
NixExpr::attributed(NixKind::Variable(first.clone()), value.origin.clone());
NixKind::ArgumentSelect(Box::new(root), path.parts()[1..].to_vec())
} else {
NixKind::ArgumentSelect(Box::new(lower_value(value)), path.parts().to_vec())
}
} else {
interop::select(lower_value(value), path).kind
};
return NixExpr::attributed(kind, node.origin.clone());
}
ValueKind::Divide(left, right) => {
NixKind::Call(Builtin::Div, vec![lower_value(left), lower_value(right)])
}
ValueKind::InRange {
value,
min,
max,
message,
} => {
let condition = binary(
BinaryOp::And,
binary(
BinaryOp::GreaterEqual,
variable("__rusix_range"),
integer(*min),
),
binary(
BinaryOp::LessEqual,
variable("__rusix_range"),
integer(*max),
),
);
let body = NixExpr::plain(NixKind::If(
Box::new(condition),
Box::new(variable("__rusix_range")),
Box::new(NixExpr::plain(NixKind::Call(
Builtin::Throw,
vec![NixExpr::plain(NixKind::String(message.clone()))],
))),
));
NixKind::Let(
"__rusix_range".into(),
Box::new(lower_value(value)),
Box::new(body),
)
}
};
if runtime_boundary(&node.kind).is_some() {
NixExpr::contextual(kind, node.origin.clone())
} else {
NixExpr::attributed(kind, node.origin.clone())
}
}