nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at flake-libs 101 lines 2.6 kB view raw
1# This module provides suggestions of packages to install if the user 2# tries to run a missing command in Bash. This is implemented using a 3# SQLite database that maps program names to Nix package names (e.g., 4# "pdflatex" is mapped to "tetex"). 5 6{ 7 config, 8 lib, 9 pkgs, 10 ... 11}: 12 13let 14 cfg = config.programs.command-not-found; 15 commandNotFound = pkgs.replaceVarsWith { 16 name = "command-not-found"; 17 dir = "bin"; 18 src = ./command-not-found.pl; 19 isExecutable = true; 20 replacements = { 21 inherit (cfg) dbPath; 22 perl = pkgs.perl.withPackages (p: [ 23 p.DBDSQLite 24 p.StringShellQuote 25 ]); 26 }; 27 }; 28 29in 30 31{ 32 options.programs.command-not-found = { 33 34 enable = lib.mkOption { 35 type = lib.types.bool; 36 default = true; 37 description = '' 38 Whether interactive shells should show which Nix package (if 39 any) provides a missing command. 40 ''; 41 }; 42 43 dbPath = lib.mkOption { 44 default = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite"; 45 description = '' 46 Absolute path to programs.sqlite. 47 48 By default this file will be provided by your channel 49 (nixexprs.tar.xz). 50 ''; 51 type = lib.types.path; 52 }; 53 }; 54 55 config = lib.mkIf cfg.enable { 56 programs.bash.interactiveShellInit = '' 57 # This function is called whenever a command is not found. 58 command_not_found_handle() { 59 local p='${commandNotFound}/bin/command-not-found' 60 if [ -x "$p" ] && [ -f '${cfg.dbPath}' ]; then 61 # Run the helper program. 62 "$p" "$@" 63 # Retry the command if we just installed it. 64 if [ $? = 126 ]; then 65 "$@" 66 else 67 return 127 68 fi 69 else 70 echo "$1: command not found" >&2 71 return 127 72 fi 73 } 74 ''; 75 76 programs.zsh.interactiveShellInit = '' 77 # This function is called whenever a command is not found. 78 command_not_found_handler() { 79 local p='${commandNotFound}/bin/command-not-found' 80 if [ -x "$p" ] && [ -f '${cfg.dbPath}' ]; then 81 # Run the helper program. 82 "$p" "$@" 83 84 # Retry the command if we just installed it. 85 if [ $? = 126 ]; then 86 "$@" 87 else 88 return 127 89 fi 90 else 91 # Indicate than there was an error so ZSH falls back to its default handler 92 echo "$1: command not found" >&2 93 return 127 94 fi 95 } 96 ''; 97 98 environment.systemPackages = [ commandNotFound ]; 99 }; 100 101}