Skip to content

Commit a19be01

Browse files
authored
[dockerfile] Copying local flakes in dockerfile (#1154)
## Summary This PR addresses the issue in generated dockerfiles by `devbox generate dockerfile` and `devbox generate devcontainer` in which packages mentioned as local flakes are not copied over in dockerfile and user has to manually do that. Addresses: #1051 ## How was it tested? - compile - add a local flake (example: https://github.com/NixOS/templates/blob/master/go-hello/) - add local flake to devbox - ./devbox generate dockerfile - check the contents of generated dockerfile and confirm there is a Step 6 comment and COPY commands for each local flake.
1 parent 2b5de1d commit a19be01

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

internal/boxcli/generate/devcontainer_util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ type vscode struct {
3838

3939
type dockerfileData struct {
4040
IsDevcontainer bool
41+
LocalFlakeDirs []string
4142
}
4243

4344
// CreateDockerfile creates a Dockerfile in path and writes devcontainerDockerfile.tmpl's content into it
44-
func CreateDockerfile(tmplFS embed.FS, path string, isDevcontainer bool) error {
45+
func CreateDockerfile(tmplFS embed.FS, path string, localFlakeDirs []string, isDevcontainer bool) error {
4546
// create dockerfile
4647
file, err := os.Create(filepath.Join(path, "Dockerfile"))
4748
if err != nil {
@@ -52,7 +53,10 @@ func CreateDockerfile(tmplFS embed.FS, path string, isDevcontainer bool) error {
5253
tmplName := "devcontainerDockerfile.tmpl"
5354
t := template.Must(template.ParseFS(tmplFS, "tmpl/"+tmplName))
5455
// write content into file
55-
return t.Execute(file, &dockerfileData{IsDevcontainer: isDevcontainer})
56+
return t.Execute(file, &dockerfileData{
57+
IsDevcontainer: isDevcontainer,
58+
LocalFlakeDirs: localFlakeDirs,
59+
})
5660
}
5761

5862
// CreateDevcontainer creates a devcontainer.json in path and writes getDevcontainerContent's output into it

internal/impl/devbox.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ func (d *Devbox) GenerateDevcontainer(force bool) error {
422422
redact.Safe(filepath.Base(devContainerPath)), err)
423423
}
424424
// generate dockerfile
425-
err = generate.CreateDockerfile(tmplFS, devContainerPath, true /* isDevcontainer */)
425+
err = generate.CreateDockerfile(tmplFS, devContainerPath, d.getLocalFlakesDirs(), true /* isDevcontainer */)
426426
if err != nil {
427427
return redact.Errorf("error generating dev container Dockerfile in <project>/%s: %w",
428428
redact.Safe(filepath.Base(devContainerPath)), err)
@@ -449,7 +449,7 @@ func (d *Devbox) GenerateDockerfile(force bool) error {
449449
}
450450

451451
// generate dockerfile
452-
return errors.WithStack(generate.CreateDockerfile(tmplFS, d.projectDir, false /* isDevcontainer */))
452+
return errors.WithStack(generate.CreateDockerfile(tmplFS, d.projectDir, d.getLocalFlakesDirs(), false /* isDevcontainer */))
453453
}
454454

455455
func PrintEnvrcContent(w io.Writer) error {

internal/impl/flakes.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package impl
55

66
import (
7+
"strings"
8+
79
"github.com/samber/lo"
810

911
"go.jetpack.io/devbox/internal/goutil"
@@ -50,3 +52,21 @@ func (d *Devbox) flakeInputs() ([]*plansdk.FlakeInput, error) {
5052

5153
return goutil.PickByKeysSorted(inputs, order), nil
5254
}
55+
56+
// getLocalFlakesDirs searches packages and returns list of directories
57+
// of local flakes that are mentioned in config.
58+
// e.g., path:./my-flake#packageName -> ./my-flakes
59+
func (d *Devbox) getLocalFlakesDirs() []string {
60+
localFlakeDirs := []string{}
61+
62+
// searching through installed packages to get location of local flakes
63+
for _, pkg := range d.Packages() {
64+
// filtering local flakes packages
65+
if strings.HasPrefix(pkg, "path:") {
66+
pkgDirAndName, _ := strings.CutPrefix(pkg, "path:")
67+
pkgDir := strings.Split(pkgDirAndName, "#")[0]
68+
localFlakeDirs = append(localFlakeDirs, pkgDir)
69+
}
70+
}
71+
return localFlakeDirs
72+
}

internal/impl/tmpl/devcontainerDockerfile.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ ENV PATH="/home/${DEVBOX_USER}/.nix-profile/bin:/home/${DEVBOX_USER}/.devbox/nix
2626
WORKDIR /code
2727
RUN sudo chown $DEVBOX_USER:root /code
2828
COPY devbox.json devbox.json
29+
{{if len .LocalFlakeDirs}}
30+
# Step 6: Copying local flakes directories
31+
{{- end}}
32+
{{range $i, $element := .LocalFlakeDirs -}}
33+
COPY {{$element}} {{$element}}
34+
{{end}}
2935
RUN devbox install
3036
{{if .IsDevcontainer}}
3137
RUN devbox shellenv --init-hook >> ~/.profile

0 commit comments

Comments
 (0)