8 cfg = config.programs.direnv;
18 options.programs.direnv = {
20 enable = lib.mkEnableOption ''
21 direnv integration. Takes care of both installation and
22 setting up the sourcing of the shell. Additionally enables nix-direnv
23 integration. Note that you need to logout and login for this change to apply
26 package = lib.mkPackageOption pkgs "direnv" { };
28 enableBashIntegration = enabledOption ''
31 enableZshIntegration = enabledOption ''
34 enableFishIntegration = enabledOption ''
38 direnvrcExtra = lib.mkOption {
39 type = lib.types.lines;
46 Extra lines to append to the sourced direnvrc
50 silent = lib.mkEnableOption ''
51 the hiding of direnv logging
54 loadInNixShell = enabledOption ''
55 loading direnv in `nix-shell` `nix shell` or `nix develop`
59 enable = enabledOption ''
60 a faster, persistent implementation of use_nix and use_flake, to replace the builtin one
63 package = lib.mkOption {
64 default = pkgs.nix-direnv.override { nix = config.nix.package; };
65 defaultText = "pkgs.nix-direnv";
66 type = lib.types.package;
68 The nix-direnv package to use
74 config = lib.mkIf cfg.enable {
77 zsh.interactiveShellInit = lib.mkIf cfg.enableZshIntegration ''
78 if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then
79 eval "$(${lib.getExe cfg.package} hook zsh)"
83 #$NIX_GCROOT for "nix develop" https://github.com/NixOS/nix/blob/6db66ebfc55769edd0c6bc70fcbd76246d4d26e0/src/nix/develop.cc#L530
84 #$IN_NIX_SHELL for "nix-shell"
85 bash.interactiveShellInit = lib.mkIf cfg.enableBashIntegration ''
86 if ${lib.boolToString cfg.loadInNixShell} || [ -z "$IN_NIX_SHELL$NIX_GCROOT$(printenv PATH | grep '/nix/store')" ] ; then
87 eval "$(${lib.getExe cfg.package} hook bash)"
91 fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration ''
92 if ${lib.boolToString cfg.loadInNixShell};
93 or printenv PATH | grep -vqc '/nix/store';
94 ${lib.getExe cfg.package} hook fish | source
101 # direnv has a fish library which automatically sources direnv for some reason
102 # I don't see any harm in doing this if we're sourcing it with fish.interactiveShellInit
104 inherit (cfg.package) name;
105 paths = [ cfg.package ];
107 rm -rf $out/share/fish
113 DIRENV_CONFIG = "/etc/direnv";
114 DIRENV_LOG_FORMAT = lib.mkIf cfg.silent "";
118 "direnv/direnvrc".text = ''
119 ${lib.optionalString cfg.nix-direnv.enable ''
121 source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc
127 #Load user-configuration if present (~/.direnvrc or ~/.config/direnv/direnvrc)
128 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
129 if [[ -f $direnv_config_dir_home/direnvrc ]]; then
130 source "$direnv_config_dir_home/direnvrc" >&2
131 elif [[ -f $HOME/.direnvrc ]]; then
132 source "$HOME/.direnvrc" >&2
135 unset direnv_config_dir_home
138 "direnv/lib/zz-user.sh".text = ''
139 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
141 for lib in "$direnv_config_dir_home/lib/"*.sh; do
145 unset direnv_config_dir_home
150 meta.maintainers = with lib.maintainers; [ gerg-l ];