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 lock --update-input [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 [options]

PARAMETERS

archive
    Create an archive of the flake.

check
    Check the flake for errors.

clone
    Clone a flake from a URL.

init
    Initialize a new flake in the current directory.

lock
    Generate or update the flake's lock file.

metadata
    Show the metadata of a flake.

new
    Create a new flake from a template.

output
    Show the outputs of a flake.

show
    Show the flake's definition.

update
    Update the flake's dependencies.

DESCRIPTION

nix flake is a powerful feature of the Nix package manager that provides a declarative, reproducible, and composable way to manage system configurations, development environments, and software packages.
Flakes define inputs (dependencies) and outputs (packages, applications, development shells) in a `flake.nix` file. This file acts as a central definition for your project's build process and dependencies, ensuring consistency across different machines and over time.
Using flakes, you can precisely specify versions of dependencies, eliminating dependency conflicts. Flakes also enable isolated development environments, meaning changes made in one project won't affect other projects.
Flakes improve the reproducibility of builds by explicitly declaring dependencies and providing a mechanism for version pinning. They simplify the sharing and collaboration of projects by providing a standardized way to define and build projects.

CAVEATS

Flakes are still considered an experimental feature in Nix. Their syntax and behavior may change in future versions. Requires Nix version 2.0 or higher and `experimental-features = nix-command flakes` enabled in the nix.conf file.

EXAMPLE: BASIC FLAKE.NIX

A basic `flake.nix` file might define a single package:
{
inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; };
outputs = { self, nixpkgs }: {
defaultPackage.x86_64-linux = nixpkgs.pkgs.hello;
};
}

This example fetches `nixpkgs` and defines a package named `hello`.

ACTIVATING FLAKES

To use flakes, the following line must be enabled in `/etc/nix/nix.conf`:
`experimental-features = nix-command flakes`

Then restart the nix daemon: `sudo systemctl restart nix-daemon`

HISTORY

Flakes were introduced as an experimental feature in Nix to address challenges with reproducibility and dependency management in traditional Nix workflows. They provide a more declarative and structured approach to defining Nix projects. The initial design and implementation were aimed at simplifying project setup, improving consistency, and enabling better collaboration. Since introduction, flakes have been improved over time by Nix community.

SEE ALSO

nix-build(1), nix-shell(1), nix.conf(5)

Copied to clipboard