Merge pull request #101 from nix-community/improve-generation-name

tool: improve sd-boot generation display name
This commit is contained in:
nikstur 2023-02-10 17:45:05 +01:00 committed by GitHub
commit a75e2b4c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 38 deletions

View File

@ -80,22 +80,12 @@ impl Generation {
///
/// This is currently implemented by poking around the filesystem to find the necessary data.
/// Ideally, the needed data should be included in the bootspec.
pub fn describe(&self) -> Result<String> {
let toplevel = &self.spec.bootspec.toplevel.0;
let nixos_version = fs::read_to_string(toplevel.join("nixos-version"))
.unwrap_or_else(|_| String::from("Unknown"));
let kernel_version =
read_kernel_version(toplevel).context("Failed to read kernel version.")?;
pub fn describe(&self) -> String {
let build_time = self
.build_time
.map(|x| x.to_string())
.unwrap_or_else(|| String::from("Unknown"));
Ok(format!(
"Generation {} NixOS {}, Linux Kernel {}, Built on {}",
self.version, nixos_version, kernel_version, build_time
))
format!("Generation {}, Built on {}", self.version, build_time)
}
}
@ -105,25 +95,6 @@ impl fmt::Display for Generation {
}
}
/// Read the kernel version from the name of a directory inside the toplevel directory.
///
/// The path looks something like this: $toplevel/kernel-modules/lib/modules/6.1.1
fn read_kernel_version(toplevel: &Path) -> Result<String> {
let path = fs::read_dir(toplevel.join("kernel-modules/lib/modules"))?
.into_iter()
.next()
.transpose()?
.map(|x| x.path())
.with_context(|| format!("Failed to read directory {:?}.", toplevel))?;
let file_name = path
.file_name()
.and_then(|x| x.to_str())
.context("Failed to convert path to filename string.")?;
Ok(String::from(file_name))
}
fn read_build_time(path: &Path) -> Result<Date> {
let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date();
Ok(build_time)

View File

@ -27,12 +27,7 @@ impl OsRelease {
// it is fine to use a dummy value.
map.insert("ID".into(), String::from("lanza"));
map.insert("PRETTY_NAME".into(), generation.spec.bootspec.label.clone());
map.insert(
"VERSION_ID".into(),
generation
.describe()
.context("Failed to describe generation.")?,
);
map.insert("VERSION_ID".into(), generation.describe());
Ok(Self(map))
}

View File

@ -30,7 +30,7 @@ fn generate_expected_os_release() -> Result<()> {
let expected = expect![[r#"
ID=lanza
PRETTY_NAME=LanzaOS
VERSION_ID=Generation 1 NixOS 23.05, Linux Kernel 6.1.1, Built on 1970-01-01
VERSION_ID=Generation 1, Built on 1970-01-01
"#]];
expected.assert_eq(&String::from_utf8(os_release_section)?);