-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.envrc
88 lines (75 loc) · 2.32 KB
/
.envrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Usage: use_nix [...]
#
# Load environment variables from `nix-shell`.
# If you have a `default.nix` or `shell.nix` one of these will be used and
# the derived environment will be stored at ./.direnv/env-<hash>
# and symlink to it will be created at ./.direnv/default.
# Dependencies are added to the GC roots, such that the environment remains persistent.
#
# Packages can also be specified directly via e.g `use nix -p ocaml`,
# however those will not be added to the GC roots.
#
# The resulting environment is cached for better performance.
#
# To trigger switch to a different environment:
# `rm -f .direnv/default`
#
# To derive a new environment:
# `rm -rf .direnv/env-$(md5sum {shell,default}.nix 2> /dev/null | cut -c -32)`
#
# To remove cache:
# `rm -f .direnv/dump-*`
#
# To remove all environments:
# `rm -rf .direnv/env-*`
#
# To remove only old environments:
# `find .direnv -name 'env-*' -and -not -name `readlink .direnv/default` -exec rm -rf {} +`
#
use_nix() {
set -e
local shell="shell.nix"
if [[ ! -f "${shell}" ]]; then
shell="default.nix"
fi
if [[ ! -f "${shell}" ]]; then
fail "use nix: shell.nix or default.nix not found in the folder"
fi
local dir="$(direnv_layout_dir)"
local default="${dir}/default"
if [[ ! -L "${default}" ]] || [[ ! -d $(readlink "${default}") ]]; then
local wd="${dir}/env-$(hashFile "${shell}")"
mkdir -p "${wd}"
local drv="${wd}/env.drv"
if [[ ! -f "${drv}" ]]; then
log_status "use nix: deriving new environment"
IN_NIX_SHELL=1 nix-instantiate --add-root "${drv}" --indirect "${shell}" > /dev/null
nix-store -r $(nix-store --query --references "${drv}") --add-root "${wd}/dep" --indirect > /dev/null
fi
rm -f "${default}"
ln -s $(basename "${wd}") "${default}"
fi
local drv=$(readlink "${default}/env.drv")
local dump="${dir}/dump-$(hashFile ".envrc")-$(hashFile ${drv})"
if [[ ! -f "${dump}" ]] || [[ "${XDG_CONFIG_DIR}/direnv/direnvrc" -nt "${dump}" ]]; then
log_status "use nix: updating cache"
old=$(find "${dir}" -name 'dump-*')
nix-shell "${drv}" --show-trace "$@" --run "$(join_args "$direnv" dump)" > "${dump}"
rm -f ${old}
fi
direnv_load cat "${dump}"
watch_file "${default}"
watch_file "${shell}"
}
hashFile() {
if has md5sum; then
md5sum "${@}" | cut -c -32
elif has md5; then
md5 -q "${@}"
fi
}
fail() {
log_error "${@}"
exit 1
}
use_nix