at 25.11-pre 5.6 kB view raw
1{ 2 lib, 3 config, 4 pkgs, 5 ... 6}: 7let 8 cfg = config.programs.direnv; 9 enabledOption = 10 x: 11 lib.mkEnableOption x 12 // { 13 default = true; 14 example = false; 15 }; 16 format = pkgs.formats.toml { }; 17in 18{ 19 options.programs.direnv = { 20 21 enable = lib.mkEnableOption '' 22 direnv integration. Takes care of both installation and 23 setting up the sourcing of the shell. Additionally enables nix-direnv 24 integration. Note that you need to logout and login for this change to apply 25 ''; 26 27 package = lib.mkPackageOption pkgs "direnv" { }; 28 29 enableBashIntegration = enabledOption '' 30 Bash integration 31 ''; 32 enableZshIntegration = enabledOption '' 33 Zsh integration 34 ''; 35 enableFishIntegration = enabledOption '' 36 Fish integration 37 ''; 38 enableXonshIntegration = enabledOption '' 39 Xonsh integration 40 ''; 41 42 direnvrcExtra = lib.mkOption { 43 type = lib.types.lines; 44 default = ""; 45 example = '' 46 export FOO="foo" 47 echo "loaded direnv!" 48 ''; 49 description = '' 50 Extra lines to append to the sourced direnvrc 51 ''; 52 }; 53 54 silent = lib.mkEnableOption '' 55 the hiding of direnv logging 56 ''; 57 58 loadInNixShell = enabledOption '' 59 loading direnv in `nix-shell` `nix shell` or `nix develop` 60 ''; 61 62 nix-direnv = { 63 enable = enabledOption '' 64 a faster, persistent implementation of use_nix and use_flake, to replace the builtin one 65 ''; 66 67 package = lib.mkOption { 68 default = pkgs.nix-direnv.override { nix = config.nix.package; }; 69 defaultText = "pkgs.nix-direnv"; 70 type = lib.types.package; 71 description = '' 72 The nix-direnv package to use 73 ''; 74 }; 75 }; 76 77 settings = lib.mkOption { 78 inherit (format) type; 79 default = { }; 80 example = lib.literalExpression '' 81 { 82 global = { 83 log_format = "-"; 84 log_filter = "^$"; 85 }; 86 } 87 ''; 88 description = '' 89 Direnv configuration. Refer to {manpage}`direnv.toml(1)`. 90 ''; 91 }; 92 }; 93 94 config = lib.mkIf cfg.enable { 95 96 programs = { 97 direnv.settings = lib.mkIf cfg.silent { 98 global = { 99 log_format = lib.mkDefault "-"; 100 log_filter = lib.mkDefault "^$"; 101 }; 102 }; 103 104 zsh.interactiveShellInit = lib.mkIf cfg.enableZshIntegration '' 105 if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then 106 eval "$(${lib.getExe cfg.package} hook zsh)" 107 fi 108 ''; 109 110 #$NIX_GCROOT for "nix develop" https://github.com/NixOS/nix/blob/6db66ebfc55769edd0c6bc70fcbd76246d4d26e0/src/nix/develop.cc#L530 111 #$IN_NIX_SHELL for "nix-shell" 112 bash.interactiveShellInit = lib.mkIf cfg.enableBashIntegration '' 113 if ${lib.boolToString cfg.loadInNixShell} || [ -z "$IN_NIX_SHELL$NIX_GCROOT$(printenv PATH | grep '/nix/store')" ] ; then 114 eval "$(${lib.getExe cfg.package} hook bash)" 115 fi 116 ''; 117 118 fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration '' 119 if ${lib.boolToString cfg.loadInNixShell}; 120 or printenv PATH | grep -vqc '/nix/store'; 121 ${lib.getExe cfg.package} hook fish | source 122 end 123 ''; 124 125 xonsh = lib.mkIf cfg.enableXonshIntegration { 126 extraPackages = ps: [ ps.xonsh.xontribs.xonsh-direnv ]; 127 config = '' 128 if ${ 129 if cfg.loadInNixShell then 130 "True" 131 else 132 "not any(map(lambda s: s.startswith('/nix/store'), __xonsh__.env.get('PATH')))" 133 }: 134 xontrib load direnv 135 ''; 136 }; 137 }; 138 139 environment = { 140 systemPackages = [ 141 # direnv has a fish library which automatically sources direnv for some reason 142 # I don't see any harm in doing this if we're sourcing it with fish.interactiveShellInit 143 (pkgs.symlinkJoin { 144 inherit (cfg.package) name; 145 paths = [ cfg.package ]; 146 nativeBuildInputs = [ pkgs.makeBinaryWrapper ]; 147 postBuild = '' 148 wrapProgram "$out/bin/direnv" \ 149 --set-default 'DIRENV_CONFIG' '/etc/direnv' 150 rm -rf "$out/share/fish" 151 ''; 152 }) 153 ]; 154 155 etc = { 156 "direnv/direnv.toml" = lib.mkIf (cfg.settings != { }) { 157 source = format.generate "direnv.toml" cfg.settings; 158 }; 159 "direnv/direnvrc".text = '' 160 ${lib.optionalString cfg.nix-direnv.enable '' 161 #Load nix-direnv 162 source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc 163 ''} 164 165 #Load direnvrcExtra 166 ${cfg.direnvrcExtra} 167 168 #Load user-configuration if present (~/.direnvrc or ~/.config/direnv/direnvrc) 169 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}" 170 if [[ -f $direnv_config_dir_home/direnvrc ]]; then 171 source "$direnv_config_dir_home/direnvrc" >&2 172 elif [[ -f $HOME/.direnvrc ]]; then 173 source "$HOME/.direnvrc" >&2 174 fi 175 176 unset direnv_config_dir_home 177 ''; 178 179 "direnv/lib/zz-user.sh".text = '' 180 direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}" 181 182 for lib in "$direnv_config_dir_home/lib/"*.sh; do 183 source "$lib" 184 done 185 186 unset direnv_config_dir_home 187 ''; 188 }; 189 }; 190 }; 191 meta.maintainers = with lib.maintainers; [ gerg-l ]; 192}