From b18d302d44268fb131033c1157d2978baf9e2207 Mon Sep 17 00:00:00 2001 From: Jonas Holst Damtoft Date: Sat, 23 Mar 2019 21:09:03 +0100 Subject: [PATCH] fish: add plugin functionality --- modules/programs/fish.nix | 84 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/modules/programs/fish.nix b/modules/programs/fish.nix index 87a17b85..5a076e2c 100644 --- a/modules/programs/fish.nix +++ b/modules/programs/fish.nix @@ -14,6 +14,39 @@ let mapAttrsToList (k: v: "alias ${k}='${v}'") cfg.shellAliases ); + fileType = types.submodule ( + { name, config, ... }: { + options = { + body = mkOption { + default = null; + type = types.nullOr types.lines; + description = "Body of the function."; + }; + + source = mkOption { + type = types.path; + description = '' + Path of the source file. The file name must not start + with a period. + ''; + }; + }; + + config = { + source = mkIf (config.body != null) ( + mkDefault (pkgs.writeTextFile { + inherit name; + text = '' + function ${name} + ${config.body} + end + ''; + executable = true; + }) + ); + }; + } + ); in { @@ -80,9 +113,27 @@ in type = types.lines; }; }; + + programs.fish.plugins = mkOption { + type = types.listOf types.package; + default = []; + description = '' + The plugins to add to fish. + Built with buildFishPlugin. + Overrides manually installed ones. + ''; + }; + + programs.fish.functions = mkOption { + type = types.attrsOf fileType; + default = {}; + description = '' + Functions to add to fish. + ''; + }; }; - config = mkIf cfg.enable { + config = mkIf cfg.enable (mkMerge [{ home.packages = [ cfg.package ]; xdg.dataFile."fish/home-manager_generated_completions".source = @@ -187,5 +238,34 @@ in set -g __fish_interactive_config_sourced 1 end ''; - }; + } { + xdg.configFile = map (n: { target = "fish/functions/${n}.fish"; source = cfg.functions.${n}.source; }) (attrNames cfg.functions); + } ( + let + wrappedPkgVersion = lib.getVersion pkgs.fish; + wrappedPkgName = lib.removeSuffix "-${wrappedPkgVersion}" pkgs.fish.name; + dependencies = concatMap (p: p.dependencies) cfg.plugins; + combinedPluginDrv = pkgs.buildEnv { + name = "${wrappedPkgName}-plugins-${wrappedPkgVersion}"; + paths = cfg.plugins; + postBuild = '' + touch $out/setup.fish + + if [ -d $out/functions ]; then + echo "set fish_function_path \$fish_function_path[1] $out/functions \$fish_function_path[2..-1]" >> $out/setup.fish + fi + + if [ -d $out/completions ]; then + echo "set fish_complete_path \$fish_complete_path[1] $out/completions \$fish_complete_path[2..-1]" >> $out/setup.fish + fi + + if [ -d $out/conf.d ]; then + echo "source $out/conf.d/*.fish" >> $out/setup.fish + fi + ''; + }; + in mkIf (length cfg.plugins > 0) { + xdg.configFile."fish/conf.d/99plugins.fish".source = "${combinedPluginDrv}/setup.fish"; + home.packages = dependencies; + })]); }