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

Allow .direnvrc to be defined in the project root #908

Closed
henrik242 opened this issue Mar 11, 2022 · 13 comments
Closed

Allow .direnvrc to be defined in the project root #908

henrik242 opened this issue Mar 11, 2022 · 13 comments
Labels

Comments

@henrik242
Copy link

direnv will try to read .env files in subfolders in our project. We're using .env for configuring docker-compose and nextjs in various subfolders of the project, and don't want to have to do a direnv allow (or deny) for all of them.

This needs to be a per-project thing, so specifying skip_dotenv = true in ~/.config/direnv/direnv.toml isn't really an option for us.

Having a .direnvrc in the project root with skip_dotenv = true would solve this.

See #880 (comment)

@henrik242
Copy link
Author

Here's a hacky Gradle workaround in case anyone else needs it. This is from buildSrc/build.gradle.kts:

tasks {
    // Fix for https://github.com/direnv/direnv/issues/908
    val fixDirenv by registering {
        val configDir = File("${System.getProperty("user.home")}/.config/direnv")
        val configFile = File(configDir, "direnv.toml")
        doLast {
            if (!configFile.isFile) {
                configDir.mkdirs()
                configFile.createNewFile()
            }
            if (!configFile.readText().contains("skip_dotenv")) {
                configFile.appendText("\nskip_dotenv = true")
            }
        }
        outputs.file(configFile)
    }
    assemble { dependsOn(fixDirenv) }
}

@felipecrs
Copy link
Contributor

felipecrs commented Mar 14, 2022

Maybe another workaround is to rename the .env files to something else, and instruct docker-compose to load them instead.

@henrik242
Copy link
Author

Yeah, I could add --env-file someOtherFile to docker-compose, but that would kind of defeat the purpose (which is to be able do run the docker-compose command with fewer arguments).

Also I guess I could do something similar with next.config.js (require('dotenv').config({ path: 'path/to/.env' })).

Still the problem is that direnv breaks the default env mechanisms in other pieces of software.

@felipecrs
Copy link
Contributor

felipecrs commented Mar 14, 2022

Still the problem is that direnv breaks the default env mechanisms in other pieces of software.

Sure, I'm not neglecting this.

This may also help:

https://docs.docker.com/compose/compose-file/compose-file-v3/#env_file

@henrik242
Copy link
Author

This may also help:

Thanks!

@henrik242
Copy link
Author

I see that skip_dotenv = true is not needed anymore after #911, so this issue is not really important for me anymore. Feel free to reopen.

@felipecrs
Copy link
Contributor

felipecrs commented Mar 28, 2022

I still think this is valid. Example:

  • In my project, I want direnv to load .env files automatically.

But let's see if someone else thinks the same. :)

@vschoener
Copy link

@felipecrs I do. I use .env by default when I run the project locally or in docker-compose and I don't want to copy-paste the same data to another file doing the same things.

@felipecrs
Copy link
Contributor

felipecrs commented Mar 31, 2022

One tip: docker-compose can pickup environment variables from your terminal (loaded by direnv from .direnvrc), so perhaps you don't need .env.

@vschoener
Copy link

One tip: docker-compose can pickup environment variables from your terminal (loaded by direnv from .direnvrc), so perhaps you don't need .env.

My app use dotenv already and .env while developing. So it's why we prefer keeping my .env and avoid a duplicate one 😉

@felipecrs
Copy link
Contributor

Even if your app use dotenv, your app will also be able to pickup environment variables directly from the environment where you run it (which will have the vars already set by direnv).

I have been using this approach to share environment variables during both Python debugging in VS Code and docker-compose.

@vschoener
Copy link

Even if your app use dotenv, your app will also be able to pickup environment variables directly from the environment where you run it (which will have the vars already set by direnv).

I have been using this approach to share environment variables during both Python debugging in VS Code and docker-compose.

Maybe it's an old habit I have now and I could change it indeed 😉

@felipecrs
Copy link
Contributor

haha, well, it was just a tip. It's a valid use-case even so.

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

No branches or pull requests

3 participants