`command-not-found` unable to open database

I am on NixOS and command-not-found feature is no longer working.

I don’t have any configuration for it in configuration.nix, it should enable by default.

What should I do to fix it?

$ command-not-found
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.

I’ve had that issue as well. What you need to make sure is that root’s channels include a channel named nixos. You can point it to a nixos-unstable.

If all of your channels are deleted (use nix-channel --delete and nix-channel --list to view them), you can add the unstable channel and name it unstable with:

nix-channel --add https://nixos.org/channels/nixos-unstable nixos

Too bad this doesn’t work, I already have nixos-unstable as nixos (for root).

Any idea?

[root@earth:~]# nix-channel --list
nixos https://nixos.org/channels/nixpkgs-unstable

[root@earth:~]# command-not-found 
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.

[wizzup@ ~]$ nix-channel --list
[wizzup@ ~]$ command-not-found 
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.

The programs.sqlite is only generated for the nixos- prefixed channels.

As root, run:

nix-channel --add https://nixos.org/channels/nixos-unstable nixos
nix-channel --update

This works! Do I need to do this command sequences every time after nixos-rebuild switch?

[root@earth:~]# nix-channel --add https://nixos.org/channels/nixos-unstable nixos

[root@earth:~]# nix-channel --list
nixos https://nixos.org/channels/nixos-unstable

[root@earth:~]# nix-channel --update
unpacking channels...

[root@earth:~]# command-not-found hello
The program ‘hello’ is currently not installed. It is provided by
several packages. You can install it by typing one of the following:
  nix-env -iA nixos.hello
  nix-env -iA nixos.mbedtls
  nix-env -iA nixos.perkeep

It should be fine now.

The issue was that the system was configured with nixpgs- unstable. Small but important difference :slight_smile:

The nixos- channels are produced by https://hydra.nixos.org/project/nixos
The nixpkgs- channels are produced by https://hydra.nixos.org/project/nixpkgs

Both have different release scripts and criteria for advancement.

I’m seeing this in 21.11, and none of the above suggestions appear to work for me.

My root user appears to have the correct channel:

$ id
uid=0(root) gid=0(root) groups=0(root)
$ nix-channel --list
nixos https://nixos.org/channels/nixos-21.11

But command-not-found still can’t find the database:

$ command-not-found
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 error message seems reasonable, since the programs.sqlite database really doesn’t exist:

$ ls -lah /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite
ls: cannot access '/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite': No such file or directory
$ ls -lah /nix/var/nix/profiles/per-user/root/channels/nixos/
total 88K
dr-xr-xr-x  8 root root 4.0K Jan  1  1970 .
dr-xr-xr-x  3 root root 4.0K Jan  1  1970 ..
-r--r--r--  1 root root 4.9K Jan  1  1970 CONTRIBUTING.md
-r--r--r--  1 root root 1.1K Jan  1  1970 COPYING
-r--r--r--  1 root root  971 Jan  1  1970 default.nix
dr-xr-xr-x 11 root root 4.0K Jan  1  1970 doc
-r--r--r--  1 root root 2.3K Jan  1  1970 .editorconfig
-r--r--r--  1 root root 3.3K Jan  1  1970 flake.nix
-r--r--r--  1 root root  598 Jan  1  1970 .gitattributes
dr-xr-xr-x  4 root root 4.0K Jan  1  1970 .github
-r--r--r--  1 root root  344 Jan  1  1970 .gitignore
-r--r--r--  1 root root   40 Jan  1  1970 .git-revision
dr-xr-xr-x  4 root root 4.0K Jan  1  1970 lib
dr-xr-xr-x  3 root root 4.0K Jan  1  1970 maintainers
dr-xr-xr-x  7 root root 4.0K Jan  1  1970 nixos
lrwxrwxrwx  1 root root    1 Jan  1  1970 nixpkgs -> .
dr-xr-xr-x 18 root root 4.0K Jan  1  1970 pkgs
-r--r--r--  1 root root 5.8K Jan  1  1970 README.md
-r--r--r--  1 root root   20 Jan  1  1970 svn-revision
-r--r--r--  1 root root    5 Jan  1  1970 .version
-r--r--r--  1 root root   19 Jan  1  1970 .version-suffix

Any idea what could be going on here?

edit: I should add my nixos commit:

$ cat /nix/var/nix/profiles/per-user/root/channels/nixos/.git-revision 
1bd4bbd49bef217a3d1adea43498270d6e779d65

Huh, apparently running sudo nix-channel --update got me the programs.sqlite database.

$ sudo nix-channel --update
$ cat /nix/var/nix/profiles/per-user/root/channels/nixos/.git-revision 
573095944e7c1d58d30fc679c81af63668b54056
$ ls -l /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite 
-r--r--r-- 1 root root 10076160 Jan  1  1970 /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite

So now the question is, is running nix-channel --update at least once required? Or are there just certain channel commits that don’t have a programs.sqlite generated?

In case you are using Nix Flakes, then you can use nix-index for the command-not-found functionality. If you use home-manager, add this to your home.nix:

programs.nix-index =
{
  enable = true;
  enable<SHELL>Integration = true;
};

I presume one still have to play around NIX_PATH: Make use of Nix flake registry instead of channel · Issue #167 · nix-community/nix-index · GitHub

Yeah you’re right, I use autoGenFromInputs module from flake-utils-plus to auto-generate NIX_PATH from Flake inputs, so everything works fine

This is the one that worked for me. The accepted solution doesn’t work.