Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the Nix package manager #172

Merged
merged 12 commits into from
Jun 22, 2024
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ windows = { version = "0.39.0", features = [
if-addrs = "0.10.2"

[target.'cfg(any(target_os="freebsd", target_os = "linux"))'.dependencies]
sqlite = "0.31.1"
sqlite = "0.36.0"

[target.'cfg(any(target_os="freebsd", target_os = "netbsd"))'.dependencies]
x11rb = "0.12.0"
Expand Down
37 changes: 37 additions & 0 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,10 @@ impl PackageReadout for LinuxPackageReadout {
packages.push((PackageManager::Homebrew, c));
}

if let Some(c) = LinuxPackageReadout::count_nix() {
packages.push((PackageManager::Nix, c));
}

packages
}
}
Expand Down Expand Up @@ -934,4 +938,37 @@ impl LinuxPackageReadout {

None
}

/// Returns the number of installed packages for systems
/// that utilize `nix` as their package manager.
fn count_nix() -> Option<usize> {
return 'sqlite: {
let db = "/nix/var/nix/db/db.sqlite";
if !Path::new(db).is_file() {
break 'sqlite None;
}

let connection = sqlite::Connection::open_with_flags(
// The nix store is immutable, so we need to inform sqlite about it
"file:".to_owned() + db + "?immutable=1",
sqlite::OpenFlags::new().with_read_only().with_uri(),
);

if let Ok(con) = connection {
let statement =
con.prepare("SELECT COUNT(path) FROM ValidPaths WHERE sigs IS NOT NULL");

if let Ok(mut s) = statement {
if s.next().is_ok() {
break 'sqlite match s.read::<Option<i64>, _>(0) {
Ok(Some(count)) => Some(count as usize),
_ => None,
};
}
}
}

None
};
}
}
2 changes: 2 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ pub enum PackageManager {
Android,
Pkg,
Scoop,
Nix,
}

impl std::fmt::Display for PackageManager {
Expand All @@ -677,6 +678,7 @@ impl std::fmt::Display for PackageManager {
PackageManager::Android => write!(f, "Android"),
PackageManager::Pkg => write!(f, "pkg"),
PackageManager::Scoop => write!(f, "Scoop"),
PackageManager::Nix => write!(f, "nix"),
}
}
}