forked from nix-community/nix-zsh-completions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_nix
259 lines (238 loc) · 9.66 KB
/
_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
#compdef nix
#autoload
# Most information is extractable from `nix --help` and `nix command --help`
# There's four different types of options and arguments which is passed to _arguments:
#
# 1. The main commands which we get from nix --help
#
# 2. The common options which are hardcoded for the moment
#
# 3. The regular arguments for the active command. The number and types of these
# arguments are taken from the "Usage:" line of `nix command --help`, $state
# is set to the name of the argument via a `->$arg` action, which are further
# handled in the case statement at the end of the file.
#
# 4. The options for the active command. These are taken from the "Flags:"
# section of `nix command --help`, and the option arguments are handled in the
# same way as in 3.
# Ensure access to eg. _nix_complete_attr_paths
_nix-common-options
setopt extendedglob
local context state state_descr line
typeset -A opt_args
type nix &> /dev/null || return
local -a common_options
common_options=("(--debug)"--debug"[enable debug output]"
"(-h --help)"{-h,--help}"[show usage information]"
"(--help-config)"--help-config"[show configuration options]"
"*--builders[The machine to build on]:Machine eg. 'root@mac x86_64-darwin': "
"--store[Select a nix store]:Store:->STORE-URI"
"*--option[set a Nix configuration option (overriding nix.conf)]:Option:->nixoption:Value:->nixoptionvalue"
"(--quiet)"--quiet"[decrease verbosity level]"
"*"{-v,--verbose}"[increase verbosity level]"
"(--version)"--version"[show version information]")
local -a main_commands
# Extract the commands with descriptions
# like ('command:some description' 'run:run some stuff'):
#
# Split the help output on newlines, and remove all lines which doesn't start
# with two spaces and a lower case character,
main_commands=(${${(f)"$(nix --help)"}:#^( [a-z]*)})
# Strip the leading spaces
main_commands=(${main_commands/# /})
# And replace the whitespace separating the command and description with ':'
main_commands=(${main_commands/ ##/:})
# Add commands to an associative array for easy lookup
local -A command_lookup
local command_description
for command_description in $main_commands; {
local command=${command_description%%\:*}
command_lookup[$command]=1
}
local -a command_options=()
local -a command_arguments=()
# Setup the correct command_arguments and command_options depending on which
# command we've typed
local word
for word in $words; do
# Check if we're in a valid command
if [[ $command_lookup[$word] == 1 ]]; then
local -a help=(${(f)"$(nix $word --help)"})
# Extract an array describing the possible arguments to the command eg.
# (NAR PATH) for cat-nar or (INSTALLABLES) for run
local -a args=(${=help[1]//(*<FLAGS>|.|<|>)/})
# And add the appropriate _argument spec
local arg
for arg in $args; do
# Prefix * if $arg ends in 'S', ie. is plural
local plural="${${(M)arg:#*S}:+*}"
command_arguments+=("$plural:$arg:->$arg")
done
# Extract the lines containing the option descriptions
local -a option_descriptions
option_descriptions=(${help:#^( -*| ??? -*)})
local option_description
local option
for option_description in $option_descriptions; do
# Extract the description by stripping everything up to the the last
# two consecutive spaces
local description=${option_description/(* )/}
# Remove the description from the help line
local option_spec=${option_description%$description}
# Extract the options by stripping everything starting at '<' (and ',')
local -a option_group=(${=option_spec//(<*|,)/})
# Extract any arguments, by stripping the options (and <|>)
local -a option_args=(${=option_spec//(*$option_group[${#option_group}]|<|>)/})
local ACTIONS=""
for arg in $option_args; do
# Prefix with ':*' if $arg ends in 'S', ie. is plural
local plural="${${(M)arg:#*S}:+:*}"
ACTIONS+="$plural:$arg:->$arg"
done
for option in $option_group; do
# Handle `run --keep/--unset` manually as there's ambiguity the NAME argument
if [[ $word == run && -z ${option:#(-k|--keep|-u|--unset)} ]]; then
command_options+=("*${option}[$description]:Environment Variable:_parameters")
elif [[ "$word" == add-to-store \
&& "$option" == (-n|--name) ]]; then
# Another <NAME> ambiguity
command_options+=("($option_group)"$option"[$description]:->store-name")
elif [[ $option == (-I|--include) ]]; then
# Special handling of --include due to type ambiguity
command_options+=("*${option}[$description]:Includes:->INCLUDE")
elif [[ -z ${option:#(--arg|--argstr|-f|--file)} ]]; then
# Repeatable options
command_options+=("*${option}[$description]"$ACTIONS)
else
# Default to mutually exclusive non-repeatable options
command_options+=("($option_group)"$option"[$description]"$ACTIONS)
fi
done
done
break
fi
done
_arguments -s \
":Command: _describe -t main_commands Command main_commands"\
$command_arguments \
$common_options \
$command_options && return 0
# Handle arguments to commands and options
case "${context%%-*} $state" in
'argument '(INSTALLABLES|INSTALLABLE|PACKAGE|DEPENDENCY))
# Complete attribute paths and files starting with either "/" or "./"
# We need to prefix relative paths with ./ so nix will evaluate it as a
# path
local prefix='-P ./'
local current_word=$words[$CURRENT]
# When referencing an absolute path we can't prefix with ./
if [[ -z ${current_word:##(/*|\~/*)} && -n $current_word ]]; then
prefix=""
fi
_alternative \
'path:Attribute path: _nix_complete_attr_paths' \
"file:File path to package:_files ${prefix}"
return
;;
'argument '(PATH|PATHS|FILES))
_files
return
;;
'argument NAR')
_files -X "NAR"
return
;;
'argument REGEX')
_message 'Regex to search for'
return
;;
'argument STRINGS')
_message 'HASH to convert to a different base'
return
;;
'option NAME')
_nix_complete_function_arg
return
;;
'option EXPR')
_message 'Expression argument'
return
;;
'option STRING')
_message 'String argument'
return
;;
'option STORE-URI')
local -a others=(
'local:Use /nix/store'
'remote:Go via the Nix daemon'
'https\://:HTTPS'
's3\://:S3 binary cache'
'ssh\://:Nix store over ssh')
_alternative \
'file:Use a chroot store:_directories' \
'url:Other: _describe "URLS and special options" others' \
'cache:Local binary cache (adds file url prefix):_files -P "file://" -/'
return
;;
'option FILE')
_alternative \
'file:Local file:_nix_complete_dotnix_files' \
'shortcuts:Shortcuts:_nix_shortcuts' \
'channel:Channel:(channel:
channel:nixos-13.10 channel:nixos-16.09
channel:nixos-14.04 channel:nixos-16.09-small
channel:nixos-14.04-small channel:nixos-17.03
channel:nixos-14.12 channel:nixos-17.03-small
channel:nixos-14.12-small channel:nixos-17.09
channel:nixos-15.09 channel:nixos-17.09-small
channel:nixos-15.09-small channel:nixos-unstable
channel:nixos-16.03 channel:nixos-unstable-small
channel:nixos-16.03-small channel:nixpkgs-17.09-darwin
channel:nixos-16.03-testing channel:nixpkgs-unstable)' \
'url:URL:(https:// http://)'
return
;;
'option FILES'|'option PATH')
_files
return
;;
'option COMMAND')
_command_names
return
;;
'option ARGS')
_message 'Arguments to command'
return
;;
'option TYPE')
_values 'Hash type' md5 sha1 sha256 sha512
return
;;
'option INCLUDE')
# --include <PATH> completion
_nix_complete_includes
return
;;
'option nixoption')
# Complete nix options with descriptions
local -a nix_options
# Strip the header line, remove leading spaces and replace separating
# whitespace with ':'
nix_options=(${${${${(f)"$(nix --help-config)"}:1:-1}/# /}/ ##/:})
_describe -t nix_options "Option" nix_options
return
;;
'option nixoptionvalue')
# Print the description of the option we're setting
local OPTION=$words[$(($CURRENT - 1))]
# Remove lines not starting with " $OPTION " and strip eveything up to the
# last two consecutive spaces
local description=${${${(f)"$(nix --help-config)"}:#^( $OPTION *)}/* /}
_message $description
return
;;
*)
# Fallback to argument name description
_message $state_descr
esac