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