6. Turn your project into a flake
→ Transform an existing project into a flake
→ Use the flake's development environment
In some of the previous steps in Zero to Nix you learned about Nix flakes and Nix development environments.
Turning your own projects into flakes can be somewhat tricky, so we at Determinate Systems have created a tool that can help in many scenarios: fh
, the CLI for the FlakeHub platform.
fh
has a utility called fh init
that creates a flake.nix
file based on two things:
- The contents of your project
- Your responses to its interactive questions
You can run fh init
using Nix:
This will start up an interactive builder that asks you a series of questions and then writes a flake.nix
file into the root of your project (plus some other files if you say yes to some of those questions).
Once you’ve generated a new flake, you can see which outputs it has:
You should see something like this:
fh init
supports a wide variety of languages and tools.
If your project has a Cargo.toml
file in the root, for example, then fh init
infers that it’s a Rust project and asks if you want to add Rust dependencies to your Nix development environment.
If you say yes, then the generated flake.nix
will include the cargo
build tool plus some other Rust-specific tools.
Note that fh init
currently only supports devShells
outputs.
That is, it only generates a development environment for you, not things like package outputs.
fh init
Be aware that fh init
operates on a “best-guess” basis to infer which
languages and tools you use in your project. It’s possible that it will miss
things or make incorrect guesses. But we hope that the flake.nix
that it
creates for you will at least serve as a solid initial template that you can
modify further.
Example project
We’ve created an example project that you can use to test out fh init
: