Added ability to set config parameters through environment variables #3476

pull jmcorgan wants to merge 4 commits into bitcoin:master from jmcorgan:envconfig changing 7 files +52 −2
  1. jmcorgan commented at 8:01 AM on December 30, 2013: 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

  2. Added ability to set config parameters through environment variables
    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>
    ba935ec954
  3. in src/util.cpp:None in ba935ec954 outdated
     548 | +static const set<string> envWhitelist(envNames, envNames+sizeof(envNames)/sizeof(envNames[0]));
     549 | +
     550 | +void ParseEnvironment()
     551 | +{
     552 | +    int i = 0;
     553 | +    while (environ[i] != NULL && strlen(environ[i]) > 0) {
    


    laanwj commented at 8:20 AM on December 30, 2013:

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

  4. in src/bitcoin-cli.cpp:None in ba935ec954 outdated
      19 | @@ -20,6 +20,8 @@ static bool AppInitRPC(int argc, char* argv[])
      20 |      //
      21 |      // Parameters
      22 |      //
      23 | +    ClearArgs();
    


    laanwj commented at 8:23 AM on December 30, 2013:

    This ClearArgs() shouldn't be needed as the args start out empty (same for bitcoind and bitcoin-qt)

  5. jmcorgan commented at 8:35 AM on December 30, 2013: contributor

    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.

  6. jmcorgan commented at 8:37 AM on December 30, 2013: contributor

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

  7. Removed unneeded ClearArgs() calls.
    Signed-off-by: Johnathan Corgan <johnathan@corganlabs.com>
    41d56c87f8
  8. Remove one additional unneeded ClearArgs(). d2ea3a3bdd
  9. Fixed whitespace issues with tab/space 0729a596da
  10. jmcorgan commented at 4:55 PM on December 30, 2013: contributor

    All of Iaanwj's comments should be addressed now.

  11. BitcoinPullTester commented at 5:27 PM on December 30, 2013: none

    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.

  12. emmix commented at 3:27 AM on December 31, 2013: none

    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.

  13. jmcorgan commented at 3:44 AM on December 31, 2013: contributor

    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.

  14. laanwj commented at 8:11 AM on December 31, 2013: member

    @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)

  15. emmix commented at 3:51 AM on January 1, 2014: none

    '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.

  16. laanwj commented at 1:19 PM on April 1, 2014: member

    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.

  17. laanwj closed this on Apr 1, 2014

  18. jmcorgan deleted the branch on Jun 7, 2014
  19. bitcoin locked this on Sep 8, 2021
  20. jmcorgan restored the branch on Dec 23, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-19 03:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me