Skip to content

[dockerfile] Copying local flakes in dockerfile #1154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/boxcli/generate/devcontainer_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ type vscode struct {

type dockerfileData struct {
IsDevcontainer bool
LocalFlakeDirs []string
}

// CreateDockerfile creates a Dockerfile in path and writes devcontainerDockerfile.tmpl's content into it
func CreateDockerfile(tmplFS embed.FS, path string, isDevcontainer bool) error {
func CreateDockerfile(tmplFS embed.FS, path string, localFlakeDirs []string, isDevcontainer bool) error {
// create dockerfile
file, err := os.Create(filepath.Join(path, "Dockerfile"))
if err != nil {
Expand All @@ -52,7 +53,10 @@ func CreateDockerfile(tmplFS embed.FS, path string, isDevcontainer bool) error {
tmplName := "devcontainerDockerfile.tmpl"
t := template.Must(template.ParseFS(tmplFS, "tmpl/"+tmplName))
// write content into file
return t.Execute(file, &dockerfileData{IsDevcontainer: isDevcontainer})
return t.Execute(file, &dockerfileData{
IsDevcontainer: isDevcontainer,
LocalFlakeDirs: localFlakeDirs,
})
}

// CreateDevcontainer creates a devcontainer.json in path and writes getDevcontainerContent's output into it
Expand Down
4 changes: 2 additions & 2 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (d *Devbox) GenerateDevcontainer(force bool) error {
redact.Safe(filepath.Base(devContainerPath)), err)
}
// generate dockerfile
err = generate.CreateDockerfile(tmplFS, devContainerPath, true /* isDevcontainer */)
err = generate.CreateDockerfile(tmplFS, devContainerPath, d.getLocalFlakesDirs(), true /* isDevcontainer */)
if err != nil {
return redact.Errorf("error generating dev container Dockerfile in <project>/%s: %w",
redact.Safe(filepath.Base(devContainerPath)), err)
Expand All @@ -419,7 +419,7 @@ func (d *Devbox) GenerateDockerfile(force bool) error {
}

// generate dockerfile
return errors.WithStack(generate.CreateDockerfile(tmplFS, d.projectDir, false /* isDevcontainer */))
return errors.WithStack(generate.CreateDockerfile(tmplFS, d.projectDir, d.getLocalFlakesDirs(), false /* isDevcontainer */))
}

func PrintEnvrcContent(w io.Writer) error {
Expand Down
20 changes: 20 additions & 0 deletions internal/impl/flakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package impl

import (
"strings"

"github.com/samber/lo"

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

return goutil.PickByKeysSorted(inputs, order), nil
}

// getLocalFlakesDirs searches packages and returns list of directories
// of local flakes that are mentioned in config.
// e.g., path:./my-flake#packageName -> ./my-flakes
func (d *Devbox) getLocalFlakesDirs() []string {
localFlakeDirs := []string{}

// searching through installed packages to get location of local flakes
for _, pkg := range d.Packages() {
// filtering local flakes packages
if strings.HasPrefix(pkg, "path:") {
pkgDirAndName, _ := strings.CutPrefix(pkg, "path:")
pkgDir := strings.Split(pkgDirAndName, "#")[0]
localFlakeDirs = append(localFlakeDirs, pkgDir)
}
}
return localFlakeDirs
}
6 changes: 6 additions & 0 deletions internal/impl/tmpl/devcontainerDockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ ENV PATH="/home/${DEVBOX_USER}/.nix-profile/bin:/home/${DEVBOX_USER}/.devbox/nix
WORKDIR /code
RUN sudo chown $DEVBOX_USER:root /code
COPY devbox.json devbox.json
{{if len .LocalFlakeDirs}}
# Step 6: Copying local flakes directories
{{- end}}
{{range $i, $element := .LocalFlakeDirs -}}
COPY {{$element}} {{$element}}
{{end}}
RUN devbox install
{{if .IsDevcontainer}}
RUN devbox shellenv --init-hook >> ~/.profile
Expand Down