Repository navigation
Description
Overview
Dagger should natively support .env files. It's a de facto standard for managing environment-specific configuration in a lightweight, portable way. It is widely supported, included by docker and docker-compose.
Proposed design
Default behavior
If .env exists in the current directory, or any parent directory, dagger should load it by default.
--env-file
An env file can be specified explicitly with dagger --env-file PATH.
Secret references
Regular env file must store a secret value in plaintext. But Dagger allows you to keep only the reference of the secret in the file, minimizing the risk of secret leaks, and making it easier to rotate credentials.
Any variable value that matches the format of a Dagger secret reference, will automatically be parsed as a secret URI, and securely resolved by the engine.
For example:
USERNAME=admin
PASSWORD=op://Private/foo/credential
PROD_AWS_TOKEN=vault://foo/bar/prod/token
CLI usage
To use the variables with the dagger CLI:
- In
dagger shell, simply reference the variable anywhere in your commands with$FOOor${FOO}. - In
dagger call, this also works, but remember to use single-quotes around arguments containing a variable expansion, to prevent your system shell from doing the expansion with its own variables - unless that's what you want!
In both cases, variable expansion only uses the contents of the env file. The host environment variables are never used.
Usage by Dagger Functions: default secrets
Env files are an acceptable sandbox for env variables, in the same way that the git context is an acceptable sandbox for local directories.
This means that, once we support Env files, and as long as we forbid escaping the sandbox, we can now allow functions to define a default env variable for their secrets.
For example:
// Login to Dagger Cloud
function (m *DaggerCloud) Login(
// Dagger Cloud token for authentication
// +default="$DAGGER_CLOUD_TOKEN"
token *dagger.Secret,
) error {
// ....
}
Activity
jedevc commentedon Feb 14, 2025
Sounds good 🎉
My main question is whether transitive dependencies should be able to use default secrets from the original caller. E.g. CLI calls module A, module A calls module B, module B has a default secret - is it allowed to access it?
It feels like the feature is a little lacking if it can't (though you can always just explicitly pass the secret between A and B), but if you can, it feels like a huge ecosystem vulnerability waiting to happen.
I'd propose that we limit this feature to modules that are called directly by the user - at least for a first pass, and then we could lift this restriction in the future. Much easier to lift it later, than to try and patch it out if we change our minds. Maybe there's a way to protect against this kind of supply chain attack, but if so, I'm not really seeing it.
Another question is whether this might conflict with how people are already using
.envfiles today that don't contain secrets, but instead just contain their own data. Do we need a dagger-specific filename to avoid loading this then? If not, we might need a way to mitigate accidentally loading unintended values from it.Would we want to allow the
.envfile to contain non secret values? e.g. so you could have a defaultstringvalue with a pragma that reads from the environment. This feels like it would be very neat. E.g. in our own CI, we've got a variable for the ID of the S3 bucket we upload artifacts to - it's not a secret, but it would still be nice to upload from the environment.helderco commentedon Feb 14, 2025
I think this would be like context directories. When you have transitive dependencies with
defaultPath, they're relative to the module that defines it, not the one you're calling. So I'd say if module B has a default env var, it would have to have an.envin its sources. That brings up another question though. Some.envfiles could be committed to git to support this from a git source, but then you may still want a.envthat's specific to the caller, so how do we feel about supporting a suffix on the file name, either static or defined by the module author?For example, I think
dotenvsupports both.envand.env.local(and a bunch of other tools), so you can commit one for sharing and keep the other specific to you. Docker compose on the other hand makes you specify the filename so you can manage it however you want. I think the static.env.localmerged on top of.env(if exists) is simple enough to implement, gets out of your way, and is "standardized" enough that a lot of people are used to it. The downside is if you already have a.envfor other purposes, and you don't want to use it indagger.There's a limitation with secret providers here. The secret references are very "caller specific" which doesn't make much sense to commit to git, but could be useful in the context of a company, where secrets are stored in the same "place" (i.e., not necessarily in a central location, but at least with the same secret reference, even if env vars).
As for normal env vars it's of limited use because we don't have an "env var" type to pass as argument, it's just the primitive types and we already support default values for those. However, could still be useful to easily replace some defaults (via
.env) on certain environments like CI vs developer machine, without having to change the code.shykes commentedon Feb 14, 2025
I don't think we should handle overrides. It's not part of the original spec, and is not universally supported. In our case it's less needed because Dagger already has a way to express sane defaults in a function. If it's safe to commit in a shared
.env, it's safe to commit in the function directly.. So better to only support the basic use of.envas an uncommitted env-specific configuration.shykes commentedon Feb 14, 2025
Actually we could support passing multiple env file paths... then we could stack them for you. So overrides would be possible, but would never happen automatically- up to the caller to manage their env files any way they want.
shykes commentedon Feb 15, 2025
Great question. I'm not sure.
I agree.
I think it's important that it works with existing
.envfiles without interference.I don't see why it wouldn't work, because we'll be using it as intended. "Loading" a
.envis a very lightweight affair, it just makes it available on the CLI side, in case the engine asks for it in a session attachable. Unless the engine asks for it (and the client agrees to send it) nothing leaves the client system. So I think we're good.The worst thing that could happen, is that people like the secret reference feature so much, that they start removing secret plaintext from their
.env, and usingop://orvault://instead, causing other tools to break. Then they will either pressure other toolmakers to support the standard (and us to open-source the reference implementation), or start bifurcating the.envfiles, or both. Maybe we can plan for this, and open-source a small utility that converts a secure .env file with secret references, to an insecure one with secret values.Yes absolutely! IMO we should support all the same types that CLI arguments support; with all the same "magic string" features:
Then we could just variable expansion in all the pragmas:
rawkode commentedon Mar 19, 2025
Could this work with direnv /
.envrcfiles, rather than Dagger itself finding environment files within a repository?This would allow us to use
source_upand other helper functions, provided by direnv.TomChv commentedon Mar 24, 2025
I think dagger should look for either .env or .envrc, but it shouldn't pick up values from the current host env, but yeah both sources should be supported in my opinion
jasonmccallister commentedon May 14, 2025
Adding some thoughts on
.envsupport with frameworks (e.g. Laravel, Symfony)..envfile is usually for supporting local development (e.g. laptop) where the user does not have an elegant way to manage the environment (typically with a UI).envsupport, you don't create a.envfile in the production environment. Instead you manage the variables using a UI or define those in Terraform or a container specI'm wondering how the experience would move from local development to putting the workflow in production (e.g. GitHub Actions). Would there be a step to create a
.envfile and then call the Dagger Action? That experience seems less than ideal.However, I wonder how people feel about this approach: #10397 (comment)
If there is an
env, maybe those can be automatically pulled from the host and you don't need to provide an argument to the function?I could also see this as a frustrating user experience. If you need to define the
envin thedagger.jsonbut forget to - some users might spend time troubleshooting why the environment variable is not being populated. Wondering if there is a new function (e.g.WithHostEnvVar) that way its explicit to the user that its coming from the host and if its not defined in thedagger.jsonit throws an error?rawkode commentedon May 14, 2025
Now that
dotenv,dotenvx, andop,infisical, etc exist; it's becoming more common for.env.productionto be supported and only store references to secrets.The nice thing about this approach is that people can still run
op run -- bun run deploywithout having to go through a Dagger pipeline.But this still leaves the security risk that some nested module cannot have access to those values just because it happens to be passed a directory with a
.envfile? So won't thedagger.jsonstep still be required?