From ebf55b950d4ea4aba6e153eb47f367dc96f3f456 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Sun, 3 Nov 2024 18:48:50 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(abbrs):=20New=20module=20for?= =?UTF-8?q?=20home.shellAbbrs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- flake.nix | 3 +- home/services/default.nix | 26 ++++----- modules/home/shellAbbrs.nix | 102 ++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 modules/home/shellAbbrs.nix diff --git a/flake.nix b/flake.nix index 41792b8..e0d0c37 100644 --- a/flake.nix +++ b/flake.nix @@ -46,8 +46,9 @@ }; modules = [ - ./home inputs.rose-pine.homeManagerModules.rose-pine + ./modules/home/shellAbbrs.nix + ./home ]; }; }; diff --git a/home/services/default.nix b/home/services/default.nix index a9d18ff..3331b8f 100644 --- a/home/services/default.nix +++ b/home/services/default.nix @@ -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 diff --git a/modules/home/shellAbbrs.nix b/modules/home/shellAbbrs.nix new file mode 100644 index 0000000..0eb9beb --- /dev/null +++ b/modules/home/shellAbbrs.nix @@ -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); + }; +}