-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
nix.go
497 lines (423 loc) · 11.3 KB
/
nix.go
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package nix
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"text/template"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/commitauthor"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
const nixConfigExtra = "NixConfig"
type errNoArchivesFound struct {
goamd64 string
ids []string
}
func (e errNoArchivesFound) Error() string {
return fmt.Sprintf("no archives found matching goos=[darwin linux] goarch=[amd64 arm arm64 386] goarm=[6 7] goamd64=%s ids=%v", e.goamd64, e.ids)
}
var (
errNoRepoName = pipe.Skip("repository name is not set")
errSkipUpload = pipe.Skip("nix.skip_upload is set")
errSkipUploadAuto = pipe.Skip("nix.skip_upload is set to 'auto', and current version is a pre-release")
)
// NewBuild returns a pipe to be used in the build phase.
func NewBuild() Pipe {
return Pipe{buildShaPrefetcher{}}
}
// NewPublish returns a pipe to be used in the publish phase.
func NewPublish() Pipe {
return Pipe{publishShaPrefetcher{
bin: nixPrefetchURLBin,
}}
}
type Pipe struct {
prefetcher shaPrefetcher
}
func (Pipe) String() string { return "nixpkgs" }
func (Pipe) ContinueOnError() bool { return true }
func (Pipe) Dependencies(_ *context.Context) []string { return []string{"nix-prefetch-url"} }
func (p Pipe) Skip(ctx *context.Context) bool {
return len(ctx.Config.Nix) == 0 || !p.prefetcher.Available()
}
func (Pipe) Default(ctx *context.Context) error {
for i := range ctx.Config.Nix {
nix := &ctx.Config.Nix[i]
nix.CommitAuthor = commitauthor.Default(nix.CommitAuthor)
if nix.CommitMessageTemplate == "" {
nix.CommitMessageTemplate = "{{ .ProjectName }}: {{ .PreviousTag }} -> {{ .Tag }}"
}
if nix.Name == "" {
nix.Name = ctx.Config.ProjectName
}
if nix.Goamd64 == "" {
nix.Goamd64 = "v1"
}
}
return nil
}
func (p Pipe) Run(ctx *context.Context) error {
cli, err := client.New(ctx)
if err != nil {
return err
}
return p.runAll(ctx, cli)
}
// Publish .
func (p Pipe) Publish(ctx *context.Context) error {
cli, err := client.New(ctx)
if err != nil {
return err
}
return p.publishAll(ctx, cli)
}
func (p Pipe) runAll(ctx *context.Context, cli client.Client) error {
for _, nix := range ctx.Config.Nix {
err := p.doRun(ctx, nix, cli)
if err != nil {
return err
}
}
return nil
}
func (p Pipe) publishAll(ctx *context.Context, cli client.Client) error {
skips := pipe.SkipMemento{}
for _, nix := range ctx.Artifacts.Filter(artifact.ByType(artifact.Nixpkg)).List() {
err := doPublish(ctx, p.prefetcher, cli, nix)
if err != nil && pipe.IsSkip(err) {
skips.Remember(err)
continue
}
if err != nil {
return err
}
}
return skips.Evaluate()
}
func (p Pipe) doRun(ctx *context.Context, nix config.Nix, cl client.ReleaserURLTemplater) error {
if nix.Repository.Name == "" {
return errNoRepoName
}
var err error
nix.Name, err = tmpl.New(ctx).Apply(nix.Name)
if err != nil {
return err
}
nix.Repository, err = client.TemplateRef(tmpl.New(ctx).Apply, nix.Repository)
if err != nil {
return err
}
nix.SkipUpload, err = tmpl.New(ctx).Apply(nix.SkipUpload)
if err != nil {
return err
}
nix.Homepage, err = tmpl.New(ctx).Apply(nix.Homepage)
if err != nil {
return err
}
nix.Description, err = tmpl.New(ctx).Apply(nix.Description)
if err != nil {
return err
}
nix.Path, err = tmpl.New(ctx).Apply(nix.Path)
if err != nil {
return err
}
if nix.Path == "" {
nix.Path = path.Join("pkgs", nix.Name, "default.nix")
}
path := filepath.Join(ctx.Config.Dist, "nix", nix.Path)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
content, err := preparePkg(ctx, nix, cl, p.prefetcher)
if err != nil {
return err
}
log.WithField("nixpkg", path).Info("writing")
if err := os.WriteFile(path, []byte(content), 0o644); err != nil { //nolint: gosec
return fmt.Errorf("failed to write nixpkg: %w", err)
}
ctx.Artifacts.Add(&artifact.Artifact{
Name: filepath.Base(path),
Path: path,
Type: artifact.Nixpkg,
Extra: map[string]interface{}{
nixConfigExtra: nix,
},
})
return nil
}
func preparePkg(
ctx *context.Context,
nix config.Nix,
cli client.ReleaserURLTemplater,
prefetcher shaPrefetcher,
) (string, error) {
filters := []artifact.Filter{
artifact.Or(
artifact.ByGoos("darwin"),
artifact.ByGoos("linux"),
),
artifact.Or(
artifact.And(
artifact.ByGoarch("amd64"),
artifact.ByGoamd64(nix.Goamd64),
),
artifact.And(
artifact.ByGoarch("arm"),
artifact.Or(
artifact.ByGoarm("6"),
artifact.ByGoarm("7"),
),
),
artifact.ByGoarch("arm64"),
artifact.ByGoarch("386"),
artifact.ByGoarch("all"),
),
artifact.And(
artifact.ByFormats("zip", "tar.gz"),
artifact.ByType(artifact.UploadableArchive),
),
artifact.OnlyReplacingUnibins,
}
if len(nix.IDs) > 0 {
filters = append(filters, artifact.ByIDs(nix.IDs...))
}
archives := ctx.Artifacts.Filter(artifact.And(filters...)).List()
if len(archives) == 0 {
return "", errNoArchivesFound{
goamd64: nix.Goamd64,
ids: nix.IDs,
}
}
if nix.URLTemplate == "" {
url, err := cli.ReleaseURLTemplate(ctx)
if err != nil {
return "", err
}
nix.URLTemplate = url
}
installs, err := installs(ctx, nix, archives[0])
if err != nil {
return "", err
}
postInstall, err := postInstall(ctx, nix, archives[0])
if err != nil {
return "", err
}
folder := artifact.ExtraOr(*archives[0], artifact.ExtraWrappedIn, ".")
if folder == "" {
folder = "."
}
data := templateData{
Name: nix.Name,
Version: ctx.Version,
Install: installs,
PostInstall: postInstall,
Archives: map[string]Archive{},
SourceRoot: folder,
Description: nix.Description,
Homepage: nix.Homepage,
License: nix.License,
}
platforms := map[string]bool{}
for _, art := range archives {
url, err := tmpl.New(ctx).WithArtifact(art).Apply(nix.URLTemplate)
if err != nil {
return "", err
}
sha, err := prefetcher.Prefetch(url)
if err != nil {
return "", err
}
archive := Archive{
URL: url,
Sha: sha,
}
for _, goarch := range expandGoarch(art.Goarch) {
data.Archives[art.Goos+goarch+art.Goarm] = archive
plat := goosToPlatform[art.Goos+goarch+art.Goarm]
platforms[plat] = true
}
}
data.Platforms = keys(platforms)
sort.Strings(data.Platforms)
return doBuildPkg(ctx, data)
}
func expandGoarch(goarch string) []string {
if goarch == "all" {
return []string{"amd64", "arm64"}
}
return []string{goarch}
}
var goosToPlatform = map[string]string{
"linuxamd64": "x86_64-linux",
"linuxarm64": "aarch64-linux",
"linuxarm6": "armv6l-linux",
"linuxarm7": "armv7l-linux",
"linux386": "i686-linux",
"darwinamd64": "x86_64-darwin",
"darwinarm64": "aarch64-darwin",
}
func keys(m map[string]bool) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func doPublish(ctx *context.Context, prefetcher shaPrefetcher, cl client.Client, pkg *artifact.Artifact) error {
nix, err := artifact.Extra[config.Nix](*pkg, nixConfigExtra)
if err != nil {
return err
}
if strings.TrimSpace(nix.SkipUpload) == "true" {
return errSkipUpload
}
if strings.TrimSpace(nix.SkipUpload) == "auto" && ctx.Semver.Prerelease != "" {
return errSkipUploadAuto
}
repo := client.RepoFromRef(nix.Repository)
gpath := nix.Path
msg, err := tmpl.New(ctx).Apply(nix.CommitMessageTemplate)
if err != nil {
return err
}
author, err := commitauthor.Get(ctx, nix.CommitAuthor)
if err != nil {
return err
}
content, err := preparePkg(ctx, nix, cl, prefetcher)
if err != nil {
return err
}
if nix.Repository.Git.URL != "" {
return client.NewGitUploadClient(repo.Branch).
CreateFile(ctx, author, repo, []byte(content), gpath, msg)
}
cl, err = client.NewIfToken(ctx, cl, nix.Repository.Token)
if err != nil {
return err
}
if !nix.Repository.PullRequest.Enabled {
return cl.CreateFile(ctx, author, repo, []byte(content), gpath, msg)
}
log.Info("nix.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener)
if !ok {
return fmt.Errorf("client does not support pull requests")
}
if err := cl.CreateFile(ctx, author, repo, []byte(content), gpath, msg); err != nil {
return err
}
return pcl.OpenPullRequest(ctx, client.Repo{
Name: nix.Repository.PullRequest.Base.Name,
Owner: nix.Repository.PullRequest.Base.Owner,
Branch: nix.Repository.PullRequest.Base.Branch,
}, repo, msg, nix.Repository.PullRequest.Draft)
}
func doBuildPkg(ctx *context.Context, data templateData) (string, error) {
t, err := template.
New(data.Name).
Parse(string(pkgTmpl))
if err != nil {
return "", err
}
var out bytes.Buffer
if err := t.Execute(&out, data); err != nil {
return "", err
}
content, err := tmpl.New(ctx).Apply(out.String())
if err != nil {
return "", err
}
out.Reset()
// Sanitize the template output and get rid of trailing whitespace.
var (
r = strings.NewReader(content)
s = bufio.NewScanner(r)
)
for s.Scan() {
l := strings.TrimRight(s.Text(), " ")
_, _ = out.WriteString(l)
_ = out.WriteByte('\n')
}
if err := s.Err(); err != nil {
return "", err
}
return out.String(), nil
}
func postInstall(ctx *context.Context, nix config.Nix, art *artifact.Artifact) ([]string, error) {
applied, err := tmpl.New(ctx).WithArtifact(art).Apply(nix.PostInstall)
if err != nil {
return nil, err
}
return split(applied), nil
}
func installs(ctx *context.Context, nix config.Nix, art *artifact.Artifact) ([]string, error) {
applied, err := tmpl.New(ctx).WithArtifact(art).Apply(nix.Install)
if err != nil {
return nil, err
}
if applied != "" {
return split(applied), nil
}
result := []string{"mkdir -p $out/bin"}
for _, bin := range artifact.ExtraOr(*art, artifact.ExtraBinaries, []string{}) {
result = append(result, fmt.Sprintf("cp -vr ./%s $out/bin/%[1]s", bin))
}
log.WithField("install", result).Warnf("guessing install")
return result, nil
}
func split(s string) []string {
var result []string
for _, line := range strings.Split(strings.TrimSpace(s), "\n") {
line := strings.TrimSpace(line)
if line == "" {
continue
}
result = append(result, line)
}
return result
}
type shaPrefetcher interface {
Prefetch(url string) (string, error)
Available() bool
}
const (
zeroHash = "0000000000000000000000000000000000000000000000000000"
nixPrefetchURLBin = "nix-prefetch-url"
)
type buildShaPrefetcher struct{}
func (buildShaPrefetcher) Prefetch(_ string) (string, error) { return zeroHash, nil }
func (buildShaPrefetcher) Available() bool { return true }
type publishShaPrefetcher struct {
bin string
}
func (p publishShaPrefetcher) Available() bool {
_, err := exec.LookPath(p.bin)
if err != nil {
log.Warnf("%s is not available", p.bin)
}
return err == nil
}
func (p publishShaPrefetcher) Prefetch(url string) (string, error) {
out, err := exec.Command(p.bin, url).Output()
outStr := strings.TrimSpace(string(out))
if err != nil {
return "", fmt.Errorf("could not prefetch url: %s: %w: %s", url, err, outStr)
}
return outStr, nil
}