diff --git a/Cargo.toml b/Cargo.toml index 4a2f806..b089b4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 01ba29e..ca48b92 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -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 } } @@ -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 { + 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::, _>(0) { + Ok(Some(count)) => Some(count as usize), + _ => None, + }; + } + } + } + + None + }; + } } diff --git a/src/traits.rs b/src/traits.rs index 8d193fa..f7eff47 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -655,6 +655,7 @@ pub enum PackageManager { Android, Pkg, Scoop, + Nix, } impl std::fmt::Display for PackageManager { @@ -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"), } } }