Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package Manager Integrations #17

Open
6 of 9 tasks
iffse opened this issue Dec 7, 2024 · 17 comments
Open
6 of 9 tasks

Package Manager Integrations #17

iffse opened this issue Dec 7, 2024 · 17 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@iffse
Copy link
Owner

iffse commented Dec 7, 2024

Package manager integration is added along with v0.5.15. However, not all package managers are supported yet.

  • Some package managers allow searching for a package containing an executable. In this case, we can retrieve the package list directly
    • E.g.: pacman -Fq /usr/bin/executable
  • Package managers of some distributions that do not provide such functionality will likely ship with a command_not_found handle by default which suggest packages to install. We can use such output to generate a suggestion

Relevant file.

Progress:

  • Pacman (pkgfile, pacman -Fq)
  • APT / Snap / pkg (apt-file, command-not-found)
  • DNF (dnf provides)
  • Portage (e-file)
  • nix (nix-locate 'bin/{}')
  • YUM (yum provides)
  • Zypper
  • Slackpkg
  • ...
@iffse iffse added enhancement New feature or request help wanted Extra attention is needed labels Dec 7, 2024
@iffse iffse pinned this issue Dec 7, 2024
@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

@SigmaSquadron iirc, nixos comes with a command_not_found hook that we can use. Could you take it a look when you have some spare time?

@SigmaSquadron
Copy link
Contributor

SigmaSquadron commented Dec 7, 2024

The default command_not_found isn't extensible. Programs like nix-locate opt to make their own implementation and replace the default command_not_found implementation.

To query certain files inside Nix Store paths, one could leverage nix-index. (which already does this, although it's extremely slow, since we have over 120k packages.)

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

although it's extremely slow

I think they are doing some fuzzy searching and apparently not a HashMap based index.

Anyway, it's an optional feature we can enable if nix-locate is installed and just skip if not.

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

@SigmaSquadron Does this work?

let result = command_output(shell, &format!("nix-locate /usr/bin/{}", executable));
let packages: Vec<String> = result
.lines()
.map(|line| line.split_whitespace().next().unwrap().trim_end_matches(".out").to_string())
.collect();
if packages.is_empty() {
None
} else {
Some(packages)
}

@SigmaSquadron
Copy link
Contributor

The command syntax should be just nix-locate {}, as /usr/bin won't be a valid path.
Not all binary outputs are named out. Sometimes they're named bin, but really they can just be arbitrarily named. What matters is that binaries will always be inside a folder named bin/ on the root of the output.

@SigmaSquadron
Copy link
Contributor

I should also note that nix-locate takes 1-2s to run on a good day. Expect pay-requests to be slowed down accordingly.

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

I should also note that nix-locate takes 1-2s to run on a good day. Expect pay-requests to be slowed down accordingly.

pay-respects itself retrieves the suggestion in less than 50ms, so the experience should be the same as the command_not_found handle of nix-index itself.

At least I would take more than 2 second to type nix-env -iA package_name!

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

as /usr/bin won't be a valid path.

Strange, official doc show an example with nix-locate 'bin/hello'.

I think the package can get installed like nix-env -iA nixpkgs.executable.out?

let result = command_output(shell, &format!("nix-locate 'bin/{}'", executable));
if result.is_empty() {
return None
}
let packages: Vec<String> = result
.lines()
.map(|line| line.split_whitespace().next().unwrap().to_string())
.collect();
if packages.is_empty() {
None
} else {
Some(packages)
}

"nix-env" => format!("nix-env -iA nixpkgs.{}", package),

@SigmaSquadron
Copy link
Contributor

SigmaSquadron commented Dec 7, 2024

Strange, official doc show an example with nix-locate 'bin/hello'.

bin/hello is valid. /bin/hello or /usr/bin/hello is not.

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

All right, current code should be good then

@SigmaSquadron
Copy link
Contributor

I think the package can get installed like nix-env -iA nixpkgs.executable.out?

You'd want to avoid installing packages, I think. Consider using nix-shell -p <output name> --command <executable name>
Most of the time, <output name> == <executable name>, but this isn't always the case.

@SigmaSquadron
Copy link
Contributor

Consider the following nix-locate output:

coreutils.out   0 s  /nix/store/k48bha2fjqzarg52picsdfwlqx75aqbb-coreutils-9.5/bin/cat

It should suggest the command nix-shell --packages coreutils --command cat

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

You'd want to avoid installing packages

Gets a bit tricky as nix-shell creates a temporary $PATH, and it won't affect host process/shells.

The use case should be that the user is familiar with a command but happens to not have it installed (new machine, for example), so I think we can keep it simple to install directly.

@SigmaSquadron
Copy link
Contributor

SigmaSquadron commented Dec 7, 2024

Then avoid using nix-env, and use the newer nix profile install nixpkgs#hello.

@iffse
Copy link
Owner Author

iffse commented Dec 7, 2024

Should be good now. Thanks!

"nix" => format!("nix profile install nixpkgs#{}", package),

@rexkyng
Copy link

rexkyng commented Jan 2, 2025

Would like to install this with brew on my mac, thanks

@iffse
Copy link
Owner Author

iffse commented Jan 7, 2025

Would like to install this with brew on my mac, thanks

@rexkyng Saw you already created a brew repo for pay-respects. I'll add it to the installation methods, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants