Migrating to a static blog

This article is also available in French.
Cet article est également disponible en français.

TL;DR My blog is now hosted at ejpcmac.net/blog.

Introduction

I have started this blog a bit less than two years ago on Medium. It was a great platform to start with: there is no setup, nothing to configure, so you can start to blog in minutes.

I just wanted to share some tips about programming in Elixir. I had seen other people doing that, and the point I wanted to talk about had not been addressed by anyone else. Since then, I have published a few articles about open source libraries I’ve written and tips for tools I am using. Now, it is time to switch to a self-hosted statically-generated blog.

Using Nix in Elixir projects

This article was originally published on Medium.

Nix is a purely functional package manager that makes possible to create reproducible setups to share between developers. I have written a rather long article about it recently and I want to continue here with some specific instructions for Elixir projects in a much more concise way. I assume you know a bit about Nix in general—if it is not the case, you can read my previous article or search the web for information.

About using Nix in my development workflow

This article was originally published on Medium.

TL;DR If you don’t want to read how I’ve got to use Nix and general information about it but only focus on its use to setup a development environment, please jump to Using Nix. Even from that part, the reading can take some time as I wanted to share what I’ve learned and found useful accross two months of intensive usage. I had some initial questions I have answered to after reading a great portion of the documentation or asking to people. I wanted to share them with you so you can get into using Nix quickly, still understanding what you are doing.


Two years ago, whenever I needed to use some language or tool which wasn’t available on my machine, I would have used the system’s package manager to install it. It worked on my computer, but when I needed to reproduce the setup elsewhere it was another story. I was still a student then.

I remember a group project in which we were using Node.js. I had a recent version installed on my Mac via Homebrew, while at the university and on other student’s computer based on Ubuntu Linux, it was installed from the default repos. Then, my code using default parameters in functions would run on my machine but not on other’s, since their Node.js version was greatly outdated.

Using this approach can quickly become a nightmare, especially when you try to write a documentation on how to set up the development environment. You end up writing things like: “If you are on macOS please do this, on Debian do that, and Fedora run this other command.” You can verify it works now, but there is no guarantee it will still work in a few months.

Typed Elixir structs without boilerplate

This article was originally published on Medium.

TD;DR A package is available on hex.pm and GitHub.

In Elixir, you can define a struct by calling defstruct in a module. This macro takes a list of atoms which becomes the keys of the struct, or a keyword list associating these keys to default values:

defstruct name: "John Smith",
          age: nil,
          phone: nil

All the keys are optional by default. To enforce some of them, you must add them to @enforce_keys :

@enforce_keys [:age]

The official documentation recommends to define a type for the struct, named t() by convention:

@type t() :: %__MODULE__{
        name: String.t(),
        age: integer(),
        phone: String.t() | nil
      }

Persistent logins in Elixir with Expected

This article was originally published on Medium.

TD;DR I’ve written an Elixir package to enable persistent logins through an authentication cookie, following Barry Jaspan’s Improved Persistent Login Cookie Best Practice. It is available on hex.pm and GitHub.

After writing my server-side session store using Mnesia, I found a new problem to solve: how should I manage persistent logins? One solution could be setting the session to exist forever, but this is a bad idea. If someone gets my session cookie, he can access to my account and that’s it. I have no way to discard the stolen session. I should have one. More: if someone steals my cookies, I should be informed of that.

I have looked for an authentication solution for browser sessions in Elixir. If there are many JWT-based ones for REST-like APIs, the lone I am aware of for browser sessions is Coherence. It seems really great if you want a framework that generates all the user management for you, but this is not what I was looking for. I feel this approach too monolithic. I want to build and use little tools that does one simple thing and compose them, like in the UNIX philosophy. So I would write mine for persistent logins.

Storing Plug sessions with Mnesia

This article was originally published on Medium.

TD;DR A package is available on hex.pm and GitHub.

I am currently writing my second Phoenix application, Kakte. This is the first one with full-featured user management, so I came to ask myself how sessions are actually handled in the Elixir world. A session is a way to associate a state with an HTTP connection, so it is a pretty important feature which enables authentication and authorisation.

Sharing fixtures between test modules in Elixir

This article was originally published on Medium.

Introduction

TL;DR If you came here only for the technical article, please jump to the next section. If you are curious about who am I and why I start publishing here, you can continue reading this introduction.

This is my first article on medium, so I thought it would be a good idea to start it with an introduction. I will present myself, who am I, what I do, and what you can expect to appear on this blog.

My name is Jean-Philippe Cugnet. I’m a 23 years old software engineer and photographer, living in Caen, France. I just got a Master degree in IT Security at the University of Caen and I’m currently working as an embedded software developer in a design house. I also love film photography a lot, making photos from little things people may not care about. The photography story goes on my website. The technical one continues here.