-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhls-wrapper-nix.nix
306 lines (279 loc) · 6.83 KB
/
hls-wrapper-nix.nix
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{ coreutils
, findutils
, gnugrep
, gnused
, hls-wrapper
, nix-project-lib
, yq-go
}:
let
progName = "hls-wrapper-nix";
meta.description =
"Haskell Language Server (HLS) wrapper for Nix";
in
nix-project-lib.writeShellCheckedExe progName
{
inherit meta;
pathPure = false;
path = [
coreutils
gnugrep
gnused
hls-wrapper
yq-go
];
}
''
set -eu
set -o pipefail
DEFAULT_CONFIG="$HOME/.config/haskell-language-server/wrapper-nix.yaml"
CONFIG=
NIX_EXE="$(command -v nix || true)"
HLS_EXE="${hls-wrapper}/bin/haskell-language-server-wrapper"
SHELL_FILE=
WORK_DIR=
NIX_PURE=
MODE= # detect | shell | bypass
HLS_ARGS=()
. "${nix-project-lib.common}/share/nix-project/common.bash"
print_usage()
{
cat - <<EOF
USAGE: ${progName} [OPTION]... [HLS_OPTIONS]...
${progName} --show-path PATH
${progName} --help
DESCRIPTION:
Run the Haskell Language Server wrapper, possibly in a Nix Shell.
OPTIONS:
--help print this help message
--show-path PATH print path to use in configuration file
--config PATH configuration file to read instead of default
--shell-file PATH explicitly specified Nix file for shell
--auto-detect don't run in Nix shell if not clear how (default)
--shell-always always run in a Nix shell
--shell-never never run in a Nix shell
--pure run pure Nix shell (no external environment)
--impure allow external environment variables in Nix shell
--nix PATH filepath to 'nix' binary to put on PATH
Note, when using options concurrently (like '--auto-detect',
'--shell-always', or '--shell-never'), the last one has precedent.
HLS OPTIONS:
$(hls_options)
EOF
}
main()
{
while ! [ "''${1:-}" = "" ]
do
case "$1" in
-h|--help)
print_usage
exit 0
;;
--show-path)
if [ -z "''${2:-}" ]
then die "$1 requires argument"
fi
readlink -f "''${2:-}"
exit 0
;;
--config)
if [ -z "''${2:-}" ]
then die "$1 requires argument"
fi
CONFIG="''${2:-}"
shift
;;
--nix)
if [ -z "''${2:-}" ]
then die "$1 requires argument"
fi
NIX_EXE="''${2:-}"
shift
;;
--auto-detect)
MODE=detect
;;
--shell-always)
MODE=shell
;;
--shell-never)
MODE=bypass
;;
--shell-file)
if [ -z "''${2:-}" ]
then die "$1 requires argument"
fi
SHELL_FILE="''${2:-}"
MODE=shell
shift
;;
--cwd)
if [ -z "''${2:-}" ]
then die "$1 requires argument"
fi
WORK_DIR="''${2:-}"
shift
;;
--pure)
NIX_PURE=true
;;
--impure)
NIX_PURE=false
;;
*)
HLS_ARGS+=("$1")
;;
esac
shift
done
if [ -n "$NIX_EXE" ]
then add_nix_to_path "$NIX_EXE"
fi
if [ -n "$WORK_DIR" ] && [ -d "$WORK_DIR" ]
then cd "$WORK_DIR" || die "can not change to directory: $WORK_DIR"
fi
integrate_args
validate_args
if should_use_shell
then call_with_shell
else call_without_shell
fi
}
integrate_args()
{
local work_dir
work_dir="$(pwd)"
work_dir="$(readlink -f "$work_dir")"
local config=""
if test -r "$DEFAULT_CONFIG" || test -n "$CONFIG"
then
local config_file="''${CONFIG:-"$DEFAULT_CONFIG"}"
log_info "using config file: $config_file"
log_info "config file key: $work_dir"
validate_config_file "$config_file" "$work_dir"
config="$(yq eval ".\"$work_dir\"" "$config_file")"
fi
if [ -z "$config" ]
then
local default_yq='.mode |= "detect" | .pure |= true'
config="$(yq --null-input eval "$default_yq")"
fi
if [ -z "$SHELL_FILE" ]
then SHELL_FILE="$(echo "$config" \
| yq eval '.shell_file // ""' -)"
fi
if [ -z "$MODE" ]
then MODE="$(echo "$config" | yq eval '.mode' -)"
fi
if [ "$MODE" = "null" ]
then MODE="detect"
fi
if [ -z "$NIX_PURE" ]
then NIX_PURE="$(echo "$config" | yq eval '.pure' -)"
fi
if [ "$NIX_PURE" = "null" ]
then NIX_PURE="true"
fi
}
validate_args()
{
case "$MODE" in
detect|shell|bypass) ;;
*) die_helpless "'mode' not \"detect\", \"shell\", or \"bypass\": $MODE" ;;
esac
case "$NIX_PURE" in
true|false) ;;
*) die_helpless "'pure' not \"true\" or \"false\": $NIX_PURE" ;;
esac
if ! [ -r "$(shell_file)" ]
then die_helpless "shell file unreadable: $(shell_file)"
fi
}
should_use_shell()
{
if [ -n "''${IN_NIX_SHELL:-}" ]
then
log_info "Already in Nix shell"
false
elif [ "$MODE" = bypass ]
then false
elif [ "$MODE" = shell ]
then
log_info "Entering Nix shell forcibly: $(shell_file)"
true
elif [ "$MODE" = detect ]
then test -r "$(shell_file)"
else die "mode not 'bypass', 'shell', or 'detect': $MODE"
fi
}
call_with_shell()
{
local file; file="$(shell_file)"
if ! [ -r "$file" ]
then die "can't read Nix file for Nix shell: $file"
fi
local pure_arg=(--pure)
local pure_type=pure
if [ "$NIX_PURE" = false ]
then
pure_arg=()
pure_type=impure
fi
log_info "Entering ''${pure_type} Nix shell"
nix-shell "''${pure_arg[@]}" \
"$file" \
--run \
"
$(declare -p HLS_ARGS)
exec \"$HLS_EXE\" \"\''${HLS_ARGS[@]}\"
"
}
call_without_shell()
{
exec "$HLS_EXE" "''${HLS_ARGS[@]}"
}
shell_file()
{
if [ -n "$SHELL_FILE" ]
then echo "$SHELL_FILE"
elif [ -f "shell.nix" ]
then echo "shell.nix"
elif [ -f "default.nix" ]
then echo "default.nix"
fi
}
validate_config_file()
{
local config_file="$1"
local work_dir="$2"
local yq_expr
local yq_res
if ! yq eval true "$config_file" > /dev/null
then die_helpless "config file malformed: $config_file"
fi
yq_res="$(yq eval "tag" "$config_file")"
if [ -z "$yq_res" ] # DESIGN: empty config file
then return 0
fi
if ! [ "$yq_res" = "!!map" ]
then die_helpless "config file not a YAML map"
fi
yq_expr=".\"$work_dir\" | tag"
yq_res="$(yq eval "$yq_expr" "$config_file")"
if ! [ "$yq_res" = "!!map" ] && ! [ "$yq_res" = "!!null" ]
then die_helpless "config entry for '$work_dir' not a map"
fi
}
hls_options()
{
"$HLS_EXE" --help \
| grep -A99 options: \
| sed '/options:/d;s/^/ /;/--help/d'
}
log_info()
{
echo "INFO: $*" >&2
}
main "$@"
''