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