Files
nix/hosts/common/configs/user/console/nix-direnv/default.nix
Nikolaos Karaolidis 6b366fc24a Re-add flake-utils
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
2025-01-15 11:07:26 +00:00

80 lines
2.0 KiB
Nix

{
user ? throw "user argument is required",
home ? throw "home argument is required",
}:
{
lib,
inputs,
system,
...
}:
{
home-manager.users.${user} = {
programs = {
direnv = {
enable = true;
silent = true;
nix-direnv.enable = true;
enableZshIntegration = true;
};
zsh = {
shellAliases.nde = "nix-direnv";
initExtra =
let
devShells = lib.strings.concatStringsSep " " (
lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells.${system}
);
in
''
nix-direnv() {
if [ -z "$1" ]; then
echo "use flake" > .envrc
else
echo "use flake self#$1" > .envrc
fi
if git rev-parse --is-inside-work-tree &> /dev/null && ! grep -q "^\.envrc$" .gitignore .git/info/exclude; then
echo "Do you want to hide the .envrc file from git? (y/N)"
read -r answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
echo ".envrc" >> .git/info/exclude
fi
fi
direnv allow
}
_nix-direnv_completion() {
local shells=(${devShells})
compadd -- $shells
}
compdef _nix-direnv_completion nix-direnv
'';
p10k.extraRightPromptElements = [ "direnv" ];
};
};
# https://github.com/direnv/direnv/wiki/Customizing-cache-location
xdg.configFile."direnv/direnvrc".text = ''
declare -A direnv_layout_dirs
direnv_layout_dir() {
local hash path
echo "''${direnv_layout_dirs[$PWD]:=$(
hash="$(sha1sum - <<< "$PWD" | head -c40)"
path="''${PWD//[^a-zA-Z0-9]/-}"
echo "${home}/.cache/direnv/layouts/''${hash}''${path}"
)}"
}
'';
};
environment.persistence = {
"/persist"."${home}/.local/share/direnv/allow" = { };
"/cache"."${home}/.cache/direnv" = { };
};
}