7 cfg = config.programs.direnv;
9 options.programs.direnv = {
11 enable = lib.mkEnableOption (lib.mdDoc ''
12 direnv integration. Takes care of both installation and
13 setting up the sourcing of the shell. Additionally enables nix-direnv
14 integration. Note that you need to logout and login for this change to apply
17 package = lib.mkPackageOptionMD pkgs "direnv" {};
19 direnvrcExtra = lib.mkOption {
20 type = lib.types.lines;
26 description = lib.mdDoc ''
27 Extra lines to append to the sourced direnvrc
31 silent = lib.mkEnableOption (lib.mdDoc ''
32 the hiding of direnv logging
36 lib.mkEnableOption (lib.mdDoc ''
37 loading direnv in `nix-shell` `nix shell` or `nix develop`
45 (lib.mkEnableOption (lib.mdDoc ''
46 a faster, persistent implementation of use_nix and use_flake, to replace the built-in one
52 package = lib.mkPackageOptionMD pkgs "nix-direnv" {};
57 (lib.mkRemovedOptionModule ["programs" "direnv" "persistDerivations"] "persistDerivations was removed as it is no longer necessary")
60 config = lib.mkIf cfg.enable {
63 zsh.interactiveShellInit = ''
64 if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then
65 eval "$(${lib.getExe cfg.package} hook zsh)"
69 #$NIX_GCROOT for "nix develop" https://github.com/NixOS/nix/blob/6db66ebfc55769edd0c6bc70fcbd76246d4d26e0/src/nix/develop.cc#L530
70 #$IN_NIX_SHELL for "nix-shell"
71 bash.interactiveShellInit = ''
72 if ${lib.boolToString cfg.loadInNixShell} || [ -z "$IN_NIX_SHELL$NIX_GCROOT$(printenv PATH | grep '/nix/store')" ] ; then
73 eval "$(${lib.getExe cfg.package} hook bash)"
77 fish.interactiveShellInit = ''
78 if ${lib.boolToString cfg.loadInNixShell};
79 or printenv PATH | grep -vqc '/nix/store';
80 ${lib.getExe cfg.package} hook fish | source
87 if cfg.loadInNixShell then [cfg.package]
89 #direnv has a fish library which sources direnv for some reason
90 (cfg.package.overrideAttrs (old: {
92 (old.installPhase or "")
94 rm -rf $out/share/fish
100 DIRENV_CONFIG = "/etc/direnv";
101 DIRENV_LOG_FORMAT = lib.mkIf cfg.silent "";
105 "direnv/direnvrc".text = ''
106 ${lib.optionalString cfg.nix-direnv.enable ''
108 source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc
114 #Load user-configuration if present (~/.direnvrc or ~/.config/direnv/direnvrc)
115 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
116 if [[ -f $direnv_config_dir_home/direnvrc ]]; then
117 source "$direnv_config_dir_home/direnvrc" >&2
118 elif [[ -f $HOME/.direnvrc ]]; then
119 source "$HOME/.direnvrc" >&2
122 unset direnv_config_dir_home
125 "direnv/lib/zz-user.sh".text = ''
126 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
128 for lib in "$direnv_config_dir_home/lib/"*.sh; do
132 unset direnv_config_dir_home