1 # This module provides suggestions of packages to install if the user
2 # tries to run a missing command in Bash. This is implemented using a
3 # SQLite database that maps program names to Nix package names (e.g.,
4 # "pdflatex" is mapped to "tetex").
6 { config, lib, pkgs, ... }:
9 cfg = config.programs.command-not-found;
10 commandNotFound = pkgs.substituteAll {
11 name = "command-not-found";
13 src = ./command-not-found.pl;
16 perl = pkgs.perl.withPackages (p: [ p.DBDSQLite p.StringShellQuote ]);
22 options.programs.command-not-found = {
24 enable = lib.mkOption {
25 type = lib.types.bool;
28 Whether interactive shells should show which Nix package (if
29 any) provides a missing command.
33 dbPath = lib.mkOption {
34 default = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite" ;
36 Absolute path to programs.sqlite.
38 By default this file will be provided by your channel
41 type = lib.types.path;
45 config = lib.mkIf cfg.enable {
46 programs.bash.interactiveShellInit =
48 # This function is called whenever a command is not found.
49 command_not_found_handle() {
50 local p='${commandNotFound}/bin/command-not-found'
51 if [ -x "$p" ] && [ -f '${cfg.dbPath}' ]; then
52 # Run the helper program.
54 # Retry the command if we just installed it.
61 echo "$1: command not found" >&2
67 programs.zsh.interactiveShellInit =
69 # This function is called whenever a command is not found.
70 command_not_found_handler() {
71 local p='${commandNotFound}/bin/command-not-found'
72 if [ -x "$p" ] && [ -f '${cfg.dbPath}' ]; then
73 # Run the helper program.
76 # Retry the command if we just installed it.
83 # Indicate than there was an error so ZSH falls back to its default handler
84 echo "$1: command not found" >&2
90 environment.systemPackages = [ commandNotFound ];