nix-index: add module (#1984)

`nix-index` is a tool to quickly locate the package providing a certain
file in `nixpkgs`. It indexes built derivations found in binary caches.

This module adds the shell integration for its `command-not-found`
script for interactive shells.
This commit is contained in:
Bruno BELANYI 2021-05-10 00:13:10 +02:00 committed by GitHub
parent 7d765d8f46
commit 3b799f6ea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 149 additions and 0 deletions

3
.github/CODEOWNERS vendored
View File

@ -113,6 +113,9 @@
/modules/programs/newsboat.nix @sumnerevans
/tests/modules/programs/newsboat @sumnerevans
/modules/programs/nix-index.nix @ambroisie
/tests/modules/programs/nix-index @ambroisie
/modules/programs/noti.nix @marsam
/modules/programs/nushell.nix @Philipp-M

View File

@ -81,4 +81,10 @@
fingerprint = "8F87 050B 0F9C B841 1515 7399 B784 3F82 3355 E9B9";
}];
};
ambroisie = {
email = "bruno.home-manager@belanyi.fr";
github = "ambroisie";
githubId = 12465195;
name = "Bruno BELANYI";
};
}

View File

@ -1973,6 +1973,13 @@ in
A new module is available: 'services.etesync-dav'
'';
}
{
time = "2021-05-06T11:01:41+00:00";
message = ''
A new module is available: 'programs.nix-index'.
'';
}
];
};
}

View File

@ -103,6 +103,7 @@ let
(loadModule ./programs/neomutt.nix { })
(loadModule ./programs/neovim.nix { })
(loadModule ./programs/newsboat.nix { })
(loadModule ./programs/nix-index.nix { })
(loadModule ./programs/noti.nix { })
(loadModule ./programs/notmuch.nix { })
(loadModule ./programs/nushell.nix { })

View File

@ -0,0 +1,63 @@
{ config, lib, pkgs, ... }:
let cfg = config.programs.nix-index;
in {
meta.maintainers = with lib.hm.maintainers; [ ambroisie ];
options.programs.nix-index = with lib; {
enable = mkEnableOption "nix-index, a file database for nixpkgs";
package = mkOption {
type = types.package;
default = pkgs.nix-index;
defaultText = literalExample "pkgs.nix-index";
description = "Package providing the <command>nix-index</command> tool.";
};
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") ];
home.packages = [ cfg.package ];
programs.bash.initExtra = lib.mkIf cfg.enableBashIntegration ''
source ${cfg.package}/etc/profile.d/command-not-found.sh
'';
programs.zsh.initExtra = 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.shellInit = 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
'';
};
}

View File

@ -65,6 +65,7 @@ import nmt {
./modules/programs/ne
./modules/programs/neomutt
./modules/programs/newsboat
./modules/programs/nix-index
./modules/programs/nushell
./modules/programs/pet
./modules/programs/powerline-go

View File

@ -0,0 +1,26 @@
{ pkgs, ... }: {
config = {
programs.bash.enable = true;
programs.fish.enable = true;
programs.zsh.enable = true;
programs.command-not-found.enable = true;
nixpkgs.overlays =
[ (self: super: { zsh = pkgs.writeScriptBin "dummy-zsh" ""; }) ];
programs.nix-index.enable = true;
# 'command-not-found' does not have a 'fish' integration
test.asserts.assertions.expected = [
''
The 'programs.command-not-found.enable' option is mutually exclusive
with the 'programs.nix-index.enableBashIntegration' option.
''
''
The 'programs.command-not-found.enable' option is mutually exclusive
with the 'programs.nix-index.enableZshIntegration' option.
''
];
};
}

View File

@ -0,0 +1,4 @@
{
nix-index-integrations = ./integrations.nix;
nix-index-assert-on-command-not-found = ./assert-on-command-not-found.nix;
}

View File

@ -0,0 +1,38 @@
{ pkgs, ... }:
let
fishRegex = ''
function __fish_command_not_found_handler --on-event fish_command_not_found
/nix/store/.*command-not-found $argv
end
'';
in {
config = {
programs.bash.enable = true;
programs.fish.enable = true;
programs.zsh.enable = true;
nixpkgs.overlays =
[ (self: super: { zsh = pkgs.writeScriptBin "dummy-zsh" ""; }) ];
programs.nix-index.enable = true;
nmt.script = ''
# Bash integration
assertFileExists home-files/.bashrc
assertFileRegex \
home-files/.bashrc \
'source /nix/store/.*nix-index.*/etc/profile.d/command-not-found.sh'
# Zsh integration
assertFileExists home-files/.zshrc
assertFileRegex \
home-files/.zshrc \
'source /nix/store/.*nix-index.*/etc/profile.d/command-not-found.sh'
# Fish integration
assertFileExists home-files/.config/fish/config.fish
assertFileRegex \
home-files/.config/fish/config.fish '${fishRegex}'
'';
};
}