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
307
|
" usage example:
"
" call nix#ExportPluginsForNix({'path_to_nixpkgs': '/etc/nixos/nixpkgs', 'names': ["vim-addon-manager", "vim-addon-nix"], 'cache_file': 'cache'})
let s:plugin_root = expand('<sfile>:h:h')
fun! nix#ToNixAttrName(s) abort
return nix#ToNixName(a:s)
endf
fun! nix#ToNixName(s) abort
return substitute(substitute(a:s, '[:/.]', '-', 'g'), 'github-', '', 'g')
endf
fun! s:System(...)
let args = a:000
let r = call('vam#utils#System', args)
if r is 0
throw "command ".join(args, '').' failed'
else
return r
endif
endf
fun! nix#DependenciesFromCheckout(opts, name, repository, dir)
" check for dependencies
" vam#PluginDirFromName(a:name)
let info = vam#ReadAddonInfo(vam#AddonInfoFile(a:dir, a:name))
return keys(get(info, 'dependencies', {}))
endf
" without deps
fun! nix#NixDerivation(opts, name, repository) abort
let n_a_name = nix#ToNixAttrName(a:name)
let n_n_name = nix#ToNixName(a:name)
let type = get(a:repository, 'type', '')
let created_notice = " # created by nix#NixDerivation"
let ancf = s:plugin_root.'/additional-nix-code/'.a:name
let additional_nix_code = file_readable(ancf) ? join(readfile(ancf), "\n") : ""
if type == 'git'
" should be using shell abstraction ..
echo 'fetching '. a:repository.url
let s = s:System('$ --fetch-submodules $ 2>&1',a:opts.nix_prefetch_git, a:repository.url)
let rev = matchstr(s, 'git revision is \zs[^\n\r]\+\ze')
let sha256 = matchstr(s, 'hash is \zs[^\n\r]\+\ze')
let dir = matchstr(s, 'path is \zs[^\n\r]\+\ze')
let date = matchstr(s, 'Commit date is \zs[0-9-]\+\ze')
let dependencies = nix#DependenciesFromCheckout(a:opts, a:name, a:repository, dir)
return {'n_a_name': n_a_name, 'n_n_name': n_n_name, 'dependencies': dependencies, 'derivation': join([
\ ' '.n_a_name.' = buildVimPluginFrom2Nix {'.created_notice,
\ ' name = "'.n_n_name.'-'.date.'";',
\ ' src = fetchgit {',
\ ' url = "'. a:repository.url .'";',
\ ' rev = "'.rev.'";',
\ ' sha256 = "'.sha256.'";',
\ ' };',
\ ' dependencies = ['.join(map(copy(dependencies), "'\"'.nix#ToNixAttrName(v:val).'\"'")).'];',
\ additional_nix_code,
\ ' };',
\ '',
\ '',
\ ], "\n")}
elseif type == 'hg'
" should be using shell abstraction ..
echo 'fetching '. a:repository.url
let s = s:System('$ $ 2>&1',a:opts.nix_prefetch_hg, a:repository.url)
let rev = matchstr(s, 'hg revision is \zs[^\n\r]\+\ze')
let sha256 = matchstr(s, 'hash is \zs[^\n\r]\+\ze')
let dir = matchstr(s, 'path is \zs[^\n\r]\+\ze')
let dependencies = nix#DependenciesFromCheckout(a:opts, a:name, a:repository, dir)
return {'n_a_name': n_a_name, 'n_n_name': n_n_name, 'dependencies': dependencies, 'derivation': join([
\ ' '.n_a_name.' = buildVimPluginFrom2Nix {'.created_notice,
\ ' name = "'.n_n_name.'";',
\ ' src = fetchhg {',
\ ' url = "'. a:repository.url .'";',
\ ' rev = "'.rev.'";',
\ ' sha256 = "'.sha256.'";',
\ ' };',
\ ' dependencies = ['.join(map(copy(dependencies), "'\"'.nix#ToNixAttrName(v:val).'\"'")).'];',
\ additional_nix_code,
\ ' };',
\ '',
\ '',
\ ], "\n")}
elseif type == 'archive'
let sha256 = split(s:System('nix-prefetch-url $ 2>/dev/null', a:repository.url), "\n")[0]
" we should unpack the sources, look for the addon-info.json file ..
" however most packages who have the addon-info.json file also are on
" github thus will be of type "git" instead. The dependency information
" from vim-pi is encoded in the reposiotry. Thus this is likely to do the
" right thing most of the time.
let addon_info = get(a:repository, 'addon-info', {})
let dependencies = keys(get(addon_info, 'dependencies', {}))
return {'n_a_name': n_a_name, 'n_n_name': n_n_name, 'dependencies': dependencies, 'derivation': join([
\ ' '.n_a_name.' = buildVimPluginFrom2Nix {'.created_notice,
\ ' name = "'.n_n_name.'";',
\ ' src = fetchurl {',
\ ' url = "'. a:repository.url .'";',
\ ' name = "'. a:repository.archive_name .'";',
\ ' sha256 = "'.sha256.'";',
\ ' };',
\ ' buildInputs = [ unzip ];',
\ ' dependencies = ['.join(map(copy(dependencies), "'\"'.nix#ToNixAttrName(v:val).'\"'")).'];',
\ ' meta = {',
\ ' url = "http://www.vim.org/scripts/script.php?script_id='.a:repository.vim_script_nr.'";',
\ ' };',
\ addon_info == {} ? '' : (' addon_info = '.nix#ToNix(string(addon_info), [], "").';'),
\ additional_nix_code,
\ ' };',
\ '',
\ '',
\ ], "\n")}
else
throw a:name.' TODO: implement source '.string(a:repository)
endif
endf
" also tries to handle dependencies
fun! nix#AddNixDerivation(opts, cache, name, ...) abort
if has_key(a:cache, a:name) | return | endif
let repository = a:0 > 0 ? a:1 : {}
let name = a:name
if repository == {}
call vam#install#LoadPool()
let list = matchlist(a:name, 'github:\([^/]*\)\%(\/\(.*\)\)\?$')
if len(list) > 0
if '' != list[2]
let name = list[2]
let repository = { 'type': 'git', 'owner': list[1], 'repo': list[2], 'url': 'git://github.com/'.list[1].'/'.list[2] }
else
let name = list[1]
let repository = { 'type': 'git', 'owner': list[1], 'repo': 'vim-addon-'.list[1], 'url': 'git://github.com/'.list[1].'/vim-addon-'.list[1] }
endif
else
let repository = get(g:vim_addon_manager.plugin_sources, a:name, {})
if repository == {}
throw "repository ".a:name." unkown!"
else
if repository.url =~ 'github'
let owner = matchstr(repository.url, 'github.com/\zs.\+\ze/')
let repo = matchstr(repository.url, '\/\zs[^\/]\+\ze$')
let url = repository.url
let repository = { 'type': 'git', 'owner': owner, 'repo': repo, 'url': url }
endif
endif
endif
endif
let a:cache[a:name] = nix#NixDerivation(a:opts, name, repository)
" take known dependencies into account:
let deps = get(a:cache[a:name], 'dependencies', [])
call extend(a:opts.names_to_process, deps)
call extend(a:opts.names_to_export, deps)
endfun
fun! nix#TopNixOptsByParent(parents)
if (a:parents == [])
return {'ind': ' ', 'next_ind': ' ', 'sep': "\n"}
else
return {'ind': '', 'next_ind': '', 'sep': ' '}
endif
endf
fun! nix#ToNix(x, parents, opts_fun) abort
let opts = a:opts_fun == "" ? "" : call(a:opts_fun, [a:parents])
let next_parents = [a:x] + a:parents
let seps = a:0 > 1 ? a:2 : []
let ind = get(opts, 'ind', '')
let next_ind = get(opts, 'next_ind', ind.' ')
let sep = get(opts, 'sep', ind.' ')
if type(a:x) == type("")
return "''". substitute(a:x, '[$]', '$$', 'g')."''"
elseif type(a:x) == type({})
let s = ind."{".sep
for [k,v] in items(a:x)
let s .= '"'.k.'" = '.nix#ToNix(v, next_parents, a:opts_fun).";".sep
unlet k v
endfor
return s.ind."}"
" let s = ind."{\n"
" for [k,v] in items(a:x)
" let s .= next_ind . nix#ToNix(k).' = '.nix#ToNix(v, next_ind)."\n"
" unlet k v
" endfor
" return s.ind."}\n"
elseif type(a:x) == type([])
let s = ind."[".sep
for v in a:x
let s .= next_ind . nix#ToNix(v, next_parents, a:opts_fun)."".sep
unlet v
endfor
return s.ind."]"
endif
endf
" with dependencies
" opts.cache_file (caches the checkout and dependency information
" opts.path_to_nixpkgs or opts.nix_prefetch_{git,hg}
" opts.plugin_dictionaries: list of any
" - string
" - dictionary having key name or names
" This is so that plugin script files can be loaded/ merged
fun! nix#ExportPluginsForNix(opts) abort
let cache_file = get(a:opts, 'cache_file', '')
let opts = a:opts
" set nix_prefetch_* scripts
for scm in ['git', 'hg']
if !has_key(opts, 'nix_prefetch_'.scm)
let opts['nix_prefetch_'.scm] = a:opts.path_to_nixpkgs.'/pkgs/build-support/fetch'.scm.'/nix-prefetch-'.scm
endif
endfor
" create list of names from dictionaries
let a:opts.names_to_process = []
for x in a:opts.plugin_dictionaries
if type(x) == type('')
call add(opts.names_to_process, x)
elseif type(x) == type({}) && has_key(x, 'name')
call add(opts.names_to_process, x.name)
elseif type(x) == type({}) && has_key(x, 'names')
call extend(opts.names_to_process, x.names)
else
throw "unexpected"
endif
unlet x
endfor
let a:opts.names_to_export = a:opts.names_to_process
let cache = (cache_file == '' || !filereadable(cache_file)) ? {} : eval(readfile(cache_file)[0])
let failed = {}
while len(opts.names_to_process) > 0
let name = opts.names_to_process[0]
if get(opts, 'try_catch', 1)
try
call nix#AddNixDerivation(opts, cache, name)
catch /.*/
echom 'failed : '.name.' '.v:exception
let failed[name] = v:exception
endtry
else
call nix#AddNixDerivation(opts, cache, name)
endif
let opts.names_to_process = opts.names_to_process[1:]
endwhile
echom join(keys(failed), ", ")
echom string(failed)
if cache_file != ''
call writefile([string(cache)], cache_file)
endif
enew
let uniq = {}
for x in a:opts.names_to_export
let uniq[x] = 1
endfor
for k in sort(keys(uniq))
call append('$', split(cache[k].derivation,"\n"))
endfor
" for VAM users output vam.pluginDictionaries which can be fed to
" vim_customizable.customize.vimrc.vam.pluginDictionaries
call append('$', ["", "", "", '# vam.pluginDictionaries'])
let ns = []
for x in a:opts.plugin_dictionaries
if type(x) == type("")
call add(ns, nix#ToNixAttrName(x))
elseif type(x) == type({})
if has_key(x, 'name')
call add(ns, extend({'name': nix#ToNixAttrName(x.name)}, x, "keep"))
elseif has_key(x, 'names')
call add(ns, extend({'names': map(copy(x.names), 'nix#ToNixAttrName(v:val)')}, x, "keep"))
else
throw "unexpected"
endif
else
throw "unexpected"
endif
unlet x
endfor
call append('$', split(nix#ToNix(ns, [], 'nix#TopNixOptsByParent'), "\n"))
" failures:
for [k,v] in items(failed)
call append('$', ['# '.k.', failure: '.v])
unlet k v
endfor
endf
|