···8with lib;
910let
11-12 commandNotFound = pkgs.substituteAll {
13 name = "command-not-found";
14 dir = "bin";
15 src = ./command-not-found.pl;
16 isExecutable = true;
17 inherit (pkgs) perl;
018 perlFlags = concatStrings (map (path: "-I ${path}/lib/perl5/site_perl ")
19 [ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite pkgs.perlPackages.StringShellQuote ]);
20 };
···22in
2324{
0002526- 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- "$@"
000000000000000037 else
038 return 127
39 fi
40- else
41- echo "$1: command not found" >&2
42- return 127
43- fi
44- }
45- '';
4647- 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 "$@"
5556- # Retry the command if we just installed it.
57- if [ $? = 126 ]; then
58- "$@"
0000059 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- '';
6667- environment.systemPackages = [ commandNotFound ];
68-69- # TODO: tab completion for uninstalled commands! :-)
7071}
···8with lib;
910let
11+ cfg = config.programs.command-not-found;
12 commandNotFound = pkgs.substituteAll {
13 name = "command-not-found";
14 dir = "bin";
15 src = ./command-not-found.pl;
16 isExecutable = true;
17 inherit (pkgs) perl;
18+ inherit (cfg) dbPath;
19 perlFlags = concatStrings (map (path: "-I ${path}/lib/perl5/site_perl ")
20 [ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite pkgs.perlPackages.StringShellQuote ]);
21 };
···23in
2425{
26+ options.programs.command-not-found = {
27+28+ enable = mkEnableOption "command-not-found hook for interactive shell";
2930+ 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
57 else
58+ echo "$1: command not found" >&2
59 return 127
60 fi
61+ }
62+ '';
00006364+ 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 "$@"
7273+ # 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
81 fi
82+ }
83+ '';
00008485+ environment.systemPackages = [ commandNotFound ];
86+ };
08788}