marleyos/modules/home/shellAbbrs.nix
punkfairie ebf55b950d feat(abbrs): New module for home.shellAbbrs
Allows setting home.shellAbbrs that either get set as Fish shell
abbreviations or converted to shell aliases depending on if Fish is
active.

Abbrs that depend on Fish-specific features get automatically filtered
out.
2024-11-03 18:48:50 -08:00

102 lines
2.6 KiB
Nix

{
config,
lib,
...
}:
with lib;
let
# https://github.com/nix-community/home-manager/blob/master/modules/programs/fish.nix
abbrModule = types.submodule {
options = {
expansion = mkOption {
type = with types; nullOr str;
default = null;
description = ''
The command expanded by an abbreviation.
'';
};
position = mkOption {
type = with types; nullOr str;
default = null;
example = "anywhere";
description = ''
If the position is "command", the abbreviation expands only if
the position is a command. If it is "anywhere", the abbreviation
expands anywhere.
'';
};
regex = mkOption {
type = with types; nullOr str;
default = null;
description = ''
The regular expression pattern matched instead of the literal name.
'';
};
setCursor = mkOption {
type = with types; (either bool str);
default = false;
description = ''
The marker indicates the position of the cursor when the abbreviation
is expanded. When setCursor is true, the marker is set with a default
value of "%".
'';
};
function = mkOption {
type = with types; nullOr str;
default = null;
description = ''
The fish function expanded instead of a literal string.
'';
};
};
};
removeFishOnly = attrsets.filterAttrs (
_: v: if (builtins.isAttrs v) then !((v ? regex) || (v ? setCursor) || (v ? function)) else true
);
abbrToAlias = attrsets.mapAttrs (
_: v: if (builtins.isAttrs v) then v.expansion else v
) removeFishOnly;
in
{
options = {
# https://github.com/nix-community/home-manager/blob/master/modules/programs/fish.nix
home.shellAbbrs = mkOption {
type = with types; attrsOf (either str abbrModule);
default = { };
example = literalExpression ''
{
l = "less";
gco = "git checkout";
"-C" = {
position = "anywhere";
expansion = "--color";
};
}
'';
description = ''
An attribute set that maps aliases (the top level attribute names
in this option) to abbreviations. Abbreviations are expanded with
the longer phrase after they are entered.
'';
};
};
config =
let
fishCfg = config.programs.fish;
shellAbbrs = config.home.shellAbbrs;
in
{
programs.fish.shellAbbrs = mkIf fishCfg.enable shellAbbrs;
home.shellAliases = mkIf (!fishCfg.enable) (abbrToAlias shellAbbrs);
};
}