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 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-01-30 13:42:30 +00:00
|
|
|
|
2014-04-14 14:26:48 +00: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 ")
|
|
|
|
[ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite ]);
|
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2013-09-18 03:18:34 +00: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.
|
|
|
|
$p "$1"
|
|
|
|
# Retry the command if we just installed it.
|
|
|
|
if [ $? = 126 ]; then
|
|
|
|
"$@"
|
2013-05-15 10:47:17 +00:00
|
|
|
else
|
|
|
|
return 127
|
2013-01-30 13:42:30 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "$1: command not found" >&2
|
2013-05-15 10:47:17 +00: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.
|
|
|
|
$p "$1"
|
|
|
|
|
|
|
|
# Retry the command if we just installed it.
|
|
|
|
if [ $? = 126 ]; then
|
|
|
|
"$@"
|
|
|
|
fi
|
2015-09-02 17:48:52 +00: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 17:48:52 +00: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! :-)
|
|
|
|
|
|
|
|
}
|