Skip to content

Commit

Permalink
Merge branch 'master' into update_flake_lock_action
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Aug 22, 2024
2 parents 6bca5ed + a3139c8 commit 3058b80
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ invocation or are purely incidental and should not be relied upon.
flake's devShell are invalid and nix-direnv has loaded the last known working
shell.

nix-direnv also respects the following environment variables for configuration.

- `NIX_DIRENV_FALLBACK_NIX`: Can be set to a fallback Nix binary location, to be
used when a compatible one isn't available in `PATH`. Defaults to
`config.nix.package` if installed via the NixOS module, otherwise needs to be
set manually. Leave unset or empty to fail immediately when a Nix
implementation can't be found on `PATH`.

## General direnv tips

- [Changing where direnv stores its cache][cache_location]
Expand Down
31 changes: 17 additions & 14 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
resholve,
lib,
coreutils,
direnv,
nix,
writeText,
}:

# resholve does not yet support `finalAttrs` call pattern hence `rec`
Expand All @@ -17,11 +17,6 @@ resholve.mkDerivation rec {
name = pname;
};

# skip min version checks which are redundant when built with nix
postPatch = ''
sed -i 1iNIX_DIRENV_SKIP_VERSION_CHECK=1 direnvrc
'';

installPhase = ''
install -m400 -D direnvrc $out/share/${pname}/direnvrc
'';
Expand All @@ -30,10 +25,7 @@ resholve.mkDerivation rec {
default = {
scripts = [ "share/${pname}/direnvrc" ];
interpreter = "none";
inputs = [
coreutils
nix
];
inputs = [ coreutils ];
fake = {
builtin = [
"PATH_add"
Expand All @@ -48,15 +40,26 @@ resholve.mkDerivation rec {
# cannot be reached when built with nix
"shasum"
];
external = [
# We want to reference the ambient Nix when possible, and have custom logic
# for the fallback
"nix"
];
};
keep = {
"$cmd" = true;
"$direnv" = true;

# Nix fallback implementation
"$_nix_direnv_nix" = true;
"$ambient_nix" = true;
"$NIX_DIRENV_FALLBACK_NIX" = true;
};
execer = [
"cannot:${direnv}/bin/direnv"
"cannot:${nix}/bin/nix"
];
prologue =
(writeText "prologue.sh" ''
NIX_DIRENV_SKIP_VERSION_CHECK=1
NIX_DIRENV_FALLBACK_NIX=''${NIX_DIRENV_FALLBACK_NIX:-${lib.getExe nix}}
'').outPath;
};
};

Expand Down
40 changes: 37 additions & 3 deletions direnvrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ _nix_direnv_warning() {

_nix_direnv_error() { log_error "${_NIX_DIRENV_LOG_PREFIX}$*"; }

_nix_direnv_nix=""

_nix() {
nix --extra-experimental-features "nix-command flakes" "$@"
${_nix_direnv_nix} --extra-experimental-features "nix-command flakes" "$@"
}

_require_version() {
Expand All @@ -53,6 +55,34 @@ _require_cmd_version() {
_require_version "$cmd" "${BASH_REMATCH[1]}" "$required"
}

_nix_direnv_resolve_nix() {
local ambient_nix

if ambient_nix=$(command -v nix); then
if _require_cmd_version "${ambient_nix}" "${NIX_MIN_VERSION}"; then
echo "${ambient_nix}"
return 0
else
_nix_direnv_warning "Nix version in PATH is too old, wanted ${NIX_MIN_VERSION}+, got $(${ambient_nix} --version), will attempt fallback"
fi
else
_nix_direnv_warning "Could not find Nix in PATH, will attempt fallback"
fi

if [ -n "${NIX_DIRENV_FALLBACK_NIX}" ]; then
if _require_cmd_version "${NIX_DIRENV_FALLBACK_NIX}" "${NIX_MIN_VERSION}"; then
echo "${NIX_DIRENV_FALLBACK_NIX}"
return 0
else
_nix_direnv_error "Fallback Nix version is too old, wanted ${NIX_MIN_VERSION}+, got $(${NIX_DIRENV_FALLBACK_NIX} --version)"
return 1
fi
else
_nix_direnv_error "Could not find fallback Nix binary, please add Nix to PATH or set NIX_DIRENV_FALLBACK_NIX"
return 1
fi
}

_nix_direnv_preflight() {
if [[ -z $direnv ]]; then
# shellcheck disable=2016
Expand All @@ -66,12 +96,16 @@ _nix_direnv_preflight() {
# _require_cmd_version because _require_cmd_version uses =~ operator which would be
# a syntax error on bash < 3
if ! _require_version bash "$BASH_VERSION" "$BASH_MIN_VERSION" ||
! _require_cmd_version "$direnv" "$DIRENV_MIN_VERSION" || # direnv stdlib defines $direnv
! _require_cmd_version nix "$NIX_MIN_VERSION"; then
# direnv stdlib defines $direnv
! _require_cmd_version "$direnv" "$DIRENV_MIN_VERSION"; then
return 1
fi
fi

if ! _nix_direnv_nix=$(_nix_direnv_resolve_nix); then
return 1
fi

local layout_dir
layout_dir=$(direnv_layout_dir)

Expand Down

0 comments on commit 3058b80

Please sign in to comment.