This post was requested by @timotheecour on 
[GitHub](https://github.com/nim-lang/Nim/issues/13166#issuecomment-631905175). 
In it, I’ll walk through my process of setting up a 
[FreeBSD](https://www.freebsd.org) Virtual Machine for the purposes of working 
on the Nim compiler and standard library.

I’ll be using macOS as a host, but this guide should work for any environment 
that supports [VirtualBox](https://www.virtualbox.org).

# Setting up the hypervisor

We’ll be using VirtualBox as our hypervisor as it is supported on the majority 
of operating systems. You can of course using other hypervisors such as Hyper-V 
or QEMU, though the instructions will differ slightly for those platforms.

Head to [the VirtualBox downloads 
page](https://www.virtualbox.org/wiki/Downloads) and download the latest 
release for your platform, then run through the installer process.

# Downloading the FreeBSD installer image

The next step is to download FreeBSD. Head to [the FreeBSD downloads 
page](https://www.freebsd.org/where.html) and select the amd64 installer image 
for the most recent release. At the time of writing, this is FreeBSD 
12.1-RELEASE.

In the directory index for the release, download the -amd64-disc1.iso file - 
this is currently 
[FreeBSD-12.1-RELEASE-amd64-disc1.iso](https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/12.1/FreeBSD-12.1-RELEASE-amd64-disc1.iso).

# Creating the FreeBSD VM

Now that you have a hypervisor setup and the installer for FreeBSD downloaded, 
it’s time to create a VM.

  1. In VirtualBox, select the New button.
  2. Set a name for the Virtual machine - I will call it FreeBSD 12.1 Nim 
Environment.
  3. VirtualBox should automatically select BSD as the VM type and FreeBSD 
(64-bit) as the VM version.
  4. Select a folder to save the VM to - I usually just use the default 
location for a development VM.
  5. Press Continue.
  6. Set the amount of memory to use for the VM. My host machine has plenty of 
memory, so I’m assigning 6GB to the VM (6,144MB). [FreeBSD’s hardware 
requirements](https://www.freebsd.org/doc/handbook/bsdinstall-hardware.html) 
recommend 2-4GB RAM for a general purpose desktop system at the minimum.
  7. Press Continue.
  8. Select Create a virtual hard disk now.
  9. Press Create.
  10. Select VDI (VirtualBox Disk Image).
  11. Press Continue.
  12. Select Dynamically allocated.
  13. Press Continue.
  14. Select a location to save the virtual disk image to - I usually just use 
the default location for a development VM.
  15. Select a size for the virtual disk image - I usually just use the default 
size of 16GB. [FreeBSD’s hardware 
requirements](https://www.freebsd.org/doc/handbook/bsdinstall-hardware.html) 
recommend 8GB for a general purpose desktop system at the minimum.
  16. Press Create.



You could now tweak the VM settings to attach serial devices, allocate more 
video memory, etc. but I have no need to do that at the moment.

# Installing FreeBSD within the VM

In the VirtualBox interface, press Start to start the VM.

You will be prompted to select a virtual optical disk file or a physical 
optical drive to start the VM from. Click the folder icon to open the media 
explorer, then click Add. Browse to the FreeBSD ISO you downloaded earlier and 
open it. Once the ISO is selected in the media explorer, click Choose to select 
the ISO.

Press the Start button to start the VM using the ISO.

The VM should start up, and after a while you should reach the FreeBSD 
installer welcome screen. Now’s the time to preform the install.

  1. Press the Enter key on your keyboard to select the Install option.
  2. Select a keyboard layout from the list - use the arrow keys on your 
keyboard to navigate the list, then press the Enter key on your keyboard to 
select. I will be using United Kingdom.
  3. After choosing a keyboard layout, navigate to the Continue with X.kbd 
keymap (where X is your chosen keyboard layout - uk.kbd in my case) entry and 
press Enter key on your keyboard.
  4. Enter a hostname for your VM, then press the Enter key on your keyboard. I 
will be using freebsd-nim-vm.
  5. Select the Distributions you wish to install - the defaults should be 
fine. You can select entries using the Space key on your keyboard. Upon 
completion, press the Enter key on your keyboard.
  6. Select Auto (ZFS) in the partitioning screen.
  7. You may then alter some parameters for the disk partitioning. I will be 
leaving them at the default for this development VM, though would normally 
select Encrypt Swap for a production machine and would normally configure 
mirroring. Press the Enter key on your keyboard to continue.
  8. In the ZFS configuration screen, select stripe. In a production machine, I 
would normally configure mirroring. Press the Enter key on your keyboard to 
continue.
  9. In the disk selection screen, select the VirtualBox hard disk option, 
using the Space key on your keyboard to select it. Press the Enter key on your 
keyboard to continue.
  10. Select Yes when asked if you really want to destroy the current contents 
on the disks.
  11. You will now have a short wait while achieves are extracted.
  12. Set a new password for the root user account at the prompt. You will have 
to repeat the password to ensure it is correct.
  13. At the network interface configuration screen, select the default network 
interface.
  14. When prompted to configure IPv4 for the interface, select Yes.
  15. When prompted to use DHCP to configure the interface, select Yes.
  16. Do the same for IPv6, if you have an IPv6 network.
  17. In the network configuration screen, make sure the detected search domain 
and DNS server settings look correct then select Ok to continue.
  18. In the timezone selector, select your timezone.
  19. In the time and date selector screens, if the time and date looks 
correct, select Skip to continue.
  20. In the system configuration screen, make sure sash, ntpdate and ntpd are 
selected at the minimum.
  21. At the system hardening screen, I normally select all entries.
  22. When asked if you want to add users to the system, select Yes then follow 
the prompts to set up your user.
  23. At the final configuration screen, select Exit to apply the configuration 
and exit the installer.
  24. You will now get the choice to enter into a shell to finish configuring 
the system or to reboot. I usually reboot, then configure afterwards. **Before 
rebooting** , select the Devices menu in VirtualBox, then select Optical Drives 
and then deselect the FreeBSD ISO. Otherwise, when you reboot the VM will boot 
back into the installer.



# Initial FreeBSD configuration

We now need to do some configuration. It’s best to login as root to perform 
these steps.

## Installing updates

First step is to make sure the system is up to date with the latest patches. We 
can easily do this using 
[freebsd-update](https://www.freebsd.org/cgi/man.cgi?freebsd-update):
    
    
    # freebsd-update fetch
    # freebsd-update install
    
    
    Run

## Installing some useful utilities

I like to install some utilities that I tend to use on any system. We’ll also 
install [the VirtualBox guest 
additions](https://www.freebsd.org/doc/handbook/virtualization-guest-virtualbox.html).
 We’ll use binary packages for these:
    
    
    # pkg install -y editors/vim \
            emulators/virtualbox-ose-additions \
            security/sudo \
            shells/bash
    
    
    Run

## Enable the VirtualBox guest additions service

We need to enable and start the VirtualBox guest additions services. During 
install, I enabled NTP so we also disable host time syncing:
    
    
    # sysrc vboxguest_enable="YES"
    # sysrc vboxservice_enable="YES"
    # sysrc vboxservice_flags="--disable-timesync"
    # service vboxguest start
    # service vboxservice start
    
    
    Run

## Allow your user account to use sudo

I like to use sudo via a group, so we need to enable group access to sudo:
    
    
    # visudo
    
    
    Run

Find the following line and uncomment it by removing the leading #:
    
    
    %wheel ALL=(ALL) ALL
    
    
    Run

Now add your user to the wheel group:
    
    
    # pw usermod USER -G wheel
    
    
    Run

## Install packages required for working with Nim and running the standard 
library tests

There are a few packages needed for working with Nim and running the standard 
library tests. These can be installed as binary packages:
    
    
    # pkg install -y databases/sqlite3 \
            devel/boehm-gc-threaded \
            devel/git \
            devel/gmake \
            devel/pcre \
            devel/sdl20 \
            devel/sfml \
            www/node
    
    
    Run

## Setting bash as your default shell

Other shells are of course available. You should probably leave the root shell 
at the default value /bin/csh:
    
    
    $ chsh -s $(which bash)
    
    
    Run

# Getting and building Nim

Now that the system is set up, we will want to get the Nim sources and build 
them so that we have a working compiler. We’ll also add Nim to the path.
    
    
    $ git clone https://github.com/nim-lang/Nim.git $HOME/Nim
    $ cd $HOME/Nim
    $ git clone --depth 1 -q https://github.com/nim-lang/csources.git ./csources
    $ gmake -C csources -j $(sysctl -n hw.ncpu)
    $ bin/nim c koch
    $ ./koch boot
    $ ./koch tools
    $ echo 'PATH=$PATH:$HOME/Nim/bin; export PATH' >> $HOME/.profile
    $ PATH=$PATH:$HOME/Nim/bin; export PATH
    
    
    Run

You should now be able to start working on Nim and running any tests you wish 
to run.

Reply via email to