LinuxCommandLibrary

nix-flake

Build and manage Nix projects using flakes

TLDR

Create a new flake (just the flake.nix file) from the default template, in the current directory

$ nix flake init
copy

Update all inputs (dependencies) of the flake in the current directory
$ nix flake update
copy

Update a specific input (dependency) of the flake in the current directory
$ nix flake update [input]
copy

Show all the outputs of a flake on github
$ nix flake show [github:owner/repo]
copy

Display help
$ nix flake --help
copy

SYNOPSIS

nix flake subcommand [flake-uri | path] [options...]

Examples:
  nix flake build
  nix flake show github:NixOS/nixpkgs
  nix flake update

PARAMETERS

build
    Builds the default output or a specific output of a flake. If no flake is specified, it defaults to the current directory.

show
    Displays the available outputs of a flake, including attributes like packages, applications, and development shells.

update
    Updates the locked inputs of a flake to their latest versions, modifying the flake.lock file.

init
    Initializes a new bare flake in the current directory, creating skeleton flake.nix and flake.lock files.

new
    Creates a new flake project from a specified template, providing a structured starting point.

info
    Shows detailed information about a flake, including its inputs, outputs, and current status.

DESCRIPTION

Nix flakes provide a modern, reproducible, and self-contained way to define Nix projects. The nix flake command serves as the primary interface for interacting with these flake-based projects. It enables users to build, develop, and share Nix configurations with well-defined inputs and outputs.
Through various subcommands like build, show, update, and init, users can manage flake dependencies, inspect outputs, and create new flake-based repositories. Flakes drastically improve reproducibility by locking input versions, ensuring that builds are consistent across different environments and times. This approach simplifies dependency management and fosters a more robust ecosystem for Nix users.

CAVEATS

Requires Nix version 2.4 or later; flakes became a stable feature in Nix 2.10, but some functionalities might benefit from newer versions.
Relies heavily on the flake.nix and flake.lock files for defining and locking project dependencies, which must be correctly maintained.
While designed for reproducibility, external factors like network availability for fetching inputs can still affect the initial setup.
Can have a learning curve for users new to the Nix ecosystem and its functional programming paradigm.

FLAKE STRUCTURE

Flakes are primarily defined by two files:
flake.nix: Describes the flake's inputs (dependencies) and outputs (like packages, applications, or development shells).
flake.lock: Automatically generated and updated, this file pins the exact versions and hashes of all transitive inputs, ensuring reproducible builds across different environments.

FLAKE URI

Flakes can be referenced using Universal Resource Identifiers (URIs), allowing them to be sourced from various locations. Common formats include:
path:./relative/path (for local directories)
git:https://example.com/repo.git?ref=main (for Git repositories)
github:owner/repo (a convenient shorthand for GitHub repositories)

HISTORY

Nix flakes were introduced as an experimental feature in Nix version 2.4, driven by the community's need for better reproducibility, simpler input management, and clearer project boundaries. They aimed to address shortcomings of the traditional Nix approach, such as implicit inputs and difficulties in sharing hermetic development environments.
After several iterations and extensive testing, flakes and the nix flake commands transitioned from experimental to stable with Nix version 2.10, marking a significant evolution in how Nix projects are structured and shared. Their adoption has streamlined Nix development workflows and fostered a more robust and interconnected ecosystem.

SEE ALSO

nix(1), nix-build(1), nix-shell(1), git(1)

Copied to clipboard