List of binary crates on crates.io

I'm developing cargo-nix, which builds Rust binaries using Nix.

I've completed implementing the minimal functionality and tried it with several crates.
I found out that some crates, like ripgrep and bat, can be built with cargo-nix, while others cannot.

I want to test cargo-nix on popular crates.
How can I get the list of binary crates and the number of downloads from crates.io?

Thanks!

The page on data access for crates.io describes some possibilities.

The first one coming to my mind is downloading the database dump and then import and query t locally.

(I didn't check if you find the differentiation between library and binary crates)

1 Like

I seriously doubt you actually can get that kind of information. Because I don't think anyone defined what a binary crate actually is. For example:

  • You can have a crate that has src/main.rs. This is likely a binary crate.
  • But you can also have multiple binaries in there.
  • You can have a crate that has both src/lib.rs and src/main.rs. It can serve only as a library onto itself, it can be a hybrid that can be used as a library or binary or the binary is just some useless devel thing.
  • Arguably, you could want to use something from the examples directory of that crate, because these are in a way binaries.

So you might develop some kind of your own heuristic on what crate you consider to be a binary crate and what not. But the list of files is probably not available in the metadata, you'd need to download the sources first to check.

1 Like

I'd assume that the criterion is, in fact, rather simple: binary crate is the one which can be installed with cargo install. The question is, how does Cargo check whether it is possible or not.

1 Like

Yeah. It would be enough if I can pull tens to hundreds of crates with a default binary target (I guess most of them are src/main.rs?).

FWIW, I'll normally write my executable as src/bin/main.rs so you don't open src/ and get confused by the presence of both a lib.rs and a main.rs.

A better method would be to use cargo as a library. Then you can use implementors of the Source trait to fetch packages (probably with their RegistrySource) and inspect each of the Targets in the Package to see if it includes binaries (probably with Target::is_bin()).

That would probably involve downloading the source code for each package, though. Source distributions from crates.io aren't normally that big, but I imagine it'll add up if you are trying to check every package in the registry.

1 Like

This category is close:

If you want to check programmatically, you need to support the "autobins" logic of Cargo. I've implemented it here.

4 Likes

It has to download the crate, and inspect the manifest and sources:

$ cargo install -v uluru
    Updating crates.io index
  Downloaded uluru v1.0.0
  Downloaded 1 crate (9.5 KB) in 0.81s
error: specified package `uluru v1.0.0` has no binaries
2 Likes

I'm going to try Lib.rs' list. Thank you all!