What is flake-utils?#
Flake outputs often require the same attribute sets to be defined repeatedly for different architectures, such as x86_64-linux and aarch64-linux, making the code very verbose.
flake-utils provides a simple helper function to eliminate this repetition.
1. The traditional approach
You need to iterate over all architectures manually.
{
outputs = { self, nixpkgs }: {
packages.x86_64-linux.default = ...;
packages.aarch64-linux.default = ...;
};
}2. A concise approach with flake-utils
{
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
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 = [ pkgs.git ]; };
}
);
}eachDefaultSystem: generates outputs for the library’s default list of systems. UseeachSystemto specify your own set of platforms.
Flake templates#
Flakes let you create templates that other people can use to initialize projects quickly with a single command.
1. View the official templates
nix flake show templates2. Initialize a project from a template
# 初始化一个简单的 python 环境
nix flake init -t templates#simple3. Define templates in your own Flake
You can turn your configuration into a template to share with others:
{
outputs = { self }: {
templates.my-rust-app = {
path = ./templates/rust;
description = "一个基础的 Rust 项目模板";
};
};
}Why does this matter?#
On NixOS, defining your own Flake templates lets you set up development environments for different languages quickly, without having to configure PATH manually.
References & further reading
Adapted from technical notes. Examples use generic names.
Continue along a reading path
Flakes: project and dependency management
- 01Flake structure: inputs and outputs
- 02flake.lock and reproducibility
- 03Flake Utils and project templates
- 04External Flake references and collaboration
- 05Migrating NixOS configuration to Flakes
- 06A tutorial on Flake projects and dependency management
- 07How Flake updates work
- 08Rolling back and manually editing flake.lock