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.
This commit is contained in:
punkfairie 2024-11-03 18:48:50 -08:00
parent 7a03cf3dea
commit ebf55b950d
3 changed files with 115 additions and 16 deletions

View file

@ -46,8 +46,9 @@
};
modules = [
./home
inputs.rose-pine.homeManagerModules.rose-pine
./modules/home/shellAbbrs.nix
./home
];
};
};

View file

@ -1,19 +1,15 @@
{ lib, config, ... }:
{ ... }:
{
# TODO: There's probably some fancy nix attr mapping that can be done to set
# these once and convert them to aliases or abbrs depending on shell.
programs.fish.shellAbbrs =
lib.mkIf (config.programs.fish.enable && config.programs.fish.preferAbbrs)
{
jf = {
position = "command";
expansion = "sudo journalctl --follow --unit";
};
je = {
position = "command";
expansion = "sudo journalctl --pager-end --unit";
};
};
home.shellAbbrs = {
jf = {
position = "command";
expansion = "sudo journalctl --follow --unit";
};
je = {
position = "command";
expansion = "sudo journalctl --pager-end --unit";
};
};
imports = [
./clipboard.nix

102
modules/home/shellAbbrs.nix Normal file
View file

@ -0,0 +1,102 @@
{
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);
};
}