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-11-25 21:14:21 -05:00
|
|
|
use crate::bootspec::Bootspec;
|
2022-11-26 08:55:15 -05:00
|
|
|
use crate::generation::Generation;
|
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-11-26 08:55:15 -05:00
|
|
|
pub fn new(esp: &str, generation: Generation, bootspec: &Bootspec) -> Result<Self> {
|
2022-11-23 09:26:26 -05:00
|
|
|
let esp = Path::new(esp);
|
|
|
|
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-25 21:14:21 -05:00
|
|
|
Ok(Self {
|
2022-11-23 09:26:26 -05:00
|
|
|
esp: esp.to_owned(),
|
|
|
|
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-25 21:14:21 -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> {
|
|
|
|
let resolved = path.as_ref().read_link().unwrap_or(path.as_ref().into());
|
|
|
|
|
|
|
|
let parent = resolved.parent().ok_or(anyhow::anyhow!(format!(
|
|
|
|
"Path: {} does not have a parent",
|
|
|
|
resolved.display()
|
|
|
|
)))?;
|
|
|
|
|
|
|
|
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-11-26 08:55:15 -05:00
|
|
|
fn generation_path(generation: Generation) -> PathBuf {
|
2022-11-25 21:14:21 -05:00
|
|
|
PathBuf::from(format!("nixos-generation-{}.efi", generation))
|
|
|
|
}
|