Integrating Nexus Repository into Your iOS Project: A Step-by-Step Guide

Anas Poovalloor
4 min readJul 31, 2024

--

Welcome to this in-depth guide on integrating a Nexus repository with your iOS project using CocoaPods. In this tutorial, we’ll walk you through the process of configuring your Podfile to work with both a Nexus repository that requires authentication and the official CocoaPods repository.

While we use Nexus as an example, you can apply the same steps to integrate any private repository as a source for installing pods into your Swift project.

You’ll learn two methods to handle Nexus authentication:

  1. Using a .netrc File: A straightforward approach for managing credentials.
  2. Using Environment Variables: A flexible method for secure credential management.

By the end of this guide, you’ll be equipped with the knowledge to seamlessly integrate and manage dependencies from both private and public repositories in your iOS projects. Let’s dive in and set up your development environment!

Prerequisites

Before we dive in, make sure you have CocoaPods installed on your system. If not, you can easily install it using:

sudo gem install cocoapods

Method 1: Using a .netrc File

Step 1: Create the .netrc File

Open Terminal and Navigate to Your Home Directory:

Type the following command to go to your home directory:

cd ~

Create or Edit the .netrc File:

Use a text editor like nano to open or create the .netrc file:

nano .netrc

Add the following lines, replacing placeholders with your actual Nexus credentials and URL:

machine your-nexus-repo-url
login your-username
password your-password

Save and exit the editor (in nano, press CTRL + X, then Y, and Enter).

When setting up your .netrc file for authentication with remote servers, it's crucial to ensure that each machine entry specifies only the hostname, without any additional path or protocol (https://) information. This ensures that the authentication process works smoothly and avoids potential errors.

Incorrect Configuration

An incorrect configuration of the .netrc file might look like this, where the protocol and path is included in the machine entry:

machine https://example.com/repository/sample-path-1
login username1
password password1

machine example.com/repository/sample-path-2
login username2
password password2

Correct Configuration

To configure the .netrc file correctly, ensure that each machine entry includes only the base domain name:

machine example1.com
login username1
password password1

machine example2.com
login username2
password password2

Step 2: Set the Correct Permissions

Ensure only you can read and write to the file by setting the permissions:

chmod 600 ~/.netrc

Verify File Access:

ls -l ~/.netrc

The output should show -rw------- permissions.

Step 3: Edit the Podfile

Open the Podfile:

Navigate to your iOS project directory and open the Podfile in your preferred text editor.

Configure the Podfile:

Add both your Nexus repository and the official CocoaPods (if required) repository to the Podfile:

# Nexus repository (credentials will be read from .netrc)
source 'https://your-nexus-repo-url'

# Official CocoaPods repository
source 'https://cdn.cocoapods.org/'

platform :ios, '12.0'

target 'YourAppTarget' do
# Pods from Nexus
pod 'YourNexusPodName'

# Pods from CocoaPods
pod 'AFNetworking'
end

Step 4: Install Pods Using CocoaPods

Run the following command to install the pods:

pod install

Method 2: Using Environment Variables

Step 1: Set Environment Variables

Open Terminal Set Environment Variables:

Use the export command to set the environment variables:

export NEXUS_USERNAME=your-username
export NEXUS_PASSWORD=your-password

Optional: Persist Variables:

To make these variables persistent, add them to your shell configuration file (e.g., ~/.bash_profile for bash):

nano ~/.bash_profile

Add the following lines:

export NEXUS_USERNAME=your-username
export NEXUS_PASSWORD=your-password

Save and apply changes with:

source ~/.bash_profile

Step 2: Edit the Podfile

Navigate to your iOS project directory and open the Podfile to configure

Use the environment variables for Nexus authentication and specify both repositories:

nexus_username = ENV['NEXUS_USERNAME']
nexus_password = ENV['NEXUS_PASSWORD']

# Nexus repository with authentication
source "https://#{nexus_username}:#{nexus_password}@your-nexus-repo-url"

# Official CocoaPods repository
source 'https://cdn.cocoapods.org/'

platform :ios, '12.0'

target 'YourAppTarget' do
# Pods from Nexus
pod 'YourNexusPodName'

# Pods from CocoaPods
pod 'AFNetworking'
end

Step 3: Install Pods Using CocoaPods

Execute the following command to install the pods:

pod install

Integrate CocoaPods and Nexus Repositories with VPN Access

Accessing the Nexus repository might require a VPN connection, which ensures secure and authorized access to your internal libraries. Follow the steps below to seamlessly integrate and update your project dependencies, even when a VPN connection is necessary.

Step 1: Podfile Configuration

Your Podfile should specify multiple sources to handle pods from both the CocoaPods official repository and your Nexus repository.

Here’s an example:

platform :ios, '13.0'

# Specify the source repositories for your pods
source 'https://github.com/CocoaPods/Specs.git' # CocoaPods official repository
source '<Nexus Source URL>' # Nexus repository

target 'YourApp' do
use_frameworks! # Use frameworks instead of static libraries (optional)

# Specify your pod dependencies here
pod 'AFNetworking', '~> 4.0'
pod 'SomeNexusPod', '~> 1.0', :source => '<Nexus Source URL>'
end

Step 2: Updating Pods

To update your pods effectively:

1. Connect to VPN: Begin by connecting to your VPN to access the Nexus repository.

2. Update Nexus Repository Pods:

pod update --sources=<Nexus Source URL>

3. Disconnect from VPN: After updating Nexus pods, disconnect the VPN.

4. Update CocoaPods Official Repository Pods:

pod update --sources=https://github.com/CocoaPods/Specs.git

5. Install Pods: Ensure your workspace is updated.

pod install

Security Considerations

  • .netrc Security: Make sure the .netrc file is not included in version control. Add it to your .gitignore file.
  • Environment Variables: Be cautious when adding environment variables to shell configuration files if they are shared or version-controlled.

In this guide, we’ve explored two effective methods for configuring Nexus repository authentication in your iOS project using CocoaPods. Whether you choose to use a .netrc file or environment variables, you can securely manage both private and public dependencies.

By following these steps, you’ll ensure a smooth integration of Nexus repositories into your workflow, enhancing your project’s flexibility and efficiency. Choose the method that best aligns with your security practices and development needs.

--

--