summaryrefslogtreecommitdiffstats
path: root/nixos/modules/programs/command-not-found/command-not-found.nix
blob: 120feacc61b9d72c823bc6d68660cae589f63528 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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,
  ...
}:

let
  cfg = config.programs.command-not-found;
  commandNotFound = pkgs.replaceVarsWith {
    name = "command-not-found";
    dir = "bin";
    src = ./command-not-found.pl;
    isExecutable = true;
    replacements = {
      inherit (cfg) dbPath;
      perl = pkgs.perl.withPackages (p: [
        p.DBDSQLite
        p.StringShellQuote
      ]);
    };
  };

in

{
  options.programs.command-not-found = {

    enable = lib.mkOption {
      type = lib.types.bool;
      default = builtins.pathExists config.programs.command-not-found.dbPath;
      defaultText = lib.literalExpression ''
        builtins.pathExists config.programs.command-not-found.dbPath
      '';
      description = ''
        Whether interactive shells should show which Nix package (if
        any) provides a missing command.

        See also nix-index and nix-index-database as an alternative for flakes-based systems.

        Additionally, having the env var NIX_AUTO_RUN set will automatically run the matching package, and with NIX_AUTO_RUN_INTERACTIVE it will confirm the package before running.
      '';
    };

    dbPath = lib.mkOption {
      type = lib.types.path;
      default = pkgs.path + "/programs.sqlite";
      defaultText = lib.literalExpression ''
        pkgs.path + "/programs.sqlite"
      '';
      description = ''
        Absolute path to `programs.sqlite`, which contains mappings from binary names to package names.

        If a nixpkgs tarball from https://channels.nixos.org is used as the source of nixpkgs, this file will be provided and this option be set by default.

        To use the stateful `programs.sqlite` database, set this option to
        `/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite`.
        If you do so, you can update it with `sudo nix-channels --update`.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    programs.bash.interactiveShellInit = ''
      command_not_found_handle() {
        '${commandNotFound}/bin/command-not-found' "$@"
      }
    '';

    programs.zsh.interactiveShellInit = ''
      command_not_found_handler() {
        '${commandNotFound}/bin/command-not-found' "$@"
      }
    '';

    # NOTE: Fish by itself checks for nixos command-not-found, let's instead makes it explicit.
    programs.fish.interactiveShellInit = ''
      function fish_command_not_found
         "${commandNotFound}/bin/command-not-found" $argv
      end
    '';

    environment.systemPackages = [ commandNotFound ];
  };
}