This commit is contained in:
Qyriad 2026-01-27 11:29:10 +01:00
parent d8ac4e157d
commit bcd11513ef
12 changed files with 1042 additions and 5 deletions

157
src/lib.rs Normal file
View file

@ -0,0 +1,157 @@
pub(crate) mod prelude {
#![allow(unused_imports)]
pub use std::{
error::Error as StdError,
ffi::{OsStr, OsString},
fmt::{Display, Formatter, Result as FmtResult},
io::{Error as IoError, Read, Seek, SeekFrom},
path::{Path, PathBuf},
process::{Command, ExitCode},
str::FromStr,
};
#[cfg(unix)]
pub use std::os::{
fd::AsRawFd,
unix::ffi::{OsStrExt, OsStringExt},
};
pub type BoxDynError = Box<dyn StdError + Send + Sync + 'static>;
pub use command_error::{CommandExt, OutputLike};
pub use fs_err::File;
#[cfg(unix)]
pub use fs_err::os::unix::fs::{FileExt, OpenOptionsExt};
}
use prelude::*;
use std::{
io::{BufRead, BufReader},
sync::Arc,
};
pub mod args;
pub use args::Parser;
mod color;
pub use color::{CLI_ENABLE_COLOR, SHOULD_COLOR};
pub mod line;
pub use line::Line;
pub mod source;
pub use source::SourceLine;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
pub struct DefinitionWithLocation {
pub file: Box<Path>,
pub value: Box<str>,
}
pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result<Box<Path>, BoxDynError> {
let expr: OsString = [
// foo
OsStr::new("import <nixpkgs/nixos> { configuration = "),
configuration_nix.as_os_str(),
OsStr::new("; }"),
]
.into_iter()
.map(ToOwned::to_owned)
.collect();
let result = Command::new("nix-instantiate")
.arg("--eval")
.arg("--json")
.arg("--strict")
.arg("--expr")
.arg(expr)
.arg("-A")
.arg(format!("options.{}.definitionsWithLocations", option_name))
.output_checked_utf8()?;
let stdout = result.stdout();
let definitions: Box<[DefinitionWithLocation]> = serde_json::from_str(&stdout)?;
let last_location = definitions.last().unwrap();
let file = &*last_location.file;
dbg!(&file);
Ok(Box::from(file))
}
pub fn get_highest_prio(
option_name: &str,
file_with_definition: &Path,
) -> Result<SourceLine, BoxDynError> {
let mut file = File::options()
.read(true)
.write(true)
.create(false)
.custom_flags(libc::O_CLOEXEC)
.open(file_with_definition)?;
// TODO: seek and read backwards.
let mut lines = BufReader::new(&mut file)
.lines()
.enumerate()
.map(|(index, line_res)| {
line_res.map(|line| SourceLine {
line: Line::from_index(index as u64),
path: Arc::from(file_with_definition),
text: Arc::from(line),
})
})
.collect::<Result<Vec<SourceLine>, IoError>>()?;
lines.reverse();
let lines_reversed = lines;
// Get the current highest priority.
let expr: OsString = [
OsStr::new("import <nixpkgs/nixos> { configuration = "),
file_with_definition.as_os_str(),
OsStr::new("; }"),
]
.into_iter()
.map(ToOwned::to_owned)
.collect();
// Get the highest priority, and the file its defined in.
let result = Command::new("nix-instantiate")
.arg("--eval")
.arg("--json")
.arg("--strict")
.arg("--expr")
.arg(expr)
.arg("-A")
.arg(format!("options.{}.highestPrio", option_name))
.output_checked_utf8()?;
let stdout = result.stdout();
let highest_prio = stdout.trim();
let needle = format!("lib.mkOverride ({})", highest_prio);
eprintln!("looking for {needle:?}");
let line_with_current_highest = lines_reversed
.iter()
.position(|line| {
eprintln!("looking for {needle} in {line}");
line.text.contains(&needle)
})
.unwrap_or_else(|| {
panic!(
"couldn't find override number {highest_prio} in {}",
file_with_definition.display()
)
});
let line = lines_reversed
.into_iter()
.nth(line_with_current_highest)
.unwrap();
eprintln!("found, on line index {}", line);
Ok(line)
}