Check out FlakeHub — the best place to discover and publish Nix flakes, from Determinate Systems.

Sign up for the Determinate Systems mailing list

* indicates required

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:

Initialize your flake
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:

Display the flake's outputs
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.

The limitations of 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:

Terminal window
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