Skip to content

Commit

Permalink
Add support for the Nix package manager (#172)
Browse files Browse the repository at this point in the history
Added support for the Nix package manager using Nix' SQLite database.
  • Loading branch information
coolGi69 authored Jun 22, 2024
1 parent 0be8883 commit 3a5bec0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
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"),
}
}
}

0 comments on commit 3a5bec0

Please sign in to comment.