基本上,我想拉我的config.programs.config.alias
条目分成单独的.bash
文件并在构建配置时动态读取它们。当前配置的代表性子集:
{
programs.git = {
config = {
alias = {
aliases = "!git config --get-regexp '^alias\.' | cut --delimiter=. --fields 2-";
git = "!git";
st = "status";
};
};
};
}
所有!git
条目最好作为单独的 shell 脚本。这样,我可以在将它们集成到 Git 别名配置中之前对它们进行 lint、格式化和运行以验证它们是否有效。
答案1
带注释的版本到目前为止的解决方案:
{
config.git.config.alias =
(
lib.attrsets.mergeAttrsList ( # Change from a list of attribute sets to a single attribute set
map (
path: {
# Create a [filename without extension as alias name] to [alias value] attribute set
"${lib.removeSuffix ".bash" (baseNameOf path)}" =
"!\"" # `!`denotes that this alias runs a command rather than a Git subcommand; quote to simplify escaping
+ builtins.replaceStrings ["\n"] ["; "] ( # Change from readable multi-line scripts to a single line
lib.escape ["\"" "\\"] ( # Escape backslash and double quotes to fit Git configuration language
lib.removeSuffix "\n" ( # Remove newline at EOF
builtins.readFile path
)
)
)
+ "\"";
}
) (
lib.filesystem.listFilesRecursive ./includes/git-aliases
)
)
)
// {
st = "status";
};
}
它似乎适用于我的所有别名,包括那些带有单引号和双引号、反斜杠和换行符的别名。希望其他人能想出一些更简单的东西,但现在就这样了。