Configure Pub with Nexus

Configure Pub with Nexus

Configure your Pub client to connect to and authenticate to a Nexus Pub Repository. Note that you must create a Pub repository in Nexus before configuring your client to connect to it.

Note
Pub Token Realm must be added to the Active realms as documented in Realms. Once the realm is activated, you can establish the authentication to a Nexus Pub repository.

Take the following steps to configure your Pub client with Nexus:

  1. Set the Pub registry URL by pointing Pub tooling to Nexus using PUB_HOSTED_URL.

For Linux / macOS:

export PUB_HOSTED_URL=https://[NEXUS_URL]/repository/<REPO_NAME>

For Windows PowerShell:

$env:PUB_HOSTED_URL = "https://[NEXUS_URL]/repository/<REPO_NAME>"

Where,

If using anonymous access, skip the authentication step and run your pub commands.

  1. If authentication is required, configure credentials for Nexus using dart pub token.

You can configure the credentials in the following methods:

Interactive Method:

  1. Run the following command for the Nexus URL.
   dart pub token add https://[NEXUS_URL]/repository/<REPO_NAME>/
  1. You get a response to enter your secret token. Enter the token.
  2. After you submit the token, Pub confirms that requests and writes it to its token store (pub-tokens.json) in the user config directory. For Linux / macOS, the token files are stored at ~/.config/dart/pub-tokens.json. and for Windows, they are stored at %APPDATA%\dart\pub-tokens.json.

Environmental Variables

  1. Export the token into an environment variable.

    For Linux/macOS:

    export DART_PUB_TOKEN="<your_token_value>"
    

    For Windows:

    $env:DART_PUB_TOKEN = "<your_token_value>"
    
  2. Register the token with Pub.

    dart pub token add "https://<NEXUS_URL>/repository/<REPO_NAME>/" --env-var DART_PUB_TOKEN
    
  3. You are now configured to run Pub commands through Nexus Repository.

Example using Interactive Method:

export PUB_HOSTED_URL=https://example.nexus.com/repository/pub-hosted

dart pub token add https://example.nexus.com/repository/pub-hosted/

# Enter your token

Example using Environmental Variables:

export PUB_HOSTED_URL=https://example.nexus.com/repository/pub-hosted

# export token into your env variable
export DART_PUB_TOKEN="XXXXXXXXXXXX"

dart pub token add "https://<NEXUS_URL>/repository/<REPO_NAME>/" --env-var DART_PUB_TOKEN

Example for Anonymous Access:

export PUB_HOSTED_URL=https://example.nexus.com/repository/pub-hosted
#Run your Pub commands after anonymous access is enabled for the repository