2023-07-15 17:15:38 +00:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
...
|
2024-07-31 10:19:44 +00:00
|
|
|
}:
|
|
|
|
let
|
2023-07-15 17:15:38 +00:00
|
|
|
cfg = config.programs.direnv;
|
2024-07-31 10:19:44 +00:00
|
|
|
enabledOption =
|
|
|
|
x:
|
|
|
|
lib.mkEnableOption x
|
|
|
|
// {
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2023-07-15 17:15:38 +00:00
|
|
|
options.programs.direnv = {
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = lib.mkEnableOption ''
|
2023-07-15 17:15:38 +00:00
|
|
|
direnv integration. Takes care of both installation and
|
|
|
|
setting up the sourcing of the shell. Additionally enables nix-direnv
|
2023-11-16 04:20:00 +00:00
|
|
|
integration. Note that you need to logout and login for this change to apply
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
|
2024-07-31 10:19:44 +00:00
|
|
|
package = lib.mkPackageOption pkgs "direnv" { };
|
|
|
|
|
|
|
|
enableBashIntegration = enabledOption ''
|
|
|
|
Bash integration
|
|
|
|
'';
|
|
|
|
enableZshIntegration = enabledOption ''
|
|
|
|
Zsh integration
|
|
|
|
'';
|
|
|
|
enableFishIntegration = enabledOption ''
|
|
|
|
Fish integration
|
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
direnvrcExtra = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
export FOO="foo"
|
|
|
|
echo "loaded direnv!"
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-07-15 17:15:38 +00:00
|
|
|
Extra lines to append to the sourced direnvrc
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
silent = lib.mkEnableOption ''
|
2023-07-15 17:15:38 +00:00
|
|
|
the hiding of direnv logging
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
|
2024-07-31 10:19:44 +00:00
|
|
|
loadInNixShell = enabledOption ''
|
|
|
|
loading direnv in `nix-shell` `nix shell` or `nix develop`
|
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
nix-direnv = {
|
2024-07-31 10:19:44 +00:00
|
|
|
enable = enabledOption ''
|
|
|
|
a faster, persistent implementation of use_nix and use_flake, to replace the builtin one
|
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = lib.mkOption {
|
|
|
|
default = pkgs.nix-direnv.override { nix = config.nix.package; };
|
|
|
|
defaultText = "pkgs.nix-direnv";
|
|
|
|
type = lib.types.package;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
The nix-direnv package to use
|
|
|
|
'';
|
|
|
|
};
|
2023-07-15 17:15:38 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
|
|
|
|
programs = {
|
2024-07-31 10:19:44 +00:00
|
|
|
zsh.interactiveShellInit = lib.mkIf cfg.enableZshIntegration ''
|
2023-07-15 17:15:38 +00:00
|
|
|
if ${lib.boolToString cfg.loadInNixShell} || printenv PATH | grep -vqc '/nix/store'; then
|
|
|
|
eval "$(${lib.getExe cfg.package} hook zsh)"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
#$NIX_GCROOT for "nix develop" https://github.com/NixOS/nix/blob/6db66ebfc55769edd0c6bc70fcbd76246d4d26e0/src/nix/develop.cc#L530
|
|
|
|
#$IN_NIX_SHELL for "nix-shell"
|
2024-07-31 10:19:44 +00:00
|
|
|
bash.interactiveShellInit = lib.mkIf cfg.enableBashIntegration ''
|
2023-07-15 17:15:38 +00:00
|
|
|
if ${lib.boolToString cfg.loadInNixShell} || [ -z "$IN_NIX_SHELL$NIX_GCROOT$(printenv PATH | grep '/nix/store')" ] ; then
|
|
|
|
eval "$(${lib.getExe cfg.package} hook bash)"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2024-07-31 10:19:44 +00:00
|
|
|
fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration ''
|
2023-07-15 17:15:38 +00:00
|
|
|
if ${lib.boolToString cfg.loadInNixShell};
|
|
|
|
or printenv PATH | grep -vqc '/nix/store';
|
|
|
|
${lib.getExe cfg.package} hook fish | source
|
|
|
|
end
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
environment = {
|
2024-07-31 10:19:44 +00:00
|
|
|
systemPackages = [
|
|
|
|
# direnv has a fish library which automatically sources direnv for some reason
|
|
|
|
# I don't see any harm in doing this if we're sourcing it with fish.interactiveShellInit
|
|
|
|
(pkgs.symlinkJoin {
|
|
|
|
inherit (cfg.package) name;
|
|
|
|
paths = [ cfg.package ];
|
|
|
|
postBuild = ''
|
|
|
|
rm -rf $out/share/fish
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
];
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
variables = {
|
|
|
|
DIRENV_CONFIG = "/etc/direnv";
|
|
|
|
DIRENV_LOG_FORMAT = lib.mkIf cfg.silent "";
|
|
|
|
};
|
|
|
|
|
|
|
|
etc = {
|
|
|
|
"direnv/direnvrc".text = ''
|
|
|
|
${lib.optionalString cfg.nix-direnv.enable ''
|
|
|
|
#Load nix-direnv
|
|
|
|
source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc
|
|
|
|
''}
|
|
|
|
|
|
|
|
#Load direnvrcExtra
|
|
|
|
${cfg.direnvrcExtra}
|
|
|
|
|
|
|
|
#Load user-configuration if present (~/.direnvrc or ~/.config/direnv/direnvrc)
|
|
|
|
direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
|
|
|
|
if [[ -f $direnv_config_dir_home/direnvrc ]]; then
|
|
|
|
source "$direnv_config_dir_home/direnvrc" >&2
|
|
|
|
elif [[ -f $HOME/.direnvrc ]]; then
|
|
|
|
source "$HOME/.direnvrc" >&2
|
|
|
|
fi
|
|
|
|
|
|
|
|
unset direnv_config_dir_home
|
|
|
|
'';
|
|
|
|
|
|
|
|
"direnv/lib/zz-user.sh".text = ''
|
|
|
|
direnv_config_dir_home="''${DIRENV_CONFIG_HOME:-''${XDG_CONFIG_HOME:-$HOME/.config}/direnv}"
|
|
|
|
|
|
|
|
for lib in "$direnv_config_dir_home/lib/"*.sh; do
|
|
|
|
source "$lib"
|
|
|
|
done
|
|
|
|
|
|
|
|
unset direnv_config_dir_home
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-07-31 10:19:44 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ gerg-l ];
|
2023-07-15 17:15:38 +00:00
|
|
|
}
|