lilyball / nix-env.fish

Nix environment setup for the fish shell
The Unlicense
278 stars 15 forks source link

Sourcing home-manager env vars #5

Closed ricardocorrea-sol closed 3 years ago

ricardocorrea-sol commented 3 years ago

Hi,

I just discovered your project today looking for options to manage my configs a bit more tightly with home-manager. It helped me solved a good portion of the issues I was solving with direnv, so thanks! One thing that I'm looking at solving is sourcing /.nix-profile/etc/profile.d/hm-session-vars.sh which is generated by home-manager and contains bash style variables that are set and exported for the user when set in their (home-manager) config.

A small example:

home.sessionVariables = {
  BAT_THEME = "Dracula";
};

Would create a file /.nix-profile/etc/profile.d/hm-session-vars.sh with contents of:

# Only source this once.
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1

export BAT_THEME="Dracula"

Is it possible to enable that in this project or should I seek for help in home-manager?

Thanks!

lilyball commented 3 years ago

I haven't actually used Home Manager yet (it's on my long list of TODOs), but Nix does have a package fishPlugins.foreign-env that gives you an fenv function you can use to perform bash commands and then apply any environment changes back to fish. This is used by the NixOS programs.fish module when it writes out /etc/fish/config.fish and /etc/fish/nixos-env-preinit.fish.

Having said that, it looks like Home Manager also has its own programs.fish module. It's not identical to NixOS, but it generates ~/.config/fish/config.fish for you and uses fenv there to source the hm-session-vars.sh, as well as adding completions generated by Home Manager. My impression is Home Manager doesn't touch system setup, and so you may still need nix-env.fish when using Home Manager.

Just from looking at the sources here, I think you can do this like

{
  programs.fish = {
    enable = true;
    plugins = [
      {
        name = "nix-env";
        src = pkgs.fetchFromGitHub {
          owner = "lilyball";
          repo = "nix-env.fish";
          rev = "a3c55307dce38c73485eac1f654c8f392341bda2";
          sha256 = lib.fakeSha256; # fill this in
        };
      }
    ];
  };
}

This will take over your ~/.config/fish/config.fish though.

lilyball commented 3 years ago

If you wanted to do this manually instead of using programs.fish you could probably do something like

{
  xdg.configFile."fish/conf.d/hm-session-vars.fish".text = ''
    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]
  '';
}

The downside here being the session vars aren't loaded until after the initial configuration.

ricardocorrea-sol commented 3 years ago

@lilyball I should've read the sources before I even posted, just installing fish-foreign-env does the trick:

https://github.com/nix-community/home-manager/blob/87e2ec341bfda373d50ab58529b4bbb0eb9eb9a0/modules/programs/fish.nix#L350

That + nix-env.fish + any-nix-shell do a lot to help fish get to the same level that zsh/bash enjoy in Nix.

In any case, many thanks for the comments, that made me look at the fish module in home-manager and figure this out!

(I consider the issue closed BTW).

lilyball commented 3 years ago

Glad to hear it!

Also, I hadn’t heard of any-nix-shell before. That looks really useful!