Flake setups are not reliably detected
emilazy opened this issue · comments
I have an all-flakes nix-darwin/home-manager setup but the command-not-found.sh
script gives me nix-shell
output. It seems like I have no user profile to look for a manifest.json
in:
emily@yuyuko ~> ls -l ~/.nix-profile
lrwxr-xr-x 1 emily staff 44 20 May 10:49 /Users/emily/.nix-profile@ -> /nix/var/nix/profiles/per-user/emily/profile
emily@yuyuko ~> ls -l /nix/var/nix/profiles/per-user/emily/profile
ls: /nix/var/nix/profiles/per-user/emily/profile: No such file or directory
I'm not sure what a reliable way to detect a flakes setup is, though.
Would it make more sense if command-not-found.sh looks at the enabled experimental features instead?
Maybe; I can see people using e.g. https://github.com/DeterminateSystems/nix-installer with a conventional non-flakes setup and getting unexpected results because of the defaulted-on experimental feature. Not sure if that's worth worrying about though. Is there an easy way to check the experimental features without trying to parse nix.conf
?
One possibility would be to check if "nix registry list" is successful?
we can run nix show-config experimental-features
and check if it contains flakes
but that will break if flakes is no longer an experimental feature, right?
yeah that's true, though I'm a bit worried that nix registry list
and other flakes commands would try to download/refresh the flake registry and slow down command-not-found
true, so let's check for nix show-config experimental-features and later add a version check if nix ever makes flakes support non-experimental?
sounds good to me, and we can just do a nix --version
check once that happens
For what it's worth it seems like nix show-config experimental-features
and nix --version
are both noticeably kind of slow for me, maybe because they're unconditionally poking the daemon or something:
emily@yuyuko ~> hyperfine --warmup 3 'nix --version' 'nix show-config experimental-features' 'rg experimental-features /etc/nix/nix.conf'
Benchmark 1: nix --version
Time (mean ± σ): 81.0 ms ± 4.0 ms [User: 67.0 ms, System: 12.0 ms]
Range (min … max): 75.6 ms … 90.3 ms 34 runs
Benchmark 2: nix show-config experimental-features
Time (mean ± σ): 78.2 ms ± 4.0 ms [User: 64.9 ms, System: 11.3 ms]
Range (min … max): 74.2 ms … 91.3 ms 37 runs
Benchmark 3: rg experimental-features /etc/nix/nix.conf
Time (mean ± σ): 7.5 ms ± 0.8 ms [User: 4.1 ms, System: 2.2 ms]
Range (min … max): 5.2 ms … 9.3 ms 255 runs
Summary
rg experimental-features /etc/nix/nix.conf ran
10.38 ± 1.18 times faster than nix show-config experimental-features
10.76 ± 1.21 times faster than nix --version
(Though I find command-not-found already a bit too slow for my tastes, so up to you whether that is reason to avoid making it worse or a reason it's not that big of a deal.)
Just opened #227 to rewrite command-not-found.sh in rust, hoping to improve a few things including this one
the new flakes detection logic parses the nix config file and looks for flakes
in experimental-features
and should be able to solve the issue
https://github.com/figsoda/nix-index/blob/0a9ed9273a6283ff79e93cef87b6bca3b1c344e1/src/bin/nix-locate.rs#L163