NixOS/nixos-channel-scripts

Bring back command-not-found support

domenkozar opened this issue ยท 13 comments

It was disabled for the second time in 7f2bbc8

It took a few hours extra on full rebuild to update the channel due to this feature.

Hydra now publishes .ls.xz for each narfile, so we just need to change the index generation to unpack the file, parse it and index it.

We need to change https://github.com/NixOS/nixos-channel-scripts/blob/master/generate-programs-index.cc to download files like https://cache.nixos.org/sb3r5grph8lyv9zvam2my3vwwag64fi7.ls.xz

Did this already hit the nixos-unstable channel?

I'd like to be able to get this working. Right now command-not-found still gives me

DBI connect('dbname=/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite','',...) failed: unable to open database file at /run/current-system/sw/bin/command-not-found line 13.
cannot open database `/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite' at /run/current-system/sw/bin/command-not-found line 13.

Where can I get a copy of the sqlite file, or how can I get nixos-rebuild to generate one?

@canndrew: it's in the channels, as you might see from the path. sudo nix-channel --update should get it, if you have channels set up correctly.

@vcunat

nix-channel --update should get it

Nope, the file still aint there ๐Ÿ˜•

I guess I'll need to look into how channels work. Right now I'm installing from a local clone of the nixpkgs repo though.

You need to use channels, as that includes the sqlite file. If you're using plain clone, there won't be any database.

@domenkozar Thanks, is there a way to either (a) download a copy of the database and stick it there (my clone is similar enough to the unstable channel) or (b) build the database however you guys are doing it when I rebuild from my clone?

You can just update the channels and never build from them, using git checkouts instead. That's what I've been doing for years.

@vcunat Thanks. I have it working now.

In case anyone with the same problem finds this thread, the (hacky) way I fixed this was:

  • Download and extract https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz
  • Copy programs.sqlite from that file to somewhere writable (such as $HOME/)
  • Go into the local nixpkgs repo and edit nixos/modules/programs/command-not-found/command-not-found.pl to make it look for programs.sqlite in the right place.
  • nixos-rebuild switch to update command-not-found with that change.
  • Finally (for zsh users) add this to .zshrc
    command_not_found_handler() {
        command-not-found "$1"
    }
    

And presto! My shell works properly again.

@vcunat Can you explain what you meant by that? Thanks.

@adrianparvino: you can just build from git checkout of nixpkgs, as the binary cache is shared for all use cases together. Still, the database for command-not-found is only in channels, so you can do sudo nix-channel --update etc. to get that database, even if you don't use the channel for anything else. I don't think a manual approach like in the March post above is necessary, but it's certainly another way.

mpcsh commented

Sorry to necrobump, but after a hiatus from NixOS I'm sad to return and find this still broken. Is there anything that needs to be done or is this feature simply not supported anymore (without manual hacks that is)?

unode commented

I'm one of the non-nix-channel users since I have custom patches on top of the channel.

This incantation both updates the local git branch and fetches programs.sqlite.
(broken to several lines for readability)

cd /var/nixpkgs-channels/nixos &&
echo "Updating channel $(git rev-parse --abbrev-ref HEAD)" &&
umask 022 &&
git pull --rebase --depth 1000 &&
git prune &&
git gc &&
echo "Channel taking $(du -hs|cut -f1)" &&
echo "Fetching command-not-found database" &&
wget https://nixos.org/channels/$(git rev-parse --abbrev-ref HEAD)/nixexprs.tar.xz -O - | tar xJf - --wildcards "nixos*/programs.sqlite" -O > programs.sqlite &&
cd - >/dev/null &&
echo "All done"

the wget | tar line takes care of fetching programs.sqlite.

then simply make the following into a symlink:

# ls -l /nix/var/nix/profiles/per-user/root/channels/nixos 
lrwxrwxrwx 1 root root 27 May 19 00:57 /nix/var/nix/profiles/per-user/root/channels/nixos -> /var/nixpkgs-channels/nixos

and you are good to go.

I'd like to be able to get this working. Right now command-not-found still gives me

DBI connect('dbname=/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite','',...) failed: unable to open database file at /run/current-system/sw/bin/command-not-found line 13.
cannot open database `/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite' at /run/current-system/sw/bin/command-not-found line 13.

Where can I get a copy of the sqlite file, or how can I get nixos-rebuild to generate one?

I ran into the same thing on nixos-19.09.

nix-channel --update did not fix it sudo nix-channel --update did though.
It's easy to forget the sudo, since the command will successfully complete either way.