What?
This blog uses Jekyll static site generator, and is being hosted at Netlify.
I recently switched my laptop and faced the problem of having to re-setup my machine to be able to develope this website.
I wanted to simplify this setup in the future by having a reproducible build without having the need to dirtify my environment (eg. installing a different version of Ruby, Bundler and so on).
I decided to use Nix to acheive this.
Introducing Nix
When I first heard of Nix it was in the context of NixOS.
Nix is a powerful package manager for Linux and other Unix systems that makes package management reliable and reproducible. Nix builds are:
- Reproducible: Nix builds packages in isolation from each other. This ensures that they are reproducible and don't have undeclared dependencies, so if a package works on one machine, it will also work on another.
- Declarative: Nix makes it trivial to share development and build environments for your projects, regardless of what programming languages and tools you’re using.
- Reliable: Nix’s ensures that installing or upgrading one package cannot break other packages. It allows you to roll back to previous versions, and ensures that no package is in an inconsistent state during an upgrade.
How to install Nix
This page list several methods to install Nix.
How to setup Nix to build this website
Some terminologies before starting:
- nixpkgs: The Nix Packages collection is a set of over 60 000 packages for the Nix package manager.
- nix-shell: A command that lets you open a shell in a new environment. In Nix; an environment is a collection of derevations (aka packages) that are put into your PATH. By default it loads
default.nix
(can be passed as an argument). This is our entry point. - bundix: Take as an argument
Gemfile
,Gemfile.lock
and generate a Nix expression that will install all listed dependencies in a file named by default:gemset.nix
- Nix expression: Learn X in Y minutes, Where X=nix
Process before Nix
- Install packages:
bundle install
- Start Jekyll server:
bundle exec jekyll serve --watch --incremental
Process after Nix
- Install packages:
bash setup.sh
: It'll usebundix
to generate a newgemset.nix
file. - Start Jekyll server:
nix-shell
: It'll executedefault.nix
bellow which will set build all packages and start the server.
setup.sh
#!/usr/bin/env bash
nix-shell -p bundler -p bundix --run 'bundler lock; bundler package --no-install --path vendor; bundix; rm -rf vendor'
default.nix
with import <nixpkgs> { };
let
jekyll_env = bundlerEnv rec {
name = "jekyll_env";
inherit ruby;
gemfile = ./Gemfile;
lockfile = ./Gemfile.lock;
gemset = ./gemset.nix;
};
in
stdenv.mkDerivation rec {
name = "mhasbini";
buildInputs = [ jekyll_env bundler ruby ];
shellHook = ''
export LC_ALL="C.UTF-8"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
exec ${jekyll_env}/bin/jekyll serve --watch --incremental
'';
}