-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixla.sh
86 lines (65 loc) · 1.37 KB
/
nixla.sh
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
# nixla - nix as a programming language
# set nixla type:
NIXLA=""
NIXLA_NIX=""
NIXLA_JSON=""
script_name=$(basename "$0")
case "$script_name" in
"nixla" | "nixla.sh")
NIXLA="--raw"
;;
"nixla-nix" | "nixla-nix.sh")
NIXLA_NIX=""
;;
"nixla-json" | "nixla-json.nix")
NIXLA_JSON="--json"
;;
*)
echo "Unknown entry-point: $script_name"
echo "Try using 'nixla', 'nixla-nix' or 'nixla-json'."
exit 1
;;
esac
# get filename
FILENAME=$1
# check filename
if [ -z "$FILENAME" ]; then
echo "File name was not provided!"
exit 1
fi
# check that given file is a valid nix expression:
nix \
--extra-experimental-features pipe-operators \
eval \
--impure \
--file \
$FILENAME \
> /dev/null
# remove filename from all other cli args
shift 1
# Collect the input:
# check if arguments is provided
if [ "$#" -gt 0 ]; then
input="$@"
# check if there is input from stdin
elif [ ! -t 0 ]; then
# src: https://stackoverflow.com/questions/6980090/how-to-read-from-a-file-or-standard-input-in-bash#7045517
input="$(cat)"
else
echo "No input provided!" >&2
exit 1
fi
# prevent nix string interpolation
input=$(echo "$input" | sed 's/${/$${/g')
# run nix function on the input:
nix \
--extra-experimental-features pipe-operators \
eval \
--option max-call-depth 1000000 \
--impure \
$NIXLA \
$NIXLA_NIX \
$NIXLA_JSON \
--expr \
"($(cat $FILENAME)
) \"$input\""