4.3 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
Warning about Flakes
The benefits of Flakes are obvious, and the entire NixOS community likes it very much. Currently, more than half of the users are using Flakes3, so we're pretty sure that Flakes will never be deprecated.
⚠️ But Flakes is still an experimental feature, there are still many problems with it, it is likely to introduce some breaking changes in the process of stablizing it, and it’s uncertain how greatly the breaking changes will be.
So overall, I still recommend everyone to use Flakes, this book is also written around NixOS and Flakes after all, but please be prepared for the problems that may be caused by the upcomming breaking changes.
Nix Flakes and the classic Nix
As nix-command
& flakes
are still experimental features, the official documentation does not cover them in detail, and the community's documentation about them is also very scattered.
However, from the perspective of reproducibility and ease of management and maintenance, the classic Nix package structure and cli are no longer recommended for use.
So I will not introduce the usage of the classic Nix. It's recommended that beginners just start with nix-command
& flakes
and ignore all the contents about the classic Nix.
Here are the classic Nix commands and related concepts that are no longer needed after you enabling nix-command
and flakes
, when searching for information, you can safely ignore them:
nix-channel
:nix-channel
is similar to other package management tools such as apt/yum/pacman, managing software package versions through stable/unstable/test channels.- In Flakes, the functionality of
nix-channel
is completely replaced byinputs
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 bynix-channel
, so the installed package's version are influenced by the channel. Packages installed withnix-env
are not automatically recorded in Nix's declarative configuration and are entirely outside of its control, making them difficult to reproduce on other machines. Therefore, it is not recommended to use this tool.- The corresponding command in Flakes is
nix profile
, it's not recommended to use it either.
- The corresponding command in Flakes is
nix-shell
:nix-shell
is used to create a temporary shell environment, which is useful for development and testing.- In Flakes, it is replaced by
nix develop
andnix shell
.
- In Flakes, it is replaced by
nix-build
:nix-build
is used to build Nix packages, and it 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,
- ...
maybe
nix-env -qa
is still useful some times, which returns all packages installed in the System.
When will flakes stablized
I dived into some details about flakes:
- https://github.com/NixOS/rfcs/pull/136: A plan to stabilize Flakes and the new CLI incrementally, still WIP.
- https://discourse.nixos.org/t/why-are-flakes-still-experimental/29317: A post, Why are flakes still experimental?
- https://grahamc.com/blog/flakes-are-an-obviously-good-thing/: Flakes are such an obviously good thing... but the design and development process should be better.
- Draft: 1 year roadmap - NixOS Foundation: A roadmap of nixos fundation, which includes plan about the stabilization of flakes.
After reading all of these, I feel like that flakes will eventually be stabilized in one or two years, maybe with some breaking changes.