-
Notifications
You must be signed in to change notification settings - Fork 177
/
flake_plan.go
153 lines (132 loc) · 4.13 KB
/
flake_plan.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
package shellgen
import (
"context"
"fmt"
"path/filepath"
"runtime/trace"
"strings"
"go.jetpack.io/devbox/internal/devpkg"
"go.jetpack.io/devbox/internal/nix"
)
// flakePlan contains the data to populate the top level flake.nix file
// that builds the devbox environment
type flakePlan struct {
BinaryCache string
NixpkgsInfo *NixpkgsInfo
Packages []*devpkg.Package
FlakeInputs []flakeInput
System string
}
func newFlakePlan(ctx context.Context, devbox devboxer) (*flakePlan, error) {
ctx, task := trace.NewTask(ctx, "devboxFlakePlan")
defer task.End()
for _, pluginConfig := range devbox.Config().IncludedPluginConfigs() {
if err := devbox.PluginManager().CreateFilesForConfig(pluginConfig); err != nil {
return nil, err
}
}
packages := devbox.InstallablePackages()
// Fill the NarInfo Cache concurrently as a perf-optimization, prior to invoking
// IsInBinaryCache in flakeInputs() and in the flake.nix template.
if err := devpkg.FillNarInfoCache(ctx, packages...); err != nil {
return nil, err
}
flakeInputs := flakeInputs(ctx, packages)
nixpkgsInfo := getNixpkgsInfo(devbox.Config().NixPkgsCommitHash())
// This is an optimization. Try to reuse the nixpkgs info from the flake
// inputs to avoid introducing a new one.
for _, input := range flakeInputs {
if input.IsNixpkgs() {
nixpkgsInfo = getNixpkgsInfo(input.HashFromNixPkgsURL())
break
}
}
return &flakePlan{
BinaryCache: devpkg.BinaryCache,
FlakeInputs: flakeInputs,
NixpkgsInfo: nixpkgsInfo,
Packages: packages,
System: nix.System(),
}, nil
}
func (f *flakePlan) needsGlibcPatch() bool {
for _, in := range f.FlakeInputs {
if in.URL == glibcPatchFlakeRef {
return true
}
}
return false
}
type glibcPatchFlake struct {
// NixpkgsGlibcFlakeRef is a flake reference to the nixpkgs flake
// containing the new glibc package.
NixpkgsGlibcFlakeRef string
// Inputs is the attribute set of flake inputs. The key is the input
// name and the value is a flake reference.
Inputs map[string]string
// Outputs is the attribute set of flake outputs. It follows the
// standard flake output schema of system.name = derivation. The
// derivation can be any valid Nix expression.
Outputs struct {
Packages map[string]map[string]string
}
}
func newGlibcPatchFlake(nixpkgsGlibcRev string, packages []*devpkg.Package) (glibcPatchFlake, error) {
flake := glibcPatchFlake{NixpkgsGlibcFlakeRef: "flake:nixpkgs/" + nixpkgsGlibcRev}
for _, pkg := range packages {
if !pkg.PatchGlibc() {
continue
}
err := flake.addPackageOutput(pkg)
if err != nil {
return glibcPatchFlake{}, err
}
}
return flake, nil
}
func (g *glibcPatchFlake) addPackageOutput(pkg *devpkg.Package) error {
if g.Inputs == nil {
g.Inputs = make(map[string]string)
}
inputName := pkg.FlakeInputName()
g.Inputs[inputName] = pkg.URLForFlakeInput()
attrPath, err := pkg.FullPackageAttributePath()
if err != nil {
return err
}
// Remove the legacyPackages.<system> prefix.
outputName := strings.SplitN(attrPath, ".", 3)[2]
if g.Outputs.Packages == nil {
g.Outputs.Packages = map[string]map[string]string{nix.System(): {}}
}
if cached, err := pkg.IsInBinaryCache(); err == nil && cached {
if expr, err := g.fetchClosureExpr(pkg); err == nil {
g.Outputs.Packages[nix.System()][outputName] = expr
return nil
}
}
g.Outputs.Packages[nix.System()][outputName] = strings.Join([]string{"pkgs", inputName, nix.System(), outputName}, ".")
return nil
}
// TODO: this only handles the first store path, but we should handle all of them
func (g *glibcPatchFlake) fetchClosureExpr(pkg *devpkg.Package) (string, error) {
storePaths, err := pkg.InputAddressedPaths()
if err != nil {
return "", err
}
if len(storePaths) == 0 {
return "", fmt.Errorf("no store path for package %s", pkg.Raw)
}
return fmt.Sprintf(`builtins.fetchClosure {
fromStore = "%s";
fromPath = "%s";
inputAddressed = true;
}`, devpkg.BinaryCache, storePaths[0]), nil
}
func (g *glibcPatchFlake) writeTo(dir string) error {
err := writeFromTemplate(dir, g, "glibc-patch.nix", "flake.nix")
if err != nil {
return err
}
return writeGlibcPatchScript(filepath.Join(dir, "glibc-patch.bash"))
}