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

nix search with -f works poorly with "installable" semantics #5642

Open
samueldr opened this issue Nov 24, 2021 · 7 comments
Open

nix search with -f works poorly with "installable" semantics #5642

samueldr opened this issue Nov 24, 2021 · 7 comments
Labels
bug UX The way in which users interact with Nix. Higher level than UI.

Comments

@samueldr
Copy link
Member

samueldr commented Nov 24, 2021

Describe the bug

According to the help text, a search query has to be given after an installable.

.../channels/nixpkgs $ nix --extra-experimental-features nix-command search --help | head
    Warning: This program is experimental and its interface is subject to change.

Name

    nix search - search for packages

Synopsis

    nix search [option...] installable regex...

With -f use, which is the non-flakes escape hatch, nix search cannot actually be used to search all the packages.

Steps To Reproduce

~ $ nix --extra-experimental-features nix-command search -f '<nixpkgs>' 'firefox'
*  (94.0.1)

~ $ nix --extra-experimental-features nix-command search -f '<nixpkgs>' 'fire'
error: attribute 'fire' in selection path 'fire' not found

(Ignore the lack of attribute names in results...)

Expected behavior

Compare with (purposefully redundant) use with Nix 2.3

 $ nix-shell -I nixpkgs=channel:nixos-21.05 -p nix

[nix-shell:.../channels/nixpkgs]$ nix search -f '<nixpkgs>' fire | head
warning: using cached results; pass '-u' to update the cache
* arrayfire (arrayfire)
  A general-purpose library for parallel and massively-parallel computations

* blackfire (blackfire-agent)
  Blackfire Profiler agent and client

* bonfire (bonfire-unstable)
  CLI Graylog Client with Follow Mode

* chrome-token-signing (chrome-token-signing-1.1.5)

[nix-shell:~]$ 

nix-env --version output

~ $ nix-env --version
nix-env (Nix) 2.4

~ $ readlink -f $(which nix)
/nix/store/ag01yxbp4z0bksh7qc67xc31rgk91yaa-nix-2.4/bin/nix
@samueldr samueldr added the bug label Nov 24, 2021
@thufschmitt
Copy link
Member

So here’s my suspicion as to what is happening (here and in #5641):

  • nix search -f '<nixpkgs>' firefox doesn’t search for firefox in nixpkgs but for the empty string (matching everything) in (import <nixpkgs> {}).firefox. So you only get one result. And because what nix search prints isn’t the package name but its attribute path (relative to the root of the search), it will print the empty attribute path.
  • Likewise, nix search -f '<nixpkgs>' fire will search in (import <nixpkgs> {}).fire wihch doesn’t exist, hence the error
  • The “correct” syntax should be nix search -f '<nixpkgs>' . firefox, but it doesn’t work because it expects import <nixpkgs> to be an attribute set (there’s some logic to magically call things if they’re a function, but it probably isn’t applied to the last element of the attribute path)
  • nix search -f <nixpkgs> pkgs firefox nearly works, but fails at the first evaluation error (which by design is the very first package that Nix encounters). That’s because nix search has a logic to ignore eval errors, but it’s only enabled if the first element of the attribute path is legacyPackages (which is why nixpkgs is exposed under legacyPackages and not packages in its flake) − https://github.com/NixOS/nix/blob/master/src/nix/search.cc#L164-L165
  • Regardless of that, recurseForDerivations is only taken into account if the first element of the attribute path is legacyPackages and the depth is bigger than 2 − https://github.com/NixOS/nix/blob/master/src/nix/search.cc#L157-L161 . So the search will recurse into a lot of stuff that it shouldn’t have to.

At the end of the day, search can be made to work with:

$ cat <<EOF > default.nix
{ legacyPackages.pkgs = import <nixpkgs> {}; }
EOF
$ nix search -f . . firefox-beta-bin
* legacyPackages.pkgs.firefox-beta-bin (94.0b2)
  Mozilla Firefox, free web browser (binary package)

* legacyPackages.pkgs.firefox-beta-bin-unwrapped (94.0b2)
  Mozilla Firefox, free web browser (binary package)

(which is a lot of hops to go through ;) )

@thufschmitt thufschmitt added the UX The way in which users interact with Nix. Higher level than UI. label Nov 24, 2021
@nbraud
Copy link
Contributor

nbraud commented Nov 24, 2021

With -f use, which is the non-flakes escape hatch, nix search cannot actually be used to search all the packages.

Why is there a “non-flakes escape hatch”? flakes are an optional, experimental feature, and presumably shouldn't be involved without explicit user opt-in.

@thufschmitt
Copy link
Member

Why is there a “non-flakes escape hatch”? flakes are an optional, experimental feature, and presumably shouldn't be involved without explicit user opt-in.

So is the new cli (including nix search). And part of the reason for that is indeed that it’s strongly tied to flakes and the non-flake use-case is a second-class citizen there

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-2-4-and-what-s-next/16257/1

@garbas garbas moved this to To discuss in Nix UX Dec 16, 2021
@garbas garbas added this to Nix UX Dec 16, 2021
@thufschmitt thufschmitt moved this from To discuss to Backlog in Nix UX Jun 30, 2022
@stale stale bot added the stale label Aug 12, 2022
@thufschmitt
Copy link
Member

Relevant: #7668

@stale stale bot removed the stale label Mar 3, 2023
@Ericson2314
Copy link
Member

CC @infinisil

@tomberek
Copy link
Contributor

tomberek commented Nov 21, 2023

Fixed by: #7668 (edit: duplicate with above, sorry)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug UX The way in which users interact with Nix. Higher level than UI.
Projects
Status: Backlog
Development

No branches or pull requests

7 participants
@samueldr @tomberek @Ericson2314 @nbraud @thufschmitt @nixos-discourse and others