don't require existing override

This commit is contained in:
Qyriad 2026-01-30 19:39:58 +01:00
parent 7bce1e7a6e
commit 80ff0b36cb
5 changed files with 120 additions and 191 deletions

View file

@ -1,4 +1,4 @@
use std::sync::{Arc, LazyLock};
use std::sync::Arc;
pub(crate) mod prelude {
#![allow(unused_imports)]
@ -25,6 +25,8 @@ pub(crate) mod prelude {
pub use fs_err::File;
#[cfg(unix)]
pub use fs_err::os::unix::fs::{FileExt, OpenOptionsExt};
pub use tap::{Pipe, Tap};
}
use prelude::*;
@ -48,20 +50,21 @@ pub const ASCII_WHITESPACE: &[char] = &['\t', '\n', '\x0C', '\r', ' '];
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
pub struct DefinitionWithLocation {
pub file: Box<Path>,
pub value: Box<str>,
pub value: Box<serde_json::Value>,
}
pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result<Box<Path>, BoxDynError> {
let expr: OsString = [
// foo
pub fn expr_for_configuration(source_file: &Path) -> OsString {
[
OsStr::new("import <nixpkgs/nixos> { configuration = "),
configuration_nix.as_os_str(),
source_file.as_os_str(),
OsStr::new("; }"),
]
.into_iter()
.map(ToOwned::to_owned)
.collect();
.collect()
}
pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result<Box<Path>, BoxDynError> {
let expr = expr_for_configuration(configuration_nix);
let attrpath = format!("options.{}.definitionsWithLocations", option_name);
let output = nixcmd::NixEvalExpr { expr, attrpath }
@ -75,19 +78,10 @@ pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result<Box<Path
Ok(Box::from(last_location.file))
}
pub fn get_highest_prio(
option_name: &str,
source: SourceFile,
) -> Result<(i64, SourceLine), BoxDynError> {
pub fn get_highest_prio(option_name: &str, source: SourceFile) -> Result<i64, BoxDynError> {
// Get the current highest priority.
let expr: OsString = [
OsStr::new("import <nixpkgs/nixos> { configuration = "),
source.path().as_os_str(),
OsStr::new("; }"),
]
.into_iter()
.collect();
let expr = expr_for_configuration(&source.path());
// Get the highest priority, and the file its defined in.
let attrpath = format!("options.{}.highestPrio", option_name);
@ -97,93 +91,44 @@ pub fn get_highest_prio(
let stdout = output.stdout();
let highest_prio = i64::from_str(stdout.trim())?;
let needle = format!("lib.mkOverride ({})", highest_prio);
let path = source.path();
let lines = source.lines()?;
let line = lines
.iter()
// We're more likely to find it at the end, so let's start there.
.rev()
.find(|&line| line.text.contains(&needle))
.unwrap_or_else(|| {
panic!(
"couldn't find override number {highest_prio} in {}",
path.display(),
)
});
Ok((highest_prio, line.clone()))
Ok(highest_prio)
}
pub fn get_next_prio_line(
source: SourceFile,
option_name: Arc<str>,
last_line_def: SourceLine,
new_prio: i64,
new_value: Arc<str>,
) -> Result<SourceLine, BoxDynError> {
if !last_line_def.text.ends_with(';') {
todo!();
}
let next_line = source.line(last_line_def.line.next())?;
if next_line.text.trim() != "}" {
todo!();
}
let (indentation, _rest) = last_line_def.text.split_at(
last_line_def
.text
.find(|ch: char| !ch.is_ascii_whitespace())
.unwrap_or_default(),
);
// FIXME: fix indentation
let new_text = format!("{indentation}{option_name} = lib.mkOverride ({new_prio}) ({new_value});",);
let source_lines = source.lines()?;
let last_line = source_lines.last();
assert_eq!(last_line.map(SourceLine::text).as_deref(), Some("]"));
let last_line = last_line.unwrap();
let new_line = SourceLine {
line: next_line.line.next(),
line: last_line.line,
path: source.path(),
text: Arc::from(new_text),
text: Arc::from(format!(
" {option_name} = lib.mkOverride ({new_prio}) ({new_value});",
)),
};
Ok(new_line)
}
pub fn write_next_prio(
mut source: SourceFile,
last_line_def: SourceLine,
new_text: Arc<str>,
) -> Result<(), BoxDynError> {
//let lines = source.lines()?;
let open_brace_line = source.line(last_line_def.line.prev())?.text();
let close_brace_line = source.line(last_line_def.line.next())?.text();
pub fn write_next_prio(mut source: SourceFile, new_line: SourceLine) -> Result<(), BoxDynError> {
let new_mod_start = SourceLine {
line: last_line_def.line.next(),
line: new_line.line.prev(),
path: source.path(),
text: open_brace_line,
};
let new_line = SourceLine {
line: new_mod_start.line.next(),
path: source.path(),
text: Arc::from(new_text),
text: Arc::from(" {"),
};
let new_mod_end = SourceLine {
line: new_line.line.next(),
path: source.path(),
text: close_brace_line,
text: Arc::from(" }"),
};
dbg!(&new_mod_start.text());
source.insert_lines(&[
new_mod_start,
new_line,
new_mod_end,
])?;
//source.insert_line(new_line.line, new_line.text())?;
source.insert_lines(&[new_mod_start, new_line, new_mod_end])?;
Ok(())
}