Flakes: nix run with multiple binaries


Previously, if you wanted to run lspci without having to install it or use nix-shell -p pciutils you could do nix run nixpkgs.pciutils -c lspci. What would be the equivalent with nixFlakes? The -c option has been removed and I can’t find an example where the package has more than one binary.

1 Like

nix shell "nixpkgs#pciutils" -c lspci

The above should work for any binary contained in the derivations bin path. You can also explicitly set multiple binaries using the apps ouput in a flake.nix:

  outputs = { ... }: {
  # omitted packages
  apps."<system>".someApp = {
    type = "app";
    program = "${self.packages."<system>".somePackage}/bin/someApp";

In that case it would be:
nix run "someFlake#someApp"

It would make sense for nix run -c to exist though.

nix run normally runs the defaultApplication, but -c would be a nice addition. The current behaviour seems to be to try to run a program with the same name as the string after # (btw, what’s the name for that?)

This is not very intuitive:

❯ nix run nixpkgs#pciutils
error: unable to execute '/nix/store/vari5b15sssvx21vjmylwy0bk8fdbjww-pciutils-3.7.0/bin/pciutils': No such file or directory

Thank you @nrdxp that’s what I was looking for.

@asymmetric that’s what I’m seeing, the removal of -c is a little puzzling as it’s implied it has to be the same name, yet for packages like geekbench calling nix run nixpkgs#geekbench it works even if the binary name is geekbench5

That what the apps output is for, to specify a path, other than the package name, for the binary.

Yeah, that is confusing. From nix run --help:

Note that vim (as of the time of writing of this page) is not an app but a package. Thus, Nix runs the eponymous file from the vim package.

If installable evaluates to a derivation, it will try to execute the program <out>/bin/<name>, where out is the primary output store path of the derivation and name is the meta.mainProgram attribute of the derivation if it exists, and otherwise the name part of the value of the name attribute of the derivation (e.g. if name is set to hello-1.10, it will run $out/bin/hello).

I wonder if for geekbench5 nix extracts the name as geekbench.

I noticed that a few dozen packages in nixpkgs have the meta.mainProgram attribute set (e.g., rg), but the vast majority doesn’t - I guess this is only necessary when the mainProgram differs from the package name.

1 Like