News for hackers. Stuff that matters.
Re:Node.js 20.6.0 will include built-in support for .env files by SOLAR_FIELDS on Friday August 18, 2023 @09:21AM (#37175241)
.env desperately needs a formal specification. I wrote a bit of code using some features python_dotenv offers but realized after the fact that other software might not parse it the same way. I’ve taken to instead doing my templating in Python itself via pydantic BaseSettings
Re:Node.js 20.6.0 will include built-in support for .env files by prng2021 on Friday August 18, 2023 @09:31AM (#37175367)
Can you provide a concrete example? What do you mean by other software not parsing it the same way?
Re:Node.js 20.6.0 will include built-in support for .env files by moojd on Friday August 18, 2023 @09:43AM (#37175512)
Some execute them as a shell script and others parse them as a newline separated list of key value pairs
Re:Node.js 20.6.0 will include built-in support for .env files by 3np on Saturday August 19, 2023 @03:38AM (#37186457)
[delayed]
Re:Node.js 20.6.0 will include built-in support for .env files by prng2021 on Saturday August 19, 2023 @06:08PM (#37193801)
I've never seen something like this in an env file. No one should be putting lines like this in an env file unless it's by mistake.
Re:Node.js 20.6.0 will include built-in support for .env files by 3np on Monday August 21, 2023 @08:37AM (#37208664)
[delayed]
Re:Node.js 20.6.0 will include built-in support for .env files by sergiotapia on Friday August 18, 2023 @09:38AM (#37175461)
I've only ever used .env as such:

    export TWILIO_SID="123123123123"
    export TWILIO_SECRET="123123123123"
And then `source .env` in my terminal, then `rails server` or whatever to run my apps. The environment variables will be present to be consumed from within the app.

Curious what features you use beyond this.

Re:Node.js 20.6.0 will include built-in support for .env files by koolba on Friday August 18, 2023 @09:44AM (#37175525)
> Curious what features you use beyond this.

There’s plenty of outstanding questions.

Is the export required?

Can variables reference other variables?

Can variables reference existing variables in the environment?

Can you use other forms of expressions?

Can you set overridable defaults for values?

Re:Node.js 20.6.0 will include built-in support for .env files by sonthonax on Friday August 18, 2023 @10:26AM (#37176081)
It's a shell script that should be executable by any UNIX shell. Parsing it (rather than executing it) is why those questions are raised.
Re:Node.js 20.6.0 will include built-in support for .env files by claytongulick on Friday August 18, 2023 @10:45AM (#37176368)
In the nodejs community it's normally used via dotenv.

So, yes it's parsed and a valid use case.

Re:Node.js 20.6.0 will include built-in support for .env files by sergiotapia on Friday August 18, 2023 @12:57PM (#37178532)
I've never used/needed those features from my .env files that's why I'm asking for use cases. Are you saying you use those kinds of features in python_dotenv? I'm asking btw, no snark.
Re:Node.js 20.6.0 will include built-in support for .env files by SOLAR_FIELDS on Sunday August 20, 2023 @12:20AM (#37196101)
I use them in python_dotenv for declaring environments that I use to provision infrastructure, as an example:

Example, where we have:

env_type="dev"

env_name="myappenv"

primary_subnet_pod="${env_type}-${env_name}-gke-pod-subnet"

primary_subnet_service="${env_type}-${env_name}-gke-service-subnet"

Which I then might want to use further down, override with defaults, etc and so forth. It strikes a decent balance in that it's a bit more powerful than a static .env file, but doesn't require you to go full-on templating language with Jinja and the like. It's also cross-platform enough in that you could throw a .env in somewhere and expect most languages to have a client library to parse and load it into env.

If your use-case is right in that sweet spot it's a pretty good tool. But the behavior, as noted, isn't specced so I can't trust go_dotenv or rust_dotenv or whatever other library to treat it the same way.

Interestingly, in practice, it seems like the Node lib is the canonical version that inspired these other ones so in a way it serves as some sort of unofficial spec. It's not something I would write production grade code trusting the unofficial spec on though.

Re:Node.js 20.6.0 will include built-in support for .env files by mixedCase on Friday August 18, 2023 @01:14PM (#37178828)
.envs generally aren't full blown shell, so you don't have `export` available. With that said, I keep a shell function in my .zshrc which, when given a .env file (or when not given one, assuming $PWD/.env) will set -a, source it and set +a, which loads everything into the environment.
Re:Node.js 20.6.0 will include built-in support for .env files by bdcravens on Saturday August 19, 2023 @12:18AM (#37185462)
I always prefer to use the dotenv gem, as many times I'd rather stay local to my Rails app, rather than setting global environment variables.
Re:Node.js 20.6.0 will include built-in support for .env files by zeroimpl on Saturday August 19, 2023 @01:34PM (#37191102)
If your env file is a script that you source manually, I’d call it env.sh instead.

Historically this has been my approach, but in production environments it’s convenient to have a static list of variables with no “export” instead since eg systemd’s EnvironmentFile only supports that.

Re:Node.js 20.6.0 will include built-in support for .env files by dubcanada on Friday August 18, 2023 @09:43AM (#37175516)
Isn't it just environment variables but rather then setting it manually it's in a file?

So the spec is the same as regular environment variables?

Re:Node.js 20.6.0 will include built-in support for .env files by SOLAR_FIELDS on Friday August 18, 2023 @10:29AM (#37176144)
No, python_dotenv allows some rudimentary templating in the .env file. So it’s not just specifying env vars
Re:Node.js 20.6.0 will include built-in support for .env files by dubcanada on Friday August 18, 2023 @10:40AM (#37176294)
That's silly, but I don't think that's solved by a spec. Even with specs there is still JSON-LD, JSON 2.0, JSON API, etc.
Re:Node.js 20.6.0 will include built-in support for .env files by SahAssar on Friday August 18, 2023 @12:35PM (#37178152)
Nitpick: JSON 2.0 is not a thing. JSON is versionless, maybe you are thinking of JSON5 (which is not a version, it's a superset of JSON with ECMAscript 5 features) or JSONC (also not a version, also a superset of JSON, but this time with comments). JSON-LD and JSON-API are both specs for the contents of JSON, not the dataformat itself.
Re:Node.js 20.6.0 will include built-in support for .env files by panzi on Friday August 18, 2023 @01:27PM (#37179088)
Different dotenv implementations disagree on weather quoting is possible (with quoting comes multi line support). There is more opportunity for disagreement, like weather comments are supported or empty lines are ignored. But all that I used support comments and empty lines (though not all of them support quoted values).
Re:Node.js 20.6.0 will include built-in support for .env files by Too on Saturday August 19, 2023 @04:09AM (#37186636)
There is no such thing as a spec for setting “regular” environment variables.

Well there is but that’s a much deeper concept in Linux, than what most people get exposed to, in the execve family of functions, where it is defined in C as an array of strings with the key separated from value only by =.

Unless you refer to how to do it in shell. That wouldn’t be applicable to a configuration language. Shell has a lot richer syntax and more complex logic, allowing variable templating and even running processes inside a string definition using $(). All the quoting rules around that must be different.

Re:Node.js 20.6.0 will include built-in support for .env files by andrew_ on Friday August 18, 2023 @09:52AM (#37175615)
https://www.npmjs.com/package/dotenv is the gold standard imho. they have a few other packages you can add-on that expand its functionality. I have a custom package which utilizes dotenv, dotenv-expand, and find-up for a truly delightful DX.
Re:Node.js 20.6.0 will include built-in support for .env files by RussianCow on Friday August 18, 2023 @02:53PM (#37180386)
Or we need to collectively stop using dotenv in favor of other, more formal formats like TOML.
Re:Node.js 20.6.0 will include built-in support for .env files by hotnfresh on Friday August 18, 2023 @03:56PM (#37181230)
If you’re ultimately going to be reading actual env vars, the nice things that toml gives you won’t be there, and having used it as a crutch may leave you with bugs. If you’re reading env as input, eat the pain early or eat it later.

If you’re not ultimately going to be reading actual env vars, then yeah—why even consider .env in the first place? Of course don’t use it.

Re:Node.js 20.6.0 will include built-in support for .env files by RussianCow on Friday August 18, 2023 @07:49PM (#37183712)
> If you’re ultimately going to be reading actual env vars

I would argue that most of the time, you don't need to do this in the first place. Far too often people shoehorn things into environment variables that really shouldn't live there, like secrets and app configuration.

If you're interfacing with existing software that reads from the environment, obviously you don't have control over that, so using a different format is out of the question (unless you're willing to convert it to env vars at runtime, but that sounds like more effort than it's worth).

Re:Node.js 20.6.0 will include built-in support for .env files by arielcostas on Friday August 18, 2023 @07:10PM (#37183325)
I like ASP.NET Core approach of having an `appsettings.json` where you just specify your settings, and also allowing to have `user-secrets` on a different directory, which prevents you from accidentally adding it to source control (which happens way more than it should). Of course, you can also use environment variables, but creating a JSON file that allows nesting properties is quite nice and probably easier for noobs than figuring out each .env implementation's quirks.