From 6f422785c3402102f444092c1d871b87a1311a80 Mon Sep 17 00:00:00 2001 From: hyperfekt Date: Mon, 17 Dec 2018 12:57:30 +0100 Subject: [PATCH] fish: autogenerate completions from man pages --- modules/programs/fish.nix | 74 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/modules/programs/fish.nix b/modules/programs/fish.nix index 61e74c45..0b46c17c 100644 --- a/modules/programs/fish.nix +++ b/modules/programs/fish.nix @@ -21,6 +21,15 @@ in programs.fish = { enable = mkEnableOption "fish friendly interactive shell"; + package = mkOption { + default = pkgs.fish; + defaultText = "pkgs.fish"; + description = '' + The fish package to install. May be used to change the version. + ''; + type = types.package; + }; + shellAliases = mkOption { default = {}; description = '' @@ -74,7 +83,70 @@ in }; config = mkIf cfg.enable { - home.packages = [ pkgs.fish ]; + home.packages = [ cfg.package ]; + + xdg.dataFile."fish/home-manager_generated_completions".source = + let + # paths later in the list will overwrite those already linked + destructiveSymlinkJoin = + args_@{ name + , paths + , preferLocalBuild ? true + , allowSubstitutes ? false + , postBuild ? "" + , ... + }: + let + args = removeAttrs args_ [ "name" "postBuild" ] + // { inherit preferLocalBuild allowSubstitutes; }; # pass the defaults + in pkgs.runCommand name args + '' + mkdir -p $out + for i in $paths; do + if [ -z "$(find $i -prune -empty)" ]; then + cp -srf $i/* $out + fi + done + ${postBuild} + ''; + generateCompletions = package: pkgs.runCommand + "${package.name}-fish-completions" + { + src = package; + nativeBuildInputs = [ pkgs.python2 ]; + buildInputs = [ cfg.package ]; + preferLocalBuild = true; + allowSubstitutes = false; + } + '' + mkdir -p $out + if [ -d $src/share/man ]; then + find $src/share/man -type f \ + | xargs python ${cfg.package}/share/fish/tools/create_manpage_completions.py --directory $out \ + > /dev/null + fi + ''; + in + destructiveSymlinkJoin { + name = "${config.home.username}-fish-completions"; + paths = + let + cmp = (a: b: (a.meta.priority or 0) > (b.meta.priority or 0)); + in + map generateCompletions (sort cmp config.home.packages); + }; + + programs.fish.interactiveShellInit = '' + # add completions generated by Home Manager to $fish_complete_path + begin + set -l joined (string join " " $fish_complete_path) + set -l prev_joined (string replace --regex "[^\s]*generated_completions.*" "" $joined) + set -l post_joined (string replace $prev_joined "" $joined) + set -l prev (string split " " (string trim $prev_joined)) + set -l post (string split " " (string trim $post_joined)) + set fish_complete_path $prev "${config.xdg.dataHome}/fish/home-manager_generated_completions" $post + end + ''; xdg.configFile."fish/config.fish".text = '' # ~/.config/fish/config.fish: DO NOT EDIT -- this file has been generated automatically.