Merge pull request #241528 from Gerg-L/nixos/direnvrc

nixos/direnv: init

authored by Pol Dellaiera and committed by GitHub f3d31474 bf5f8aa2

+148
+1
nixos/modules/module-list.nix
··· 160 160 ./programs/darling.nix 161 161 ./programs/dconf.nix 162 162 ./programs/digitalbitbox/default.nix 163 + ./programs/direnv.nix 163 164 ./programs/dmrconfig.nix 164 165 ./programs/droidcam.nix 165 166 ./programs/environment.nix
+147
nixos/modules/programs/direnv.nix
··· 1 + { 2 + lib, 3 + config, 4 + pkgs, 5 + ... 6 + }: let 7 + cfg = config.programs.direnv; 8 + in { 9 + options.programs.direnv = { 10 + 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 + ''); 16 + 17 + package = lib.mkPackageOptionMD 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 = lib.mdDoc '' 27 + Extra lines to append to the sourced direnvrc 28 + ''; 29 + }; 30 + 31 + silent = lib.mkEnableOption (lib.mdDoc '' 32 + the hiding of direnv logging 33 + ''); 34 + 35 + persistDerivations = 36 + (lib.mkEnableOption (lib.mdDoc '' 37 + setting keep-derivations and keep-outputs to true 38 + to prevent shells from getting garbage collected 39 + '')) 40 + // { 41 + default = true; 42 + }; 43 + 44 + loadInNixShell = 45 + lib.mkEnableOption (lib.mdDoc '' 46 + loading direnv in `nix-shell` `nix shell` or `nix develop` 47 + '') 48 + // { 49 + default = true; 50 + }; 51 + 52 + nix-direnv = { 53 + enable = 54 + (lib.mkEnableOption (lib.mdDoc '' 55 + a faster, persistent implementation of use_nix and use_flake, to replace the built-in one 56 + '')) 57 + // { 58 + default = true; 59 + }; 60 + 61 + package = lib.mkPackageOptionMD pkgs "nix-direnv" {}; 62 + }; 63 + }; 64 + 65 + config = lib.mkIf cfg.enable { 66 + 67 + programs = { 68 + zsh.interactiveShellInit = '' 69 + if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then 70 + eval "$(${lib.getExe cfg.package} hook zsh)" 71 + fi 72 + ''; 73 + 74 + #$NIX_GCROOT for "nix develop" https://github.com/NixOS/nix/blob/6db66ebfc55769edd0c6bc70fcbd76246d4d26e0/src/nix/develop.cc#L530 75 + #$IN_NIX_SHELL for "nix-shell" 76 + bash.interactiveShellInit = '' 77 + if ${lib.boolToString cfg.loadInNixShell} || [ -z "$IN_NIX_SHELL$NIX_GCROOT$(printenv PATH | grep '/nix/store')" ] ; then 78 + eval "$(${lib.getExe cfg.package} hook bash)" 79 + fi 80 + ''; 81 + 82 + fish.interactiveShellInit = '' 83 + if ${lib.boolToString cfg.loadInNixShell}; 84 + or printenv PATH | grep -vqc '/nix/store'; 85 + ${lib.getExe cfg.package} hook fish | source 86 + end 87 + ''; 88 + }; 89 + 90 + nix.settings = lib.mkIf cfg.persistDerivations { 91 + keep-outputs = true; 92 + keep-derivations = true; 93 + }; 94 + 95 + environment = { 96 + systemPackages = 97 + if cfg.loadInNixShell then [cfg.package] 98 + else [ 99 + #direnv has a fish library which sources direnv for some reason 100 + (cfg.package.overrideAttrs (old: { 101 + installPhase = 102 + (old.installPhase or "") 103 + + '' 104 + rm -rf $out/share/fish 105 + ''; 106 + })) 107 + ]; 108 + 109 + variables = { 110 + DIRENV_CONFIG = "/etc/direnv"; 111 + DIRENV_LOG_FORMAT = lib.mkIf cfg.silent ""; 112 + }; 113 + 114 + etc = { 115 + "direnv/direnvrc".text = '' 116 + ${lib.optionalString cfg.nix-direnv.enable '' 117 + #Load nix-direnv 118 + source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc 119 + ''} 120 + 121 + #Load direnvrcExtra 122 + ${cfg.direnvrcExtra} 123 + 124 + #Load user-configuration if present (~/.direnvrc or ~/.config/direnv/direnvrc) 125 + direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}" 126 + if [[ -f $direnv_config_dir_home/direnvrc ]]; then 127 + source "$direnv_config_dir_home/direnvrc" >&2 128 + elif [[ -f $HOME/.direnvrc ]]; then 129 + source "$HOME/.direnvrc" >&2 130 + fi 131 + 132 + unset direnv_config_dir_home 133 + ''; 134 + 135 + "direnv/lib/zz-user.sh".text = '' 136 + direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}" 137 + 138 + for lib in "$direnv_config_dir_home/lib/"*.sh; do 139 + source "$lib" 140 + done 141 + 142 + unset direnv_config_dir_home 143 + ''; 144 + }; 145 + }; 146 + }; 147 + }