Skip to main content

Module Dependencies

You can call Dagger Functions from any other Dagger module in your own Dagger module simply by adding it as a module dependency with dagger install, as in the following example:

dagger install github.com/shykes/daggerverse/hello@v0.3.0

This module will be added to your dagger.json:

...
"dependencies": [
{
"name": "hello",
"source": "github.com/shykes/daggerverse/hello@54d86c6002d954167796e41886a47c47d95a626d"
}
]

When you add a dependency to your module with dagger install, the dependent module will be added to the code-generation routines and can be accessed from your own module's code.

The entrypoint to accessing dependent modules from your own module's code is dag, the Dagger client, which is pre-initialized. It contains all the core types (like Container, Directory, etc.), as well as bindings to any dependencies your module has declared.

Here is an example of accessing the installed hello module from your own module's code:

func (m *MyModule) Greeting(ctx context.Context) (string, error) {
return dag.Hello().Hello(ctx)
}

Here is a more complex example. It is a Dagger Function that utilizes a module from the Daggerverse to build a Go project, then chains a Dagger API method to open an interactive terminal session in the build directory.

First, install the module:

dagger install github.com/kpenfound/dagger-modules/golang@v0.2.0

Next, create a new Dagger Function:

package main

import (
"dagger/my-module/internal/dagger"
)

type MyModule struct{}

func (m *MyModule) Example(buildSrc *dagger.Directory, buildArgs []string) *dagger.Directory {
return dag.
Golang().
Build(buildArgs, dagger.GolangBuildOpts{Source: buildSrc}).
Terminal()
}

This Dagger Function accepts two arguments - the source directory and a list of build arguments - and does the following:

  • It invokes the Golang module via the dag Dagger client.
  • It calls a Dagger Function from the module to build the source code and return a just-in-time directory with the compiled binary.
  • It chains a core Dagger API method to open an interactive terminal session in the returned directory.

Here is an example call for this Dagger Function:

dagger call example --build-src=https://github.com/golang/example#master:/hello --build-args=.

You can also use local modules as dependencies. However, they must be stored in a sub-directory of your module. For example:

dagger install ./path/to/module