It’s common to use a very similar set of tools from one project to the next, but it’s also very common to need to set up a developer environment from scratch from one project to the next. In this tutorial you’ll see how to significantly cut down on the work required to bootstrap new projects.Documentation Index
Fetch the complete documentation index at: https://flox.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
Create a reusable toolchain
Let’s say that you frequently work on Python projects and use Poetry as your package manager of choice. If you work on a bunch of projects that need these tools, you can save yourself some time by creating an environment that containspython312 and poetry and reusing it in some way.
Let’s do that now and we’ll show you some examples of different ways you can reuse it.
Create a new directory called myproject and cd into it.
Then create a new environment called python_env in it with the flox init command.
python312 and poetry to this environment with the flox install command:
myuser you will see your username.
Template environments
Creating a template for new projects
Now that the environment is on FloxHub, we can use it to bootstrap new projects. One way to do this is to make a new, local copy of the environment that’s not connected to FloxHub. You accomplish this with theflox pull --copy command.
Let’s create a new copy of this environment in a directory called new_python_project:
new_python_project directory contains a Flox environment that already contains Python development tools, and you’re ready to start developing.
Staying in sync with the template
By using the--copy flag to flox pull we have created a new copy of the environment that is completely disconnected from the copy on FloxHub.
This makes sense if you plan to add project-specific dependencies, otherwise your template environment would accumulate dependencies not needed by all of your Python projects.
The downside is that if someone makes changes to the template environment, your local copy won’t get those updates.
In some cases, however, you may actually want your project to stay in sync with the template.
If that’s something you want, you can simply omit the --copy flag and periodically run the flox pull command to get the latest updates to the environment.
This amounts to using the template environment directly.
Composing environments
Creating a composed environment
In the previous example we showed you how you could create a local copy of a template environment (and lose access to updates), or use the template environment directly (and either stuff it full of every project’s dependencies, or not add dependencies to it at all). What if I told you that you could have the best of both worlds? We call this feature “composition”. One environment (we call it the “composing” environment) can “include” another environment (we call this an “included” environment), treating it like a dependency. You can install packages to the composing environment just like you would any other environment, which allows you to reuse the template environment, get updates to it, and add project specific dependencies directly to the composing environment. Let’s see an example of this in action. As a reminder, you currently have this directory structure:composed_python_project directory.
composed_python_project environment and include the python_env environment.
Run the flox edit command and make the include section of the manifest look like this:
include.environments list tells the Flox CLI where to find the environments you’d like to include.
When there is more than one environment in this list, the order of the environments in the list also specifies their priority (later ones in the list have higher priority).
Once you save and exit you should see this output:
options.systems is simply a result of the fact that the default manifest sets this field explicitly.
If you now run flox list, you should see the packages from the python_env environment, even though we never ran a flox install command on the composed_python_project environment!
flox list -c command.
When you use this command without a composed environment, it prints the (unmerged) manifest.
When you use this command with a composed environment, it prints the merged manifest.
[install] section contains the python312 and poetry packages that were merged in from the python_env environment.
Installing project specific dependencies
Now let’s install some project-specific dependencies. When you run theflox install command on a composing environment, the packages are installed to the composing environment itself, not the merged manifest or any of the included environments.
In short, it does what you would expect.
Let’s add the pytest package:
composed_python_project environment.
If you run flox edit -d composed_python_project you’ll see that the package is contained in the [install] section of composed_python_project’s manifest.
Getting the latest versions of included environments
At some point a template environment may change. Say that you decide you want to use hypothesis for testing all of your Python projects. To do that, you would install thehypothesis package to your python_env environment, and somehow propagate those changes to the composed environment.
This is accomplished with the flox include upgrade command, which fetches the latest versions of each of the included environments.
Let’s add that package to python_env:
flox list you should see that the composed environment now contains the hypothesis package:
composed_python_project directly is pytest.
All of the other packages you get for free just by including the python_env environment.
Including FloxHub environments
Environments can also be included directly from FloxHub, such as themyuser/python_env environment that we pushed previously.
This is especially useful if you’re including the same environment across multiple projects and repositories because you don’t need to ensure that they are checked out and synced locally.
Push the additional package that we installed to myuser/python_env earlier:
flox edit command and make the include section of the manifest look like this:
Conclusion
The ability to reuse and combine environments means that you can now assemble a developer environment for a project from reusable building blocks. This means you can spend less time getting started, and more time developing your software. Similarly, since you’re treating environments like dependencies, if you make an improvement to a template environment while working on one project, the improvement will become available to all of your other projects that use that environment as soon as they runflox include upgrade.