DEV Community

Andrea Giacobino
Andrea Giacobino

Posted on

Get the upstream distro name in Rust

The other day we were chatting with a colleague of mine about retrieving the OS name of the distribution. I don't recall exactly the details, but it was something about always getting the actual distribution name and not the upstream one, and he could not sort that out (I have my suspicions that he didn't try very hard).

Fast forward 2 weeks, I'm sipping coffee and installing updates on my laptop and suddenly this appears on the screen:

Alt Text

Oh, that's interesting 🤔, let's google "rust distribution id_like"? LMGTFY

BOOM! first result: https://docs.rs/os-release/0.1.0/os_release/struct.OsRelease.html

Looks good, let's give it a try:

> cargo new guess_os
> cd guess_os 
> cargo add os-release
> vim src/main.go
Enter fullscreen mode Exit fullscreen mode

very well now let's add the code

extern crate os_release;

use os_release::OsRelease;
use std::io;

pub fn main() -> io::Result<()> {
    let release = OsRelease::new()?;
    println!("You say '{}', I say '{}'", release.name, release.id_like);
    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

And now the moment of truth 🥁

asciicast

🏆

Top comments (0)