Skip to content
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

Add support for .env files #284

Closed
fgblomqvist opened this issue Jul 13, 2017 · 21 comments
Closed

Add support for .env files #284

fgblomqvist opened this issue Jul 13, 2017 · 21 comments

Comments

@fgblomqvist
Copy link

I am currently using a tool that only supports the standard .env format (var=value), but I would love to be able to use direnv as well. Though, that would require me to have two separate files with the same content in them. One .env and one .envrc. Seems a bit annoying to me.

Would it be possible to add support for .env as well? Is there a reason to why this does not exist?
Other than that direnv seems like a really well-written little tool 👍

@jameysharp
Copy link

I just discovered direnv today, and I don't use .env files myself, so please forgive me if this response is not useful, but:

Is the dotenv function in the direnv stdlib not what you're looking for? It looks to me like you should be able to write a .envrc containing only this:

dotenv

and, if I'm reading the documentation correctly, direnv will load the .env file that's in the same directory.

@zimbatm
Copy link
Member

zimbatm commented Jul 14, 2017

Yes that's correct @jameysharp. There are still two files per project but at least you don't have to duplicate the content between both.

@fgblomqvist
Copy link
Author

Thanks @jameysharp for pointing that out! It's not ideal, but it'll have to do! :)

On a side note, @zimbatm, since .env files are pretty common, maybe the dotenv function should be easier to find (document it right in the Readme?) or maybe at least link to where the stdlib docs can be found online (if they can). Just a couple of suggestions.

@schickling
Copy link

schickling commented Nov 21, 2017

Is there a way to have a .envrc file in a parent directory that contains dotenv so that it loads .env files in child directories.

Ideally there would also be a way to tell direnv to globally load .env files (i.e. treat them the same as .envrc files).

Looking forward to hearing your thoughts @zimbatm!

@zimbatm
Copy link
Member

zimbatm commented Nov 21, 2017

The current execution model of direnv doesn't really allow for that kind of mode. Each of the folders will need that .envrc right now. It might change in direnv 3.0 where I plan to overhaul the configuration.

The easiest option right now is to add an alias to your shell: load_dotenv() { echo "dotenv" > .envrc; direnv allow }

@timuckun
Copy link

It would be nice to be able to use the same .env. Right now I have to create a symlink between them (the ruby dotenv library is able to parse the direnv format).

Ideally direnv could handle key=value format without needing to type EXPORT.

@timwis
Copy link

timwis commented Dec 20, 2017

I'd love this too. In the communities I interact with most frequently, .env is more common than .envrc. it would be great if this worked with either.

@nitrocode
Copy link

nitrocode commented Aug 23, 2018

I use .env for a lot of different projects. I would like support for automatic .env files if a .envrc file is missing.

Edit: Below does not work. I was mistaken...

Why put dotenv in your .envrc file when a softlink is even easier.

$ ln -s .env .envrc
direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
$ direnv allow
direnv: loading .envrc

@aude
Copy link

aude commented Apr 4, 2019

I have created a pull request that adds support for .env files: #438

@xenoterracide
Copy link

I would also like to say, that I'm not sure that the dotenv program asks permission before sourcing, making it less secure. Also I don't want to load .env for all projects. I'd rather be able to reject some.

@leohxj
Copy link

leohxj commented Apr 21, 2021

@aude the PR is still opening...

@adamluzsi
Copy link

If anyone wishes to have automatic .env sourcing in every project,
then please consider adding dotenv call to the direnv's rc file:

~/.config/direnv/direnvrc

#!/bin/sh

if [ -f ".env" ]; then
        dotenv
fi

@felipecrs
Copy link
Contributor

@adamluzsi I can't make it work with your suggestion. Is there anything else needed?

@adamluzsi
Copy link

@felipecrs could you double check if the file is accessable? (~/.config/direnv/direnvrc)

Does your .env use .env format or shell envrc format?

# example .env format
FOO=baz
# example shell format
FOO=baz
export FOO

Also, try debug by printing you position from the direnvrc config file. Maybe the [ -f ".env" ] simply can't see anything.

~/.config/direnv/direnvrc

#!/bin/sh

pwd > ~/out

if [ -f ".env" ]; then
	dotenv
  # or if you use shell format:
  # source_env ".env" 
fi

and check the results in ~/out.

Best of luck with your investigation!

@felipecrs
Copy link
Contributor

felipecrs commented Nov 26, 2021

@adamluzsi wow, thanks a lot for the tips. I realized that it only works when there is a .envrc alongside (even without a dotenv inside of it). This is what triggers direnvrc to execute, it seems.

@felipecrs
Copy link
Contributor

felipecrs commented Nov 26, 2021

By the way, for those subscribed in this conversation, I created a PR for implementing automatic .env loading, which builds on @aude's work.

#845

@kazuma1989
Copy link

I don't want automatic .env loading. How to opt out the behavior?

@felipecrs
Copy link
Contributor

@zimbatm I think you can close this issue.

@kazuma1989: #880 (comment)

@zimbatm zimbatm closed this as completed Apr 21, 2022
@dbarnett
Copy link

dbarnett commented Mar 25, 2023

I see .env is mentioned in the README and various docs but can't find any reference doc about the actual .env vs. .envrc formats. Could that be noted somewhere in README etc?

Is it just something like newline-separated literal variable assignments like var1=val1? (Possibly supporting some forms of interpolation?)

@rmchale
Copy link

rmchale commented Apr 21, 2023

@dbarnett I think this is what you are looking for

.envrc

export MY_VAR=foo
dotenv_if_exists .env.local

.env.local

MY_OTHER_VAR=foo

@dbarnett
Copy link

dbarnett commented May 6, 2023

But could that be noted somewhere in README etc? I couldn't find any docs, only discussion in random bugs like this.

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

No branches or pull requests