Flakes

Links to this page
  • nixpkgs

    In flakes, nixpkgs is the most commonly used input.

  • nix repl
    To load a flake, use :lf <flake-url>
  • flake-parts

    flake-parts brings the NixOS module system to flakes, thus providing a cleaner and simpler way to write otherwise complex flakes.

  • Rust FFI in Haskell

    This tutorial will guide you through using Nix to simplify the workflow of incorporating Rust library as a dependency in your Haskell project via FFI. If you are new to Nix and Flakes, we recommend starting with the Nix Tutorial Series.

  • Replacing docker-compose with Nix for development
    By default, the data of each service is stored under ./data/<service-name>, where ./ refers to the path where the process-compose app, exported by the project flake is run (usually in the project root).
  • Rapid Introduction to Nix

    A Nix flake is defined in the flake.nix file, which denotes an attrset containing two keys inputs and outputs. Outputs can reference inputs. Thus, changing an input can change the outputs. The following is a simple example of a flake:

    The goal of this mini-tutorial is to introduce you to Nix the language, including flakes, as quickly as possible while also preparing the motivated learner to dive deeper into the whole Nix ecosystem. At the end of this introduction, you will be able to create a flake.nix that builds a package and provides a developer environment shell.

    The goal of this mini-tutorial is to introduce you to Nix the language, including flakes, as quickly as possible while also preparing the motivated learner to dive deeper into the whole Nix ecosystem. At the end of this introduction, you will be able to create a flake.nix that builds a package and provides a developer environment shell.

  • Nixifying a Haskell project using nixpkgs
    A basic understanding of the Nix and Flakes is assumed. See Rapid Introduction to Nix

    Welcome to the Nixify Haskell projects series, where we start our journey by integrating a Haskell application, particularly one using a PostgreSQL database, into a single-command deployable package. By the end of this article, you’ll have a flake.nix file that’s set to build the project, establish the development environment, and execute the Haskell application along with all its dependent services like PostgreSQL and PostgREST. We’ll be using todo-app as a running case study throughout the series, demonstrating the process of building a Haskell project and effectively managing runtime dependencies, such as databases and other services, thereby illustrating the streamlined and powerful capabilities Nix introduces to Haskell development.

  • Nix does not recognize a new file I added

    This usually means you have not staged this new file/ directory to the Git index. When using Flakes, Nix will not see untracked files/ directories by default. To resolve this, just git add -N the untracked file/ directory.

  • Module System

    This module system is not natively supported in Flakes. However, flakes can define and use modules using flake-parts.

  • Introduction to module system

    Consider the following Nix code, defined in a flake:

    We shall begin by understanding the low-levels: how to use evalModules from nixpkgs to define and use our own modules from scratch, using the aforementioned lsd use-case. The next tutorial in this series will go one high-level up and talk about how to work with modules across flakes, using flake-parts.

    You have just read a quick introduction to the module system, in particular how to define, use and share them in Flakes. To learn more about the module system, we recommend this video from Tweag as well the article “Module system deep dive” from nix.dev. Look out for the next tutorial in this series, where we will talk about flake-parts.

  • Install NixOS with Flake configuration on Git

    As a final step, let’s permanently enable Flakes on our system, which is particular useful if you do a lot of software development. This time, instead of editing configuration.nix again, let’s do it in a separate module (for no particular reasons other than pedagogic purposes). Remember the modules argument to nixosSystem function in our flake.nix? It is a list of modules, so we can add a second module there:

    This tutorial will walk you through the steps necessary to install NixOS, enable flakes while tracking the resulting system configuration in a Git repository.

    You have successfully installed NixOS. The entire system configuration is also stored in a Git repo, and can be reproduced at will during either a reinstallation or a new machine purchase. You can make changes to your configuration, commit them to Git, and push it to GitHub. Additionally we enabled Flakes permanently, which means you can now use all the modern nix commands, such as running a package directly from nixpkgs (same version pinned in flake.lock file):

  • Install Nix
    As it does not automatically enable Flakes, you will have to manually enable it.
  • Flake registry

    Global registry of Nix flakes

  • Flake URL

    Flakes are referred using an URL-like syntax that is documented here.

  • First steps with Nix
    See Rapid Introduction to Nix where we will go over writing simple Nix expressions and flakes.

    [!info] nix run nix run command will run the specified package from the flake. Here nixpkgs is the flake, followed by the letter #, which is followed by the package (Derivation) name cowsay that is outputted by that flake. See Flake URL for details on the syntax.

    nixpkgs is not the only way to get software packaged by Nix. As you have seen immediately above, you can install programs from any flake by specifying its flake URL to the nix ? commands. For example, Emanote (which is used to build this very website) can be executed or installed directly off its flake on GitHub:

  • Convert configuration.nix to be a flake

    Now, /etc/nixos is technically a flake. We can “inspect” this flake using the nix flake show command:

    A problem with the default NixOS configuration.nix generated by the official installer is that it is not “pure” and thus not reproducible (see here), as it still uses a mutable Nix channel (which is generally discouraged). For this reason (among others), it is recommended to immediately switch to using Flakes for our NixOS configuration. Doing this is pretty simple. Just add a flake.nix file in /etc/nixos: