aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/command-not-found/command-not-found.nix
blob: 0053fe36ad7ef9fac15c712aaa02eaaecb24d0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Adapted from Nixpkgs.

{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.programs.command-not-found;
  commandNotFound = pkgs.substituteAll {
    name = "command-not-found";
    dir = "bin";
    src = ./command-not-found.pl;
    isExecutable = true;
    inherit (pkgs) perl;
    inherit (cfg) dbPath;
    perlFlags = concatStrings (map (path: "-I ${path}/lib/perl5/site_perl ")
      [ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite pkgs.perlPackages.StringShellQuote ]);
  };

  shInit = commandNotFoundHandlerName: ''
    # This function is called whenever a command is not found.
    ${commandNotFoundHandlerName}() {
      local p=${commandNotFound}/bin/command-not-found
      if [ -x $p -a -f ${cfg.dbPath} ]; then
        # Run the helper program.
        $p "$@"
      else
        echo "$1: command not found" >&2
        return 127
      fi
    }
  '';

in

{
  options.programs.command-not-found = {
    enable = mkEnableOption "command-not-found hook for interactive shell";

    dbPath = mkOption {
      default = "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite" ;
      description = ''
        Absolute path to <filename>programs.sqlite</filename>. By
        default this file will be provided by your channel
        (nixexprs.tar.xz).
      '';
      type = types.path;
    };
  };

  config = mkIf cfg.enable {
    programs.bash.initExtra = shInit "command_not_found_handle";
    programs.zsh.initExtra = shInit "command_not_found_handler";

    home.packages = [ commandNotFound ];
  };
}