summary refs log tree commit diff
path: root/pkgs/shells
diff options
context:
space:
mode:
authorLily Ballard <lily@sb.org>2021-02-04 16:26:53 -0800
committerCole Helbling <cole.e.helbling@outlook.com>2021-02-04 19:05:22 -0800
commitc4f980a06346b0cc4811bcc61747d8ec1b9f925d (patch)
treed320581246069601ba2e910b62a85c862879d12c /pkgs/shells
parentdca20136ca3c80291a82c5dbb921cf2e609d5fb2 (diff)
downloadnixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.tar
nixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.tar.gz
nixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.tar.bz2
nixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.tar.lz
nixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.tar.xz
nixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.tar.zst
nixpkgs-c4f980a06346b0cc4811bcc61747d8ec1b9f925d.zip
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.
Diffstat (limited to 'pkgs/shells')
-rw-r--r--pkgs/shells/fish/default.nix32
1 files changed, 31 insertions, 1 deletions
diff --git a/pkgs/shells/fish/default.nix b/pkgs/shells/fish/default.nix
index a39a2fc740b..7e38d9dec3a 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";