2013-01-30 13:42:30 +00:00
|
|
|
# This module provides suggestions of packages to install if the user
|
|
|
|
# tries to run a missing command in Bash. This is implemented using a
|
|
|
|
# SQLite database that maps program names to Nix package names (e.g.,
|
|
|
|
# "pdflatex" is mapped to "tetex").
|
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-01-30 13:42:30 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2013-01-30 13:42:30 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
commandNotFound = pkgs.substituteAll {
|
|
|
|
name = "command-not-found";
|
|
|
|
dir = "bin";
|
|
|
|
src = ./command-not-found.pl;
|
|
|
|
isExecutable = true;
|
|
|
|
inherit (pkgs) perl;
|
|
|
|
perlFlags = concatStrings (map (path: "-I ${path}/lib/perl5/site_perl ")
|
2015-12-27 15:36:17 +00:00
|
|
|
[ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite pkgs.perlPackages.StringShellQuote ]);
|
2013-01-30 13:42:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
programs.bash.interactiveShellInit =
|
2013-01-30 13:42:30 +00:00
|
|
|
''
|
|
|
|
# This function is called whenever a command is not found.
|
|
|
|
command_not_found_handle() {
|
|
|
|
local p=/run/current-system/sw/bin/command-not-found
|
2013-03-25 13:14:55 +00:00
|
|
|
if [ -x $p -a -f /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite ]; then
|
2013-01-30 13:42:30 +00:00
|
|
|
# Run the helper program.
|
2015-12-28 13:49:53 +00:00
|
|
|
$p "$@"
|
2013-01-30 13:42:30 +00:00
|
|
|
# Retry the command if we just installed it.
|
|
|
|
if [ $? = 126 ]; then
|
|
|
|
"$@"
|
2013-05-15 11:47:17 +01:00
|
|
|
else
|
|
|
|
return 127
|
2013-01-30 13:42:30 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "$1: command not found" >&2
|
2013-05-15 11:47:17 +01:00
|
|
|
return 127
|
2013-01-30 13:42:30 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2015-01-12 08:45:48 +00:00
|
|
|
programs.zsh.interactiveShellInit =
|
|
|
|
''
|
|
|
|
# This function is called whenever a command is not found.
|
|
|
|
command_not_found_handler() {
|
|
|
|
local p=/run/current-system/sw/bin/command-not-found
|
|
|
|
if [ -x $p -a -f /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite ]; then
|
|
|
|
# Run the helper program.
|
2015-12-28 13:49:53 +00:00
|
|
|
$p "$@"
|
2015-01-12 08:45:48 +00:00
|
|
|
|
|
|
|
# Retry the command if we just installed it.
|
|
|
|
if [ $? = 126 ]; then
|
|
|
|
"$@"
|
|
|
|
fi
|
2015-09-02 18:48:52 +01:00
|
|
|
else
|
2015-01-12 08:45:48 +00:00
|
|
|
# Indicate than there was an error so ZSH falls back to its default handler
|
2015-09-02 18:48:52 +01:00
|
|
|
return 127
|
2015-01-12 08:45:48 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2013-01-30 13:42:30 +00:00
|
|
|
environment.systemPackages = [ commandNotFound ];
|
|
|
|
|
|
|
|
# TODO: tab completion for uninstalled commands! :-)
|
|
|
|
|
|
|
|
}
|