home/modules/shellAliases: add support for restricting shells

This commit is contained in:
2025-05-21 00:25:47 +02:00
parent abac62b42b
commit 3dcab3e966

View File

@@ -25,7 +25,7 @@
}; };
}; };
isAlias = v: builtins.isAttrs v && v ? "alias" && v ? "type"; isAlias = v: builtins.isAttrs v && v ? alias;
}; };
in { in {
lib = formatLib; lib = formatLib;
@@ -47,6 +47,29 @@
"grep nix" "grep nix"
]; ];
}; };
shells = lib.mkOption {
description = "List of shells for which the alias is valid.";
type = with lib.types; listOf (enum [
"bash"
"zsh"
"fish"
"nushell"
]);
default = [
"bash"
"zsh"
"fish"
"nushell"
];
example = [
"bash"
"zsh"
];
};
# TODO:
# subshell = lib.mkEnableOption "" // {
# description = "Whether to run the aliased command in a subshell";
# };
}; };
}; };
@@ -59,45 +82,34 @@
check = v: builtins.isString v || formatLib.isAlias v; check = v: builtins.isString v || formatLib.isAlias v;
}; };
aliasTreeType = with lib.types; attrsOf (either coercedAliasType aliasTreeType); aliasTreeType = with lib.types; attrsOf (either coercedAliasType aliasTreeType) // {
description = "Alias tree";
};
in aliasTreeType; in aliasTreeType;
# Alias Tree -> { :: Alias } # Alias Tree -> String -> { :: Alias }
generateAttrs = let generateAttrs = shell: let
inherit (lib) mapAttrs attrValues filterAttrs isAttrs generateAttrs' = attrset: lib.pipe attrset [
isString concatStringsSep foldr; (attrset: {
right = lib.filterAttrs (_: v: formatLib.isAlias v) attrset;
applyFunctor = attrset: formatLib.functors.${attrset.type}.apply attrset; wrong = lib.filterAttrs (_: v: !(formatLib.isAlias v)) attrset;
})
# TODO: better naming ({ right, wrong }:
allAttrValuesAreStrings = attrset: let # Leaf nodes
(lib.pipe right [
# [ {String} ] (lib.filterAttrs (_: v: lib.elem shell v.shells))
filteredAliases = [(filterAttrs (_: isString) attrset)]; (builtins.mapAttrs (_: v: formatLib.functors.${v.type}.apply v))
])
# [ {String} ] //
remainingFunctors = let # Subsets
functorSet = filterAttrs (_: formatLib.isAlias) attrset; (lib.pipe wrong [
appliedFunctorSet = mapAttrs (_: applyFunctor) functorSet; builtins.attrValues
in [ appliedFunctorSet ]; (map generateAttrs')
(lib.foldr (a: b: a // b) { })
# [ {AttrSet} ] ])
remainingAliasSets = attrValues (filterAttrs (_: v: isAttrs v && !formatLib.isAlias v) attrset); )
];
# [ {String} ] in generateAttrs';
recursedAliasSets = filteredAliases
++ (remainingFunctors)
++ (map allAttrValuesAreStrings remainingAliasSets);
in foldr (a: b: a // b) {} recursedAliasSets;
in
allAttrValuesAreStrings;
# TODO:
# generateAttrs = pipe [
# collect leave nodes
# map apply functor
# ]
# Alias Tree -> String # Alias Tree -> String
generateText = aliases: let generateText = aliases: let
@@ -243,20 +255,20 @@ in {
programs = { programs = {
zsh = { zsh = {
shellAliases = shellAliasesFormat.generateAttrs cfg.aliases; shellAliases = shellAliasesFormat.generateAttrs "zsh" cfg.aliases;
sessionVariables = cfg.variables; sessionVariables = cfg.variables;
}; };
bash = { bash = {
shellAliases = shellAliasesFormat.generateAttrs cfg.aliases; shellAliases = shellAliasesFormat.generateAttrs "bash" cfg.aliases;
sessionVariables = cfg.variables; sessionVariables = cfg.variables;
}; };
fish = { fish = {
shellAliases = shellAliasesFormat.generateAttrs cfg.aliases; shellAliases = shellAliasesFormat.generateAttrs "fish" cfg.aliases;
# TODO: fish does not support session variables? # TODO: fish does not support session variables?
# localVariables = cfg.variables; # localVariables = cfg.variables;
}; };
nushell = { nushell = {
shellAliases = shellAliasesFormat.generateAttrs cfg.aliases; shellAliases = shellAliasesFormat.generateAttrs "nushell" cfg.aliases;
environmentVariables = cfg.variables; environmentVariables = cfg.variables;
}; };
}; };