|
1 | 1 | package impl
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 | 4 | "fmt"
|
6 |
| - "io/fs" |
7 | 5 | "os"
|
8 | 6 | "os/exec"
|
9 | 7 | "path/filepath"
|
10 | 8 | "strings"
|
11 | 9 |
|
12 |
| - "github.com/fatih/color" |
13 | 10 | "github.com/pkg/errors"
|
14 | 11 | "go.jetpack.io/devbox/internal/boxcli/featureflag"
|
15 | 12 | "go.jetpack.io/devbox/internal/debug"
|
16 | 13 | "go.jetpack.io/devbox/internal/fileutil"
|
17 | 14 | "go.jetpack.io/devbox/internal/nix"
|
18 |
| - "go.jetpack.io/devbox/internal/xdg" |
19 | 15 | )
|
20 | 16 |
|
21 | 17 | // packages.go has functions for adding, removing and getting info about nix packages
|
@@ -53,10 +49,6 @@ func (d *Devbox) addPackagesToProfile(mode installMode) error {
|
53 | 49 | return nil
|
54 | 50 | }
|
55 | 51 |
|
56 |
| - if err := d.ensureNixpkgsPrefetched(); err != nil { |
57 |
| - return err |
58 |
| - } |
59 |
| - |
60 | 52 | pkgs, err := d.pendingPackagesForInstallation()
|
61 | 53 | if err != nil {
|
62 | 54 | return err
|
@@ -84,29 +76,17 @@ func (d *Devbox) addPackagesToProfile(mode installMode) error {
|
84 | 76 | stepNum := idx + 1
|
85 | 77 |
|
86 | 78 | stepMsg := fmt.Sprintf("[%d/%d] %s", stepNum, total, pkg)
|
87 |
| - fmt.Printf("%s\n", stepMsg) |
88 |
| - |
89 |
| - cmd := exec.Command( |
90 |
| - "nix", "profile", "install", |
91 |
| - "--profile", profileDir, |
92 |
| - "--priority", d.getPackagePriority(pkg), |
93 |
| - "--impure", // Needed to allow flags from environment to be used. |
94 |
| - nix.FlakeNixpkgs(d.cfg.Nixpkgs.Commit)+"#"+pkg, |
95 |
| - ) |
96 |
| - cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...) |
97 |
| - cmd.Stdout = &nixPackageInstallWriter{d.writer} |
98 | 79 |
|
99 |
| - cmd.Env = nix.DefaultEnv() |
100 |
| - cmd.Stderr = cmd.Stdout |
101 |
| - err = cmd.Run() |
102 |
| - if err != nil { |
103 |
| - fmt.Fprintf(d.writer, "%s: ", stepMsg) |
104 |
| - color.New(color.FgRed).Fprintf(d.writer, "Fail\n") |
105 |
| - return errors.Wrapf(err, "Command: %s", cmd) |
| 80 | + if err := nix.ProfileInstall(&nix.ProfileInstallArgs{ |
| 81 | + CustomStepMessage: stepMsg, |
| 82 | + ExtraFlags: []string{"--priority", d.getPackagePriority(pkg)}, |
| 83 | + NixpkgsCommit: d.cfg.Nixpkgs.Commit, |
| 84 | + Package: pkg, |
| 85 | + ProfilePath: profileDir, |
| 86 | + Writer: d.writer, |
| 87 | + }); err != nil { |
| 88 | + return err |
106 | 89 | }
|
107 |
| - |
108 |
| - fmt.Fprintf(d.writer, "%s: ", stepMsg) |
109 |
| - color.New(color.FgGreen).Fprintf(d.writer, "Success\n") |
110 | 90 | }
|
111 | 91 |
|
112 | 92 | return nil
|
@@ -146,6 +126,7 @@ func (d *Devbox) removePackagesFromProfile(pkgs []string) error {
|
146 | 126 | return errors.Errorf("Did not find AttributePath for package: %s", pkg)
|
147 | 127 | }
|
148 | 128 |
|
| 129 | + // TODO: unify this with nix.ProfileRemove |
149 | 130 | cmd := exec.Command("nix", "profile", "remove",
|
150 | 131 | "--profile", profileDir,
|
151 | 132 | attrPath,
|
@@ -240,99 +221,3 @@ func resetProfileDirForFlakes(profileDir string) (err error) {
|
240 | 221 |
|
241 | 222 | return errors.WithStack(os.Remove(profileDir))
|
242 | 223 | }
|
243 |
| - |
244 |
| -// ensureNixpkgsPrefetched runs the prefetch step to download the flake of the registry |
245 |
| -func (d *Devbox) ensureNixpkgsPrefetched() error { |
246 |
| - // Look up the cached map of commitHash:nixStoreLocation |
247 |
| - commitToLocation, err := d.nixpkgsCommitFileContents() |
248 |
| - if err != nil { |
249 |
| - return err |
250 |
| - } |
251 |
| - |
252 |
| - // Check if this nixpkgs.Commit is located in the local /nix/store |
253 |
| - location, isPresent := commitToLocation[d.cfg.Nixpkgs.Commit] |
254 |
| - if isPresent { |
255 |
| - if fi, err := os.Stat(location); err == nil && fi.IsDir() { |
256 |
| - // The nixpkgs for this commit hash is present, so we don't need to prefetch |
257 |
| - return nil |
258 |
| - } |
259 |
| - } |
260 |
| - |
261 |
| - fmt.Fprintf(d.writer, "Ensuring nixpkgs registry is downloaded.\n") |
262 |
| - cmd := exec.Command( |
263 |
| - "nix", "flake", "prefetch", |
264 |
| - nix.FlakeNixpkgs(d.cfg.Nixpkgs.Commit), |
265 |
| - ) |
266 |
| - cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...) |
267 |
| - cmd.Env = nix.DefaultEnv() |
268 |
| - cmd.Stdout = d.writer |
269 |
| - cmd.Stderr = cmd.Stdout |
270 |
| - if err := cmd.Run(); err != nil { |
271 |
| - fmt.Fprintf(d.writer, "Ensuring nixpkgs registry is downloaded: ") |
272 |
| - color.New(color.FgRed).Fprintf(d.writer, "Fail\n") |
273 |
| - return errors.Wrapf(err, "Command: %s", cmd) |
274 |
| - } |
275 |
| - fmt.Fprintf(d.writer, "Ensuring nixpkgs registry is downloaded: ") |
276 |
| - color.New(color.FgGreen).Fprintf(d.writer, "Success\n") |
277 |
| - |
278 |
| - return d.saveToNixpkgsCommitFile(commitToLocation) |
279 |
| -} |
280 |
| - |
281 |
| -func (d *Devbox) nixpkgsCommitFileContents() (map[string]string, error) { |
282 |
| - path := nixpkgsCommitFilePath() |
283 |
| - if !fileutil.Exists(path) { |
284 |
| - return map[string]string{}, nil |
285 |
| - } |
286 |
| - |
287 |
| - contents, err := os.ReadFile(path) |
288 |
| - if err != nil { |
289 |
| - return nil, errors.WithStack(err) |
290 |
| - } |
291 |
| - |
292 |
| - commitToLocation := map[string]string{} |
293 |
| - if err := json.Unmarshal(contents, &commitToLocation); err != nil { |
294 |
| - return nil, errors.WithStack(err) |
295 |
| - } |
296 |
| - return commitToLocation, nil |
297 |
| -} |
298 |
| - |
299 |
| -func (d *Devbox) saveToNixpkgsCommitFile(commitToLocation map[string]string) error { |
300 |
| - // Make a query to get the /nix/store path for this commit hash. |
301 |
| - cmd := exec.Command("nix", "flake", "prefetch", "--json", |
302 |
| - nix.FlakeNixpkgs(d.cfg.Nixpkgs.Commit), |
303 |
| - ) |
304 |
| - cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...) |
305 |
| - out, err := cmd.Output() |
306 |
| - if err != nil { |
307 |
| - return errors.WithStack(err) |
308 |
| - } |
309 |
| - |
310 |
| - // read the json response |
311 |
| - var prefetchData struct { |
312 |
| - StorePath string `json:"storePath"` |
313 |
| - } |
314 |
| - if err := json.Unmarshal(out, &prefetchData); err != nil { |
315 |
| - return errors.WithStack(err) |
316 |
| - } |
317 |
| - |
318 |
| - // Ensure the nixpkgs commit file path exists so we can write an update to it |
319 |
| - path := nixpkgsCommitFilePath() |
320 |
| - if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil && !errors.Is(err, fs.ErrExist) { |
321 |
| - return errors.WithStack(err) |
322 |
| - } |
323 |
| - |
324 |
| - // write to the map, jsonify it, and write that json to the nixpkgsCommit file |
325 |
| - commitToLocation[d.cfg.Nixpkgs.Commit] = prefetchData.StorePath |
326 |
| - serialized, err := json.Marshal(commitToLocation) |
327 |
| - if err != nil { |
328 |
| - return errors.WithStack(err) |
329 |
| - } |
330 |
| - |
331 |
| - err = os.WriteFile(path, serialized, 0644) |
332 |
| - return errors.WithStack(err) |
333 |
| -} |
334 |
| - |
335 |
| -func nixpkgsCommitFilePath() string { |
336 |
| - cacheDir := xdg.CacheSubpath("devbox") |
337 |
| - return filepath.Join(cacheDir, "nixpkgs.json") |
338 |
| -} |
0 commit comments