1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-09-19 04:47:29 +02:00
home-manager/modules/programs/yazi.nix

240 lines
6.7 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.yazi;
tomlFormat = pkgs.formats.toml { };
bashIntegration = ''
function ya() {
local tmp="$(mktemp -t "yazi-cwd.XXXXX")"
yazi "$@" --cwd-file="$tmp"
if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
builtin cd -- "$cwd"
fi
rm -f -- "$tmp"
}
'';
fishIntegration = ''
function ya
set tmp (mktemp -t "yazi-cwd.XXXXX")
yazi $argv --cwd-file="$tmp"
if set cwd (cat -- "$tmp"); and [ -n "$cwd" ]; and [ "$cwd" != "$PWD" ]
builtin cd -- "$cwd"
end
rm -f -- "$tmp"
end
'';
nushellIntegration = ''
def --env ya [...args] {
2024-01-03 20:18:53 +01:00
let tmp = (mktemp -t "yazi-cwd.XXXXX")
yazi ...$args --cwd-file $tmp
let cwd = (open $tmp)
if $cwd != "" and $cwd != $env.PWD {
cd $cwd
}
rm -fp $tmp
}
'';
in {
2024-05-29 05:01:52 +02:00
meta.maintainers = with maintainers; [ xyenon ];
options.programs.yazi = {
enable = mkEnableOption "yazi";
2024-05-29 05:01:52 +02:00
package = mkPackageOption pkgs "yazi" { };
enableBashIntegration = mkEnableOption "Bash integration";
enableZshIntegration = mkEnableOption "Zsh integration";
enableFishIntegration = mkEnableOption "Fish integration";
enableNushellIntegration = mkEnableOption "Nushell integration";
keymap = mkOption {
type = tomlFormat.type;
default = { };
example = literalExpression ''
{
input.keymap = [
{ exec = "close"; on = [ "<C-q>" ]; }
{ exec = "close --submit"; on = [ "<Enter>" ]; }
{ exec = "escape"; on = [ "<Esc>" ]; }
{ exec = "backspace"; on = [ "<Backspace>" ]; }
];
manager.keymap = [
{ exec = "escape"; on = [ "<Esc>" ]; }
{ exec = "quit"; on = [ "q" ]; }
{ exec = "close"; on = [ "<C-q>" ]; }
];
}
'';
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/yazi/keymap.toml`.
See <https://yazi-rs.github.io/docs/configuration/keymap>
for the full list of options.
'';
};
settings = mkOption {
type = tomlFormat.type;
default = { };
example = literalExpression ''
{
log = {
enabled = false;
};
manager = {
show_hidden = false;
sort_by = "modified";
sort_dir_first = true;
sort_reverse = true;
};
}
'';
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/yazi/yazi.toml`.
See <https://yazi-rs.github.io/docs/configuration/yazi>
for the full list of options.
'';
};
theme = mkOption {
type = tomlFormat.type;
default = { };
example = literalExpression ''
{
filetype = {
rules = [
{ fg = "#7AD9E5"; mime = "image/*"; }
{ fg = "#F3D398"; mime = "video/*"; }
{ fg = "#F3D398"; mime = "audio/*"; }
{ fg = "#CD9EFC"; mime = "application/x-bzip"; }
];
};
}
'';
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/yazi/theme.toml`.
See <https://yazi-rs.github.io/docs/configuration/theme>
for the full list of options
'';
};
2024-05-29 05:01:52 +02:00
initLua = mkOption {
type = with types; nullOr path;
default = null;
description = ''
The init.lua for Yazi itself.
'';
example = literalExpression "./init.lua";
};
plugins = mkOption {
type = with types; attrsOf (oneOf [ path package ]);
default = { };
description = ''
Lua plugins. Will be linked into {file}`$XDG_CONFIG_HOME/yazi/plugins/`.
2024-05-29 05:01:52 +02:00
See <https://yazi-rs.github.io/docs/plugins/overview>
for documentation.
2024-05-29 05:01:52 +02:00
'';
example = literalExpression ''
{
foo = ./foo;
bar = pkgs.bar;
}
'';
};
flavors = mkOption {
type = with types; attrsOf (oneOf [ path package ]);
default = { };
description = ''
Pre-made themes.
See <https://yazi-rs.github.io/docs/flavors/overview/> for documentation.
2024-05-29 05:01:52 +02:00
'';
example = literalExpression ''
{
foo = ./foo;
bar = pkgs.bar;
}
'';
};
};
config = mkIf cfg.enable {
home.packages = [ cfg.package ];
programs.bash.initExtra = mkIf cfg.enableBashIntegration bashIntegration;
programs.zsh.initExtra = mkIf cfg.enableZshIntegration bashIntegration;
programs.fish.interactiveShellInit =
mkIf cfg.enableFishIntegration fishIntegration;
programs.nushell.extraConfig =
mkIf cfg.enableNushellIntegration nushellIntegration;
xdg.configFile = {
"yazi/keymap.toml" = mkIf (cfg.keymap != { }) {
source = tomlFormat.generate "yazi-keymap" cfg.keymap;
};
"yazi/yazi.toml" = mkIf (cfg.settings != { }) {
source = tomlFormat.generate "yazi-settings" cfg.settings;
};
"yazi/theme.toml" = mkIf (cfg.theme != { }) {
source = tomlFormat.generate "yazi-theme" cfg.theme;
};
2024-05-29 05:01:52 +02:00
"yazi/init.lua" = mkIf (cfg.initLua != null) { source = cfg.initLua; };
} // (mapAttrs'
(name: value: nameValuePair "yazi/flavors/${name}" { source = value; })
cfg.flavors) // (let
# Make sure that the directory ends in `.yazi`, to comply with specification.
# `pluginName` is essential, it's needed to apply config in yazi's `init.lua`
ensureSuffix = pluginName:
if lib.hasSuffix ".yazi" pluginName then
"yazi/plugins/${pluginName}"
else
"yazi/plugins/${pluginName}.yazi";
mkPluginLink = pluginName: pluginPackageOrPath: {
name = ensureSuffix pluginName;
value.source = pluginPackageOrPath;
};
pluginLinks = mapAttrs' mkPluginLink cfg.plugins;
in pluginLinks);
assertions = (mapAttrsToList (pluginName: pluginPackageOrPath:
let
isDir = pathIsDirectory "${pluginPackageOrPath}";
hasInitLua = pathExists "${pluginPackageOrPath}/init.lua"
&& !(pathIsDirectory "${pluginPackageOrPath}/init.lua");
in {
assertion = isDir && hasInitLua;
message =
"Value at `programs.yazi.plugins.${pluginName}` is not a valid yazi plugin."
+ (optionalString (!isDir) ''
The path or package should be a directory, not a single file.'')
+ (optionalString (!hasInitLua) ''
The path or package must contain a file `init.lua`.'') + ''
Evaluated value: `${pluginPackageOrPath}`'';
}) cfg.plugins);
};
}