fish: use babelfish for hm-session-vars.sh (#4012)

* home-environment: add `home.sessionVariablesPackage`

Allow the `hm-session-vars.sh` derivation to be referenced from other
modules, e.g. to translate it to fish with babelfish at build time.

* fish: use babelfish for `hm-session-vars.sh`

Translate `hm-session-vars.sh` to fish at system build time,
significantly decreasing shell startup time.

Based on https://github.com/NixOS/nixpkgs/pull/108947 by @kevingriffin.
This commit is contained in:
Emily 2023-05-31 23:01:27 +01:00 committed by GitHub
parent f1490b8caf
commit 53ccbe0170
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 20 deletions

View file

@ -38,6 +38,14 @@ with Nix flakes uses this new command.
The standard installation method remains the same but uses the new command internally.
See <<sec-flakes-standalone>> for more.
* When using <<opt-programs.fish.enable>>, the setup code
for <<opt-home.sessionVariables>> is now translated
with https://github.com/bouk/babelfish[babelfish].
This should result in significantly faster shell startup times
but could theoretically break
if you have very complex bash expressions in a session variable.
Please report any issues you experience.
[[sec-release-23.05-state-version-changes]]
=== State Version Changes

View file

@ -291,6 +291,15 @@ in
'';
};
home.sessionVariablesPackage = mkOption {
type = types.package;
internal = true;
description = ''
The package containing the
<filename>hm-session-vars.sh</filename> file.
'';
};
home.sessionPath = mkOption {
type = with types; listOf str;
default = [ ];
@ -544,24 +553,22 @@ in
//
(maybeSet "LC_MEASUREMENT" cfg.language.measurement);
home.packages = [
# Provide a file holding all session variables.
(
pkgs.writeTextFile {
name = "hm-session-vars.sh";
destination = "/etc/profile.d/hm-session-vars.sh";
text = ''
# Only source this once.
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
# Provide a file holding all session variables.
home.sessionVariablesPackage = pkgs.writeTextFile {
name = "hm-session-vars.sh";
destination = "/etc/profile.d/hm-session-vars.sh";
text = ''
# Only source this once.
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
${config.lib.shell.exportAll cfg.sessionVariables}
'' + lib.optionalString (cfg.sessionPath != [ ]) ''
export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}"
'' + cfg.sessionVariablesExtra;
}
)
];
${config.lib.shell.exportAll cfg.sessionVariables}
'' + lib.optionalString (cfg.sessionPath != [ ]) ''
export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}"
'' + cfg.sessionVariablesExtra;
};
home.packages = [ config.home.sessionVariablesPackage ];
# A dummy entry acting as a boundary between the activation
# script's "check" and the "write" phases.

View file

@ -152,6 +152,13 @@ let
passAsFile = [ "text" ];
} "env HOME=$(mktemp -d) fish_indent < $textPath > $out";
translatedSessionVariables =
pkgs.runCommandLocal "hm-session-vars.fish" { } ''
${pkgs.babelfish}/bin/babelfish \
<${config.home.sessionVariablesPackage}/etc/profile.d/hm-session-vars.sh \
>$out
'';
in {
imports = [
(mkRemovedOptionModule [ "programs" "fish" "promptInit" ] ''
@ -354,9 +361,7 @@ in {
set -q __fish_home_manager_config_sourced; and exit
set -g __fish_home_manager_config_sourced 1
set --prepend fish_function_path ${pkgs.fishPlugins.foreign-env}/share/fish/vendor_functions.d
fenv source ${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh > /dev/null
set -e fish_function_path[1]
source ${translatedSessionVariables}
${cfg.shellInit}