9

I'm struggling to add new NuGet source to store private packages. I have already tried to add config into my .csproj like this post suggests

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Artifactory-DEV" value="https://theluggage-agct.gray.net/artifactory/api/nuget/nuget-institutional-development-local" protocolVersion="3"/>
  </packageSources>
</configuration>

I also tried this but was not able to find an equivalent in dotnet nuget.

nuget sources Add -Name "MyServer" -Source \myserver\packages

OBS: I'm not using Visual Studio 2017 and currently using Ubuntu.

Vikto
  • 512
  • 1
  • 7
  • 19
  • 2
    The post you'd linked to mentions adding the sources to a `Nuget.Config` file (local to the project directory), not the `.csproj` file. Also, there is no equivalent `dotnet nuget` for configuration functions (though you can use the `--source` argument to specify a package source in the `dotnet pack|restore|nuget` commands); you'd use `nuget config` ([see here](https://learn.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior)) to do that. – Frank Alvaro Oct 25 '18 at 19:47

3 Answers3

16

It's a shame that the user ffa didn't post their comment as an answer, because it's correct. The XML is supposed to be in a file named nuget.config.

Also, if you have a newish dotnet SDK/CLI installed, you can also run dotnet new nugetconfig and it will create the file from a basic template for you. NuGet docs have some information about how settings are applied if you care, but generally people put their nuget.config files in the root of their repository, or in the same place as their .sln file.

zivkan
  • 12,793
  • 2
  • 34
  • 51
2

There is another way.

dotnet add package <PACKAGE> -s <SOURCE>

The problem is that I need to add -s every single time I want to add a package from a certain private repo.

EDIT: See "imps" comment.

Vikto
  • 512
  • 1
  • 7
  • 19
  • 1
    Actually I would strongly discourage you from ever installing packages from a specific source in your project. The guidance from the NuGet team is to define your sources for a repository, so that they are shared across all projects. Installing a package from a potentially different source than the one configured in the nuget.config can lead to repeatability concerns. Namely you push you changes and then later you have trouble getting the exact same packages on a different machine. – imps Oct 26 '18 at 19:59
  • 1
    Correct. The command solves my problem by creating another one. That`s why I wanted to statically register a private source when adding new packages. :( – Vikto Oct 27 '18 at 17:30
2

There is a new feature called Package Source Mapping introduced for:

  • Visual Studio 2022 preview 4 and later
  • .NET SDK 6.0.100-rc.1 and later
  • nuget.exe 6.0.0-preview.4 and later

You can find the reference here. Also, this blog explains the new feature.

You can update your nuget.conf file as following:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Artifactory-DEV" value="https://theluggage-agct.gray.net/artifactory/api/nuget/nuget-institutional-development-local" protocolVersion="3"/>
  </packageSources>

  <!-- Actual answer is here -->
  <packageSourceMapping>
    <packageSource key="nuget.org">
      <package pattern="*" />
    </packageSource>
    <packageSource key="Artifactory-DEV">
      <package pattern="Artifactory-DEV.*" />
    </packageSource>
  </packageSourceMapping>


</configuration>

Beware that the key in packageSource should match the key in packageSources.

Iman Kermani
  • 919
  • 9
  • 14