Configuration

This document will explain how Cargo’s configuration system works, as well asavailable keys or configuration. For configuration of a package through itsmanifest, see the manifest format.

Hierarchical structure

Cargo allows local configuration for a particular package as well as globalconfiguration, like git. Cargo extends this to a hierarchical strategy.If, for example, Cargo were invoked in /projects/foo/bar/baz, then thefollowing configuration files would be probed for and unified in this order:

  • /projects/foo/bar/baz/.cargo/config
  • /projects/foo/bar/.cargo/config
  • /projects/foo/.cargo/config
  • /projects/.cargo/config
  • /.cargo/config
  • $CARGO_HOME/config ($CARGO_HOME defaults to $HOME/.cargo)With this structure, you can specify configuration per-package, and evenpossibly check it into version control. You can also specify personal defaultswith a configuration file in your home directory.

Configuration format

All configuration is currently in the TOML format (like the manifest),with simple key-value pairs inside of sections (tables) which all get mergedtogether.

Configuration keys

All of the following keys are optional, and their defaults are listed as theirvalue unless otherwise noted.

Key values that specify a tool may be given as an absolute path, a relative pathor as a pathless tool name. Absolute paths and pathless tool names are used asgiven. Relative paths are resolved relative to the parent directory of the.cargo directory of the config file that the value resides within.

  1. # An array of paths to local repositories which are to be used as overrides for
  2. # dependencies. For more information see the Specifying Dependencies guide.
  3. paths = ["/path/to/override"]
  4. [cargo-new]
  5. # This is your name/email to place in the `authors` section of a new Cargo.toml
  6. # that is generated. If not present, then `git` will be probed, and if that is
  7. # not present then `$USER` and `$EMAIL` will be used.
  8. name = "..."
  9. email = "..."
  10. # By default `cargo new` will initialize a new Git repository. This key can
  11. # be set to change the version control system used. Valid values are `git`,
  12. # `hg` (for Mercurial), `pijul`, `fossil`, or `none` to disable this behavior.
  13. vcs = "none"
  14. # For the following sections, $triple refers to any valid target triple, not the
  15. # literal string "$triple", and it will apply whenever that target triple is
  16. # being compiled to. 'cfg(...)' refers to the Rust-like `#[cfg]` syntax for
  17. # conditional compilation.
  18. [target.$triple]
  19. # This is the linker which is passed to rustc (via `-C linker=`) when the `$triple`
  20. # is being compiled for. By default this flag is not passed to the compiler.
  21. linker = ".."
  22. # Same but for the library archiver which is passed to rustc via `-C ar=`.
  23. ar = ".."
  24. # If a runner is provided, compiled targets for the `$triple` will be executed
  25. # by invoking the specified runner executable with actual target as first argument.
  26. # This applies to `cargo run`, `cargo test` and `cargo bench` commands.
  27. # By default compiled targets are executed directly.
  28. runner = ".."
  29. # custom flags to pass to all compiler invocations that target $triple
  30. # this value overrides build.rustflags when both are present
  31. rustflags = ["..", ".."]
  32. [target.'cfg(...)']
  33. # Similar for the $triple configuration, but using the `cfg` syntax.
  34. # If several `cfg` and $triple targets are candidates, then the rustflags
  35. # are concatenated. The `cfg` syntax only applies to rustflags, and not to
  36. # linker.
  37. rustflags = ["..", ".."]
  38. # Similar for the $triple configuration, but using the `cfg` syntax.
  39. # If one or more `cfg`s, and a $triple target are candidates, then the $triple
  40. # will be used
  41. # If several `cfg` are candidates, then the build will error
  42. runner = ".."
  43. # Configuration keys related to the registry
  44. [registry]
  45. index = "..." # URL of the registry index (defaults to the index of crates.io)
  46. default = "..." # Name of the default registry to use (can be overridden with
  47. # --registry)
  48. # Configuration keys for registries other than crates.io.
  49. # `$name` should be the name of the registry, which will be used for
  50. # dependencies in `Cargo.toml` files and the `--registry` command-line flag.
  51. # Registry names should only contain alphanumeric characters, `-`, or `_`.
  52. [registries.$name]
  53. index = "..." # URL of the registry index
  54. [http]
  55. proxy = "host:port" # HTTP proxy to use for HTTP requests (defaults to none)
  56. # in libcurl format, e.g., "socks5h://host:port"
  57. timeout = 30 # Timeout for each HTTP request, in seconds
  58. cainfo = "cert.pem" # Path to Certificate Authority (CA) bundle (optional)
  59. check-revoke = true # Indicates whether SSL certs are checked for revocation
  60. low-speed-limit = 5 # Lower threshold for bytes/sec (10 = default, 0 = disabled)
  61. multiplexing = true # whether or not to use HTTP/2 multiplexing where possible
  62. # This setting can be used to help debug what's going on with HTTP requests made
  63. # by Cargo. When set to `true` then Cargo's normal debug logging will be filled
  64. # in with HTTP information, which you can extract with
  65. # `CARGO_LOG=cargo::ops::registry=debug` (and `trace` may print more).
  66. #
  67. # Be wary when posting these logs elsewhere though, it may be the case that a
  68. # header has an authentication token in it you don't want leaked! Be sure to
  69. # briefly review logs before posting them.
  70. debug = false
  71. [build]
  72. jobs = 1 # number of parallel jobs, defaults to # of CPUs
  73. rustc = "rustc" # the rust compiler tool
  74. rustdoc = "rustdoc" # the doc generator tool
  75. target = "triple" # build for the target triple (ignored by `cargo install`)
  76. target-dir = "target" # path of where to place all generated artifacts
  77. rustflags = ["..", ".."] # custom flags to pass to all compiler invocations
  78. rustdocflags = ["..", ".."] # custom flags to pass to rustdoc
  79. incremental = true # whether or not to enable incremental compilation
  80. # If `incremental` is not set, then the value from
  81. # the profile is used.
  82. dep-info-basedir = ".." # full path for the base directory for targets in depfiles
  83. [term]
  84. verbose = false # whether cargo provides verbose output
  85. color = 'auto' # whether cargo colorizes output
  86. # Network configuration
  87. [net]
  88. retry = 2 # number of times a network call will automatically retried
  89. git-fetch-with-cli = false # if `true` we'll use `git`-the-CLI to fetch git repos
  90. offline = false # do not access the network, but otherwise try to proceed if possible
  91. # Alias cargo commands. The first 4 aliases are built in. If your
  92. # command requires grouped whitespace use the list format.
  93. [alias]
  94. b = "build"
  95. c = "check"
  96. t = "test"
  97. r = "run"
  98. rr = "run --release"
  99. space_example = ["run", "--release", "--", "\"command list\""]

Environment variables

Cargo can also be configured through environment variables in addition to theTOML syntax above. For each configuration key above of the form foo.bar theenvironment variable CARGO_FOO_BAR can also be used to define the value. Forexample the build.jobs key can also be defined by CARGO_BUILD_JOBS.

Environment variables will take precedent over TOML configuration, and currentlyonly integer, boolean, and string keys are supported to be defined byenvironment variables. This means that source replacement, which is expressed bytables, cannot be configured through environment variables.

In addition to the system above, Cargo recognizes a few other specificenvironment variables.

Credentials

Configuration values with sensitive information are stored in the$CARGO_HOME/credentials file. This file is automatically created and updatedby cargo login. It follows the same format as Cargo config files.

  1. [registry]
  2. token = "..." # Access token for crates.io
  3. # `$name` should be a registry name (see above for more information about
  4. # configuring registries).
  5. [registries.$name]
  6. token = "..." # Access token for the named registry

Tokens are used by some Cargo commands such as cargo publish forauthenticating with remote registries. Care should be taken to protect thetokens and to keep them secret.

As with most other config values, tokens may be specified with environmentvariables. The token for crates.io may be specified with theCARGO_REGISTRY_TOKEN environment variable. Tokens for other registries maybe specified with environment variables of the formCARGO_REGISTRIES_NAME_TOKEN where NAME is the name of the registry in allcapital letters.