mirror of
https://github.com/nix-community/nix-direnv.git
synced 2024-09-11 11:35:54 +03:00
graceful fallback if no nixpkgs is in NIX_PATH
This commit is contained in:
parent
b583f37302
commit
28a0597659
38
direnvrc
38
direnvrc
@ -252,27 +252,29 @@ use_flake() {
|
||||
use_nix() {
|
||||
_nix_direnv_preflight
|
||||
|
||||
local layout_dir path
|
||||
path=$(_nix_direnv_realpath "$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)")
|
||||
local layout_dir path version
|
||||
layout_dir=$(direnv_layout_dir)
|
||||
path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs 2>/dev/null)
|
||||
if [[ -n "$path" ]]; then
|
||||
path=$(_nix_direnv_realpath "$path")
|
||||
|
||||
local version
|
||||
if [[ -f "${path}/.version-suffix" ]]; then
|
||||
version=$(< "${path}/.version-suffix")
|
||||
elif [[ -f "${path}/.git/HEAD" ]]; then
|
||||
local head
|
||||
read -r head < "${path}/.git/HEAD"
|
||||
local regex="ref: (.*)"
|
||||
if [[ "$head" =~ $regex ]]; then
|
||||
read -r version < "${path}/.git/${BASH_REMATCH[1]}"
|
||||
else
|
||||
version="$head"
|
||||
if [[ -f "${path}/.version-suffix" ]]; then
|
||||
version=$(< "${path}/.version-suffix")
|
||||
elif [[ -f "${path}/.git/HEAD" ]]; then
|
||||
local head
|
||||
read -r head < "${path}/.git/HEAD"
|
||||
local regex="ref: (.*)"
|
||||
if [[ "$head" =~ $regex ]]; then
|
||||
read -r version < "${path}/.git/${BASH_REMATCH[1]}"
|
||||
else
|
||||
version="$head"
|
||||
fi
|
||||
elif [[ -f "${path}/.version" && "${path}" == "/nix/store/"* ]]; then
|
||||
# borrow some bits from the store path
|
||||
local version_prefix
|
||||
read -r version_prefix < "${path}/.version"
|
||||
version="${version_prefix}-${path:11:16}"
|
||||
fi
|
||||
elif [[ -f "${path}/.version" && "${path}" == "/nix/store/"* ]]; then
|
||||
# borrow some bits from the store path
|
||||
local version_prefix
|
||||
read -r version_prefix < "${path}/.version"
|
||||
version="${version_prefix}-${path:11:16}"
|
||||
fi
|
||||
|
||||
local profile
|
||||
|
@ -40,6 +40,8 @@ def direnv_project(test_root: Path, project_root: Path) -> Iterator[DirenvProjec
|
||||
with TemporaryDirectory() as _dir:
|
||||
dir = Path(_dir) / "proj"
|
||||
shutil.copytree(test_root / "testenv", dir)
|
||||
shutil.copyfile(project_root / "flake.nix", dir / "flake.nix")
|
||||
shutil.copyfile(project_root / "flake.lock", dir / "flake.lock")
|
||||
nix_direnv = project_root / "direnvrc"
|
||||
|
||||
c = DirenvProject(Path(dir), nix_direnv)
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import subprocess
|
||||
from typing import List, Union, IO, Any
|
||||
from typing import List, Union, IO, Any, Optional
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@ -16,10 +16,11 @@ def run(
|
||||
cwd: _DIR = None,
|
||||
stderr: _FILE = None,
|
||||
stdout: _FILE = None,
|
||||
env: Optional[dict[str, str]] = None,
|
||||
) -> subprocess.CompletedProcess:
|
||||
if cwd is not None:
|
||||
print(f"cd {cwd}")
|
||||
print("$ " + " ".join(cmd))
|
||||
return subprocess.run(
|
||||
cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout
|
||||
cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout, env=env
|
||||
)
|
||||
|
@ -1,20 +1,27 @@
|
||||
#!/usr/bin/env python2
|
||||
|
||||
import sys
|
||||
import os
|
||||
import subprocess
|
||||
import unittest
|
||||
from typing import Optional
|
||||
|
||||
from procs import run
|
||||
from direnv_project import DirenvProject
|
||||
|
||||
|
||||
def direnv_exec(direnv_project: DirenvProject, cmd: str) -> None:
|
||||
def direnv_exec(
|
||||
direnv_project: DirenvProject, cmd: str, env: Optional[dict[str, str]] = None
|
||||
) -> None:
|
||||
args = ["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd]
|
||||
print("$ " + " ".join(args))
|
||||
out = run(
|
||||
["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd],
|
||||
args,
|
||||
stderr=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
check=False,
|
||||
cwd=direnv_project.dir,
|
||||
env=env,
|
||||
)
|
||||
sys.stdout.write(out.stdout)
|
||||
sys.stderr.write(out.stderr)
|
||||
@ -28,6 +35,13 @@ def test_attrs(direnv_project: DirenvProject) -> None:
|
||||
direnv_exec(direnv_project, "echo $THIS_IS_A_SUBSHELL")
|
||||
|
||||
|
||||
def test_no_nix_path(direnv_project: DirenvProject) -> None:
|
||||
direnv_project.setup_envrc("use nix --argstr someArg OK")
|
||||
env = os.environ.copy()
|
||||
del env["NIX_PATH"]
|
||||
direnv_exec(direnv_project, "echo $SHOULD_BE_SET", env=env)
|
||||
|
||||
|
||||
def test_args(direnv_project: DirenvProject) -> None:
|
||||
direnv_project.setup_envrc("use nix --argstr someArg OK")
|
||||
direnv_exec(direnv_project, "echo $SHOULD_BE_SET")
|
||||
|
@ -1,6 +1,9 @@
|
||||
{ pkgs ? import <nixpkgs> { }, someArg ? null, shellHook ? ''
|
||||
{ pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { }
|
||||
, someArg ? null
|
||||
, shellHook ? ''
|
||||
echo "Executing shellHook."
|
||||
'' }:
|
||||
''
|
||||
}:
|
||||
pkgs.mkShellNoCC {
|
||||
inherit shellHook;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user