Skip to content

Using Nix Flakes

Key takeaways

  • A project is a flake if it has a flake.nix and flake.lock file.
  • Flakes are a system by which developers can share projects and packages.
  • Flakes can be used to build packages, run programs, and declare development environments.
  • With Nix flakes, you can install, run, or include a package that is not part of the standard package set in nixpkgs. This requires the target package to itself be a flake.
  • Flakes can have up to 4 sections: description, inputs, outputs, and nixConfig. Reference the flake schema when developing your own flakes.
  • To work correctly, a flake has to be tracked by git.

Getting Started

Create a file in the root of your project called flake.nix. Since writing a flake by hand is labourious and error prone, it's best to use an existing template (See Essential Flake Templates). For example,

Managing a Flake

nix flake update

  • Updates your flake's lock file. Important if you are working on a flake that is a dependency of another.

Development Environments with Direnv

By combining flakes with direnv, you can enter isolated development environments by simply cding into a directory.

Setup

  1. Ensure Nix and direnv are installed
  2. Add a flake.nix file with a devShells output
  3. Add use flake . to your .envrc file, e.g., echo "use flake ." > .envrc
  4. Issue direnv allow

Essential Flake Templates

To initialize a flake from a template use the command

nix flake init -t <source>#<template>
nix flake init -t <source>#<template>

Below I list a few essential projects, but bany more can be found online, just search, e.g., "nix flake template for TypeScript".

C++, JavaScript, Python, Rust, Go

Alternatively, copy and paste the snippet below into flake.nix and adjust to your needs. flake.nix

nix
# Pulled from https://all-dressed-programming.com/posts/nix-flake-composition/
{
  description = "Good basic flake template";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
     flake-utils.lib.eachDefaultSystem (system:
       let
         pkgs = nixpkgs.legacyPackages.${system};
       in
       {
         packages = {
          default = pkgs.hello;
         };
         devShells.default = pkgs.mkShell {
            buildInputs = with pkgs; [deno];
         };
       }
     );
}
# Pulled from https://all-dressed-programming.com/posts/nix-flake-composition/
{
  description = "Good basic flake template";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
     flake-utils.lib.eachDefaultSystem (system:
       let
         pkgs = nixpkgs.legacyPackages.${system};
       in
       {
         packages = {
          default = pkgs.hello;
         };
         devShells.default = pkgs.mkShell {
            buildInputs = with pkgs; [deno];
         };
       }
     );
}
ESSENTIAL READING
ADDITIONAL RESOURCES
  • Standard a neat and efficient DevOps framework based on Nix.
  • devenv composable developer environments with Nix, like direnv with more features.