1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-21 09:19:47 +01:00

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. The standard installation method remains the same but uses the new command internally.
See <<sec-flakes-standalone>> for more. 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]] [[sec-release-23.05-state-version-changes]]
=== 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 { home.sessionPath = mkOption {
type = with types; listOf str; type = with types; listOf str;
default = [ ]; default = [ ];
@ -544,24 +553,22 @@ in
// //
(maybeSet "LC_MEASUREMENT" cfg.language.measurement); (maybeSet "LC_MEASUREMENT" cfg.language.measurement);
home.packages = [ # Provide a file holding all session variables.
# Provide a file holding all session variables. home.sessionVariablesPackage = pkgs.writeTextFile {
( name = "hm-session-vars.sh";
pkgs.writeTextFile { destination = "/etc/profile.d/hm-session-vars.sh";
name = "hm-session-vars.sh"; text = ''
destination = "/etc/profile.d/hm-session-vars.sh"; # Only source this once.
text = '' if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
# Only source this once. export __HM_SESS_VARS_SOURCED=1
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
${config.lib.shell.exportAll cfg.sessionVariables} ${config.lib.shell.exportAll cfg.sessionVariables}
'' + lib.optionalString (cfg.sessionPath != [ ]) '' '' + lib.optionalString (cfg.sessionPath != [ ]) ''
export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}" export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}"
'' + cfg.sessionVariablesExtra; '' + cfg.sessionVariablesExtra;
} };
)
]; home.packages = [ config.home.sessionVariablesPackage ];
# A dummy entry acting as a boundary between the activation # A dummy entry acting as a boundary between the activation
# script's "check" and the "write" phases. # script's "check" and the "write" phases.

View file

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