mirror of
https://github.com/nix-community/nix-direnv.git
synced 2025-11-08 19:46:11 +01:00
Merge pull request #35 from nix-community/cleanup-1
This commit is contained in:
commit
892015898f
2 changed files with 56 additions and 66 deletions
27
README.md
27
README.md
|
|
@ -172,6 +172,15 @@ pwd_hash=$(echo -n $PWD | shasum | cut -d ' ' -f 1)
|
||||||
direnv_layout_dir=$XDG_CACHE_HOME/direnv/layouts/$pwd_hash
|
direnv_layout_dir=$XDG_CACHE_HOME/direnv/layouts/$pwd_hash
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Manually re-triggering evaluation
|
||||||
|
|
||||||
|
In some case nix-direnv does not detect if imported file has changed and still provides
|
||||||
|
the old cached values. An evaluation can be triggered by updating your `.envrc`:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ touch .envrc
|
||||||
|
```
|
||||||
|
|
||||||
## Known Bugs
|
## Known Bugs
|
||||||
|
|
||||||
At the moment `nix-direnv` depends on GNU Grep and a modern Bash version.
|
At the moment `nix-direnv` depends on GNU Grep and a modern Bash version.
|
||||||
|
|
@ -180,11 +189,13 @@ As a work-around we suggest that macOS users install `direnv`/`grep` via Nix or
|
||||||
|
|
||||||
## Why not use `lorri` instead?
|
## Why not use `lorri` instead?
|
||||||
|
|
||||||
Lorri causes large CPU load when nixpkgs in `NIX_PATH` is pointed to a directory, e.g. a
|
- nix-direnv has flakes support.
|
||||||
git checkout. This is because it tries to watch all referenced Nix files and
|
- High CPU load/resource usage in some cases: When nixpkgs in `NIX_PATH` is
|
||||||
re-evaluate them when they change. Nix-direnv compromises between performance and
|
pointed to a directory, i.e. a git checkout, Lorri will try to evaluate
|
||||||
correctness, and only re-evaluates direnv if either the project-specific
|
nixpkgs everytime something changes causing high cpu load. Nix-direnv
|
||||||
`default.nix` / `shell.nix` changes, or if there is a new commit added to
|
compromises between performance and correctness, and only re-evaluates direnv
|
||||||
`nixpkgs`. A re-evaluation can be also triggered by using `touch shell.nix` in
|
if either the project-specific `default.nix` / `shell.nix` changes, or if
|
||||||
the same project. Also `nix-direnv` does not require an additional daemon, so it can be
|
there is a new commit added to `nixpkgs`. A re-evaluation can be also
|
||||||
included into the project itself and enabled without further user effort.
|
triggered by using `touch .envrc` in the same project.
|
||||||
|
- No additional daemon or services required: The codesize is small enough that it can be vendored
|
||||||
|
into a project itself.
|
||||||
|
|
|
||||||
95
direnvrc
95
direnvrc
|
|
@ -1,40 +1,37 @@
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
|
|
||||||
|
_nix_export_or_unset() {
|
||||||
|
local key=$1 value=$2
|
||||||
|
if [[ "$value" == __UNSET__ ]]; then
|
||||||
|
unset "$key"
|
||||||
|
else
|
||||||
|
export "$key=$value"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
_nix_import_env() {
|
_nix_import_env() {
|
||||||
local env=$1
|
local env=$1
|
||||||
|
|
||||||
local term_backup=$TERM path_backup=$PATH
|
local old_path=${PATH:-}
|
||||||
if [[ -n ${TMPDIR+x} ]]; then
|
local old_term=${TERM:-__UNSET__}
|
||||||
local tmp_backup=$TMPDIR
|
local old_tmpdir=${TMPDIR:-__UNSET__}
|
||||||
fi
|
local old_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__}
|
||||||
local impure_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__}
|
local old_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__}
|
||||||
local impure_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__}
|
|
||||||
|
|
||||||
eval "$env"
|
eval "$env"
|
||||||
|
|
||||||
# `nix-shell --pure` sets invalid ssl certificate paths
|
# `nix-shell --pure` sets invalid ssl certificate paths
|
||||||
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
|
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
|
||||||
if [[ $impure_ssl_cert_file == __UNSET__ ]]; then
|
_nix_export_or_unset SSL_CERT_FILE "$old_ssl_cert_file"
|
||||||
unset SSL_CERT_FILE
|
|
||||||
else
|
|
||||||
export SSL_CERT_FILE=$impure_ssl_cert_file
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
|
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
|
||||||
if [[ $impure_nix_ssl_cert_file == __UNSET__ ]]; then
|
_nix_export_or_unset NIX_SSL_CERT_FILE "$old_nix_ssl_cert_file"
|
||||||
unset NIX_SSL_CERT_FILE
|
|
||||||
else
|
|
||||||
export NIX_SSL_CERT_FILE=${impure_nix_ssl_cert_file}
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export PATH=$PATH:$path_backup TERM=$term_backup
|
export PATH=$PATH${old_path:+":"}$old_path
|
||||||
if [[ -n ${tmp_backup+x} ]]; then
|
_nix_export_or_unset TEMPDIR "$old_tmp"
|
||||||
export TMPDIR=${tmp_backup}
|
_nix_export_or_unset TERM "$old_term"
|
||||||
else
|
|
||||||
unset TMPDIR
|
|
||||||
fi
|
|
||||||
|
|
||||||
# misleading since we are in an impure shell now
|
# misleading since we are in an impure shell now
|
||||||
export IN_NIX_SHELL=impure
|
export IN_NIX_SHELL=impure
|
||||||
|
|
@ -58,12 +55,13 @@ use_flake() {
|
||||||
local profile="$(direnv_layout_dir)/flake-profile"
|
local profile="$(direnv_layout_dir)/flake-profile"
|
||||||
local profile_rc="${profile}.rc"
|
local profile_rc="${profile}.rc"
|
||||||
|
|
||||||
if [[ ! -e "$profile" ]] || \
|
if [[ ! -e "$profile" \
|
||||||
[[ ! -e "$profile_rc" ]] || \
|
|| ! -e "$profile_rc" \
|
||||||
[[ "$HOME/.direnvrc" -nt "$profile_rc" ]] || \
|
|| "$HOME/.direnvrc" -nt "$profile_rc"
|
||||||
[[ .envrc -nt "$profile_rc" ]] || \
|
|| .envrc -nt "$profile_rc"
|
||||||
[[ flake.nix -nt "$profile_rc" ]] || \
|
|| flake.nix -nt "$profile_rc"
|
||||||
[[ flake.lock -nt "$profile_rc" ]];
|
|| flake.lock -nt "$profile_rc"
|
||||||
|
]];
|
||||||
then
|
then
|
||||||
local tmp_profile="$(direnv_layout_dir)/flake-profile.$$"
|
local tmp_profile="$(direnv_layout_dir)/flake-profile.$$"
|
||||||
[[ -d "$(direnv_layout_dir)" ]] || mkdir "$(direnv_layout_dir)"
|
[[ -d "$(direnv_layout_dir)" ]] || mkdir "$(direnv_layout_dir)"
|
||||||
|
|
@ -90,31 +88,11 @@ use_flake() {
|
||||||
rmdir "$NIX_BUILD_TOP"
|
rmdir "$NIX_BUILD_TOP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$old_nix_build_top" == __UNSET__ ]]; then
|
_nix_export_or_unset NIX_BUILD_TOP "$old_nix_build_top"
|
||||||
unset NIX_BUILD_TOP
|
_nix_export_or_unset TMP "$old_tmp"
|
||||||
else
|
_nix_export_or_unset TMPDIR "$old_tmpdir"
|
||||||
export NIX_BUILD_TOP=$old_nix_build_top
|
_nix_export_or_unset TEMP "$old_temp"
|
||||||
fi
|
_nix_export_or_unset TEMPDIR "$old_tempdir"
|
||||||
if [[ "$old_tmp" == __UNSET__ ]]; then
|
|
||||||
unset TMP
|
|
||||||
else
|
|
||||||
export TMP=$old_temp
|
|
||||||
fi
|
|
||||||
if [[ "$old_tmpdir" == __UNSET__ ]]; then
|
|
||||||
unset TMPDIR
|
|
||||||
else
|
|
||||||
export TMPDIR=$old_tmpdir
|
|
||||||
fi
|
|
||||||
if [[ "$old_temp" == __UNSET__ ]]; then
|
|
||||||
unset TEMP
|
|
||||||
else
|
|
||||||
export TEMP=$old_temp
|
|
||||||
fi
|
|
||||||
if [[ "$old_tempdir" == __UNSET__ ]]; then
|
|
||||||
unset TEMPDIR
|
|
||||||
else
|
|
||||||
export TEMPDIR=$old_tempdir
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
use_nix() {
|
use_nix() {
|
||||||
|
|
@ -143,11 +121,12 @@ use_nix() {
|
||||||
local cache="$direnv_dir/cache-${version:-unknown}"
|
local cache="$direnv_dir/cache-${version:-unknown}"
|
||||||
|
|
||||||
local update_drv=0
|
local update_drv=0
|
||||||
if [[ ! -e "$cache" ]] || \
|
if [[ ! -e "$cache" \
|
||||||
[[ "$HOME/.direnvrc" -nt "$cache" ]] || \
|
|| "$HOME/.direnvrc" -nt "$cache" \
|
||||||
[[ .envrc -nt "$cache" ]] || \
|
|| .envrc -nt "$cache" \
|
||||||
[[ default.nix -nt "$cache" ]] || \
|
|| default.nix -nt "$cache" \
|
||||||
[[ shell.nix -nt "$cache" ]];
|
|| shell.nix -nt "$cache" \
|
||||||
|
]];
|
||||||
then
|
then
|
||||||
[[ -d "$direnv_dir" ]] || mkdir "$direnv_dir"
|
[[ -d "$direnv_dir" ]] || mkdir "$direnv_dir"
|
||||||
local dump_cmd tmp
|
local dump_cmd tmp
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue