mirror of
https://github.com/nix-community/home-manager
synced 2024-11-23 11:39:46 +01:00
fish: format user and generated .fish files
Adds a `fishIndent` wrapper to pass fish scripts to the built in `fish_indent` function.
This commit is contained in:
parent
518dca61c0
commit
ca48fced83
3 changed files with 99 additions and 28 deletions
|
@ -145,6 +145,16 @@ let
|
||||||
aliasesStr = concatStringsSep "\n"
|
aliasesStr = concatStringsSep "\n"
|
||||||
(mapAttrsToList (k: v: "alias ${k} ${escapeShellArg v}") cfg.shellAliases);
|
(mapAttrsToList (k: v: "alias ${k} ${escapeShellArg v}") cfg.shellAliases);
|
||||||
|
|
||||||
|
fishIndent = name: text:
|
||||||
|
if cfg.formatFishScripts then
|
||||||
|
pkgs.runCommand name {
|
||||||
|
nativeBuildInputs = [ cfg.package ];
|
||||||
|
inherit text;
|
||||||
|
passAsFile = [ "text" ];
|
||||||
|
} "fish_indent < $textPath > $out"
|
||||||
|
else
|
||||||
|
pkgs.writeText name text;
|
||||||
|
|
||||||
in {
|
in {
|
||||||
imports = [
|
imports = [
|
||||||
(mkRemovedOptionModule [ "programs" "fish" "promptInit" ] ''
|
(mkRemovedOptionModule [ "programs" "fish" "promptInit" ] ''
|
||||||
|
@ -279,6 +289,15 @@ in {
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
programs.fish.formatFishScripts = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = ''
|
||||||
|
Whether to process fish configuration and scripts with
|
||||||
|
<literal>fish_indent</literal>.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable (mkMerge [
|
config = mkIf cfg.enable (mkMerge [
|
||||||
|
@ -340,7 +359,7 @@ in {
|
||||||
end
|
end
|
||||||
'';
|
'';
|
||||||
|
|
||||||
xdg.configFile."fish/config.fish".text = ''
|
xdg.configFile."fish/config.fish".source = fishIndent "config.fish" ''
|
||||||
# ~/.config/fish/config.fish: DO NOT EDIT -- this file has been generated
|
# ~/.config/fish/config.fish: DO NOT EDIT -- this file has been generated
|
||||||
# automatically by home-manager.
|
# automatically by home-manager.
|
||||||
|
|
||||||
|
@ -379,7 +398,7 @@ in {
|
||||||
xdg.configFile = mapAttrs' (name: def: {
|
xdg.configFile = mapAttrs' (name: def: {
|
||||||
name = "fish/functions/${name}.fish";
|
name = "fish/functions/${name}.fish";
|
||||||
value = {
|
value = {
|
||||||
text = let
|
source = let
|
||||||
modifierStr = n: v: optional (v != null) ''--${n}="${toString v}"'';
|
modifierStr = n: v: optional (v != null) ''--${n}="${toString v}"'';
|
||||||
modifierStrs = n: v: optional (v != null) "--${n}=${toString v}";
|
modifierStrs = n: v: optional (v != null) "--${n}=${toString v}";
|
||||||
modifierBool = n: v: optional (v != null && v) "--${n}";
|
modifierBool = n: v: optional (v != null && v) "--${n}";
|
||||||
|
@ -397,9 +416,9 @@ in {
|
||||||
|
|
||||||
modifiers = if isAttrs def then " ${toString mods}" else "";
|
modifiers = if isAttrs def then " ${toString mods}" else "";
|
||||||
body = if isAttrs def then def.body else def;
|
body = if isAttrs def then def.body else def;
|
||||||
in ''
|
in fishIndent "${name}.fish" ''
|
||||||
function ${name}${modifiers}
|
function ${name}${modifiers}
|
||||||
${body}
|
${lib.strings.removeSuffix "\n" body}
|
||||||
end
|
end
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
@ -410,34 +429,35 @@ in {
|
||||||
# in the paths and any initialization scripts.
|
# in the paths and any initialization scripts.
|
||||||
(mkIf (length cfg.plugins > 0) {
|
(mkIf (length cfg.plugins > 0) {
|
||||||
xdg.configFile = mkMerge ((map (plugin: {
|
xdg.configFile = mkMerge ((map (plugin: {
|
||||||
"fish/conf.d/plugin-${plugin.name}.fish".text = ''
|
"fish/conf.d/plugin-${plugin.name}.fish".source =
|
||||||
# Plugin ${plugin.name}
|
fishIndent "${plugin.name}.fish" ''
|
||||||
set -l plugin_dir ${plugin.src}
|
# Plugin ${plugin.name}
|
||||||
|
set -l plugin_dir ${plugin.src}
|
||||||
|
|
||||||
# Set paths to import plugin components
|
# Set paths to import plugin components
|
||||||
if test -d $plugin_dir/functions
|
if test -d $plugin_dir/functions
|
||||||
set fish_function_path $fish_function_path[1] $plugin_dir/functions $fish_function_path[2..-1]
|
set fish_function_path $fish_function_path[1] $plugin_dir/functions $fish_function_path[2..-1]
|
||||||
end
|
|
||||||
|
|
||||||
if test -d $plugin_dir/completions
|
|
||||||
set fish_complete_path $fish_complete_path[1] $plugin_dir/completions $fish_complete_path[2..-1]
|
|
||||||
end
|
|
||||||
|
|
||||||
# Source initialization code if it exists.
|
|
||||||
if test -d $plugin_dir/conf.d
|
|
||||||
for f in $plugin_dir/conf.d/*.fish
|
|
||||||
source $f
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if test -f $plugin_dir/key_bindings.fish
|
if test -d $plugin_dir/completions
|
||||||
source $plugin_dir/key_bindings.fish
|
set fish_complete_path $fish_complete_path[1] $plugin_dir/completions $fish_complete_path[2..-1]
|
||||||
end
|
end
|
||||||
|
|
||||||
if test -f $plugin_dir/init.fish
|
# Source initialization code if it exists.
|
||||||
source $plugin_dir/init.fish
|
if test -d $plugin_dir/conf.d
|
||||||
end
|
for f in $plugin_dir/conf.d/*.fish
|
||||||
'';
|
source $f
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -f $plugin_dir/key_bindings.fish
|
||||||
|
source $plugin_dir/key_bindings.fish
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -f $plugin_dir/init.fish
|
||||||
|
source $plugin_dir/init.fish
|
||||||
|
end
|
||||||
|
'';
|
||||||
}) cfg.plugins));
|
}) cfg.plugins));
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
|
fish-format-scripts = ./format-scripts.nix;
|
||||||
fish-functions = ./functions.nix;
|
fish-functions = ./functions.nix;
|
||||||
fish-no-functions = ./no-functions.nix;
|
fish-no-functions = ./no-functions.nix;
|
||||||
fish-plugins = ./plugins.nix;
|
fish-plugins = ./plugins.nix;
|
||||||
|
|
50
tests/modules/programs/fish/format-scripts.nix
Normal file
50
tests/modules/programs/fish/format-scripts.nix
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
{ config, pkgs, ... }:
|
||||||
|
|
||||||
|
let
|
||||||
|
|
||||||
|
expectedFunc = pkgs.writeText "func.fish" ''
|
||||||
|
function func
|
||||||
|
echo foo
|
||||||
|
end
|
||||||
|
'';
|
||||||
|
|
||||||
|
expectedFuncMulti = pkgs.writeText "func-multi.fish" ''
|
||||||
|
function func-multi
|
||||||
|
echo bar
|
||||||
|
if foo
|
||||||
|
bar
|
||||||
|
baz
|
||||||
|
end
|
||||||
|
end
|
||||||
|
'';
|
||||||
|
|
||||||
|
in {
|
||||||
|
config = {
|
||||||
|
programs.fish = {
|
||||||
|
enable = true;
|
||||||
|
|
||||||
|
formatFishScripts = true;
|
||||||
|
|
||||||
|
functions = {
|
||||||
|
func = ''echo "foo"'';
|
||||||
|
func-multi = ''
|
||||||
|
echo bar
|
||||||
|
if foo
|
||||||
|
bar
|
||||||
|
baz
|
||||||
|
end
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
nmt.script = ''
|
||||||
|
assertFileExists home-files/.config/fish/functions/func.fish
|
||||||
|
echo ${expectedFunc}
|
||||||
|
assertFileContent home-files/.config/fish/functions/func.fish ${expectedFunc}
|
||||||
|
|
||||||
|
assertFileExists home-files/.config/fish/functions/func-multi.fish
|
||||||
|
echo ${expectedFuncMulti}
|
||||||
|
assertFileContent home-files/.config/fish/functions/func-multi.fish ${expectedFuncMulti}
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue