# \`command-not-found\` unable to open database

**URL:** https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807
**Category:** Help
**Created:** [August 23, 2019, 10:02am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807 "2019-08-23T10:02:45Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![wizzup](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wizzup/32/1178_2.png) [@wizzup](https://discourse.nixos.org/u/wizzup)
#### Post date: [August 23, 2019, 10:02am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/1 "2019-08-23T10:02:45Z")

</div>

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](https://nixos.org/nixos/options.html#command-not-found).

What should I do to fix it?

```auto
$ 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.

```

---

<div class="post-metadata">

### Author: ![doronbehar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/doronbehar/32/1387_2.png) [@doronbehar](https://discourse.nixos.org/u/doronbehar)
#### Post date: [August 23, 2019, 3:10pm UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/2 "2019-08-23T15:10:07Z")

</div>

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](https://nixos.org/channels/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:

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

```

---

<div class="post-metadata">

### Author: ![wizzup](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wizzup/32/1178_2.png) [@wizzup](https://discourse.nixos.org/u/wizzup)
#### Post date: [August 26, 2019, 6:22am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/3 "2019-08-26T06:22:33Z")

</div>

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

Any idea?

```auto
[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.

```

---

<div class="post-metadata">

### Author: ![zimbatm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/zimbatm/32/4_2.png) [@zimbatm](https://discourse.nixos.org/u/zimbatm)
#### Post date: [August 26, 2019, 9:12am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/4 "2019-08-26T09:12:09Z")

</div>

> [@wizzup](#):
>
> [root@earth:~]# nix-channel --list  
> nixos [nixpkgs-unstable release nixpkgs-25.05pre709559.5083ec887760](https://nixos.org/channels/nixpkgs-unstable)

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

As root, run:

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

```

---

<div class="post-metadata">

### Author: ![wizzup](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wizzup/32/1178_2.png) [@wizzup](https://discourse.nixos.org/u/wizzup)
#### Post date: [August 26, 2019, 9:19am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/5 "2019-08-26T09:19:07Z")

</div>

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

```auto
[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

```

---

<div class="post-metadata">

### Author: ![zimbatm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/zimbatm/32/4_2.png) [@zimbatm](https://discourse.nixos.org/u/zimbatm)
#### Post date: [August 26, 2019, 9:22am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/6 "2019-08-26T09:22:57Z")

</div>

It should be fine now.

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

The `nixos-` channels are produced by [https://hydra.nixos.org/project/nixos](https://hydra.nixos.org/project/nixos)  
The `nixpkgs-` channels are produced by [https://hydra.nixos.org/project/nixpkgs](https://hydra.nixos.org/project/nixpkgs)

Both have different release scripts and criteria for advancement.

---

<div class="post-metadata">

### Author: ![cdepillabout](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cdepillabout/32/410_2.png) [@cdepillabout](https://discourse.nixos.org/u/cdepillabout)
#### Post date: [December 14, 2021, 4:24am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/7 "2021-12-14T04:24:00Z")

</div>

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:

```plaintext
$ 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:

```plaintext
$ 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:

```plaintext
$ 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:

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

```

---

<div class="post-metadata">

### Author: ![cdepillabout](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cdepillabout/32/410_2.png) [@cdepillabout](https://discourse.nixos.org/u/cdepillabout)
#### Post date: [December 14, 2021, 4:32am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/8 "2021-12-14T04:32:36Z")

</div>

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

```plaintext
$ 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?

---

<div class="post-metadata">

### Author: ![maydayv7](https://discourse.nixos.org/user_avatar/discourse.nixos.org/maydayv7/32/5263_2.png) [@maydayv7](https://discourse.nixos.org/u/maydayv7)
#### Post date: [December 18, 2021, 8:21am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/9 "2021-12-18T08:21:29Z")

</div>

In case you are using Nix Flakes, then you can use [nix-index](https://github.com/bennofs/nix-index) for the `command-not-found` functionality. If you use `home-manager`, add this to your `home.nix`:

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

```

---

<div class="post-metadata">

### Author: ![ikovnatsky](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/i/bbe5ce/32.png) [@ikovnatsky](https://discourse.nixos.org/u/ikovnatsky)
#### Post date: [January 5, 2022, 7:54am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/10 "2022-01-05T07:54:52Z")

</div>

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](https://github.com/bennofs/nix-index/issues/167#issuecomment-967764056)

---

<div class="post-metadata">

### Author: ![maydayv7](https://discourse.nixos.org/user_avatar/discourse.nixos.org/maydayv7/32/5263_2.png) [@maydayv7](https://discourse.nixos.org/u/maydayv7)
#### Post date: [January 5, 2022, 1:14pm UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/11 "2022-01-05T13:14:09Z")

</div>

> [@ikovnatsky](#):
>
> I presume one still have to play around NIX\_PATH: [Make use of](https://github.com/bennofs/nix-index/issues/167#issuecomment-967764056)

Yeah you’re right, I use [`autoGenFromInputs`](https://github.com/gytis-ivaskevicius/flake-utils-plus/blob/b4848f55c7017dadc2ff4f82296a3ca0b533e1fa/lib/options.nix) module from [`flake-utils-plus`](https://github.com/gytis-ivaskevicius/flake-utils-plus) to auto-generate `NIX_PATH` from Flake `inputs`, so everything works fine

---

<div class="post-metadata">

### Author: ![mattfbacon](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mattfbacon/32/5277_2.png) [@mattfbacon](https://discourse.nixos.org/u/mattfbacon)
#### Post date: [March 13, 2022, 4:33am UTC](https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/12 "2022-03-13T04:33:42Z")

</div>

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