summaryrefslogtreecommitdiffstats
path: root/nixos/modules/programs/nix-index.nix
blob: 21e9eb9e176567c7d5ee9f55a1e4ca033346b88a (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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.programs.nix-index;
in
{
  options.programs.nix-index = with lib; {
    enable = mkEnableOption "nix-index, a file database for nixpkgs";

    package = mkPackageOption pkgs "nix-index" { };

    enableBashIntegration = mkEnableOption "Bash integration" // {
      default = true;
    };

    enableZshIntegration = mkEnableOption "Zsh integration" // {
      default = true;
    };

    enableFishIntegration = mkEnableOption "Fish integration" // {
      default = true;
    };
  };

  config = lib.mkIf cfg.enable {
    assertions =
      let
        checkOpt = name: {
          assertion = cfg.${name} -> !config.programs.command-not-found.enable;
          message = ''
            The 'programs.command-not-found.enable' option is mutually exclusive
            with the 'programs.nix-index.${name}' option.
          '';
        };
      in
      [
        (checkOpt "enableBashIntegration")
        (checkOpt "enableZshIntegration")
      ];

    environment.systemPackages = [ cfg.package ];

    programs.bash.interactiveShellInit = lib.mkIf cfg.enableBashIntegration ''
      source ${cfg.package}/etc/profile.d/command-not-found.sh
    '';

    programs.zsh.interactiveShellInit = lib.mkIf cfg.enableZshIntegration ''
      source ${cfg.package}/etc/profile.d/command-not-found.sh
    '';

    # See https://github.com/bennofs/nix-index/issues/126
    programs.fish.interactiveShellInit =
      let
        wrapper = pkgs.writeScript "command-not-found" ''
          #!${pkgs.bash}/bin/bash
          source ${cfg.package}/etc/profile.d/command-not-found.sh
          command_not_found_handle "$@"
        '';
      in
      lib.mkIf cfg.enableFishIntegration ''
        function __fish_command_not_found_handler --on-event fish_command_not_found
            ${wrapper} $argv
        end
      '';
  };
}