From Vagrant to NixOps

I have been following the development of NixOps for some months. NixOps is a cloud deployment tool using nix, the functional package manager for unix systems. Nix makes it very intuitive to define absolute package dependencies. No more thinking and guessing about required runtime dependencies.
NixOps supports deploying to different platforms. Bare-metal, cloud, and even virtual environments like virtualbox work out of the box. I have worked in many projects using vagrant. Out of curiosity I migrated an existing vagrant project using wasted (Web Application STack for Extreme Development) to nix and NixOps.
This post is a walkthrough to configure a symfony2 project with nginx, mysql, and php-fpm from scratch.

Demo

In the demo project you can see the final setup using the code from this blog post.

Demo link: https://github.com/hschaeidt/cbase

Preparations

Before we get started we need following tools:

Also ensure the following settings in virtualbox:

  • open general settings
  • navigate to „Network“ section
  • in „Network“ switch to „Host-only Networks“ tab
  • if no network with the name „vboxnet0“ exists, create one

We are ready to go now.

Overview

The first setup we want to achieve is development machine for developers. The next goal will be to have the exact same configuration for production deployments using the exact same tool, NixOps, but with another provider – maybe ec2, maybe hetzner bare-metal, maybe azure, … – that depends on your use case. But this will be too much for this post, so let’s first focus on our development machine using virtualbox.

What we have:

  • a local symfony project on the host machine (the computer you are hacking on right now)
  • NixOps installed

Well, that’s all we actually need.

What our final setup will look like:

  • the host machine’s symfony2 project is mounted in the virtual machine provisioned by NixOps
  • all the project’s dependencies are available within the virtualbox development machine
    • php
    • composer
    • mysql
    • nginx
  • it is not required to install those tools on the host machine

Hands on

Now let’s start by creating a new folder for our new configuration. Let’s say ./server.
We start by declaring a virtualbox configuration file in nix. I will paste the entire configuration in here as it is pretty easy to follow up. If you are not yet familiar enough with the nix language, try out a tour of nix: https://nixcloud.io/tour/?id=1 – it’s concise, straightforward, and afterwards there are no more surprises in the config syntax.

Let’s say we have a file called ./server/cbase-vbox.nix

From here on I will call my virtual development machine the „target machine“. Also my local laptop will be called „host machine“ from here on.

I split the configuration in 7 sections I will describe below:

section 1

Defines the server name exposed to NixOps. This name will be used to ssh into the target machine later on.

section 2

Defines the deployment target – here virtualbox – below some virtualbox specific settings like the memory available to the target machine.

section 3

Virtualbox guest additions are required on the target machine in order to properly mount the folder in section 4 and section 5.

section 4

Declaring the virtualbox shared folders. The key of the set is cbase, note that this must equal the device name used in section 5.
The host machine path points to the checked out git project, change it to your needs.
Read-only is set to false because we want to do some operations like composer install within the target machine etc.

section 5

Defining the filesystem mount from the previously declared sharedFolder from the host machine.
/var/www/cbase will be mounted on the target machine using the virtualbox shared folder containing the git symfony project.
Note: The uid and gid will be the one of the www-data user that will be created in the next step. This makes sure symfony can write its caches.

section 6

Enabling the possibility to roll back to a previous configuration using nixops rollback command. This can also be omitted for the development machine.

section 7

Basically this is the same as writing cbase = cbase;
Apply the declared variable cbase to the nix config.

Now we have defined our target machine we want to use for development. Let’s keep going by creating another file configuring this machine.

Setting up php, nginx and mysql

Let’s get started by creating a new file called ./server/cbase.nix. This file is way bigger, but I think it’s important to paste it entirely first and going through it in a second step.

The configuration is again split into 7 sections we will go through now.

section 1

The cbase variable here again corresponds to the server name exposed to nixops. It has to correspond to the one defined in the previous configuration file (see section 1 from the previous configuration).
Additionally, we define some other variables we will use for convenience within the let in block.

section 2

We open the ports 80 and 443 on our application server. This is necessary for the nginx to be available to the host machine.

section 3

Declaring the packages available for execution from all target machines users (including www-data user). We actually only need the php and composer packages for development. Adapt to your needs on the target machine.

section 4

Enables the php-fpm service on the target machine. The user should be the same as for nginx. For detailed pool configuration options refer to http://php.net/manual/en/install.fpm.configuration.php

section 5

Enables and configures the nginx service. Please note the variables used within the configuration file. It is mostly the recommended minimal nginx configuration file as described in https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/

section 6

Enables the mysql service. Note the inital database configuration. The minimal required configuration in order to get the databases available after deployment is:

section 7

Creates the www-data user on the target machine. We enable the default shell here, because we will work with this user within our target machine. Note that the gid and uid should correspond to the ones defined in section 5 of the previous configuration file.

That covers all we need to do to describe our server setup.

Deploying the development machine

nixops deployment

Okay here we go. We just finished our minimal symfony configuration file. It’s time to test this setup now. Just a few steps to go.

First we have to create the nixops deployment configuration.

Now we can test if the machine was created succesfully

And finally deploying it to virtualbox

Now we have a virtualbox able to serve our symfony application.

symfony setup

We need to execute composer install from within the target machine to download all dependencies.

Testing the setup

Now all we have to do is look up the IP-address from the previously executed nixops info --deployment cbase command, which in my case is 192.168.56.101.
And navigating to http://192.168.56.101/app_dev.php we can see our symfony application running.

Happy hacking!

Für neue Blogupdates anmelden:


22 Gedanken zu “From Vagrant to NixOps

  1. Pingback: From Vagrant to Nixops

  2. Pingback: From Vagrant to NixOps | StratCom

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.