From 53ccbe017079d5fba2b605cb9f9584629bebd03a Mon Sep 17 00:00:00 2001 From: Emily Date: Wed, 31 May 2023 23:01:27 +0100 Subject: [PATCH] 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. --- docs/release-notes/rl-2305.adoc | 8 +++++++ modules/home-environment.nix | 41 +++++++++++++++++++-------------- modules/programs/fish.nix | 11 ++++++--- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/docs/release-notes/rl-2305.adoc b/docs/release-notes/rl-2305.adoc index b4491bb6..d919478d 100644 --- a/docs/release-notes/rl-2305.adoc +++ b/docs/release-notes/rl-2305.adoc @@ -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 <> for more. +* When using <>, the setup code +for <> 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 diff --git a/modules/home-environment.nix b/modules/home-environment.nix index 9abf223b..87dea106 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -291,6 +291,15 @@ in ''; }; + home.sessionVariablesPackage = mkOption { + type = types.package; + internal = true; + description = '' + The package containing the + hm-session-vars.sh 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. diff --git a/modules/programs/fish.nix b/modules/programs/fish.nix index 39dc246c..4d7598a4 100644 --- a/modules/programs/fish.nix +++ b/modules/programs/fish.nix @@ -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}