In NuGet, how are nuget.config files at repository, user and machine level combined, and how should a CI build authenticate to a private feed?
answer
- closest file wins for single values
- packageSources merge unless <clear />
- --configfile uses that file alone
- NuGetPackageSourceCredentials_{name} beats the config file
- %VAR% macros, even on Linux
basics
~20 sNuGet loads machine, user and then every nuget.config from the drive root down to the solution folder; the closest value wins and lists such as packageSources merge unless a <clear /> resets them. CI should pass feed credentials through environment variables or a credential provider, never committed clear text.
solid answer
~40 sWithout `--configfile`, NuGet reads the **computer-level** file, then the **user-level** file, then every `nuget.config` from the drive root down to the solution (or current) folder. Single-value settings take the **closest** value; collections such as `<packageSources>` **merge** across files, so a repo file should start its sources with `<clear />` to drop a developer's personal feeds. A `nuget.config` inside a project folder has no effect when the solution is restored. For authentication, after a 401 NuGet looks for credentials in this order: an environment variable `NuGetPackageSourceCredentials_{name}` (`Username=...;Password=...`), `<packageSourceCredentials>` in nuget.config, then a **credential provider** plugin. In CI, inject a token through that variable or through `%VAR%` macros in the config; reserve `--store-password-in-clear-text` for throwaway agents.
code
xml · 12 lines<configuration>
<packageSources>
<clear />
<add key="BankFeed" value="https://pkgs.example.internal/nuget/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<BankFeed>
<add key="Username" value="ci" />
<add key="ClearTextPassword" value="%BANKFEED_TOKEN%" />
</BankFeed>
</packageSourceCredentials>
</configuration>go deeper
Recall that nuget.config lists package sources, that the repository can carry its own copy, and that private feeds need credentials.
Explain the load order, closest-wins for values, merging for packageSources, <clear />, and what --configfile changes.
Secure feed access in CI: credential providers or environment variables over clear text, the precedence of NuGetPackageSourceCredentials_{name}, %VAR% macros and the unlogged-variable trap.
Set the organisation's feed policy: which sources are allowed, how credentials are issued and rotated, and how developer machines and pipelines see the same configuration.
## Where the settings come from NuGet's behaviour — which feeds to use, where packages go, which credentials to send — comes from `nuget.config` files. When no file is given explicitly, NuGet loads them in this order: 1. the uncommon `NuGetDefaults.Config` (package sources only); 2. the **computer-level** file (for example `/etc/opt/NuGet/Config` on Linux); 3. the **user-level** file (`%appdata%\NuGet\NuGet.Config` on Windows, `~/.nuget/NuGet/NuGet.Config` for the .NET CLI on macOS and Linux); 4. every `nuget.config` from the drive root down to the solution folder or current folder. With `dotnet restore --configfile build/nuget.config` (or `nuget -configFile`), **only** that file is used. ## How they combine - **Single-item settings** such as `globalPackagesFolder` or `defaultPushSource`: the file closest to the solution wins. - **Collections** such as `<packageSources>`: entries from all files are **combined**. - **`<clear />`** inside a section discards everything inherited for that section. That merge is why a bank's repository file should look like this, so a developer's personal feeds on their laptop never take part in the build: ```xml <configuration> <packageSources> <clear /> <add key="BankFeed" value="https://pkgs.example.internal/nuget/v3/index.json" /> </packageSources> </configuration> ``` Two placement traps: a `nuget.config` inside a **project** folder has no effect on that project when you restore the solution, and restoring a single project treats the project folder as the "solution" folder, so solution and project restores can see different settings. ## How NuGet finds credentials NuGet first calls an HTTP feed anonymously. When the server answers **401**, it searches for credentials in a fixed order: | Order | Where | Notes | |---|---|---| | 1 | environment variable `NuGetPackageSourceCredentials_{name}` | value `Username=...;Password=...`, `{name}` is the source's `key` | | 2 | `<packageSourceCredentials>` in nuget.config | encrypted `Password` (Windows only) or `ClearTextPassword` | | 3 | a NuGet **credential provider** plugin | interactive or token-based sign-in | The environment variable is consulted **first** and its use is not logged. If it holds an expired token while a fresh one sits in `nuget.config`, restore keeps failing with no hint why — a real debugging trap. ## What NuGet recommends, best first - A **credential provider**, if the feed offers one: no stored secrets and fewer places to rotate. - **Encrypted credentials** in nuget.config — usable only on Windows, and only by the same user on the same machine. - **Environment-variable macros** in nuget.config, such as `<add key="ClearTextPassword" value="%BANKFEED_TOKEN%" />` — the `%VAR%` syntax is required even on Linux, and `$VAR` is not expanded. - The `NuGetPackageSourceCredentials_{name}` variable directly. - **Clear-text credentials** in the file as a last resort, never in source control. Most private feeds want a personal access token as the password rather than a login password. ## Finding out which configuration applied When a restore uses an unexpected feed or setting, inspect the merged result instead of guessing: - `dotnet nuget config paths` lists every config file that will be applied when NuGet runs in the current folder. - `dotnet nuget config get <key>` shows the effective value of one setting, such as `globalPackagesFolder`. - `dotnet nuget list source` shows every enabled and disabled source after merging. - Running the same command from the solution folder and from a project folder shows the solution-versus-project difference directly. ## Commands that write the config - `dotnet nuget add source <url> --name BankFeed --configfile nuget.config` adds a source; `--username` and `--password` add credentials, and because password encryption works only on Windows, other platforms need `--store-password-in-clear-text`. - `dotnet nuget list source` shows the merged, effective sources; `dotnet nuget disable source` and `enable source` toggle one without deleting it. For a bank, the resulting shape is one committed repo-level `nuget.config` with `<clear />` and the approved feed, and credentials that exist only in the pipeline's secret store, mapped into `NuGetPackageSourceCredentials_BankFeed` or into a macro at restore time.
- A developer's build pulls a package from a feed the repository never configured. How?Their user-level or machine-level `nuget.config` adds the feed, and `<packageSources>` entries merge across files. Starting the repository's `<packageSources>` with `<clear />` removes inherited sources so only the repository's list applies; package source mapping then controls which package may come from which of those sources.
- Why does a rotated token in nuget.config not fix a 401?NuGet consults the `NuGetPackageSourceCredentials_{name}` environment variable before the config file and does not log that it used it. If the agent still exports the old token there, the new one in the file is never read. Remove or update the variable.
- Only the nuget.config closest to the solution is read; all others are ignored.
- A nuget.config placed in a project folder configures that project's restore inside the solution.
- On Linux, nuget.config expands $VAR environment variables in credential values.
- Encrypted nuget.config passwords can be shared across build agents.
- NuGet sends credentials only from nuget.config, never from environment variables.