Skip to content

Example on how to nixify a project [maintainer=@Rizary]

Notifications You must be signed in to change notification settings

nix-community/todomvc-nix

Repository files navigation

TodoMVC-Nix

One-stop reference to build TodoMVC application inside the Nix world

built with nix

Nix Flake

Are you new to Nix? Or are you use Nix, but still don't know how Nix is useful for your company? Maybe you have use Nix/NixOS in your current project, and you want to know how other project (using other programming language) fits into Nix world? TodoMVC-Nix can help you to understand how your project is structured using Nix to ease your project development setting.

This project's goal is to provide Nix user on every level (beginner, intermediate, and advance) a reference for building project using Nix programming language new feature called Flakes. This project contains several project written in different programming language and also how to nixified it.

Related article: todomvc-nix: One-Stop Solution for Developing Projects Using Nix

Structure

Technology Stack

[Back to the Table of Contents] ↑

TodoMVC-Nix has several examples on how fullstack project is structured using Nix Flakes. This stack can be used as an example for early stage startup or some new project which might want to consider to use Nix in their development journey. The stack consists of the following:

  • Backend
    • Rust
    • Haskell
  • Frontend
    • Rust
    • Haskell
  • Database
    • PostgreSQL

Another language and stack will be added accordingly.

Get Started

[Back to the Table of Contents] ↑

Before exploring the project, there are several things that need to be prepared first as explained below.

Prerequisites

[Back to the Table of Contents] ↑

Make sure that you have the following package installed in your machine:

Setup

[Back to the Table of Contents] ↑

Non-nix-flake user

This repository can be used using plain nix-build or nix-shell. This is possible with the help of flake-compat as mentioned in default.nix. To build the package, just run:

$ nix-build -A defaultNix.legacyPackages.x86_64-linux.nix.haskellBackend

and to enter into Nix shell, run:

$ nix-shell

Nix-flake user

If you want to use this repo with flakes feature, please enable it using the following method:

Linux and Windows Subsystem Linux 2 (WSL2)

Install Nix as instructed above. Next, install nixUnstable by running the following code:

nix-env -iA nixpkgs.nixFlakes

Lastly, open your ~/.config/nix/nix.conf or /etc/nix/nix.conf file and add:

experimental-features = nix-command flakes

NixOS

Add the following code into your configuration.nix:

{ pkgs, ... }: {
  nix = {
    package = pkgs.nixFlakes;
      extraOptions = ''
        experimental-features = nix-command flakes
      '';
  };
}

Usage

[Back to the Table of Contents] ↑

Clone this repository, and enter into it:

$ git clone https://github.com/nix-community/todomvc-nix.git
$ cd todomvc-nix

run nix develop and wait until the downloading/caching the dependencies are finished.

Once it is finished, you can refer to the documentation below:

For Developers

[Back to the Table of Contents] ↑

Build

[Back to the Table of Contents] ↑

Building package in Nix Flakes is simply by running the following code:

nix build .#todomvc.nix.haskellBackend

The above example will build todo-haskell package which is a backend built in Haskell. Here is the breakdown of the command:

  • nix build - is a nix v2 command to build package
  • .#todomvc.nix.haskellBackend
    • . - it means the directory contain flake.nix,
    • # - it is to separate between directory and package,
    • todomvc.nix.haskellBackend - is the package describe in the overlay that we want to build.

The result of the build will be available in the result folder.

Run

[Back to the Table of Contents] ↑

By running nix develop in the project, some command as written in shell.nix will be available in the PATH.

There is also custom script that can be created to simplify the command as specified in the commands attributes in shell.nix.

For example, command pgstart can be run to start the postgresql server. pgstart is custom script based on the following code:

{
  name = "pgstart";
  help = "start psql service";
  command = "${todomvc.nix.database.pgutil.start_pg} || echo '''PG start failed''' ";
}

Please refer to shell.nix for further command and package.

Acknowledgements

[Back to the Table of Contents] ↑

Most of the code that we put in this repo has previously been made by others. Here we credit the repository that we take the code from.