From 3b799f6ea42f850316ae4741ff323dc74ce09467 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 10 May 2021 00:13:10 +0200 Subject: [PATCH] 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. --- .github/CODEOWNERS | 3 + modules/lib/maintainers.nix | 6 ++ modules/misc/news.nix | 7 +++ modules/modules.nix | 1 + modules/programs/nix-index.nix | 63 +++++++++++++++++++ tests/default.nix | 1 + .../nix-index/assert-on-command-not-found.nix | 26 ++++++++ tests/modules/programs/nix-index/default.nix | 4 ++ .../programs/nix-index/integrations.nix | 38 +++++++++++ 9 files changed, 149 insertions(+) create mode 100644 modules/programs/nix-index.nix create mode 100644 tests/modules/programs/nix-index/assert-on-command-not-found.nix create mode 100644 tests/modules/programs/nix-index/default.nix create mode 100644 tests/modules/programs/nix-index/integrations.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 003ac4ea..0a8cb5e5 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -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 diff --git a/modules/lib/maintainers.nix b/modules/lib/maintainers.nix index 87815604..e41645b1 100644 --- a/modules/lib/maintainers.nix +++ b/modules/lib/maintainers.nix @@ -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"; + }; } diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 4c367524..6234dc7b 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -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'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index e6e178a3..6a6f4f08 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -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 { }) diff --git a/modules/programs/nix-index.nix b/modules/programs/nix-index.nix new file mode 100644 index 00000000..38115d4c --- /dev/null +++ b/modules/programs/nix-index.nix @@ -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 nix-index 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 + ''; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 72decbe7..6721c14a 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -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 diff --git a/tests/modules/programs/nix-index/assert-on-command-not-found.nix b/tests/modules/programs/nix-index/assert-on-command-not-found.nix new file mode 100644 index 00000000..757ac65b --- /dev/null +++ b/tests/modules/programs/nix-index/assert-on-command-not-found.nix @@ -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. + '' + ]; + }; +} diff --git a/tests/modules/programs/nix-index/default.nix b/tests/modules/programs/nix-index/default.nix new file mode 100644 index 00000000..eaebd731 --- /dev/null +++ b/tests/modules/programs/nix-index/default.nix @@ -0,0 +1,4 @@ +{ + nix-index-integrations = ./integrations.nix; + nix-index-assert-on-command-not-found = ./assert-on-command-not-found.nix; +} diff --git a/tests/modules/programs/nix-index/integrations.nix b/tests/modules/programs/nix-index/integrations.nix new file mode 100644 index 00000000..6da5fd04 --- /dev/null +++ b/tests/modules/programs/nix-index/integrations.nix @@ -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}' + ''; + }; +}