mirror of
https://codeberg.org/mhwombat/nix-book.git
synced 2025-12-27 00:34:58 +08:00
71 lines
2.1 KiB
Text
71 lines
2.1 KiB
Text
= Hello, flake!
|
|
|
|
Before learning to write Nix flakes, let's learn how to use them. I've
|
|
created a simple example of a flake in this git repository:
|
|
https://codeberg.org/mhwombat/hello-flake.
|
|
To run this flake, you don't need to install anything;
|
|
simply run the command below.
|
|
The first time you use a flake, Nix has to fetch and build it, which
|
|
may take time. Subsequent invocations should be instantaneous.
|
|
|
|
....
|
|
$ nix run "git+https://codeberg.org/mhwombat/hello-flake"
|
|
....
|
|
|
|
That's a lot to type every time we want to use this package. Instead, we
|
|
can enter a shell with the package available to us, using the
|
|
`nix shell` command.
|
|
|
|
....
|
|
$ nix shell "git+https://codeberg.org/mhwombat/hello-flake"
|
|
....
|
|
|
|
In this shell, the command is on our `$PATH`, so we can execute the
|
|
command by name.
|
|
|
|
....
|
|
$ hello-flake
|
|
....
|
|
|
|
Nix didn't _install_ the package; it merely built and placed it in a
|
|
directory called the "`Nix store`". Thus we can have multiple versions
|
|
of a package without worrying about conflicts. We can find out the
|
|
location of the executable, if we're curious.
|
|
|
|
....
|
|
$ which hello-flake
|
|
....
|
|
|
|
Once we exit that shell, the `hello-flake` command is no longer
|
|
directly available.
|
|
|
|
....
|
|
$# echo executable=$(which hello-flake) > temp.sh # save variable
|
|
$ exit
|
|
$ hello-flake # Fails outside development shell
|
|
....
|
|
|
|
However, we can still run the command using the store path we found
|
|
earlier. That's not particularly convenient, but it does demonstrate
|
|
that the package remains in the store for future use.
|
|
|
|
....
|
|
$# source temp.sh # restore variable
|
|
$# rm temp.sh
|
|
$# echo '$ '${executable}
|
|
$# ${executable}
|
|
....
|
|
|
|
== Flake outputs
|
|
|
|
You can find out what packages and apps a flake provides using the `nix flake show` command.
|
|
|
|
....
|
|
$# echo '$ nix flake show --all-systems git+https://codeberg.org/mhwombat/hello-flake'
|
|
$# nix flake show --all-systems git+https://codeberg.org/mhwombat/hello-flake --quiet | sed -e 's/\x1b\[[0-9;]*m//g'
|
|
....
|
|
|
|
Examining the output of this command,
|
|
we see that this flake supports multiple architectures
|
|
(aarch64-darwin, aarch64-linux, x86_64-darwin and x86_64-linux)
|
|
and provides both a package and an app called `hello`.
|