Skip to content

Commit

Permalink
fish: expose session variables package
Browse files Browse the repository at this point in the history
This allows fish users to source the `hm-session-vars.fish` if they are
not using the generated `config.fish` (which now sources the same file).
  • Loading branch information
ian-h-chamberlain committed Jan 8, 2025
1 parent 7e00856 commit b06023b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
36 changes: 25 additions & 11 deletions modules/programs/fish.nix
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,6 @@ let
passAsFile = [ "text" ];
} "env HOME=$(mktemp -d) fish_indent < $textPath > $out";

translatedSessionVariables =
pkgs.runCommandLocal "hm-session-vars.fish" { } ''
(echo "function setup_hm_session_vars;"
${pkgs.buildPackages.babelfish}/bin/babelfish \
<${config.home.sessionVariablesPackage}/etc/profile.d/hm-session-vars.sh
echo "end"
echo "setup_hm_session_vars") > $out
'';

in {
imports = [
(mkRemovedOptionModule [ "programs" "fish" "promptInit" ] ''
Expand Down Expand Up @@ -393,10 +384,33 @@ in {
<https://fishshell.com/docs/current/cmds/function.html>.
'';
};

# TODO: maybe this makes more sense as `programs.fish.sessionVariablesPackage`?
# if `programs.fish.sessionVariables` were added, would that need its own
# separate package, or would they just go in config.fish directly?
home.fishSessionVariablesPackage = mkOption {
type = types.package;
internal = true;
description = ''
The package containing the {file}`hm-session-vars.fish` file.
'';
};
};

config = mkIf cfg.enable (mkMerge [
{ home.packages = [ cfg.package ]; }
{
home.fishSessionVariablesPackage =
pkgs.runCommandLocal "hm-session-vars.fish" { } ''
mkdir -p $out/etc/profile.d
(echo "function setup_hm_session_vars;"
${pkgs.buildPackages.babelfish}/bin/babelfish \
<${config.home.sessionVariablesPackage}/etc/profile.d/hm-session-vars.sh
echo "end"
echo "setup_hm_session_vars"
) > $out/etc/profile.d/hm-session-vars.fish
'';

home.packages = [ cfg.package config.home.fishSessionVariablesPackage ];

(mkIf cfg.generateCompletions {
# Support completion for `man` by building a cache for `apropos`.
Expand Down Expand Up @@ -473,7 +487,7 @@ in {
set -q __fish_home_manager_config_sourced; and exit
set -g __fish_home_manager_config_sourced 1
source ${translatedSessionVariables}
source ${config.home.fishSessionVariablesPackage}/etc/profile.d/hm-session-vars.fish
${cfg.shellInit}
Expand Down
1 change: 1 addition & 0 deletions tests/modules/programs/fish/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
fish-functions = ./functions.nix;
fish-no-functions = ./no-functions.nix;
fish-plugins = ./plugins.nix;
fish-session-variables = ./session-variables.nix;
}
26 changes: 26 additions & 0 deletions tests/modules/programs/fish/session-variables.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ config, lib, pkgs, ... }:

with lib;

{
config = {
home.sessionVariables = {
V1 = "v1";
V2 = "v2-${config.home.sessionVariables.V1}";
};

programs.fish = {
enable = true;

# TODO: should there be an equivalent sessionVariables here like bash + zsh?
};

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'"
'';
};
}
2 changes: 1 addition & 1 deletion tests/modules/programs/zsh/session-variables.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

nmt.script = ''
assertFileExists home-files/.zshenv
assertFileRegex home-files/.zshenv 'export V1="v1"'
assertFileRegex home-files/.zshenv 'export V1="v2"'
assertFileRegex home-files/.zshenv 'export V2="v2-v1"'
'';
};
Expand Down

0 comments on commit b06023b

Please sign in to comment.