nix-book/source/hello-flake-repo/main.adoc0
Amy de Buitléir d0be400f85 expanded
2025-10-12 15:58:37 +01:00

90 lines
2.7 KiB
Text

= The hello-flake repo
Let's clone the repository and see how the flake is defined.
....
$ git clone https://codeberg.org/mhwombat/hello-flake
$ cd hello-flake
$ ls
....
This is a simple repo with just a few files. Like most git repos, it
includes `LICENSE`, which contains the software license, and `README.md`
which provides information about the repo.
The `hello-flake` file is the executable we ran earlier.
This particular executable is just a shell script, so we can view it.
It's an extremely simple script with just two lines.
[source,bash,linenums]
.hello-flake
....
$# cat hello-flake
....
Now that we have a copy of the repo, we can execute this script
directly.
....
$ ./hello-flake
....
Not terribly exciting, I know. But starting with such a simple package
makes it easier to focus on the flake system without getting bogged down
in the details. We'll make this script a little more interesting later.
Let's look at another file. The file that defines how to package a flake
is always called `flake.nix`.
[source,nix,linenums]
.flake.nix
....
$# cat flake.nix
....
If this is your first time seeing a flake definition, it probably looks
intimidating.
Flakes are written in the Nix language, introduced in <<_the_nix_language>>.
However, you don't really need to know Nix to follow this example.
For now, I'd like to focus on the inputs section.
[source,nix]
....
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
....
There are just two entries, one for `nixpkgs` and one for `flake-utils`.
The first one, `nixpkgs` refers to the collection of standard software
packages that can be installed with the Nix package manager. The second,
`flake-utils`, is a collection of utilities that simplify writing
flakes. The important thing to note is that the `hello-flake` package
_depends_ on `nixpkgs` and `flake-utils`.
Finally, let's look at `flake.lock`, or rather, just part of it.
[source,,linenums]
.flake.lock
....
$# head -n 40 flake.lock
. . .
....
If `flake.nix` seemed intimidating, then this file looks like an
invocation for Cthulhu. The good news is that this file is automatically
generated; you never need to write it. It contains information about all
of the dependencies for the flake, including where they came from, the
exact version/revision, and hash. This lockfile _uniquely_ specifies all
flake dependencies, (e.g., version number, branch, revision, hash), so
that _anyone, anywhere, any time, can re-create the exact same
environment that the original developer used._
No more complaints of "but it works on my machine!". That is the
benefit of using flakes.
////
Good adoc0 scripts clean up after themselves.
$ cd .. ; rm -rf hello-flake # clean up
////