1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2024-12-14 11:57:55 +00:00

wip improve path handling

This commit is contained in:
Kristopher James Kent (kjkent) 2024-12-10 21:48:23 +00:00
parent 823381517a
commit aeef45b3cf
No known key found for this signature in database
GPG key ID: D94D794E6BF77966

View file

@ -3,29 +3,33 @@
with lib; with lib;
let let
homeDir = config.home.homeDirectory;
cfg = config.programs.zsh; cfg = config.programs.zsh;
relToDotDir = file: (optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file; dotDirParsed =
escapeShellArg (
removeSuffix "/" (
(optionalString (!hasPrefix "/" cfg.dotDir) homeDir) + "/${cfg.dotDir}"
)
);
pluginsDir = if cfg.dotDir != null then dotDirRelToHome = removePrefix "${homeDir}/" dotDirParsed;
relToDotDir "plugins" else ".zsh/plugins";
pluginsDir = dotDirParsed
+ (optionalString homeDir == dotDirParsed "/.zsh")
+ "/plugins";
envVarsStr = config.lib.zsh.exportAll cfg.sessionVariables; envVarsStr = config.lib.zsh.exportAll cfg.sessionVariables;
localVarsStr = config.lib.zsh.defineAll cfg.localVariables; localVarsStr = config.lib.zsh.defineAll cfg.localVariables;
aliasesStr = concatStringsSep "\n" ( aliasesStr = concatStringsSep "\n" (
mapAttrsToList (k: v: "alias -- ${lib.escapeShellArg k}=${lib.escapeShellArg v}") cfg.shellAliases mapAttrsToList (k: v: "alias -- ${escapeShellArg k}=${escapeShellArg v}") cfg.shellAliases
); );
dirHashesStr = concatStringsSep "\n" ( dirHashesStr = concatStringsSep "\n" (
mapAttrsToList (k: v: ''hash -d ${k}="${v}"'') cfg.dirHashes mapAttrsToList (k: v: ''hash -d ${k}="${v}"'') cfg.dirHashes
); );
# Absolute paths are assigned unmutated, relative paths
# are prepended with the user's home directory.
zdotdir = (strings.optionalString (!strings.hasPrefix "/" cfg.dotDir)
config.home.homeDirectory) + escapeShellArg cfg.dotDir;
bindkeyCommands = { bindkeyCommands = {
emacs = "bindkey -e"; emacs = "bindkey -e";
viins = "bindkey -v"; viins = "bindkey -v";
@ -66,14 +70,9 @@ let
path = mkOption { path = mkOption {
type = types.str; type = types.str;
default = if versionAtLeast stateVersion "20.03" default = "${dotDirParsed}/.zsh_history";
then "$HOME/.zsh_history" defaultText = "`\${config.programs.zsh.dotDir}/.zsh_history`";
else relToDotDir ".zsh_history"; example = "`\${config.xdg.dataHome}/zsh/zsh_history`";
defaultText = literalExpression ''
"$HOME/.zsh_history" if state version 20.03,
"$ZDOTDIR/.zsh_history" otherwise
'';
example = literalExpression ''"''${config.xdg.dataHome}/zsh/zsh_history"'';
description = "History file location"; description = "History file location";
}; };
@ -183,7 +182,7 @@ let
custom = mkOption { custom = mkOption {
default = ""; default = "";
type = types.str; type = types.str;
example = "$HOME/my_customizations"; example = "\${config.home.homeDirectory}/my_customizations";
description = '' description = ''
Path to a custom oh-my-zsh package to override config of Path to a custom oh-my-zsh package to override config of
oh-my-zsh. See <https://github.com/robbyrussell/oh-my-zsh/wiki/Customization> oh-my-zsh. See <https://github.com/robbyrussell/oh-my-zsh/wiki/Customization>
@ -307,12 +306,14 @@ in
}; };
dotDir = mkOption { dotDir = mkOption {
default = null; default = homeDir;
example = ".config/zsh"; defaultText = "`config.home.homeDirectory`";
example = "`\${config.xdg.configHome}/zsh`";
description = '' description = ''
Directory where the zsh configuration should be located. The default Custom directory for zsh configuration and data. If unset, .zshrc,
is the home directory. This option accepts absolute paths, or paths .zshenv and similar are stored in the user's home directory, while
relative to `config.home.homeDirectory`. plugins and other data are stored in `.zsh/`. This option accepts
absolute paths, or paths relative to the user's home directory.
''; '';
type = types.nullOr types.str; type = types.nullOr types.str;
}; };
@ -351,9 +352,9 @@ in
default = {}; default = {};
example = literalExpression '' example = literalExpression ''
{ {
docs = "$HOME/Documents"; docs = "''${config.home.homeDirectory}/Documents";
vids = "$HOME/Videos"; vids = "''${config.home.homeDirectory}/Videos";
dl = "$HOME/Downloads"; dl = "''${config.home.homeDirectory)/Downloads";
} }
''; '';
description = '' description = ''
@ -544,31 +545,31 @@ in
config = mkIf cfg.enable (mkMerge [ config = mkIf cfg.enable (mkMerge [
(mkIf (cfg.envExtra != "") { (mkIf (cfg.envExtra != "") {
home.file."${relToDotDir ".zshenv"}".text = cfg.envExtra; home.file."${dotDirRelToHome}/.zshenv".text = cfg.envExtra;
}) })
(mkIf (cfg.profileExtra != "") { (mkIf (cfg.profileExtra != "") {
home.file."${relToDotDir ".zprofile"}".text = cfg.profileExtra; home.file."${dotDirRelToHome}/.zprofile".text = cfg.profileExtra;
}) })
(mkIf (cfg.loginExtra != "") { (mkIf (cfg.loginExtra != "") {
home.file."${relToDotDir ".zlogin"}".text = cfg.loginExtra; home.file."${dotDirRelToHome}/.zlogin".text = cfg.loginExtra;
}) })
(mkIf (cfg.logoutExtra != "") { (mkIf (cfg.logoutExtra != "") {
home.file."${relToDotDir ".zlogout"}".text = cfg.logoutExtra; home.file."${dotDirRelToHome}/.zlogout".text = cfg.logoutExtra;
}) })
(mkIf cfg.oh-my-zsh.enable { (mkIf cfg.oh-my-zsh.enable {
home.file."${relToDotDir ".zshenv"}".text = '' home.file."${dotDirRelToHome}/.zshenv".text = ''
ZSH="${cfg.oh-my-zsh.package}/share/oh-my-zsh"; ZSH="${cfg.oh-my-zsh.package}/share/oh-my-zsh";
ZSH_CACHE_DIR="${config.xdg.cacheHome}/oh-my-zsh"; ZSH_CACHE_DIR="${config.xdg.cacheHome}/oh-my-zsh";
''; '';
}) })
(mkIf (cfg.dotDir != null) { (mkIf (dotDirParsed != homeDir) {
home.file."${relToDotDir ".zshenv"}".text = '' home.file."${dotDirRelToHome}/.zshenv".text = ''
export ZDOTDIR=${zdotdir} export ZDOTDIR=${dotDirParsed}
''; '';
# When dotDir is set, only use ~/.zshenv to source ZDOTDIR/.zshenv, # When dotDir is set, only use ~/.zshenv to source ZDOTDIR/.zshenv,
@ -576,12 +577,12 @@ in
# already set correctly (by e.g. spawning a zsh inside a zsh), all env # already set correctly (by e.g. spawning a zsh inside a zsh), all env
# vars still get exported # vars still get exported
home.file.".zshenv".text = '' home.file.".zshenv".text = ''
source ${zdotdir}/.zshenv source ${dotDirParsed}/.zshenv
''; '';
}) })
{ {
home.file."${relToDotDir ".zshenv"}".text = '' home.file."${dotDirRelToHome}/.zshenv".text = ''
# Environment variables # Environment variables
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh" . "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
@ -598,7 +599,7 @@ in
++ optional cfg.enableCompletion pkgs.nix-zsh-completions ++ optional cfg.enableCompletion pkgs.nix-zsh-completions
++ optional cfg.oh-my-zsh.enable cfg.oh-my-zsh.package; ++ optional cfg.oh-my-zsh.enable cfg.oh-my-zsh.package;
home.file."${relToDotDir ".zshrc"}".text = concatStringsSep "\n" ([ home.file."${dotDirRelToHome}/.zshrc".text = concatStringsSep "\n" ([
# zprof must be loaded before everything else, since it # zprof must be loaded before everything else, since it
# benchmarks the shell initialization. # benchmarks the shell initialization.
(optionalString cfg.zprof.enable '' (optionalString cfg.zprof.enable ''
@ -679,7 +680,7 @@ in
# See https://github.com/nix-community/home-manager/issues/177. # See https://github.com/nix-community/home-manager/issues/177.
HISTSIZE="${toString cfg.history.size}" HISTSIZE="${toString cfg.history.size}"
SAVEHIST="${toString cfg.history.save}" SAVEHIST="${toString cfg.history.save}"
${optionalString (cfg.history.ignorePatterns != []) "HISTORY_IGNORE=${lib.escapeShellArg "(${lib.concatStringsSep "|" cfg.history.ignorePatterns})"}"} ${optionalString (cfg.history.ignorePatterns != []) "HISTORY_IGNORE=${escapeShellArg "(${concatStringsSep "|" cfg.history.ignorePatterns})"}"}
${if versionAtLeast config.home.stateVersion "20.03" ${if versionAtLeast config.home.stateVersion "20.03"
then ''HISTFILE="${cfg.history.path}"'' then ''HISTFILE="${cfg.history.path}"''
else ''HISTFILE="$HOME/${cfg.history.path}"''} else ''HISTFILE="$HOME/${cfg.history.path}"''}
@ -701,7 +702,7 @@ in
${aliasesStr} ${aliasesStr}
'' ''
] ]
++ (mapAttrsToList (k: v: "alias -g -- ${lib.escapeShellArg k}=${lib.escapeShellArg v}") cfg.shellGlobalAliases) ++ (mapAttrsToList (k: v: "alias -g -- ${escapeShellArg k}=${escapeShellArg v}") cfg.shellGlobalAliases)
++ [ ('' ++ [ (''
# Named Directory Hashes # Named Directory Hashes
${dirHashesStr} ${dirHashesStr}
@ -730,13 +731,13 @@ in
# https://github.com/zsh-users/zsh-history-substring-search#usage # https://github.com/zsh-users/zsh-history-substring-search#usage
'' ''
source ${pkgs.zsh-history-substring-search}/share/zsh-history-substring-search/zsh-history-substring-search.zsh source ${pkgs.zsh-history-substring-search}/share/zsh-history-substring-search/zsh-history-substring-search.zsh
${lib.concatMapStringsSep "\n" ${concatMapStringsSep "\n"
(upKey: "bindkey \"${upKey}\" history-substring-search-up") (upKey: "bindkey \"${upKey}\" history-substring-search-up")
(lib.toList cfg.historySubstringSearch.searchUpKey) (toList cfg.historySubstringSearch.searchUpKey)
} }
${lib.concatMapStringsSep "\n" ${concatMapStringsSep "\n"
(downKey: "bindkey \"${downKey}\" history-substring-search-down") (downKey: "bindkey \"${downKey}\" history-substring-search-down")
(lib.toList cfg.historySubstringSearch.searchDownKey) (toList cfg.historySubstringSearch.searchDownKey)
} }
'') '')