6. Turn your project into a flake

Quick start / Turn your project into a flake
Guide 6 of 8
In this guide

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:

  1. The contents of your project
  2. Your responses to its interactive questions

You can run fh init using Nix:

nix run "https://flakehub.com/f/NixOS/nixpkgs/*.tar.gz#fh" -- init

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:

nix flake show

You should see something like this:

git+file:///path/to/fh-init-example-project
├───devShells
│   ├───aarch64-darwin
│   │   └───default: development environment 'nix-shell'
│   ├───aarch64-linux
│   │   └───default omitted (use '--all-systems' to show)
│   ├───x86_64-darwin
│   │   └───default omitted (use '--all-systems' to show)
│   └───x86_64-linux
│       └───default omitted (use '--all-systems' to show)
└───schemas: unknown

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.

Example project

We've created an example project that you can use to test out fh init:

git clone https://github.com/DeterminateSystems/fh-init-example-project
cd fh-init-example-project
nix run "https://flakehub.com/f/DeterminateSystems/fh/*.tar.gz" -- init

# respond to the prompts

nix flake show

Was this page helpful?