1package models
2
3import (
4 "fmt"
5 "path"
6 "strings"
7
8 "tangled.sh/tangled.sh/core/api/tangled"
9)
10
11func nixConfStep() Step {
12 setupCmd := `echo 'extra-experimental-features = nix-command flakes' >> /etc/nix/nix.conf
13echo 'build-users-group = ' >> /etc/nix/nix.conf`
14 return Step{
15 Command: setupCmd,
16 Name: "Configure Nix",
17 }
18}
19
20// checkoutStep checks out the specified ref in the cloned repository.
21func checkoutStep(twf tangled.Pipeline_Workflow, tr tangled.Pipeline_TriggerMetadata) Step {
22 if twf.Clone.Skip {
23 return Step{}
24 }
25
26 var ref string
27 switch tr.Kind {
28 case "push":
29 ref = tr.Push.Ref
30 case "pull_request":
31 ref = tr.PullRequest.TargetBranch
32
33 // TODO: this needs to be specified in lexicon
34 case "manual":
35 ref = tr.Repo.DefaultBranch
36 }
37
38 checkoutCmd := fmt.Sprintf("git config advice.detachedHead false; git checkout --progress --force %s", ref)
39
40 return Step{
41 Command: checkoutCmd,
42 Name: "Checkout ref " + ref,
43 }
44}
45
46// cloneOptsAsSteps processes clone options and adds corresponding steps
47// to the beginning of the workflow's step list if cloning is not skipped.
48func cloneStep(twf tangled.Pipeline_Workflow, tr tangled.Pipeline_TriggerRepo, dev bool) Step {
49 if twf.Clone.Skip {
50 return Step{}
51 }
52
53 uri := "https://"
54 if dev {
55 uri = "http://"
56 tr.Knot = strings.ReplaceAll(tr.Knot, "localhost", "host.docker.internal")
57 }
58
59 cloneUrl := uri + path.Join(tr.Knot, tr.Did, tr.Repo)
60 cloneCmd := []string{"git", "clone", cloneUrl, "."}
61
62 // default clone depth is 1
63 cloneDepth := 1
64 if twf.Clone.Depth > 1 {
65 cloneDepth = int(twf.Clone.Depth)
66 }
67 cloneCmd = append(cloneCmd, []string{"--depth", fmt.Sprintf("%d", cloneDepth)}...)
68
69 if twf.Clone.Submodules {
70 cloneCmd = append(cloneCmd, "--recursive")
71 }
72
73 fmt.Println(strings.Join(cloneCmd, " "))
74
75 cloneStep := Step{
76 Command: strings.Join(cloneCmd, " "),
77 Name: "Clone repository into workspace",
78 }
79 return cloneStep
80}
81
82// dependencyStep processes dependencies defined in the workflow.
83// For dependencies using a custom registry (i.e. not nixpkgs), it collects
84// all packages and adds a single 'nix profile install' step to the
85// beginning of the workflow's step list.
86func dependencyStep(twf tangled.Pipeline_Workflow) *Step {
87 var customPackages []string
88
89 for _, d := range twf.Dependencies {
90 registry := d.Registry
91 packages := d.Packages
92
93 if registry == "nixpkgs" {
94 continue
95 }
96
97 // collect packages from custom registries
98 for _, pkg := range packages {
99 customPackages = append(customPackages, fmt.Sprintf("'%s#%s'", registry, pkg))
100 }
101 }
102
103 if len(customPackages) > 0 {
104 installCmd := "nix --extra-experimental-features nix-command --extra-experimental-features flakes profile install"
105 cmd := fmt.Sprintf("%s %s", installCmd, strings.Join(customPackages, " "))
106 installStep := Step{
107 Command: cmd,
108 Name: "Install custom dependencies",
109 Environment: map[string]string{
110 "NIX_NO_COLOR": "1",
111 "NIX_SHOW_DOWNLOAD_PROGRESS": "0",
112 },
113 }
114 return &installStep
115 }
116 return nil
117}