1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.programs.nix-index;
9in
10{
11 options.programs.nix-index = with lib; {
12 enable = mkEnableOption "nix-index, a file database for nixpkgs";
13
14 package = mkPackageOption pkgs "nix-index" { };
15
16 enableBashIntegration = mkEnableOption "Bash integration" // {
17 default = true;
18 };
19
20 enableZshIntegration = mkEnableOption "Zsh integration" // {
21 default = true;
22 };
23
24 enableFishIntegration = mkEnableOption "Fish integration" // {
25 default = true;
26 };
27 };
28
29 config = lib.mkIf cfg.enable {
30 assertions =
31 let
32 checkOpt = name: {
33 assertion = cfg.${name} -> !config.programs.command-not-found.enable;
34 message = ''
35 The 'programs.command-not-found.enable' option is mutually exclusive
36 with the 'programs.nix-index.${name}' option.
37 '';
38 };
39 in
40 [
41 (checkOpt "enableBashIntegration")
42 (checkOpt "enableZshIntegration")
43 ];
44
45 environment.systemPackages = [ cfg.package ];
46
47 programs.bash.interactiveShellInit = lib.mkIf cfg.enableBashIntegration ''
48 source ${cfg.package}/etc/profile.d/command-not-found.sh
49 '';
50
51 programs.zsh.interactiveShellInit = lib.mkIf cfg.enableZshIntegration ''
52 source ${cfg.package}/etc/profile.d/command-not-found.sh
53 '';
54
55 # See https://github.com/bennofs/nix-index/issues/126
56 programs.fish.interactiveShellInit =
57 let
58 wrapper = pkgs.writeScript "command-not-found" ''
59 #!${pkgs.bash}/bin/bash
60 source ${cfg.package}/etc/profile.d/command-not-found.sh
61 command_not_found_handle "$@"
62 '';
63 in
64 lib.mkIf cfg.enableFishIntegration ''
65 function __fish_command_not_found_handler --on-event fish_command_not_found
66 ${wrapper} $argv
67 end
68 '';
69 };
70}