typioca: 2.7.0 -> 2.8.0
[NixPkgs.git] / nixos / modules / programs / direnv.nix
blob2566fa7699bb50886ce4d7d820f247acab92d848
2   lib,
3   config,
4   pkgs,
5   ...
6 }: let
7   cfg = config.programs.direnv;
8 in {
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
15     '');
17     package = lib.mkPackageOptionMD pkgs "direnv" {};
19     direnvrcExtra = lib.mkOption {
20       type = lib.types.lines;
21       default = "";
22       example = ''
23         export FOO="foo"
24         echo "loaded direnv!"
25       '';
26       description = lib.mdDoc ''
27         Extra lines to append to the sourced direnvrc
28       '';
29     };
31     silent = lib.mkEnableOption (lib.mdDoc ''
32       the hiding of direnv logging
33     '');
35     loadInNixShell =
36       lib.mkEnableOption (lib.mdDoc ''
37         loading direnv in `nix-shell` `nix shell` or `nix develop`
38       '')
39       // {
40         default = true;
41       };
43     nix-direnv = {
44       enable =
45         (lib.mkEnableOption (lib.mdDoc ''
46           a faster, persistent implementation of use_nix and use_flake, to replace the built-in one
47         ''))
48         // {
49           default = true;
50         };
52       package = lib.mkPackageOptionMD pkgs "nix-direnv" {};
53     };
54   };
56   imports = [
57     (lib.mkRemovedOptionModule ["programs" "direnv" "persistDerivations"] "persistDerivations was removed as it is no longer necessary")
58   ];
60   config = lib.mkIf cfg.enable {
62     programs = {
63       zsh.interactiveShellInit = ''
64         if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then
65          eval "$(${lib.getExe cfg.package} hook zsh)"
66         fi
67       '';
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)"
74         fi
75       '';
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
81         end
82       '';
83     };
85     environment = {
86       systemPackages =
87         if cfg.loadInNixShell then [cfg.package]
88         else [
89           #direnv has a fish library which sources direnv for some reason
90           (cfg.package.overrideAttrs (old: {
91             installPhase =
92               (old.installPhase or "")
93               + ''
94                 rm -rf $out/share/fish
95               '';
96           }))
97         ];
99       variables = {
100         DIRENV_CONFIG = "/etc/direnv";
101         DIRENV_LOG_FORMAT = lib.mkIf cfg.silent "";
102       };
104       etc = {
105         "direnv/direnvrc".text = ''
106           ${lib.optionalString cfg.nix-direnv.enable ''
107             #Load nix-direnv
108             source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc
109           ''}
111            #Load direnvrcExtra
112            ${cfg.direnvrcExtra}
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
120            fi
122            unset direnv_config_dir_home
123         '';
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
129             source "$lib"
130           done
132           unset direnv_config_dir_home
133         '';
134       };
135     };
136   };