blob: 5a8f2521d2791dbd547c1aaa0ffcb172de770aab (
plain)
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
|
#! /bin/sh
#
# NAME
# get - evaluate ./default.nix
#
# SYNOPSIS
# get [-jlprs] [attrPath]
#
# DESCRIPTION
# The command nix evaluates the Nix expression in ./default.nix.
#
# OPTIONS
#
# -j Format output as JSON. Implies -s.
#
# -l List attrNames, one per line.
#
# -p Pretty-print output. (Only effectiv in conjunction with -j.)
#
# -r If result looks is a string, then it will be written directly to
# standard output rather than being formatted as JSON or Nix string
# with quotes. Refs jq(1)'s --raw-output.
#
# -s Recursively evaluate list elements and attributes.
# Refs nix-instantiate(1)'s --strict.
#
set -efu
args=$*
has_lopt() {
echo "$args" | grep -q '\(^\|\s\)--'"$1"'\(\s\|$\)'
}
has_sopt() {
echo "$args" | grep -q '\(^\|\s\)-[a-z]*'"$1"'[a-z]*\(\s\|$\)'
}
filter() { cat; }
if has_sopt j; then
json=1
strict=1
fi
if has_sopt l; then
unset json
unset strict
filter() { sed 's/\({ \)\?\(\S\+\) = <CODE>; }\?/\2\n/g' | grep .; }
fi
if has_sopt p && has_sopt j; then
filter() { jq .; }
fi
if has_sopt r; then
raw_output=1
fi
if has_sopt s; then
strict=1
fi
if x=$(nix-instantiate --json --eval -A NIX_PATH 2>/dev/null); then
NIX_PATH=$(echo "$x" | jq -r .)
export NIX_PATH
unset x
fi
result=$(nix-instantiate \
--eval \
--argstr user-name "$LOGNAME" \
--argstr host-name "$HOSTNAME" \
${json+--json} \
${strict+--strict} \
$(
for i; do
echo "$i"
done | sed -n '
s/^[A-Za-z_][0-9A-Za-z_./-]*$/--attr &/p
s/^\([0-9A-Za-z-]\+\)=\([0-9A-Za-z-]*\)$/--argstr \1-name \2/p
'
))
case ${raw_output-0} in 1)
case ${result:0:1} in \")
result=$(echo "$result" | jq -e -r .)
esac
esac
echo "$result" | filter
|