diff --git a/docs/manual/faq/session-variables.md b/docs/manual/faq/session-variables.md index 5c8586628..767275add 100644 --- a/docs/manual/faq/session-variables.md +++ b/docs/manual/faq/session-variables.md @@ -17,8 +17,11 @@ way. In Bash and Z shell this can be done by adding to your `.profile` and `.zshrc` files, respectively. The `hm-session-vars.sh` file should work in most Bourne-like shells. For fish shell, it is possible to source it using [the foreign-env -plugin](https://github.com/oh-my-fish/plugin-foreign-env) +plugin](https://github.com/oh-my-fish/plugin-foreign-env) or using the builtin +[babelfish](https://github.com/bouk/babelfish)-translated variables: ``` bash fenv source "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh" > /dev/null +# or +source "$HOME/.nix-profile/etc/profile.d/hm-session-vars.fish" ``` diff --git a/modules/programs/fish.nix b/modules/programs/fish.nix index 48ca7d1d6..8290f61e5 100644 --- a/modules/programs/fish.nix +++ b/modules/programs/fish.nix @@ -388,12 +388,14 @@ let passAsFile = [ "text" ]; } "env HOME=$(mktemp -d) fish_indent < $textPath > $out"; - translatedSessionVariables = pkgs.runCommandLocal "hm-session-vars.fish" { } '' + sessionVarsFile = "etc/profile.d/hm-session-vars.fish"; + sessionVarsPkg = pkgs.runCommandLocal "hm-session-vars.fish" { } '' + mkdir -p "$(dirname $out/${sessionVarsFile})" (echo "function setup_hm_session_vars;" ${pkgs.buildPackages.babelfish}/bin/babelfish \ - <${config.home.sessionVariablesPackage}/etc/profile.d/hm-session-vars.sh + <${config.home.sessionVariablesPackage}/etc/profile.d/hm-session-vars.sh echo "end" - echo "setup_hm_session_vars") > $out + echo "setup_hm_session_vars") > $out/${sessionVarsFile} ''; in @@ -591,11 +593,25 @@ in . ''; }; + + programs.fish.sessionVariablesPackage = mkOption { + type = types.package; + internal = true; + description = '' + The package containing the translated {file}`hm-session-vars.fish` file. + ''; + }; }; config = mkIf cfg.enable ( lib.mkMerge [ - { home.packages = [ cfg.package ]; } + { + home.packages = [ + cfg.package + cfg.sessionVariablesPackage + ]; + programs.fish.sessionVariablesPackage = sessionVarsPkg; + } (mkIf cfg.generateCompletions ( let @@ -708,7 +724,7 @@ in set -q __fish_home_manager_config_sourced; and exit set -g __fish_home_manager_config_sourced 1 - source ${translatedSessionVariables} + source ${cfg.sessionVariablesPackage}/${sessionVarsFile} ${cfg.shellInit} diff --git a/tests/modules/programs/fish/default.nix b/tests/modules/programs/fish/default.nix index 6ee6aa2a7..1e7353a0d 100644 --- a/tests/modules/programs/fish/default.nix +++ b/tests/modules/programs/fish/default.nix @@ -7,4 +7,5 @@ fish-plugins = ./plugins.nix; fish-manpage = ./manpage.nix; fish-binds = ./binds.nix; + fish-session-variables = ./session-variables.nix; } diff --git a/tests/modules/programs/fish/session-variables.nix b/tests/modules/programs/fish/session-variables.nix new file mode 100644 index 000000000..bd6e8fec6 --- /dev/null +++ b/tests/modules/programs/fish/session-variables.nix @@ -0,0 +1,20 @@ +{ config, ... }: + +{ + config = { + home.sessionVariables = { + V1 = "v1"; + V2 = "v2-${config.home.sessionVariables.V1}"; + }; + + programs.fish.enable = true; + + nmt.script = '' + assertFileExists home-path/etc/profile.d/hm-session-vars.fish + assertFileRegex home-path/etc/profile.d/hm-session-vars.fish \ + "set -gx V1 'v1'" + assertFileRegex home-path/etc/profile.d/hm-session-vars.fish \ + "set -gx V1 'v1'" + ''; + }; +}