Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions README.vagrant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Vagrantfile for local development environment

This Vagrantfile sets up a local development environment for by creating a
virtual machine with Nix, direnv and a few other tools pre-installed.

## Requirements

- [Vagrant](https://www.vagrantup.com/downloads)
- [VirtualBox](https://www.virtualbox.org/wiki/Downloads)

## Start

```shell
vagrant up
```

## Use

```shell
vagrant ssh
cd cn-quickstart
```

## Stop

```shell
vagrant halt
```

## Clean up

```shell
vagrant destroy

# Optionally, remove the Nix cache to free up space, this will make the next
# `vagrant up` slower as it will need to re-download all Nix packages.
rm -r vagrant-nix-cache
```

## Repopulating Nix cache without recreating the VM

```shell
vagrant halt
rm -r vagrant-nix-cache
vagrant up --provision
```

## Notes

- Only Ubuntu 24.04 is tested as a host OS however other Linux distributions
and Intel-based macOS should work as well.
- Vagrant creates the `vagrant-nix-cache` directory with `nix-cache.img` file
which is shared with the VM. This file is used to store /nix/cache and
/nix/var/nix/db for faster builds and installations of Nix packages. The size
of the image can be changed in the [Vagrantfile](Vagrantfile).
- IP of the VM is set to `192.168.56.10` (it can be changed in the
[Vagrantfile](Vagrantfile)).
- The VM host can be accessed using [nip.io](https://nip.io) domain names, for
example:
- 192.168.56.10.nip.io
- 192-168-56-10.nip.io
- myapp.192-168-56-10.nip.io
- Alternatively you can configure port forwarding to localhost by adjusting
`ports_to_forward` in the [Vagrantfile](Vagrantfile).
120 changes: 120 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# VM configuration
cpus = 4 # Cores
memory = 8 # GiB
ipaddr = "192.168.56.10"

# List of ports to forward from the VM to localhost
ports_to_forward = [
8082, # Keycloak
]

nix_cache_size = 10 # GiB
nix_cache_image_path = "/vagrant/vagrant-nix-cache/nix-cache.img"
nix_cache_mount_path = "/nix-cache"

shell_variables = <<~"SHELL"
ROOT_DIR="/vagrant"
NIX_CACHE_SIZE="#{nix_cache_size}"
NIX_CACHE_IMAGE_PATH="#{nix_cache_image_path}"
NIX_CACHE_MOUNT_PATH="#{nix_cache_mount_path}"
SHELL

shell_common = <<~'SHELL'
set -euo pipefail
trap exit_message EXIT
SHELL

shell_helpers = <<~'SHELL'
exit_message () {
if [[ $? -ne 0 ]]; then
echo -e "The provisioning has been interrupted. Please try again:\n\n vagrant up --provision\n " >&2
fi
}

append_line_to_file () {
local line="$1"
local file="$2"

grep -qxF "$line" "$file" || echo "$line" >> "$file"
}
SHELL

Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-24.04"

config.vm.network "private_network", ip: ipaddr

ports_to_forward.each do |port|
config.vm.network "forwarded_port", guest: port, host_ip: "127.0.0.1", host: port
end

config.vm.provider "virtualbox" do |vb|
vb.cpus = cpus
vb.memory = memory * 1024
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--hostiocache", "on"]
end

config.vm.provision "shell", name: "system", upload_path: "/tmp/vagrant-shell-system", reset: true do |s|
s.inline = shell_variables + shell_common + shell_helpers + <<~'SHELL'
apt-get update

apt-get install -y \
bash-completion \
command-not-found \
git \
direnv \
nix \
docker-compose-v2

adduser vagrant nix-users
adduser vagrant docker

# Create a file to be mounted and used as a back store for the Nix cache
# The image is used to give Nix freedom to set ownership and permissions which is not possible with synced folders directly
[[ -d "$NIX_CACHE_MOUNT_PATH" ]] || mkdir -p "$NIX_CACHE_MOUNT_PATH"
[[ -d "$(dirname "$NIX_CACHE_IMAGE_PATH")" ]] || mkdir -p "$(dirname "$NIX_CACHE_IMAGE_PATH")"
[[ -f "$NIX_CACHE_IMAGE_PATH" ]] || { truncate -s "${NIX_CACHE_SIZE}G" "$NIX_CACHE_IMAGE_PATH"; mkfs.btrfs "$NIX_CACHE_IMAGE_PATH"; }
nix_cache_image_fstab="$NIX_CACHE_IMAGE_PATH $NIX_CACHE_MOUNT_PATH btrfs loop,compress=zstd 0 0"
append_line_to_file "$nix_cache_image_fstab" /etc/fstab
nix_daemon_needs_restart=false
mountpoint "$NIX_CACHE_MOUNT_PATH" || { mount "$NIX_CACHE_MOUNT_PATH" && nix_daemon_needs_restart=true; }

# Bind mounts for /nix/store and /nix/var/nix/db
mkdir -p "$NIX_CACHE_MOUNT_PATH/store" "$NIX_CACHE_MOUNT_PATH/var/nix/db"
mkdir -p /nix/store /nix/var/nix/db
nix_store_fstab="$NIX_CACHE_MOUNT_PATH/store /nix/store none bind 0 0"
nix_db_fstab="$NIX_CACHE_MOUNT_PATH/var/nix/db /nix/var/nix/db none bind 0 0"
append_line_to_file "$nix_store_fstab" /etc/fstab
append_line_to_file "$nix_db_fstab" /etc/fstab
mount -a

# Restart the Nix Daemon if needed
if "$nix_daemon_needs_restart"; then systemctl restart nix-daemon.service; fi
SHELL
end

config.vm.provision "shell", name: "user", upload_path: "/tmp/vagrant-shell-user", privileged: false do |s|
s.inline = shell_variables + shell_common + shell_helpers + <<~'SHELL'
# Configure nix
mkdir -p ~/.config/nix/
echo "extra-experimental-features = nix-command flakes" > ~/.config/nix/nix.conf

# Enable direnv
direnv_bash_hook='eval "$(direnv hook bash)"'
append_line_to_file "$direnv_bash_hook" ~/.bashrc

# Change to the cn-quickstart directory and allow direnv
[[ -d "$HOME/cn-quickstart" ]] || ln -s "$ROOT_DIR" "$HOME/cn-quickstart"
cd "$ROOT_DIR"
direnv allow

# Execute direnv export to set up the environment
eval "$(direnv export bash)"

# Install the Daml SDK
cd quickstart
make install-daml-sdk
append_line_to_file 'PATH="$HOME/.daml/bin:$PATH"' ~/.profile
SHELL
end
end