Command-not-found not working

I just recently converted my system to use a flake (nixos configuration and standalone home-manager). I assumed that when running in this new flake setup, I no longer need channels. Is that correct?

This is what I have in my flake.nix:

  description = "NixOS configuration";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-22.11";
    home-manager.url = "github:nix-community/home-manager/release-22.11";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";

But, when I try to run comamnd-not-found, I get this:

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.

This programs.sqlite is published only in channels. I’m not aware of any really nice way for this to work, but on the other hand I find even using a year old DB useful. Right now, maybe it’s easiest to also add a channel (as root) and occasionally sync it.

I personally use nix-index instead and just update it manually from time to time. It has a command-not-found script and gives the benefit of having a handy nix-locate CLI tool for doing more stuff.

1 Like

I use nix-index, with the nix-index-database repository to have automatic updates tracked as a flake input.

I wrote a small home-manager module that I use for this (but you could do it system-wide as well):

1 Like

I found programs.nix-index.enable in home-manager options. I enabled it, but afterwards, I get this when I try to run it:

❯ nix-index
+ querying available packages
error: querying available packages failed
caused by: nix-env failed with error: nix-env failed with exit code 1:
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist, ignoring
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

That’s where the additional logic that I posted above comes in, to put the database in place.

EDIT: also, you don’t usually run the nix-index command itself, as it’s used only to create the database. Once you have the database, nix-index is used internally by command-not-found.