NuGet Packaging: Multiple & Custom Nuget Registries

How to setup multiple package source feeds in multiple ways

Dec 16, 2023

Engineering

Nuget Packaging: Multiple & Custom Nuget Registries
Nuget Packaging: Multiple & Custom Nuget Registries

NuGet Packaging Series

This article is part of the Creating NuGet Packages series. You can find the complete list of articles in the series at the end.


Introduction

In continuation of the previous article about publishing NuGet packages, this article describes configuring IDEs and a .NET project to fetch NuGet packages from multiple NuGet feeds. This includes official NuGet.org feeds, private (GitHub) feeds, and local directories.


Using JetBrains Rider

In Rider, open the Nuget Window and:

  • Select Feeds tab

  • Add a new repository by clicking the + and specifying the feed properties

Adding Local or Shared Directories

To add local or shared directories on your disk, set the feed properties as follows:

  • Name is the name of the feed as you see fit.

  • URL is the disk path where you are storing NuGet packages.


Adding GitHub NuGet Package Feed

Adding GitHub Packages requires a bit more effort, but it is still a straightforward process.

Where:

  • Name is the name of the feed as you see fit.

  • URL is the URL of your GitHub Packages feed. The format is https://nuget.pkg.github.com/USERNAME/index.json where USERNAME is your GitHub username or organization name under which the repository resides.

  • User is your GitHub username or organization name under which the repository resides.

  • Password is your Personal Access Token (PAT) generated on GitHub for the GitHub Packages feed. This is NOT your GitHub password.


Using Visual Studio

  • Right-click on the solution in Solution Explorer.

  • Choose "Manage NuGet Packages for Solution."

  • Click on "Settings" or the gear icon.

  • Under "Package Sources," click the "+" button to add a new source.

The rest is the same as we did in JetBrains Rider.


Manually adding using Nuget.config

Using nuget.config: You can manually edit or create a nuget.config file at the solution, project, or user profile levels.

While solution and project-level package repositories are simple as you place nuget.config either in the solution or project directory; if you want to add the local package repository on a user profile level on Windows, you will have to dig into the User/AppData directory where the path is usually:

C:\Users\{Name of the User}\AppData\Roaming\NuGet\NuGet.Config

Here's how the configuration can look:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!-- Add the official NuGet source -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />

    <!-- Add a private repository -->
    <add key="MyPrivateRepo" value="https://myprivaterepo.com/nuget" />

    <!-- Add a local folder -->
    <add key="LocalFolder" value="C:\path\to\local\nuget\packages" />

    <!-- Add a GitHub Packages -->
    <add key="GitHub" value="https://nuget.pkg.github.com/USERNAME/index.json" />
  </packageSources>

  <!-- If you have authentication for private repos -->
  <packageSourceCredentials>
    <GitHub>
      <add key="Username" value="USERNAME" />
      <add key="ClearTextPassword" value="PASSWORD_or_PAT" />
    </GitHub>
  </packageSourceCredentials>
</configuration>

If you're using a local path, it's important to note that the path can be either an absolute path or a relative path from the location of the nuget.config file.

> Remember to be cautious when storing credentials in nuget.config, especially if this file is checked into a public version control repository. Using environment variables or other mechanisms to keep secrets out of your source code is a good practice.

> NOTE: You can also add a custom nuget.config in the same directory where the .sln file is and will be additionally used when that particular solution runs.


Using the NuGet CLI

You can also add, remove, and manage package sources using the NuGet CLI:

To add a source

nuget sources add -Name "MySource" -Source "https://myprivaterepo.com/nuget"

To add a source with a Password or PAT

Replace YOUR_USERNAME with your username and YOUR_PERSONAL_ACCESS_TOKEN with your Personal Access Token or password.

Please note it's important to be cautious when using the -Password option in the CLI, especially on shared or public systems, as it can expose sensitive credentials in the command history or logs.

nuget sources add -Name "MySource" -Source "https://myprivaterepo.com/nuget" -UserName YOUR_USERNAME -Password YOUR_PERSONAL_ACCESS_TOKEN

To list all sources

nuget sources list

To remove a source

nuget sources remove -Name "MySource"


Order of Execution

When you have multiple sources, NuGet will try to fetch packages from all of them according to the order specified in the configuration.

If multiple sources contain the same package version, NuGet will fetch it from the first source where it is found.


What Have We Learned?

Providing multiple source feeds for NuGet packages is a straightforward and adaptable process that can be accomplished in various ways to meet different needs.

In this article, we learned how to consume from multiple NuGet feeds using:

  • JetBrains Rider

  • Visual Studio

  • global NuGet.Config

  • the nuget CLI

Example Project on GitHub

An example project for this article can be found here:

Workshop-Nuget-Packaging

Creating NuGet Packages Series

This article is part of the Creating NuGet Packages series. If you enjoyed this one and want more, here is the complete list in the series:

Happy Coding!

More Articles

Thanks
for Visiting

.. and now that you've scrolled down here, maybe I can invite you to explore other sections of this site

Thanks
for Visiting

.. and now that you've scrolled down here, maybe I can invite you to explore other sections of this site