7.0 KiB
Introduction to Flakes
The flakes experimental feature is a major development for Nix, it introduces a policy for managing dependencies between Nix expressions, it improves reproducibility, composability and usability in the Nix ecosystem. Although it's still an experimental feature, flakes have been widely used by the Nix community.1
Flakes is one of the most significant changes the nix project has ever seen.2
In simple terms, if you've worked with some JavaScript/Go/Rust/Python, you should be familiar with files like package.json
/go.mod
/Cargo.toml
/pyproject.toml
. In these programming languages, these files are used to describe the dependencies between software packages and how to build projects.
Similarly, the package managers in these programming languages also use files like package-lock.json
/go.sum
/Cargo.lock
/poetry.lock
to lock the versions of dependencies, ensuring the reproducibility of projects.
Flakes borrow ideas from these package managers to enhance the reproducibility, composability, and usability of the Nix ecosystem.
Flakes introduce flake.nix
, similar to package.json
, to describe the dependencies between Nix packages and how to build projects.
Additionally, it provides flake.lock
, akin to package-lock.json
, to lock the versions of dependencies, ensuring project reproducibility.
A Word of Caution about Flakes
The benefits of Flakes are evident, and the entire NixOS community has embraced it wholeheartedly. Currently, more than half of the users utilize Flakes3, providing assurance that Flakes will not be deprecated.
⚠️ However, it's important to note that Flakes is still an experimental feature. Some issues persist, and there is a possibility of introducing breaking changes during the stabilization process. The extent of these breaking changes remains uncertain.
Overall, I strongly recommend everyone to use Flakes, especially since this book revolves around NixOS and Flakes. However, it's crucial to be prepared for potential problems that may arise due to forthcoming breaking changes.
Nix Flakes and Classic Nix
Since the nix-command
and flakes
features are still experimental, the official documentation lacks detailed coverage, and the community's documentation on them is also scattered. From the perspective of reproducibility, ease of management, and maintenance, the classic Nix package structure and CLI are no longer recommended. Therefore, I will not delve into the usage of classic Nix. Beginners are advised to start with nix-command
and flakes
while disregarding any content related to classic Nix.
The following are classic Nix commands and associated concepts that are no longer necessary after enabling nix-command
and flakes
. When searching for information, you can safely ignore them:
nix-channel
:nix-channel
manages software package versions through stable/unstable/test channels, similar to other package management tools such as apt/yum/pacman.- In Flakes, the functionality of
nix-channel
is entirely replaced by theinputs
section inflake.nix
.
- In Flakes, the functionality of
nix-env
:nix-env
is a core command-line tool for classic Nix used to manage software packages in the user environment.- It installs packages from the data sources added by
nix-channel
, causing the installed package's version to be influenced by the channel. Packages installed withnix-env
are not automatically recorded in Nix's declarative configuration and are completely independent of its control, making them challenging to reproduce on other machines. Therefore, it is not recommended to use this tool. - The corresponding command in Flakes is
nix profile
. Personally, I don't recommend it either.
- It installs packages from the data sources added by
nix-shell
:nix-shell
creates a temporary shell environment, which is useful for development and testing.- In Flakes, this tool is divided into three sub-commands:
nix develop
,nix shell
, andnix run
. We will discuss these three commands in detail in the "Development" chapter.
- In Flakes, this tool is divided into three sub-commands:
nix-build
:nix-build
builds Nix packages and places the build results in/nix/store
, but it does not record them in Nix's declarative configuration.- In Flakes,
nix-build
is replaced bynix build
.
- In Flakes,
nix-collect-garbage
: Garbage collection command used to clean up unused Store Objects in/nix/store
.- In Nix Flakes, the corresponding command is
nix store gc --debug
.
- In Nix Flakes, the corresponding command is
- And other less commonly used commands are not listed here.
- You can refer to the detailed command comparison list in Try to explain nix commands.
NOTE:
nix-env -qa
may still be useful in some cases, as it returns all packages installed in the system.
Flakes Tutorials
I won't go into too much detail here, please refer to the following documents:
- Nix Flakes, Part 1: An introduction and tutorial (Eelco Dolstra, 2020)
- Nix Flakes, Part 2: Evaluation caching (Eelco Dolstra, 2020)
- Nix Flakes, Part 3: Managing NixOS systems (Eelco Dolstra, 2020)
- Practical Nix Flakes
When Will Flakes Be Stabilized?
I delved into some details regarding Flakes:
- [RFC 0136] A Plan to Stabilize Flakes and the New CLI Incrementally: A plan to incrementally stabilize Flakes and the new CLI, merged.
- CLI stabilization effort: An issue tracking the progress of the New CLI stabilization effort.
- Why Are Flakes Still Experimental? - NixOS Discourse: A post discussing why Flakes are still considered experimental.
- Flakes Are Such an Obviously Good Thing - Graham Christensen: An article emphasizing the advantages of Flakes while suggesting areas for improvement in its design and development process.
- teaching Nix 3 CLI and Flakes #281 - nix.dev: An issue about "Teaching Nix 3 CLI and Flakes" in nix.dev, and the conclusion is that we should not promote unstable features in nix.dev.
- Draft: 1-year Roadmap - NixOS Foundation: A roadmap provided by the NixOS Foundation, which includes plans regarding the stabilization of Flakes.
After reviewing these resources, it seems likely that Flakes will be stabilized within one or two years, possibly accompanied by some breaking changes.