LinuxCommandLibrary

nix-flake-init

Initialize a new Nix flake

TLDR

Create a new flake from the default template, in the current directory

$ nix flake init
copy

Create a new flake with a specified template (see nix flake show for information about templates)
$ nix flake init [[-t|--template]] templates#[your_template]
copy

SYNOPSIS

nix flake init [TEMPLATE] [-t TEMPLATE] [--force] [common options]

PARAMETERS

-t, --template TEMPLATE
    Initialize from the specified flake template (URL or path exposing templates.<name>).

-f, --force
    Overwrite existing flake.nix if present.

--help
    Display help and exit.

--version
    Display version information.

DESCRIPTION

The nix flake init command scaffolds a new Nix flake in the current working directory by generating a basic flake.nix file. Nix flakes provide a standardized way to define Nix projects with explicit inputs (like nixpkgs) and outputs (such as packages, apps, devShells, or checks), enabling reproducible builds and easy sharing via URLs like GitHub repositories.

Without arguments, it creates a minimal flake.nix featuring a default package using pkgs.hello and a development shell. Specify a template to use pre-defined scaffolds from other flakes, such as simple Rust, Node.js, or Python projects. Templates must expose an output.template attribute.

This command is essential for starting new flake-based projects, promoting purity and lockfiles (flake.lock) for pinning dependencies. It requires Nix with flakes enabled (version 2.4+), typically via experimental-features = nix-command flakes in nix.conf.

After initialization, run nix develop for the shell or nix build for the package.

CAVEATS

Requires flakes enabled in Nix (≥2.4); modifies current directory; templates must provide valid template outputs; does not create Git repo or lockfile automatically.

BASIC USAGE

nix flake init # Minimal flake
nix flake init -t github:NixOS/templates#rust-simple # Rust template

GENERATED FLAKE.NIX STRUCTURE

Includes inputs: {nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";}
outputs: {packages.default = pkgs.hello; devShells.default = pkgs.mkShell {...};}

HISTORY

Introduced in Nix 2.4 (2021) as part of experimental flakes feature for better reproducibility. Stabilized as first-class in Nix 2.20+ (2024), with templates evolving via community flakes like github:NixOS/templates.

SEE ALSO

nix(1), nix flake(1), nix develop(1), nix build(1)

Copied to clipboard