Setting up a local chocolatey repository with nexus

Increasing our automation approach at work and after some rate limiting fun hitting the chocolatey servers from an ansible playbook I realized I would need a local repository to get this done so here we go!

Spin up a windows server 2019 vm, cruise through the oobe, rename and join domain, install chocolatey from an administrative powershell

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

close and reopen powershell then run

choco install nexus-repository

failed while waiting for the web ui to be available

Waiting on Nexus Web UI to be available at 'http://example.com:8081/'
ERROR: The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Nexus did not respond to requests at 'http://example.com:8081/' within 3 minutes of the service being started.
The install of nexus-repository was NOT successful.
Error while running 'C:\ProgramData\chocolatey\lib\nexus-repository\tools\chocolateyinstall.ps1'.
See log for details.
Chocolatey installed 2/3 packages. 1 packages failed.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Failures
nexus-repository (exited -1) - Error while running 'C:\ProgramData\chocolatey\lib\nexus-repository\tools\chocolateyinstall.ps1'.
See log for details.

looks like theres a fix for this so let's try this

choco install nexus-repository -params "'/Fqdn:chocorepo.example.com'" -y -f -s "'https://chocolatey.org/api/v2'"

failed again!

Failures
nexus-repository (exited -1) - Error while running 'C:\ProgramData\chocolatey\lib\nexus-repository\tools\chocolateyinstall.ps1'.
See log for details.

seems a helper script is failing, lets run it directly

Get-PackageParameters : The term 'Get-PackageParameters' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At C:\ProgramData\chocolatey\lib\Temurinjre\tools\chocolateyinstall.ps1:6 char:16

$parameters = (Get-PackageParameters); $pp = ( Test-PackageParamaters ...

           ~~~~~~~~~~~~~~~~~~~~~

CategoryInfo          : ObjectNotFound: (Get-PackageParameters:String) [], ParentContainsErrorRecordE
xception

FullyQualifiedErrorId : CommandNotFoundException

okay lets find Get-PackageParameters! looks like a powershell running

Install-Module -Name PackageManagement -Force

installs nuget and upgrades the PackageManagement module, lets try again

choco install nexus-repository -params "'/Fqdn:chocorepo.example.com'" -y -f -s "'https://chocolatey.org/api/v2'"

Success!

The output advises on the default user and password location, and tells you to run
netsh advfirewall firewall add rule name="Nexus Repository" dir=in action=allow protocol=TCP localport=8081
to open a firewall port to the service.  

Open http://chocorepo.example.com:8081 in a browser, use admin and the password from c:\ProgramData\sonatype-work\nexus3\admin.password and we are cruising.

First thing I did was change the default location of the downloaded packages so it wouldn't be under my user profile, by going to settings, repository, blob stores and setting an absolute path of c:\nexus and hitting save.  This took almost no time to create a new folder and set it up as a repository.

Next go to repositories, create repository, and select nuget proxy as type.
Give it a name like chocolatey-proxy, (!!! make sure you select NuGet V2 as the version type !!!) and enter a remote storage location of https://chocolatey.org/api/v2 before hitting save.

Next create another repository, select nuget hosted as type, call it something like chocolatey-hosted, and save.

Finally create one last repository, nuget group type, and call it chocolatey-group.  Select your chocolatey-proxy and chocolatey-hosted repositories and add them to this group and save.  This will give you the url we will use for our chocolatey source, in this case http://chocorepo.example.com:8081/repository/choclatey-group/

I'm using this with ansible so I'll just be using that url as my ch0colatey source from here on out when installing packages, anything not already in the repo will get downloaded to nexus and shared accordingly.  Fun!