Skip to content

User defaults: persist any dagger function argument to .env - #11034

Merged
shykes merged 1 commit into
dagger:mainfrom
shykes:local-defaults
Oct 7, 2025
Merged

shykes merged 1 commit into
dagger:mainfrom
shykes:local-defaults

Conversation

@shykes

@shykes shykes commented Sep 12, 2025 •

Copy link
Copy Markdown
Contributor

Overview

Local defaults: user can add default arguments to their modules, using a local .env file.

This avoids typing too many CLI arguments on every call.

How it works

  1. Choose a module argument you want to persist locally. For example, the argument token of the module github.com/shykes/x/discord
  2. Pick a location for your .env file. This could be in the module directory, or in a parent directory on your filesystem, depending on when you want it to be loaded. See standard .env conventions.
  3. Write the argument as a variable in .env. The variable name must be <module name>_<argument name> (case-insensitive). If your .env file is within your module directory, you can also use the variable name <argument name> (the module name is implicit). You can combine both notations. Variables that don't match this convention will be ignored. The variable value should be the same value you passed explicitly on the CLI. For example if you called dagger -m github.com/shykes/x/discord --token=op://foo/bar, your variable should be DISCORD_TOKEN=op://foo/bar.
  4. Call your module the usual way, but omit the argument. For example github.com/shykes/x/discord -c servers.

Supported values

Variable values support the same format as command-line arguments:

  • Literal scalar values
  • JSON-encoded arrays
  • Directory: local path or git address
  • File: local path or git address
  • Socket: local path
  • Container: container image address
  • Secret: url with schemes op:// (1password), vault:// (hashicorp vault), env:// (environment variable), cmd:// (command execution), file:// (local file path), libsecret:// (libsecret provider)
  • GitRepository: git address
  • GitRef: git address
  • Service local tcp:// url

Example

cat > .env <<.
DISCORD_TOKEN=op://myvault/mydiscordtoken/credential
.
dagger -m github.com/shykes/x/discord -c servers

--> This will list your discord servers, using the token in the .env file.

TODO

  • dagger --env-file to select an alternative filename
  • dagger --no-env-file to disable local defaults entirely
  • Clean up implementation
  • Local defaults can be cleanly introspected by the CLI and SDKs
  • Support arguments to functions other than the constructor (as requested by @nipuna-perera and @nadirollo )
  • Clearly tell the user which default was applied where
  • Evaluate quotes (with escaping supported) in env file
  • Tests
  • Don't inject defaults in dependencies by default. Make it a configurable opt-in

See also

@shykes
shykes requested review from a team as code owners September 12, 2025 08:22
@shykes
shykes force-pushed the local-defaults branch 3 times, most recently from 264c67a to 9b7e6cd Compare September 16, 2025 00:01
@nipuna-perera

Copy link
Copy Markdown
Contributor

Can't wait to try this! Does this still only support constructor args? Or is it expanded to support any arg?

@shykes

shykes commented Sep 16, 2025

Copy link
Copy Markdown
Contributor Author

@nipuna-perera I'm making my way to "any args" :)

@shykes
shykes force-pushed the local-defaults branch 3 times, most recently from d6f8ce2 to 3cdac2f Compare September 16, 2025 21:31
@shykes

shykes commented Sep 16, 2025

Copy link
Copy Markdown
Contributor Author

I added suppport for implicit module name.

@nadirollo

Copy link
Copy Markdown

This is going to be a game changer for us! With this we can finally replace many makefiles/justfiles that wrap around dagger!
If it ends up supporting function args, that would be amazing :)

Can't wait to try this out 👍

@shykes

shykes commented Sep 16, 2025

Copy link
Copy Markdown
Contributor Author

This is going to be a game changer for us! With this we can finally replace many makefiles/justfiles that wrap around dagger!

Awesome! Yes, removing the need for wrappers is the goal :)

If it ends up supporting function args, that would be amazing :)

Do you mean arguments to functions other than the constructor? Yeah I'm working on that right now :) @nipuna-perera also asked for that.

May I ask, are you thinking of functions attached to the top-level object? Or even further, functions attached to potentially any object type in the module?

@shykes shykes changed the title Local defaults [draft] Local defaults Sep 16, 2025
@nadirollo

Copy link
Copy Markdown

I think args to the top-level object would be great. Our blueprints are "simple" enough that don't require many chainable objects (yet...?)

@nipuna-perera

Copy link
Copy Markdown
Contributor

May I ask, are you thinking of functions attached to the top-level object? Or even further, functions attached to potentially any object type in the module?

I replied here too - https://discord.com/channels/707636530424053791/1413254056188579901/1417690771175243828

I think it should apply uniformly to any dagger addressable object functions. Otherwise it adds a layer of complexity. One may expect a chained function to have the same behavior of the main dagger object. It's probably a niche use case but for the sake of UX I don't see a drawback of it applying to everything.

@babo

babo commented Sep 18, 2025

Copy link
Copy Markdown

Is this supports dagger.Secret as a string value or as a file?

@shykes

shykes commented Sep 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Is this supports dagger.Secret as a string value or as a file?

It supports all the same secret addresses as for CLI arguments:

  • file: file://
  • env variable: env://
  • 1password: op://
  • hashicorp vault: vault://

I don't believe literal string values are supported

See https://docs.dagger.io/features/secrets

@shykes

shykes commented Oct 3, 2025

Copy link
Copy Markdown
Contributor Author

This looks like a legitimate test failure... https://dagger.cloud/dagger/traces/3a0f35ffcb5847a40c5bae36330d3eda?span=a2ef985d225c652e

TestCall/TestByName/local_ref_with_@

To repro locally: dagger call -m github.com/shykes/dagger@local-defaults test specific --run=TestCall/TestByName/local_ref_with_@

@shykes
shykes force-pushed the local-defaults branch 2 times, most recently from 704fd08 to 87c6f5b Compare October 3, 2025 19:52
@marcofranssen

Copy link
Copy Markdown

I'm mostly using .envrc files used by direnv. Maybe just relying on exported envars is sufficient and leaving the rest to tools like direnv.

Or at least support both .env and .envrc

@shykes
shykes requested review from jedevc, sipsma and vito October 6, 2025 20:00
@shykes

shykes commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

I'm mostly using .envrc files used by direnv. Maybe just relying on exported envars is sufficient and leaving the rest to tools like direnv.

Or at least support both .env and .envrc

Supporting .envrc seems like a can of worms, and feature creep on Dagger's part. Our starting point was a Dagger-specific config file, something like dagger.env.json or something like that. But it seemed like a nice convenience to piggyback on a pre-existing standard instead. The extreme simplicity of .env was well-suited.

Expanding into .envrc and other more complex systems, with dependencies and edge cases, seems like a recipe for disaster.

I'll think about the idea of also supporting straight system env variables. With the scaffolding in place, this could be done safely I think. I just need to think through the UX implications - for example there's an elevated risk of accidental false positives. (eg. your dagger module accepts an argument token and you happen to have a variable TOKEN in your system environment, so they match, but you did not expect or want them to.

In the short term, we're going to merge .env support only, see how people use and how many problems it can solve. Then discuss our next move.

I understand how you would prefer to do everything with your favorite environment config (in your case .envrc), but realistically you're always going to deal with tool-specific config files. Think of this .env as a Dagger-specific config file.

@sipsma

sipsma commented Oct 6, 2025

Copy link
Copy Markdown
Contributor

This looks like a legitimate test failure... https://dagger.cloud/dagger/traces/3a0f35ffcb5847a40c5bae36330d3eda?span=a2ef985d225c652e

TestCall/TestByName/local_ref_with_@

To repro locally: dagger call -m github.com/shykes/dagger@local-defaults test specific --run=TestCall/TestByName/local_ref_with_@

It seems like quite a few tests besides that one are failing. From ones I looked at it seems like they are failing during dagger init because the code is trying to read the inner env file and failing (due to the fact that the directory wasn't created yet):

load user defaults: failed to check for inner env file in /work/test@test: select: failed to get snapshot: failed to receive stat message: rpc error: code = NotFound desc = get full root path: rpc error: code = NotFound desc = eval symlinks: lstat /work/test@test: no such file or directory

This chunk of code looks wrong:

dagger/core/modulesource.go

Lines 291 to 301 in e577d90

); err != nil {
return nil, "", fmt.Errorf("failed to check for inner env file in %s: %w",
moduleDirPath, err)
} // Check for file not found error specifically
var fileNotFoundErr *os.PathError
if errors.As(err, &fileNotFoundErr) && fileNotFoundErr.Op == "open" {
return nil, "", nil
}
if !envFileExists {
return nil, "", nil
}

It unconditionally returns an error when err != nil but then attempts to handle certain errors.

@marcofranssen

marcofranssen commented Oct 6, 2025 •

Copy link
Copy Markdown

I'm mostly using .envrc files used by direnv. Maybe just relying on exported envars is sufficient and leaving the rest to tools like direnv.
Or at least support both .env and .envrc

Supporting .envrc seems like a can of worms, and feature creep on Dagger's part. Our starting point was a Dagger-specific config file, something like dagger.env.json or something like that. But it seemed like a nice convenience to piggyback on a pre-existing standard instead. The extreme simplicity of .env was well-suited.

Expanding into .envrc and other more complex systems, with dependencies and edge cases, seems like a recipe for disaster.

I'll think about the idea of also supporting straight system env variables. With the scaffolding in place, this could be done safely I think. I just need to think through the UX implications - for example there's an elevated risk of accidental false positives. (eg. your dagger module accepts an argument token and you happen to have a variable TOKEN in your system environment, so they match, but you did not expect or want them to.

In the short term, we're going to merge .env support only, see how people use and how many problems it can solve. Then discuss our next move.

I understand how you would prefer to do everything with your favorite environment config (in your case .envrc), but realistically you're always going to deal with tool-specific config files. Think of this .env as a Dagger-specific config file.

Honestly I think .env vs .envrc there is not much difference.

.env is usually used with the tool dotenv while .envrc is usually used with the tool direnv. Both tools add the variables to your shell exports. From a format perspective in .envrc I have to explicitly add the exports MY_VAR while in .env I can just define it as MY_VAR.

Working with dagger and other tools in our projects we solely rely on the shell exports for things to work the way we want. (direnv helps us loading and unloading the variables when switching directories) to not have side effects as you described above.

I think for a better integration with other tooling and projects it would be a huge benefit if this was less opiniated and just treat these things as env vars.

At the moment I therefore do Makefiles to dynamically compose the dagger commands based on shell exports being available or not. Which makes things very verbose and from a UX perspective massively long commands to pass in all the secret variables and such.

Why not pickup all env vars with a convention like DAGGER_MYMODULE_VAR

@shykes

shykes commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

This looks like a legitimate test failure... https://dagger.cloud/dagger/traces/3a0f35ffcb5847a40c5bae36330d3eda?span=a2ef985d225c652e
TestCall/TestByName/local_ref_with_@
To repro locally: dagger call -m github.com/shykes/dagger@local-defaults test specific --run=TestCall/TestByName/local_ref_with_@

It seems like quite a few tests besides that one are failing. From ones I looked at it seems like they are failing during dagger init because the code is trying to read the inner env file and failing (due to the fact that the directory wasn't created yet):

load user defaults: failed to check for inner env file in /work/test@test: select: failed to get snapshot: failed to receive stat message: rpc error: code = NotFound desc = get full root path: rpc error: code = NotFound desc = eval symlinks: lstat /work/test@test: no such file or directory

This chunk of code looks wrong:

dagger/core/modulesource.go

Lines 291 to 301 in e577d90

); err != nil {
return nil, "", fmt.Errorf("failed to check for inner env file in %s: %w",
moduleDirPath, err)
} // Check for file not found error specifically
var fileNotFoundErr *os.PathError
if errors.As(err, &fileNotFoundErr) && fileNotFoundErr.Op == "open" {
return nil, "", nil
}
if !envFileExists {
return nil, "", nil
}

It unconditionally returns an error when err != nil but then attempts to handle certain errors.

Sorry about that @sipsma ... This looks like residue from my trying to find an answer to this: #11034 (comment).

@shykes

shykes commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

Working with dagger and other tools in our projects we solely rely on the shell exports for things to work the way we want. (direnv helps us loading and unloading the variables when switching directories) to not have side effects as you described above.

I think for a better integration with other tooling and projects it would be a huge benefit if this was less opiniated and just treat these things as env vars.

The problem is there no such thing as "less opinionated". Your approach of relying on system variables + a tool to manage them is by no means a universally accepted method. The ecosystem is highly fragmented. For example if we only supported system env variables, someone would make the exact same argument as you, in reverse: "why not support .env"?

At the moment I therefore do Makefiles to dynamically compose the dagger commands based on shell exports being available or not. Which makes things very verbose and from a UX perspective massively long commands to pass in all the secret variables and such.

Once this PR is merged, you have another option, which is to write a .env. I understand it's not your favorite option, you would prefer to manage everything with direnv. But just wanted to clarify that you don't have to write a Makefile wrapper or run super-long commands every time. You just prefer the inconvenience of a Makefile wrapper to the inconvenience of writing a .env.

Why not pickup all env vars with a convention like DAGGER_MYMODULE_VAR

Yes we might do that, as I said before. I need to think about the implications. But, it won't be a universal solution either, because not everyone uses system env vars to configure their tools.

@shykes

shykes commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

This chunk of code looks wrong:

dagger/core/modulesource.go

Lines 291 to 301 in e577d90

); err != nil {
return nil, "", fmt.Errorf("failed to check for inner env file in %s: %w",
moduleDirPath, err)
} // Check for file not found error specifically
var fileNotFoundErr *os.PathError
if errors.As(err, &fileNotFoundErr) && fileNotFoundErr.Op == "open" {
return nil, "", nil
}
if !envFileExists {
return nil, "", nil
}

I removed the offending error checking code... But still getting failed tests, investigating further.

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shykes
shykes force-pushed the local-defaults branch 2 times, most recently from 319a427 to a7c06f3 Compare October 6, 2025 22:28
@shykes shykes mentioned this pull request Oct 6, 2025
10 tasks
@shykes
shykes dismissed jedevc’s stale review October 6, 2025 23:26

Not actually "dismissing", I believe all issues have been discussed & addressed already :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants