# 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](https://help.sonatype.com/en/realms.html "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,
   
   - `[NEXUS_URL]` \- The URL of your Nexus instance
   
   - `<REPO_NAME>` \- The name of the target repository in Nexus
   
   If using anonymous access, skip the authentication step and run your pub commands.

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

You can configure the credentials in the following methods:
   
   - [Interactive Method](https://help.sonatype.com/en/configure-pub-with-nexus.html#UUID-661b709f-712c-dce1-22bc-b6889603f9fd_N1776168387391 "Interactive Method:")
   
   - [Environmental Variables](https://help.sonatype.com/en/configure-pub-with-nexus.html#UUID-661b709f-712c-dce1-22bc-b6889603f9fd_N1776168417067 "Environmental Variables")

### Interactive Method:

1. Run the following command for the Nexus URL.

```
   dart pub token add https://[NEXUS_URL]/repository/<REPO_NAME>/
   ```
   
2. You get a response to enter your secret token. Enter the token.
3. 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:

```bash
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:

```bash
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:

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