2022-11-25 21:14:21 -05:00
|
|
|
use anyhow::{Context, Result};
|
2022-11-23 09:26:26 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2022-12-17 17:55:38 -05:00
|
|
|
use crate::generation::OSGeneration;
|
2022-11-25 21:14:21 -05:00
|
|
|
|
2022-11-23 09:26:26 -05:00
|
|
|
pub struct EspPaths {
|
|
|
|
pub esp: PathBuf,
|
|
|
|
pub nixos: PathBuf,
|
|
|
|
pub kernel: PathBuf,
|
|
|
|
pub initrd: PathBuf,
|
2022-11-23 11:26:56 -05:00
|
|
|
pub linux: PathBuf,
|
|
|
|
pub lanzaboote_image: PathBuf,
|
2022-11-24 05:10:19 -05:00
|
|
|
pub efi_fallback_dir: PathBuf,
|
|
|
|
pub efi_fallback: PathBuf,
|
|
|
|
pub systemd: PathBuf,
|
|
|
|
pub systemd_boot: PathBuf,
|
2022-11-23 09:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EspPaths {
|
2022-12-17 17:55:38 -05:00
|
|
|
pub fn new(esp: impl AsRef<Path>, generation: &OSGeneration) -> Result<Self> {
|
2022-11-26 16:23:00 -05:00
|
|
|
let esp = esp.as_ref();
|
2022-11-23 09:26:26 -05:00
|
|
|
let esp_nixos = esp.join("EFI/nixos");
|
2022-11-23 11:26:56 -05:00
|
|
|
let esp_linux = esp.join("EFI/Linux");
|
2022-11-24 05:10:19 -05:00
|
|
|
let esp_systemd = esp.join("EFI/systemd");
|
|
|
|
let esp_efi_fallback_dir = esp.join("EFI/BOOT");
|
2022-11-23 09:26:26 -05:00
|
|
|
|
2022-11-26 16:23:00 -05:00
|
|
|
let bootspec = &generation.bootspec;
|
|
|
|
|
2022-11-25 21:14:21 -05:00
|
|
|
Ok(Self {
|
2022-11-26 16:23:00 -05:00
|
|
|
esp: esp.to_path_buf(),
|
2022-11-23 09:26:26 -05:00
|
|
|
nixos: esp_nixos.clone(),
|
2022-11-25 21:14:21 -05:00
|
|
|
kernel: esp_nixos.join(nixos_path(&bootspec.kernel, "bzImage")?),
|
|
|
|
initrd: esp_nixos.join(nixos_path(&bootspec.initrd, "initrd")?),
|
2022-11-23 11:26:56 -05:00
|
|
|
linux: esp_linux.clone(),
|
2022-11-26 17:19:08 -05:00
|
|
|
lanzaboote_image: esp_linux.join(generation_path(generation)),
|
2022-11-24 05:10:19 -05:00
|
|
|
efi_fallback_dir: esp_efi_fallback_dir.clone(),
|
|
|
|
efi_fallback: esp_efi_fallback_dir.join("BOOTX64.EFI"),
|
|
|
|
systemd: esp_systemd.clone(),
|
|
|
|
systemd_boot: esp_systemd.join("systemd-bootx64.efi"),
|
2022-11-25 21:14:21 -05:00
|
|
|
})
|
2022-11-23 09:26:26 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 21:14:21 -05:00
|
|
|
|
|
|
|
fn nixos_path(path: impl AsRef<Path>, name: &str) -> Result<PathBuf> {
|
2022-11-26 17:19:08 -05:00
|
|
|
let resolved = path
|
|
|
|
.as_ref()
|
|
|
|
.read_link()
|
|
|
|
.unwrap_or_else(|_| path.as_ref().into());
|
2022-11-25 21:14:21 -05:00
|
|
|
|
2022-11-26 17:19:08 -05:00
|
|
|
let parent = resolved.parent().ok_or_else(|| {
|
|
|
|
anyhow::anyhow!(format!(
|
|
|
|
"Path: {} does not have a parent",
|
|
|
|
resolved.display()
|
|
|
|
))
|
|
|
|
})?;
|
2022-11-25 21:14:21 -05:00
|
|
|
|
|
|
|
let without_store = parent.strip_prefix("/nix/store").with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Failed to strip /nix/store from path {}",
|
|
|
|
path.as_ref().display()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let nixos_filename = format!("{}-{}.efi", without_store.display(), name);
|
|
|
|
|
|
|
|
Ok(PathBuf::from(nixos_filename))
|
|
|
|
}
|
|
|
|
|
2022-12-17 17:55:38 -05:00
|
|
|
fn generation_path(generation: &OSGeneration) -> PathBuf {
|
2022-11-27 05:19:02 -05:00
|
|
|
if let Some(specialisation_name) = generation.is_specialized() {
|
|
|
|
PathBuf::from(format!(
|
|
|
|
"nixos-generation-{}-specialisation-{}.efi",
|
|
|
|
generation, specialisation_name
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
PathBuf::from(format!("nixos-generation-{}.efi", generation))
|
|
|
|
}
|
2022-11-25 21:14:21 -05:00
|
|
|
}
|