fork/home-manager
git clone https://git.julia.blue/fork/home-manager
4f02e35f
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . direnv ; 8 9tomlFormat = pkgs . formats . toml { }; 10 11in { 12imports = [ 13( mkRenamedOptionModule [ 14"programs" 15"direnv" 16"enableNixDirenvIntegration" 17] [ "programs" "direnv" "nix-direnv" "enable" ]) 18( mkRemovedOptionModule [ "programs" "direnv" "nix-direnv" "enableFlakes" ] 19"Flake support is now always enabled." ) 20]; 21 22meta . maintainers = [ maintainers . rycee ]; 23 24options . programs . direnv = { 25enable = mkEnableOption "direnv, the environment switcher" ; 26 27package = mkPackageOption pkgs "direnv" { }; 28 29config = mkOption { 30type = tomlFormat . type ; 31default = { }; 32description = '' 33Configuration written to 34{file}`$XDG_CONFIG_HOME/direnv/direnv.toml`. 35 36See 37{manpage}`direnv.toml(1)`. 38for the full list of options. 39'' ; 40}; 41 42stdlib = mkOption { 43type = types . lines ; 44default = "" ; 45description = '' 46Custom stdlib written to 47{file}`$XDG_CONFIG_HOME/direnv/direnvrc`. 48'' ; 49}; 50 51enableBashIntegration = mkOption { 52default = true ; 53type = types . bool ; 54description = '' 55Whether to enable Bash integration. 56'' ; 57}; 58 59enableZshIntegration = mkOption { 60default = true ; 61type = types . bool ; 62description = '' 63Whether to enable Zsh integration. 64'' ; 65}; 66 67enableFishIntegration = mkOption { 68default = true ; 69type = types . bool ; 70readOnly = true ; 71description = '' 72Whether to enable Fish integration. Note, enabling the direnv module 73will always active its functionality for Fish since the direnv package 74automatically gets loaded in Fish. If this is not the case try adding 75```nix 76environment.pathsToLink = [ "/share/fish" ]; 77``` 78to the system configuration. 79'' ; 80}; 81 82enableNushellIntegration = mkOption { 83default = true ; 84type = types . bool ; 85description = '' 86Whether to enable Nushell integration. 87'' ; 88}; 89 90nix-direnv = { 91enable = mkEnableOption '' 92[nix-direnv](https://github.com/nix-community/nix-direnv), 93a fast, persistent use_nix implementation for direnv'' ; 94 95package = mkPackageOption pkgs "nix-direnv" { }; 96}; 97 98}; 99 100config = mkIf cfg . enable { 101home . packages = [ cfg . package ]; 102 103xdg . configFile . "direnv/direnv.toml" = mkIf ( cfg . config != { }) { 104source = tomlFormat . generate "direnv-config" cfg . config ; 105}; 106 107xdg . configFile . "direnv/direnvrc" = let 108text = concatStringsSep "\n" ( optional ( cfg . stdlib != "" ) cfg . stdlib 109++ optional cfg . nix-direnv . enable 110"source ${ cfg . nix-direnv . package } /share/nix-direnv/direnvrc" ); 111in mkIf ( text != "" ) { inherit text ; }; 112 113programs . bash . initExtra = mkIf cfg . enableBashIntegration ( 114# Using mkAfter to make it more likely to appear after other 115# manipulations of the prompt. 116mkAfter '' 117eval "$( ${ cfg . package } /bin/direnv hook bash)" 118'' ); 119 120programs . zsh . initExtra = mkIf cfg . enableZshIntegration '' 121eval "$( ${ cfg . package } /bin/direnv hook zsh)" 122'' ; 123 124programs . fish . interactiveShellInit = mkIf cfg . enableFishIntegration ( 125# Using mkAfter to make it more likely to appear after other 126# manipulations of the prompt. 127mkAfter '' 128${ cfg . package } /bin/direnv hook fish | source 129'' ); 130 131programs . nushell . extraConfig = mkIf cfg . enableNushellIntegration ( 132# Using mkAfter to make it more likely to appear after other 133# manipulations of the prompt. 134mkAfter '' 135$env.config = ($env | default {} config).config 136$env.config = ($env.config | default {} hooks) 137$env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt)) 138$env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append { 139code: " 140let direnv = ( ${ cfg . package } /bin/direnv export json | from json) 141let direnv = if not ($direnv | is-empty) { $direnv } else { {} } 142$direnv | load-env 143" 144})) 145'' ); 146}; 147}