Use environment variable in NuGet config?

16,469

Solution 1

If you run nuget pack MyProject.csproj on a project with a corresponding MyProject.nuspec file in the same directory, NuGet will make the MSBuild properties available as tokens in the format $Property$, where Property is the name of the MSBuild property. For example:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <copyright>Copyright 2013</copyright>
  </metadata>
  <files>
    <file src="$OutputPath$MyProject.pdb" target="lib\net40"/>
  </files>
</package>

In this example $id$, $version$, $title$, $author$ and $description$ are special values provided by NuGet itself based on the project file and AssemblyVersion attribute (normally found in AssemblyInfo.cs). However, $OutputPath$ is an MSBuild property defined in the common targets. You could also use $MSBuildProjectDirectory$ or any other standard property.

More info here ("Replacement Tokens" section):

NuSpec Reference

Solution 2

The following documentation indicates environment variables can be used in key values.

https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file#using-environment-variables

Solution 3

NuGet V3 supports a Global Packages Folder, see http://blog.nuget.org/20151008/NuGet-3-What-and-Why.html.

NuGet V3.2 supports a global NUGET_PACKAGES environment variable to specify the location of the shared global packages folder. See: https://docs.nuget.org/release-notes/nuget-3.2

Maybe one of these can help you solve your issues.

I had the same question as you did and these features look promising in solving the actual issues I had. NuGet 3.2 is only available for VS2015, and we have some other issues with 2015, so I haven't tried it yet.

Solution 4

This works using standard %foo% environment variable references as of nuget 3.4 and higher: https://github.com/NuGet/Home/issues/1852

Solution 5

Here is the syntax :

<configuration>
  <config>       
    <add key="repositoryPath" value="%ENVIRONMENT_VARIABLE%/teampackages" />
  </config>
</configuration>

Important Notes

  1. The variable name should be surrounded with %%; Example %env_variable_name%
  2. Do NOT use $env_variable_name syntax. (for MAC and Linux compatibility reasons)
  3. The directory separator is / and NOT \ (for MAC compatibility reasons)
  4. This requires (NuGet 3.4+)
  5. For more info here is the doc
Share:
16,469

Related videos on Youtube

Andriy Drozdyuk
Author by

Andriy Drozdyuk

I like AI and in particular Reinforcement Learning. I used to like Erlang, Scala, python, Akka and Zeromq! Did my undergrad in Computer Science at University of Toronto. Did my masters in Data Mining at University of New Brunswick. Working as a programmer at NRC and doing PhD part time at Carleton University.

Updated on February 20, 2022

Comments

  • Andriy Drozdyuk
    Andriy Drozdyuk over 2 years

    Is there a way to use an environment variable in NuGet.Config file?

    Currently I am constrained to using relative path, as follows:

    <configuration>
      <config>       
        <add key="repositoryPath" value="..\..\teampackages" />
      </config>
    </configuration>
    

    But would be really handy to have an environment variable with absolute path instead.

  • Andriy Drozdyuk
    Andriy Drozdyuk almost 11 years
    Oh, so that's what the nuspec file is for. Thanks! What about environment variable, like PATH for example?
  • Jesse Sweetland
    Jesse Sweetland almost 11 years
    They might work via MSBuild properties. See: msdn.microsoft.com/en-us/library/ms171459(v=vs.80).aspx
  • kiki
    kiki almost 8 years
    I am not sure I understand how it answers the question. When I use a nuget.config with <?xml version="1.0" encoding="utf-8"?> <configuration> <config> <add key="repositoryPath" value="$MSBuildProjectDirectory$" /> </config> </configuration> it creates a folder called $MSBuildProjectDirectory$ next to the .sln file instead of using the project directory. Where do I go wrong?
  • Jesse Sweetland
    Jesse Sweetland almost 8 years
    I've jused MSBuild properties successfully in .nuspec files, but not in nuget.config. It may be that they are not supported in nuget.config.