Macchina-CLI / libmacchina

A library providing access to all sorts of system information.
https://crates.io/crates/libmacchina
MIT License
68 stars 20 forks source link

Add nix package count #150

Closed Gobidev closed 5 months ago

Gobidev commented 1 year ago

closes #120

This PR adds nix package count. It works by counting the amount of directories in /nix/store/ and filtering out the ones that don't have a version number in their package name. This value differs from running nix-store -q --requisites /run/current-system/sw | wc -l and nix-store -q --requisites ~/.nix-profile | wc -l and adding the values (method used by other fetch utilities) because the directory also contains packages that are not counted by nix-store -q. However, spawning the commands is not an option as they both take around .3s to execute which is way longer than all other readouts combined take to run. This is the best alternative I could find.

To get an idea of how much they differ, here are some numbers:

System Method Amount
non-NixOS nix-store -q ... 14
libmacchina 56
NixOS nix-store -q ... 936
libmacchina 1235

I also simplified the count_snap function by replacing the if let ... return with map()

grtcdr commented 1 year ago

As long as the numbers are accurate, this looks like a neat solution. I kinda sorta wish more package managers kept a little SQLite database like rpm does, it makes things simple.

Gobidev commented 1 year ago

Yea it would be nice to have something like that, I looked through nix source code for a while and tried to figure out how their method works but it seemed to complicated to replicate.

grtcdr commented 1 year ago

This doesn't look like it's discounting duplicates, which means that, for example, two bash entries in /nix/store are viewed as two distinct packages. I presume this is what makes the nix implementation complex. In any case, we should try to match their counting mechanism in order to report correct information.

Gobidev commented 1 year ago

Sounds good, I will try to replicate the count method of nix-store when I have more time. Btw, can you make a release in the near future? It would be nice to have easy access to the features of the past few months.

grtcdr commented 1 year ago

Sure thing, I'll get to that tomorrow.

Gobidev commented 5 months ago

See #172