From 6cba6dc61bb0530ad03419364c19b299c6c9c6d4 Mon Sep 17 00:00:00 2001 From: Spencer Whitt Date: Mon, 12 Jan 2015 03:45:48 -0500 Subject: command-not-found: Add ZSH Support --- nixos/modules/module-list.nix | 2 +- nixos/modules/programs/bash/command-not-found.nix | 51 ---------------- nixos/modules/programs/bash/command-not-found.pl | 48 --------------- .../command-not-found/command-not-found.nix | 71 ++++++++++++++++++++++ .../command-not-found/command-not-found.pl | 48 +++++++++++++++ 5 files changed, 120 insertions(+), 100 deletions(-) delete mode 100644 nixos/modules/programs/bash/command-not-found.nix delete mode 100644 nixos/modules/programs/bash/command-not-found.pl create mode 100644 nixos/modules/programs/command-not-found/command-not-found.nix create mode 100644 nixos/modules/programs/command-not-found/command-not-found.pl (limited to 'nixos/modules') diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 844fae536e95..c74ba7557e3d 100755 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -54,8 +54,8 @@ ./misc/version.nix ./programs/atop.nix ./programs/bash/bash.nix - ./programs/bash/command-not-found.nix ./programs/blcr.nix + ./programs/command-not-found/command-not-found.nix ./programs/dconf.nix ./programs/environment.nix ./programs/info.nix diff --git a/nixos/modules/programs/bash/command-not-found.nix b/nixos/modules/programs/bash/command-not-found.nix deleted file mode 100644 index 8c86d48b0808..000000000000 --- a/nixos/modules/programs/bash/command-not-found.nix +++ /dev/null @@ -1,51 +0,0 @@ -# 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"). - -{ config, lib, pkgs, ... }: - -with lib; - -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 - -{ - - programs.bash.interactiveShellInit = - '' - # This function is called whenever a command is not found. - command_not_found_handle() { - 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 - "$@" - else - return 127 - fi - else - echo "$1: command not found" >&2 - return 127 - fi - } - ''; - - environment.systemPackages = [ commandNotFound ]; - - # TODO: tab completion for uninstalled commands! :-) - -} diff --git a/nixos/modules/programs/bash/command-not-found.pl b/nixos/modules/programs/bash/command-not-found.pl deleted file mode 100644 index 916649059d37..000000000000 --- a/nixos/modules/programs/bash/command-not-found.pl +++ /dev/null @@ -1,48 +0,0 @@ -#! @perl@/bin/perl -w @perlFlags@ - -use strict; -use DBI; -use DBD::SQLite; -use Config; - -my $program = $ARGV[0]; - -my $dbPath = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite"; - -my $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "") - or die "cannot open database `$dbPath'"; -$dbh->{RaiseError} = 0; -$dbh->{PrintError} = 0; - -my $system = $ENV{"NIX_SYSTEM"} // $Config{myarchname}; - -my $res = $dbh->selectall_arrayref( - "select package from Programs where system = ? and name = ?", - { Slice => {} }, $system, $program); - -if (!defined $res || scalar @$res == 0) { - print STDERR "$program: command not found\n"; -} elsif (scalar @$res == 1) { - my $package = @$res[0]->{package}; - if ($ENV{"NIX_AUTO_INSTALL"} // "") { - print STDERR <{package}\n" foreach @$res; -} - -exit 127; diff --git a/nixos/modules/programs/command-not-found/command-not-found.nix b/nixos/modules/programs/command-not-found/command-not-found.nix new file mode 100644 index 000000000000..bead2dcdcf90 --- /dev/null +++ b/nixos/modules/programs/command-not-found/command-not-found.nix @@ -0,0 +1,71 @@ +# 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"). + +{ config, lib, pkgs, ... }: + +with lib; + +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 + +{ + + programs.bash.interactiveShellInit = + '' + # This function is called whenever a command is not found. + command_not_found_handle() { + 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 + "$@" + else + return 127 + fi + else + echo "$1: command not found" >&2 + return 127 + fi + } + ''; + + 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 + else + # Indicate than there was an error so ZSH falls back to its default handler + return 127 + fi + } + ''; + + environment.systemPackages = [ commandNotFound ]; + + # TODO: tab completion for uninstalled commands! :-) + +} diff --git a/nixos/modules/programs/command-not-found/command-not-found.pl b/nixos/modules/programs/command-not-found/command-not-found.pl new file mode 100644 index 000000000000..916649059d37 --- /dev/null +++ b/nixos/modules/programs/command-not-found/command-not-found.pl @@ -0,0 +1,48 @@ +#! @perl@/bin/perl -w @perlFlags@ + +use strict; +use DBI; +use DBD::SQLite; +use Config; + +my $program = $ARGV[0]; + +my $dbPath = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite"; + +my $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "") + or die "cannot open database `$dbPath'"; +$dbh->{RaiseError} = 0; +$dbh->{PrintError} = 0; + +my $system = $ENV{"NIX_SYSTEM"} // $Config{myarchname}; + +my $res = $dbh->selectall_arrayref( + "select package from Programs where system = ? and name = ?", + { Slice => {} }, $system, $program); + +if (!defined $res || scalar @$res == 0) { + print STDERR "$program: command not found\n"; +} elsif (scalar @$res == 1) { + my $package = @$res[0]->{package}; + if ($ENV{"NIX_AUTO_INSTALL"} // "") { + print STDERR <{package}\n" foreach @$res; +} + +exit 127; -- cgit 1.4.1