command-not-found: add options

add option to disable command-not-found as well as option to define dbPath.
Disabling this may remove the perl dependency for bash/zsh prompts

makefu 5a5db609 f0fac3b5

+56 -39
+55 -38
nixos/modules/programs/command-not-found/command-not-found.nix
··· 8 8 with lib; 9 9 10 10 let 11 - 11 + cfg = config.programs.command-not-found; 12 12 commandNotFound = pkgs.substituteAll { 13 13 name = "command-not-found"; 14 14 dir = "bin"; 15 15 src = ./command-not-found.pl; 16 16 isExecutable = true; 17 17 inherit (pkgs) perl; 18 + inherit (cfg) dbPath; 18 19 perlFlags = concatStrings (map (path: "-I ${path}/lib/perl5/site_perl ") 19 20 [ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite pkgs.perlPackages.StringShellQuote ]); 20 21 }; ··· 22 23 in 23 24 24 25 { 26 + options.programs.command-not-found = { 27 + 28 + enable = mkEnableOption "command-not-found hook for interactive shell"; 25 29 26 - programs.bash.interactiveShellInit = 27 - '' 28 - # This function is called whenever a command is not found. 29 - command_not_found_handle() { 30 - local p=/run/current-system/sw/bin/command-not-found 31 - if [ -x $p -a -f /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite ]; then 32 - # Run the helper program. 33 - $p "$@" 34 - # Retry the command if we just installed it. 35 - if [ $? = 126 ]; then 36 - "$@" 30 + dbPath = mkOption { 31 + default = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite" ; 32 + description = '' 33 + Absolute path to programs.sqlite. 34 + 35 + By default this file will be provided by your channel 36 + (nixexprs.tar.xz). 37 + ''; 38 + type = types.path; 39 + }; 40 + }; 41 + 42 + config = mkIf cfg.enable { 43 + programs.bash.interactiveShellInit = 44 + '' 45 + # This function is called whenever a command is not found. 46 + command_not_found_handle() { 47 + local p=${commandNotFound} 48 + if [ -x $p -a -f ${cfg.dbPath} ]; then 49 + # Run the helper program. 50 + $p "$@" 51 + # Retry the command if we just installed it. 52 + if [ $? = 126 ]; then 53 + "$@" 54 + else 55 + return 127 56 + fi 37 57 else 58 + echo "$1: command not found" >&2 38 59 return 127 39 60 fi 40 - else 41 - echo "$1: command not found" >&2 42 - return 127 43 - fi 44 - } 45 - ''; 61 + } 62 + ''; 46 63 47 - programs.zsh.interactiveShellInit = 48 - '' 49 - # This function is called whenever a command is not found. 50 - command_not_found_handler() { 51 - local p=/run/current-system/sw/bin/command-not-found 52 - if [ -x $p -a -f /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite ]; then 53 - # Run the helper program. 54 - $p "$@" 64 + programs.zsh.interactiveShellInit = 65 + '' 66 + # This function is called whenever a command is not found. 67 + command_not_found_handler() { 68 + local p=${commandNotFound} 69 + if [ -x $p -a -f ${cfg.dbPath} ]; then 70 + # Run the helper program. 71 + $p "$@" 55 72 56 - # Retry the command if we just installed it. 57 - if [ $? = 126 ]; then 58 - "$@" 73 + # Retry the command if we just installed it. 74 + if [ $? = 126 ]; then 75 + "$@" 76 + fi 77 + else 78 + # Indicate than there was an error so ZSH falls back to its default handler 79 + echo "$1: command not found" >&2 80 + return 127 59 81 fi 60 - else 61 - # Indicate than there was an error so ZSH falls back to its default handler 62 - return 127 63 - fi 64 - } 65 - ''; 82 + } 83 + ''; 66 84 67 - environment.systemPackages = [ commandNotFound ]; 68 - 69 - # TODO: tab completion for uninstalled commands! :-) 85 + environment.systemPackages = [ commandNotFound ]; 86 + }; 70 87 71 88 }
+1 -1
nixos/modules/programs/command-not-found/command-not-found.pl
··· 8 8 9 9 my $program = $ARGV[0]; 10 10 11 - my $dbPath = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite"; 11 + my $dbPath = "@dbPath@"; 12 12 13 13 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "") 14 14 or die "cannot open database `$dbPath'";