use crate::interop::raw::AttrPath;
use crate::interop::raw::NixFunctionExt;
use crate::interop::{ModuleRef, Nixpkgs, PackageRef, raw::NixValue};
use crate::ir::{IntoNode, ReferencedExpression};
use crate::{Config, Expr, IntoConfig, Node, Origin, ValueKind};
use std::marker::PhantomData;
mod schema;
pub use schema::{OptionDecl, OptionType};
#[track_caller]
pub fn assertion(condition: impl Into<NixValue>, message: impl Into<NixValue>) -> NixValue {
NixValue::record([("assertion", condition.into()), ("message", message.into())])
}
#[track_caller]
pub fn merge(values: impl IntoIterator<Item = NixValue>) -> NixValue {
Nixpkgs::new()
.function("mkMerge")
.call(NixValue::list(values))
}
impl NixValue {
#[track_caller]
pub fn when(self, condition: impl Into<Self>) -> Self {
Nixpkgs::new()
.function("mkIf")
.apply([condition.into(), self])
}
#[track_caller]
pub fn priority(self, priority: DefinitionPriority) -> Self {
let library = Nixpkgs::new();
match priority {
DefinitionPriority::Normal => self,
DefinitionPriority::Default => library.function("mkDefault").call(self),
DefinitionPriority::Force => library.function("mkForce").call(self),
DefinitionPriority::Override(value) => {
library.function("mkOverride").apply([value.into(), self])
}
}
}
#[track_caller]
pub fn before(self) -> Self {
Nixpkgs::new().function("mkBefore").call(self)
}
#[track_caller]
pub fn after(self) -> Self {
Nixpkgs::new().function("mkAfter").call(self)
}
}
#[derive(Clone, Debug)]
pub struct OptionRef<T> {
path: AttrPath,
origin: Origin,
ty: PhantomData<T>,
}
impl<T> OptionRef<T> {
#[track_caller]
pub fn new(path: &str) -> Self {
Self::from_segments(path.split('.'))
}
#[track_caller]
pub fn from_segments(parts: impl IntoIterator<Item = impl Into<String>>) -> Self {
let path = AttrPath::segments(parts);
Self {
origin: Origin::caller(format!("NixOS option reference {}", path.parts().join("."))),
path,
ty: PhantomData,
}
}
pub fn into_expr(self) -> Expr<T> {
Expr::new(ValueKind::OptionReference(self.path), self.origin)
}
}
impl<T> crate::interop::raw::AsNixValue for OptionRef<T> {
fn as_value(&self) -> NixValue {
NixValue::from_node(Node {
origin: self.origin.clone(),
kind: ValueKind::OptionReference(self.path.clone()),
})
}
}
#[derive(Clone, Debug)]
pub struct Import {
pub path: String,
pub origin: Origin,
}
#[derive(Clone, Debug)]
pub struct Assertion {
pub name: String,
pub condition: Node,
pub message: String,
pub origin: Origin,
}
#[derive(Clone, Debug)]
pub struct NixosModule {
pub config: Config,
pub options: Config,
pub generated_imports: Vec<(NixValue, Origin)>,
pub imports: Vec<Import>,
pub assertions: Vec<Assertion>,
pub modules: Vec<NixosModule>,
pub priority: DefinitionPriority,
pub opaque_imports: Vec<(ModuleRef, Origin)>,
}
#[derive(Clone, Copy, Debug, Default)]
pub enum DefinitionPriority {
#[default]
Normal,
Default,
Force,
Override(u16),
}
impl DefinitionPriority {
pub fn override_priority(self) -> Option<u16> {
match self {
Self::Normal => None,
Self::Default => Some(1000),
Self::Force => Some(50),
Self::Override(priority) => Some(priority),
}
}
}
impl NixosModule {
#[track_caller]
pub fn empty() -> Self {
Self::new(Config::new())
}
pub fn new(config: Config) -> Self {
Self {
config,
options: Config::new(),
generated_imports: vec![],
imports: vec![],
assertions: vec![],
modules: vec![],
priority: DefinitionPriority::Normal,
opaque_imports: vec![],
}
}
#[track_caller]
#[allow(clippy::should_implement_trait)]
pub fn add<T: IntoConfig>(self, value: T) -> Self {
self.module(Self::new(value.into_config()))
}
pub fn module(mut self, module: NixosModule) -> Self {
self.modules.push(module);
self
}
pub fn priority(mut self, priority: DefinitionPriority) -> Self {
self.priority = priority;
self
}
#[track_caller]
pub fn import(mut self, path: impl Into<String>) -> Self {
let path = path.into();
self.imports.push(Import {
origin: Origin::caller(format!("import {path}")),
path,
});
self
}
#[track_caller]
pub fn declare<T: IntoConfig>(mut self, schema: T) -> Self {
let mut child = Self::empty();
child.options = schema.into_config();
self.modules.push(child);
self
}
#[track_caller]
pub fn import_value(mut self, module: NixValue) -> Self {
let origin = Origin::caller("import generated NixOS module");
self.generated_imports.push((module, origin));
self
}
#[track_caller]
pub fn import_ref(mut self, module: ModuleRef) -> Self {
let origin = Origin::caller(format!(
"import opaque module {}",
module.reference().origin.purpose
));
self.opaque_imports.push((module, origin));
self
}
#[track_caller]
pub fn system_packages(mut self, packages: Vec<PackageRef>) -> Self {
self.config = self
.config
.set_dynamic("environment.systemPackages", packages);
self
}
#[track_caller]
pub fn assertion(
mut self,
name: impl Into<String>,
condition: Expr<bool>,
message: impl Into<String>,
) -> Self {
let name = name.into();
let origin = Origin::caller(format!("assertion assertions.{name}"));
self.assertions.push(Assertion {
name,
condition: condition.into_node(origin.clone()),
message: message.into(),
origin,
});
self
}
}
mod compile;
pub(crate) use compile::pin;
pub use compile::{
Boundary, DRIVER, NIXPKGS_REVISION, NixosArtifact, compile_module, compile_module_with_options,
evaluation_source,
};