What is a flake

I have seen the term flake a bit everywhere.
But I have not grasped (yet) its meaning.
Can you explain or even define what a flake is?

I’ll share a 2 year old comment I made on Reddit that I think is still accurate. Reddit - Dive into anything

Quoting:

The basic idea of flakes isn’t that complicated, but it does require a little bit of basic understanding of the nix programming language. A flake.nix file is an attribute set with two attributes called inputs and outputs. The inputs attribute describes the other flakes that you would like to use; things like nixpkgs or home-manager. You have to give it the url where the code for that other flake is, and usually people use GitHub. The outputs attribute is a function, which is where we really start getting into the nix programming language. Nix will go and fetch all the inputs, load up their flake.nix files, and it will call your outputs function with all of their outputs as arguments. The outputs of a flake are just whatever its outputs function returns, which can be basically anything the flake wants it to be. Finally, nix records exactly which revision was fetched from GitHub in flake.lock so that the versions of all your inputs are pinned to the same thing until you manually update the lock file.

Now, I said that the outputs of a flake can be basically anything you want, but by convention, there’s a schema that most flakes adhere to. For instance, flakes often include outputs like packages.x86_64-linux.foo as the derivation for the foo package for x86_64-linux. But it’s important to understand that this is a convention, which the nix CLI uses by default for a lot of commands. The reason I consider this important to understand is because people often assume flakes are some complicated thing, and that therefore flakes somehow change the fundamentals of how nix works and how we use it. They don’t. All the flakes feature does is look at the inputs you need, fetch them, and call your outputs function. It’s truly that simple. Pretty much everything else that comes up when using flakes is actually just traditional nix, and not flakes-related at all.

The only other significant difference from traditional nix is “purity”. Flakes disallow a lot of “impure” ideas, like depending on config files or environment variables that aren’t a part of the flake’s source directory or its inputs’. This makes it so that a derivation / system / etc. is reproducible no matter what context you’re evaluating the flake from.

A great fundamental piece on what flakes are is “Flakes Aren’t Real”: Flakes aren't real and cannot hurt you: a guide to using Nix flakes the non-flake way - jade's www site

4 Likes

Also for a less involved conceptual overview (that doesn’t go into detail about why flakes even came to be, what problems they were supposed to solve and how; but maybe it should):

https://nix.dev/concepts/flakes

1 Like