nuget.config for a private feed

Serving developers since 2016 Founder-run, no venture capital MFA, SSO and audit log on every plan

Every feature on every plan, with unlimited users. 14 days free. No credit card required.

Start free trial

Starting at $99/month billed annually.

A nuget.config file tells dotnet and NuGet which package feeds to use when restoring dependencies, listing those feeds under <packageSources>. NuGet looks for nuget.config files at the computer level, the user level, and in your solution folder (or any parent folder up to the drive root), and the behavior of dotnet restore is affected by those settings if a file is present.

Where nuget.config lives

Knowing each nuget.config location helps when a restore picks up the wrong feed. NuGet checks three levels, and the exact path depends on your operating system and tooling.

Level Path What it applies to
Solution Current folder (solution folder) or any folder up to the drive root All projects in subfolders of that folder
User Windows: %appdata%\NuGet\NuGet.Config; Mac/Linux: ~/.nuget/NuGet/NuGet.Config (.NET CLI) or ~/.config/NuGet/NuGet.Config (Mono; varies by tooling) NuGet operations for that user
Computer Windows: %ProgramFiles(x86)%\NuGet\Config; Linux: /etc/opt/NuGet/Config; Mac: /Library/Application Support (by default) NuGet operations on that computer

If you place a nuget.config file inside a project folder, it has no effect on that project. Put the file in your solution folder, or in a parent folder above the projects it should cover.

Settings apply to any NuGet operation invoked from the command line or from Visual Studio, with settings closest to the solution or current folder taking precedence. NuGet loads the computer-level file first, then the user-level file, and then a file in every folder from the drive root down to the current folder.

When more than one file is found, NuGet merges them in two ways:

  • For collection elements such as <packageSources>, NuGet combines the values from all configuration files into a single collection.
  • For single-item elements, NuGet replaces any previously found value for the same key.

To ignore that chain for a single command, pass a config file explicitly on the command line, such as nuget -configFile my.config or dotnet restore --configfile my.config. When you specify a file that way, only the settings from that file are used. See Microsoft’s Configuring NuGet behavior docs for the full rules.

Package sources and <clear />

The <packageSources> element lists all known package sources. Each entry sets a key (the name you assign to the package source), a value (the path or URL of the package source), and protocolVersion (the NuGet server protocol version to use; the current version is "3").

Do not rely on line order to pick a feed. The order of entries in <packageSources> is ignored during restore operations and with any project using the PackageReference format.

Because NuGet combines <packageSources> across computer, user, and folder files, a machine-level or user-level nuget.config can pull in feeds you did not put in your solution file. Adding <clear /> inside a node tells NuGet to ignore previously defined configuration values for that node. As Microsoft explains in the nuget.config reference, you may need <clear /> elements to ensure no user-specific or machine-specific configuration is applied.

If you need to override <packageSources> from the command line without editing nuget.config, you can pass --source with the .NET CLI, pass -Source with the NuGet CLI, or set the RestoreSources MSBuild property.

Credentials without a password in the file

Private feeds store their credentials under <packageSourceCredentials>. NuGet supports two password keys:

  • password holds an encrypted password, which is NuGet’s default unless -storepasswordincleartext is used. Encrypted passwords are only supported on Windows, and they can only be decrypted on the same machine by the same user who ran the original encryption.
  • cleartextpassword (written ClearTextPassword in Microsoft’s XML example) holds an unencrypted password. Microsoft strongly discourages storing passwords in clear text, and notes that environment variables can be used here for improved security.

NuGet 3.4 and later support environment variables in nuget.config values. Keep two rules in mind:

  1. You have to use Windows-style environment variable syntax, starting and ending with %, even on Mac and Linux.
  2. If an environment variable is not found, NuGet uses the literal value from the configuration file.

Here is Microsoft’s example for using an unencrypted password stored in an environment variable:

<packageSourceCredentials>
<Contoso>
<add key="Username" value="user@contoso.com" />
<add key="ClearTextPassword" value="%ContosoPassword%" />
</Contoso>
</packageSourceCredentials>

dotnet nuget add source

The dotnet nuget add source command adds a new package source to your NuGet configuration files. Here is the command synopsis from Microsoft’s dotnet nuget add source documentation:

dotnet nuget add source <PACKAGE_SOURCE_PATH> [--name <SOURCE_NAME>] [--username <USER>]
[--password <PASSWORD>] [--store-password-in-clear-text]
[--valid-authentication-types <TYPES>] [--configfile <FILE>] [--allow-insecure-connections]
[--min-publish-age-hours <HOURS>]

Two options matter most when you are configuring a shared nuget.config file:

  • --configfile <FILE> specifies the nuget.config file to use. When specified, only the settings from that file are used; when omitted, dotnet uses the hierarchy of configuration files from the current directory.
  • --store-password-in-clear-text enables storing portable package source credentials by disabling password encryption.

If you plan to commit nuget.config for your team and build machines, commit the environment variable form in ClearTextPassword instead of an encrypted password or a literal secret. Encrypted passwords only decrypt for the same user on the same Windows machine, and storing passwords in clear text is strongly discouraged. Putting an environment variable in ClearTextPassword keeps credentials out of the file and works on Windows, Mac, and Linux.

Package Source Mapping

By default, NuGet searches all configured package sources when it needs to download a package. When a package exists on multiple sources, it may not be deterministic which source the package will be downloaded from.

Added in NuGet 6.0, Package Source Mapping lets you filter, per package, which source or sources NuGet will search. Microsoft calls it a way “to improve your supply chain security”.

Two rules apply once you turn it on:

  • Once you define a <packageSourceMapping> element, you must explicitly define which sources every package, including transitive packages, will be restored from.
  • When multiple unique patterns match a package ID, the most specific one is preferred.

Here is Microsoft’s example, where each <packageSource> key matches a key from <packageSources>:

<packageSourceMapping>
<!-- key value for <packageSource> should match key values from <packageSources> element -->
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
<packageSource key="contoso.com">
<package pattern="Contoso.*" />
<package pattern="NuGet.Common" />
</packageSource>
</packageSourceMapping>

dotnet nuget push

The dotnet nuget push command pushes a package to the server and publishes it. It uses server and credential details found in the system’s NuGet config file or chain of config files. Here is the synopsis from Microsoft’s dotnet nuget push documentation:

dotnet nuget push [<ROOT>] [--allow-insecure-connections] [-d|--disable-buffering] [--force-english-output]
[--interactive] [-k|--api-key <API_KEY>] [-n|--no-symbols]
[--no-service-endpoint] [-s|--source <SOURCE>] [--skip-duplicate]
[-sk|--symbol-api-key <API_KEY>] [-ss|--symbol-source <SOURCE>]
[-t|--timeout <TIMEOUT>]
[--configfile <FILE>]

Two flags are worth noting:

  • -s|--source <SOURCE>: Starting with NuGet 3.4.2, --source is mandatory unless your nuget.config file specifies a DefaultPushSource value.
  • --skip-duplicate: When pushing multiple packages to an HTTP(S) server, this treats any 409 Conflict response as a warning so other pushes can continue.

Where CloudRepo helps

CloudRepo is fully managed, so there is no NuGet server for you to run, and you restore and push with the dotnet you already use. NuGet repositories in CloudRepo are in early access and give a .NET team its own packages and packages from nuget.org in one source.

In the CloudRepo admin portal, you create three NuGet repositories: a hosted repository for the packages you push, a proxy of nuget.org that fetches public packages and keeps a copy, and a group of the two so one URL serves both. You also need a repository token that covers all three. The username is the email address of the account that made the token.

Save this file beside your solution and put your group’s name in place of your-repo. It holds no token, so you can commit it. <clear /> makes the group your only source, so every package comes through CloudRepo.

nuget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="cloudrepo" value="https://your-org.mycloudrepo.io/repositories/your-repo/index.json" protocolVersion="3" />
</packageSources>
<auditSources>
<clear />
<add key="nuget.org-audit" value="https://data.nuget.org/v3/index.json" />
</auditSources>
<packageSourceCredentials>
<cloudrepo>
<add key="Username" value="you@example.com" />
<add key="ClearTextPassword" value="%CLOUDREPO_TOKEN%" />
</cloudrepo>
</packageSourceCredentials>
</configuration>

Set CLOUDREPO_TOKEN to your repository token first, in your shell or as a CI secret. Without it, the restore fails with a 401. Then restore or add packages:

Terminal
dotnet restore
Terminal
dotnet add package Newtonsoft.Json --version 13.0.3

To publish your own packages, push to your hosted repository. A group or a proxy takes no push, and dotnet says it “does not support updating packages”. Add your hosted repository to nuget.config as a second source named cloudrepo-push, and put your hosted repository’s name in place of your-hosted-repo:

nuget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="cloudrepo" value="https://your-org.mycloudrepo.io/repositories/your-repo/index.json" protocolVersion="3" />
<add key="cloudrepo-push" value="https://your-org.mycloudrepo.io/repositories/your-hosted-repo/index.json" protocolVersion="3" />
</packageSources>
<auditSources>
<clear />
<add key="nuget.org-audit" value="https://data.nuget.org/v3/index.json" />
</auditSources>
<packageSourceCredentials>
<cloudrepo>
<add key="Username" value="you@example.com" />
<add key="ClearTextPassword" value="%CLOUDREPO_TOKEN%" />
</cloudrepo>
<cloudrepo-push>
<add key="Username" value="you@example.com" />
<add key="ClearTextPassword" value="%CLOUDREPO_TOKEN%" />
</cloudrepo-push>
</packageSourceCredentials>
</configuration>

Pack and push to cloudrepo-push:

Terminal
dotnet pack -c Release -o out
dotnet nuget push out/*.nupkg --source cloudrepo-push

Your package then restores through the group, alongside packages from nuget.org. Moving NuGet feeds from Artifactory or Nexus has its own migration docs page.

More: Private NuGet repository, Use NuGet with dotnet, Push a package, Move NuGet feeds from Artifactory or Nexus, Repository tokens, and all client guides.

Every feature on every plan, with unlimited users

14 days free. No credit card required.

Starting at $99/month billed annually.

Chris Shellenbarger, founder of CloudRepo

Founder-run since 2016, with no venture capital. A real person answers within one business day. Usually it's me.

Chris, founder of CloudRepo