From c4f980a06346b0cc4811bcc61747d8ec1b9f925d Mon Sep 17 00:00:00 2001 From: Lily Ballard Date: Thu, 4 Feb 2021 16:26:53 -0800 Subject: [PATCH] fish: Add fishEnvPreInit option This new option allows for replacing the sourcing of /etc/fish/nixos-env-preinit.fish with another file, optionally passing it through `fenv`. The idea here is that non-NixOS users can do something like fish.override { fishEnvPreInit = sourceBash: sourceBash "${nix}/etc/profile.d/nix-daemon.sh"; } and this will set up their shell environment for Nix just as though they were running NixOS. --- pkgs/shells/fish/default.nix | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pkgs/shells/fish/default.nix b/pkgs/shells/fish/default.nix index a39a2fc740bd..7e38d9dec3a1 100644 --- a/pkgs/shells/fish/default.nix +++ b/pkgs/shells/fish/default.nix @@ -16,11 +16,21 @@ , ncurses , python3 , cmake +, fishPlugins , runCommand , writeText , nixosTests , useOperatingSystemEtc ? true + # An optional string containing Fish code that initializes the environment. + # This is run at the very beginning of initialization. If it sets $NIX_PROFILES + # then Fish will use that to configure its function, completion, and conf.d paths. + # For example: + # fishEnvPreInit = "source /etc/fish/my-env-preinit.fish"; + # It can also be a function that takes one argument, which is a function that + # takes a path to a bash file and converts it to fish. For example: + # fishEnvPreInit = source: source "${nix}/etc/profile.d/nix-daemon.sh"; +, fishEnvPreInit ? null }: let etcConfigAppendix = writeText "config.fish.appendix" '' @@ -62,8 +72,12 @@ let # 2. Before the shell is initialized, so that config snippets can find the commands they use on the PATH builtin status --is-login or test -z "$__fish_nixos_env_preinit_sourced" -a -z "$ETC_PROFILE_SOURCED" -a -z "$ETC_ZSHENV_SOURCED" + ${if fishEnvPreInit != null then '' + and begin + ${lib.removeSuffix "\n" (if lib.isFunction fishEnvPreInit then fishEnvPreInit sourceWithFenv else fishEnvPreInit)} + end'' else '' and test -f /etc/fish/nixos-env-preinit.fish - and source /etc/fish/nixos-env-preinit.fish + and source /etc/fish/nixos-env-preinit.fish''} and set -gx __fish_nixos_env_preinit_sourced 1 test -n "$NIX_PROFILES" @@ -94,6 +108,22 @@ let end ''; + # This is wrapped in begin/end in case the user wants to apply redirections. + # This does mean the basic usage of sourcing a single file will produce + # `begin; begin; …; end; end` but that's ok. + sourceWithFenv = path: '' + begin # fenv + # This happens before $__fish_datadir/config.fish sets fish_function_path, so it is currently + # unset. We set it and then completely erase it, leaving its configuration to $__fish_datadir/config.fish + set fish_function_path ${fishPlugins.foreign-env}/share/fish/vendor_functions.d $__fish_datadir/functions + fenv source ${lib.escapeShellArg path} + set -l fenv_status $status + # clear fish_function_path so that it will be correctly set when we return to $__fish_datadir/config.fish + set -e fish_function_path + test $fenv_status -eq 0 + end # fenv + ''; + fish = stdenv.mkDerivation rec { pname = "fish"; version = "3.1.2";