![]() And so I put them in the user's home directory instead. ![]() Initially I wanted to have the database-related directories within the Rails app project directory, but it turned out I couldn't do this because VirtualBox doesn't allow hard links within shared directories (at least not on MacOS). However, the documentation says: "Note that ~/.bashrc and (depending on your Bash installation) /etc/bashrc are still sourced, so any variables set there will affect the interactive shell." The latter was the main source of my previous isolation woes, so perhaps it wouldn't have helped that much after all. ![]() Very belatedly, I came across the -pure option for nix-shell which might've been a simpler way to isolate my development environment. The steps I've described above are documented in the README and the corresponding code is in a couple of inline provisioning scripts within the Vagrantfile. If you want to follow along at home, I've published the source code in a GitHub repository. Nix-shell> open The blogging functionality was also accessible at the relevant endpoint and seemed to persist new posts successfully. I then created and migrated the development and test databases for the Rails app in the usual way: nix-shell> rails db:createĪnd ran the Rails tests as follows: nix-shell> rails testĪnd finally, the moment of truth (!), I ran the Rails server and opened a browser at the home page □: nix-shell> rails server -binding 0.0.0.0 -daemon To make things slightly more interesting, I decided to create the canonical simple Rails "blog" app using the rails generate scaffold command: nix-shell> rails generate scaffold post title:string content:text Rails development environmentīy this stage, the nix-shell Rails development environment was pretty much ready to go. I set the database server up with minimal security to make it easy to access from psql and so the generated database.yml would just work out of the box. Pg_ctl start -l $PGLOG -o "-unix_socket_directories='$PGHOST'" Initdb -auth=trust -no-locale -encoding=UTF8 To this end I added a shellHook to shell.nix to idempotently configure the server and start it when entering the nix-shell: shellHook = '' I wanted a PostgreSQL database server to be available, but only from within the nix-shell, i.e. the libxml2 OS package was automatically installed just because nokogiri was in the bundled gems! PostgreSQL server Running the "inner" nix-shell for the first time resulted in the bundled gems and their OS dependencies being installed along with runtime dependencies mentioned above. I used the Nix multi-user installation instructions to write an inline provisioning script in the Vagrantfile and I even worked out how to make the script idempotent: nix -versionĮcho 'nix is already installed (skipping installation)' Installing Nixįirst I had to install Nix on the Ubuntu VM. I found this a really productive way to tackle the problem. By doing it this way, I could easily snapshot the VM and restore the snapshot or even destroy the VM and build it again to get back to a clean slate. So this time I decided to setup a completely seperate VM running Ubuntu Xenial (with minimal OS packages) using Vagrant to continue with my Nix experiments.įurthermore I decided to try to write a provisioning script for the Vagrant configuration to create a new Rails app from scratch and to complete all the steps necessary for getting both the Rails tests and the Rails server running. Previously, in order to ensure full isolation, I ended up editing my "dot" files and even modifying environment variables in the current shell, but this was fiddly and error prone. However, in my second attempt where I set up a simple Rails development environment, I wasn't so careful and I didn't do the same checks on the run-time dependencies. In particular, I noticed that unless I specifically added nodejs to the list of buildInputs, middleman ended up using the Node version in the underlying MacOS environment. In my first attempt at setting up a simple Ruby development environment, I was quite careful to make sure the Nix shell environment wasn't accidentally relying on anything available from the underlying environment. Projects Generating and running a Rails app with PostgreSQL using Nix on Ubuntu Isolating a Nix environment by running on an Ubuntu VM provisioned by Vagrant
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |