2.home-manager/modules/programs/oh-my-posh.nix
Evgeny Zislis 0c7c71a212
oh-my-posh: add cache clearing on package version changes (#7757)
* add cache clearing for oh-my-posh package changes

Using oh-my-posh creates script files in `~/.cache/oh-my-posh` which include an old derivation path. Once that path is garbage collected, oh-my-posh stops working preventing to successfully create new shells.

```
bash: /nix/store/5ddhz8nsahf1d03smzx2xpmynjspjfh8-oh-my-posh-26.8.0/bin/oh-my-posh: No such file or directory
```

* fix oh-my-posh package reference in cache clear

Use the configured value from programs.oh-my-posh.package as the value of oh-my-posh to compare during cache cleanup.
2025-09-09 21:15:16 -05:00

114 lines
3.5 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
inherit (lib) mkIf;
cfg = config.programs.oh-my-posh;
jsonFormat = pkgs.formats.json { };
configArgument =
if cfg.settings != { } then
"--config ${config.xdg.configHome}/oh-my-posh/config.json"
else if cfg.useTheme != null then
"--config ${cfg.package}/share/oh-my-posh/themes/${cfg.useTheme}.omp.json"
else
"";
in
{
meta.maintainers = [ lib.maintainers.arjan-s ];
options.programs.oh-my-posh = {
enable = lib.mkEnableOption "oh-my-posh, a prompt theme engine for any shell";
package = lib.mkPackageOption pkgs "oh-my-posh" { };
settings = lib.mkOption {
type = jsonFormat.type;
default = { };
example = lib.literalExpression ''builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile "''${pkgs.oh-my-posh}/share/oh-my-posh/themes/space.omp.json"))'';
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/oh-my-posh/config.json`. See
<https://ohmyposh.dev/docs/configuration/overview>
for details. The `useTheme` option is ignored when this
option is used.
'';
};
useTheme = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Use one of the official themes. This should be a name from this list:
<https://ohmyposh.dev/docs/themes>. Because a theme
is essentially a configuration file, this option is not used when a
`configFile` is set.
'';
};
enableBashIntegration = lib.hm.shell.mkBashIntegrationOption { inherit config; };
enableFishIntegration = lib.hm.shell.mkFishIntegrationOption { inherit config; };
enableNushellIntegration = lib.hm.shell.mkNushellIntegrationOption { inherit config; };
enableZshIntegration = lib.hm.shell.mkZshIntegrationOption { inherit config; };
};
config = mkIf cfg.enable {
home.packages = [ cfg.package ];
xdg.configFile."oh-my-posh/config.json" = mkIf (cfg.settings != { }) {
source = jsonFormat.generate "oh-my-posh-settings" cfg.settings;
};
programs.bash.initExtra = mkIf cfg.enableBashIntegration ''
eval "$(${lib.getExe cfg.package} init bash ${configArgument})"
'';
programs.zsh.initContent = mkIf cfg.enableZshIntegration ''
eval "$(${lib.getExe cfg.package} init zsh ${configArgument})"
'';
programs.fish.shellInit = mkIf cfg.enableFishIntegration ''
${lib.getExe cfg.package} init fish ${configArgument} | source
'';
programs.nushell = mkIf cfg.enableNushellIntegration {
extraConfig = ''
${
if lib.versionAtLeast (lib.versions.major cfg.package.version) "26" then
"${lib.getExe cfg.package} init nu ${configArgument}"
else
''source ${
pkgs.runCommand "oh-my-posh-nushell-config.nu" { } ''
${lib.getExe cfg.package} init nu ${configArgument} --print >> "$out"
''
}''
}
'';
};
# Clear oh-my-posh cache when the oh-my-posh package derivation changes
home.activation.ohMyPoshClearCache = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
set -eu
nixver="${config.programs.oh-my-posh.package}"
cache="${config.xdg.cacheHome}/oh-my-posh"
state="$cache/pkg-path"
if [ ! -f "$state" ] || [ "$(cat "$state")" != "$nixver" ]; then
rm -rf "$cache"
fi
mkdir -p "$cache"
printf '%s' "$nixver" > "$state"
'';
};
}