News for hackers. Stuff that matters.
Re:Don’t use environment variables for configuration by mrkeen on Thursday April 01, 2021 @02:55AM (#26657367)
Easy fix (if it's the shared, mutable state which bugs you):

* Create one class responsible for ingesting env vars at startup.

* Call it from main, and abort early with nice messages if it fails to read something.

* Now you have a nice (preferably immutable) class which guarantees the config is in a 'good state', and is self-documenting because it lists all the keys it uses to lookup env vars with.

Re:Don’t use environment variables for configuration by zrail on Thursday April 01, 2021 @08:42AM (#26659738)
This is essentially what I do in Rails apps. The only reference to an env var is in an initializer that sets an option in the global rails config structure.
Re:Don’t use environment variables for configuration by marcrosoft on Thursday April 01, 2021 @11:13AM (#26661431)
The mutable state can be helpful. It is sometimes helpful to be able to change an app’s config without having to restart it. Ingesting the envs on startup into a class removes this ability.
Re:Don’t use environment variables for configuration by marcosdumay on Thursday April 01, 2021 @12:39PM (#26662795)
Please, never do that on a server.

It's ok for interactive applications, but if you are writing a CLI command (what is different from an interactive CLI application), a system library or a deamon, don't ever let the same application that uses a configuration also change it.

When your non-interactive programs do that and anything at all goes wrong, it's basically impossible to determine the source of the problem. Also, it is common that bugs that one could just avoid triggering by configuration now become unavoidable.

(But if you mean reload the config after getting a SIGHUP or something like that, yeah, this is ok, and the best way to do that is by restarting everything on your program, even if you keep the same process, so your read-once class won't be a problem.)

Re:Don’t use environment variables for configuration by throwaway10110 on Thursday April 01, 2021 @11:46AM (#26661985)
thats quite an antipattern in production.

Immutable config via a config class that can exit early (prefereably startup) if there is a misconfiguration

Re:Don’t use environment variables for configuration by kristjansson on Thursday April 01, 2021 @02:28PM (#26664272)
If The same pattern works well in python at the module level, if your application is setup as a package. A module config.py sets a bunch of python variables like

    import os
    
    ENV_VAR=os.environ.get('ENV_VAR', default_value)
then the rest of the application can grab configuration with

    from .config import ENV_VAR
Since the assignment code executes on import, all config is read in when any piece of it is first used, consistency checks and logging can be written into the config.py module as normal python statements, config values can be cast to appropriate types (raising exceptions if they fail), etc.
Re:Don’t use environment variables for configuration by FunnyLookinHat on Thursday April 01, 2021 @09:40AM (#26660383)
In node.js there are quite a few packages that do exactly this - it's a great pattern and forces you to define which env values you will rely on in one place, rather than dripping them all over your codebase.
Re:Don’t use environment variables for configuration by trey-jones on Thursday April 01, 2021 @08:15AM (#26659486)
This is more or less the way I handle configuration in most applications that I create. +1
Re:Don’t use environment variables for configuration by david422 on Thursday April 01, 2021 @08:41AM (#26659729)
Agreed - this solution works well, and works nicely with statically typed languages.