1{
2 lib,
3 config,
4 pkgs,
5 ...
6}: let
7 cfg = config.programs.direnv;
8in {
9 options.programs.direnv = {
10
11 enable = lib.mkEnableOption ''
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
15 '';
16
17 package = lib.mkPackageOption pkgs "direnv" {};
18
19 direnvrcExtra = lib.mkOption {
20 type = lib.types.lines;
21 default = "";
22 example = ''
23 export FOO="foo"
24 echo "loaded direnv!"
25 '';
26 description = ''
27 Extra lines to append to the sourced direnvrc
28 '';
29 };
30
31 silent = lib.mkEnableOption ''
32 the hiding of direnv logging
33 '';
34
35 loadInNixShell =
36 lib.mkEnableOption ''
37 loading direnv in `nix-shell` `nix shell` or `nix develop`
38 ''
39 // {
40 default = true;
41 };
42
43 nix-direnv = {
44 enable =
45 (lib.mkEnableOption ''
46 a faster, persistent implementation of use_nix and use_flake, to replace the built-in one
47 '')
48 // {
49 default = true;
50 };
51
52 package = lib.mkOption {
53 default = pkgs.nix-direnv.override { nix = config.nix.package; };
54 defaultText = "pkgs.nix-direnv";
55 type = lib.types.package;
56 description = ''
57 The nix-direnv package to use
58 '';
59 };
60 };
61 };
62
63 imports = [
64 (lib.mkRemovedOptionModule ["programs" "direnv" "persistDerivations"] "persistDerivations was removed as it is no longer necessary")
65 ];
66
67 config = lib.mkIf cfg.enable {
68
69 programs = {
70 zsh.interactiveShellInit = ''
71 if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then
72 eval "$(${lib.getExe cfg.package} hook zsh)"
73 fi
74 '';
75
76 #$NIX_GCROOT for "nix develop" https://github.com/NixOS/nix/blob/6db66ebfc55769edd0c6bc70fcbd76246d4d26e0/src/nix/develop.cc#L530
77 #$IN_NIX_SHELL for "nix-shell"
78 bash.interactiveShellInit = ''
79 if ${lib.boolToString cfg.loadInNixShell} || [ -z "$IN_NIX_SHELL$NIX_GCROOT$(printenv PATH | grep '/nix/store')" ] ; then
80 eval "$(${lib.getExe cfg.package} hook bash)"
81 fi
82 '';
83
84 fish.interactiveShellInit = ''
85 if ${lib.boolToString cfg.loadInNixShell};
86 or printenv PATH | grep -vqc '/nix/store';
87 ${lib.getExe cfg.package} hook fish | source
88 end
89 '';
90 };
91
92 environment = {
93 systemPackages =
94 if cfg.loadInNixShell then [cfg.package]
95 else [
96 #direnv has a fish library which sources direnv for some reason
97 (cfg.package.overrideAttrs (old: {
98 installPhase =
99 (old.installPhase or "")
100 + ''
101 rm -rf $out/share/fish
102 '';
103 }))
104 ];
105
106 variables = {
107 DIRENV_CONFIG = "/etc/direnv";
108 DIRENV_LOG_FORMAT = lib.mkIf cfg.silent "";
109 };
110
111 etc = {
112 "direnv/direnvrc".text = ''
113 ${lib.optionalString cfg.nix-direnv.enable ''
114 #Load nix-direnv
115 source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc
116 ''}
117
118 #Load direnvrcExtra
119 ${cfg.direnvrcExtra}
120
121 #Load user-configuration if present (~/.direnvrc or ~/.config/direnv/direnvrc)
122 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
123 if [[ -f $direnv_config_dir_home/direnvrc ]]; then
124 source "$direnv_config_dir_home/direnvrc" >&2
125 elif [[ -f $HOME/.direnvrc ]]; then
126 source "$HOME/.direnvrc" >&2
127 fi
128
129 unset direnv_config_dir_home
130 '';
131
132 "direnv/lib/zz-user.sh".text = ''
133 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
134
135 for lib in "$direnv_config_dir_home/lib/"*.sh; do
136 source "$lib"
137 done
138
139 unset direnv_config_dir_home
140 '';
141 };
142 };
143 };
144}