-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib
123 lines (102 loc) · 3.01 KB
/
lib
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2022 The Standard Authors
# SPDX-License-Identifier: Unlicense
# shellcheck disable=SC2016
: '
USAGE:
Example (.envrc):
source $(fetchurl https://raw.githubusercontent.com/paisano-nix/direnv/main/lib <hash>)
Find hash:
direnv fetchurl https://raw.githubusercontent.com/paisano-nix/direnv/main/lib
'
# signature direnv & nixago output styling
export DIRENV_LOG_FORMAT=$'\E[mdirenv: \E[38;5;8m%s\E[m'
{ # PRJ Base Directory Specification: https://github.com/numtide/prj-spec
# shellcheck source=/dev/null
source "$(
fetchurl \
"https://raw.githubusercontent.com/numtide/prj-spec/9b0ffcd0fddcb261bcd73ad9dad18a096760b4a0/contrib/direnv" \
"sha256-54YaaGly6Q0E8GhFT9fB/h9tN1PDERo2/4R4X0Pdi/c="
)"
}
{ # Paisano Block
registry="git+file://$PRJ_ROOT#__std"
nix=("nix")
nix_eval_args=(
"--no-update-lock-file"
"--no-write-lock-file"
"--no-warn-dirty"
"--accept-flake-config"
)
nix_eval=("${nix[@]}" "eval" "${nix_eval_args[@]}")
nix_build_args=(
"--no-link"
"--keep-outputs"
"--build-poll-interval" "0"
"--builders-use-substitutes"
)
nix_build=("${nix[@]}" "build" "${nix_eval_args[@]}" "${nix_build_args[@]}")
system="$("${nix_eval[@]}" 2>/dev/null --raw --impure --expr builtins.currentSystem)"
cellsFrom="$("${nix_eval[@]}" 2>/dev/null --raw "$registry.cellsFrom")"
# Usage: use env <target>
#
# Loads the environment determined by the given target
#
# use env //local/env/default
use_env() {
local input="$1"
read -r cell block target <<<"${input//\// }"
action="enter"
local profile_path="${PRJ_DATA_HOME}/$cell/$block/$target"
mkdir -p "${PRJ_DATA_HOME}/$cell/$block/$target"
cmd=(
"${nix_build[@]}"
"--print-out-paths"
"--profile"
"$profile_path/enter-action"
"$registry.actions.$system.$cell.$block.$target.$action"
)
if ! enter="$("${cmd[@]}")"; then
log_status "The follwing command just failed:"
log_status "${cmd[@]}"
read -rp "Rerun with --show-trace (y/N): " diagnose
[[ "$diagnose" == "y" ]] && "${cmd[@]}" --show-trace
exit 1
fi
export STD_DIRENV=1
eval "$(<"$enter")"
# this is not true
unset IN_NIX_SHELL STD_DIRENV
}
_set_watchers() {
local input="$1"
read -r cell block target <<<"${input//\// }"
local BLOCK_PATH="$cellsFrom/$cell/$block"
local watchableFile=
local watchableDir=
if [[ -f "$BLOCK_PATH/$target.nix" ]]; then
watchableFile="$BLOCK_PATH/$target.nix"
elif [[ -f "$BLOCK_PATH/default.nix" ]]; then
watchableFile="$BLOCK_PATH/default.nix"
elif [[ -f "$BLOCK_PATH.nix" ]]; then
watchableFile="$BLOCK_PATH.nix"
fi
if [[ -d "$BLOCK_PATH/$target" ]]; then
watchableDir="$BLOCK_PATH/$target"
fi
if [[ -n $watchableFile ]]; then
log_status "Watching: ${watchableFile}"
watch_file "$watchableFile"
fi
if [[ -n $watchableDir ]]; then
log_status "Watching: ${watchableDir} (recursively)"
watch_dir "$watchableDir"
fi
}
use_envreload() {
for target in "${@}"; do
_set_watchers "$target"
done
use_env "$1"
}
}