Skip to content

Added ability to set config parameters through environment variables#3476

Closed
jmcorgan wants to merge 4 commits intobitcoin:masterfrom
jmcorgan:envconfig
Closed

Added ability to set config parameters through environment variables#3476
jmcorgan wants to merge 4 commits intobitcoin:masterfrom
jmcorgan:envconfig

Conversation

@jmcorgan
Copy link
Copy Markdown
Contributor

The environment is parsed for variables starting with BITCOIN_, and
the remaining portion of the name becomes the argument name, with
the value set to the portion after the = sign, e.g.,

BITCOIN_DATADIR=FOO is equivalent to -datadir=FOO

The environment is parsed before the command line, allowing the
command line to take precedence over the environment.

There is a whitelist in util.cpp that only allows specified environment
variables to be used this way; currently, this is set to just -datadir
and -conf.

Signed-off-by: Johnathan Corgan johnathan@corganlabs.com

The environment is parsed for variables starting with BITCOIN_, and
the remaining portion of the name becomes the argument name, with
the value set to the portion after the = sign, e.g.,

BITCOIN_DATADIR=FOO is equivalent to -datadir=FOO

The environment is parsed before the command line, allowing the
command line to take precedence over the environment.

There is a whitelist in util.cpp that only allows specified environment
variables to be used this way; currently, this is set to just -datadir
and -conf.

Signed-off-by: Johnathan Corgan <johnathan@corganlabs.com>
Comment thread src/util.cpp
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation looks a bit strange here. Are you mixing tabs and spaces? Please use four spaces for indentation.

@jmcorgan
Copy link
Copy Markdown
Contributor Author

I'm using emacs C++ mode, which defaults to two spaces, then trying to make things match up with what is already there. It didn't work out so well.

Regarding the ClearArgs, I'm just replicating what was already there at the start of ParseParameters(). It's true they start out empty, but in the test code it calls ParseParameters multiple times and assumes that the args get cleared in between.

I'm happy to make whatever changes you feel are necessary, of course.

@jmcorgan
Copy link
Copy Markdown
Contributor Author

Also, there is a backport to 0.8.6 on my branch envconfig-0.8.6.

@jmcorgan
Copy link
Copy Markdown
Contributor Author

All of Iaanwj's comments should be addressed now.

@BitcoinPullTester
Copy link
Copy Markdown

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/0729a596da7caeda1a0243d3739513d193f5f590 for binaries and test log.
This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/
Contact BlueMatt on freenode if something looks broken.

@emmix
Copy link
Copy Markdown

emmix commented Dec 31, 2013

It seems that do not have the strong requirement for this extended feature, since we already have configure file(bitcoin.conf) to do it. Also it make the bitcoin to tangle the OSs environments. Different OS may have some quirks.

@jmcorgan
Copy link
Copy Markdown
Contributor Author

This serves several needs that I personally have, to run bitcoind with different conf files and not have to always specify them with -conf=foo on the command line. Instead I set the BITCOIN_CONF=foo environment variable and I can type bitcoind (or bitcoin-cli) repeatedly and it will use that value. Being able to specify alternate datadir locations works the same way.

It also allows running common scripts whose parameters can be changed via the environment they are passed, instead of having to point at different conf files.

In any case, I'll keep using this locally, and the pull request is offered up to the devs to integrate or not as they see fit.

@laanwj
Copy link
Copy Markdown
Member

laanwj commented Dec 31, 2013

@emmix How does this "tangle" the OS environment? It reads it only, uses clearly identifiable variable names that you'd never use by accident, and currently only allows two settings (-datadir and -conf) to be set through the environment.

I can see how this can be useful. Though, to be fair, you can accomplish the same with something like:

alias bitcoin-cli='bitcoin-cli -datadir=${BITCOIN_DATADIR}'

(sure, an alias won't nest into scripts, but the scripts could pass the environment variable to bitcoin-cli)

@emmix
Copy link
Copy Markdown

emmix commented Jan 1, 2014

'Tangle" which I mean couple with the OS environment. Sorry for my unclarity.
My concerns are:
[1] Windows, Mac, Linux, etc have the same style to deal with environment? Do we need to consider the OS's quirks? I do not look into it.
[2] The change bring the convenience as the global variables in programming language. Sure, it is useful if we have clearly identifiable names. But the convenience can obtain only by scripts without need to introduce the environment variables into source code.

Anyway the change takes convenience actually. But type the whole commands and options let you know what you are dong clearly.

@laanwj
Copy link
Copy Markdown
Member

laanwj commented Apr 1, 2014

This adds more testing work. Especially in the case of the GUI, there are already many sources of configuration options that interact in non-trivial ways and this has resulted in bugs in the past.

And as said this can be easily handled from outside the code; either by defining an alias or function for bitcoin-cli with arguments, or by using a script.

In general we don't add extra code to the core for what can be handled easily from outside.
So - I'm closing this.

@laanwj laanwj closed this Apr 1, 2014
@jmcorgan jmcorgan deleted the envconfig branch June 7, 2014 17:22
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
@jmcorgan jmcorgan restored the envconfig branch December 23, 2025 00:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants