Skip to content

Environments

flox installs packages into collections called environments.

If you haven't already, please install flox before continuing.

1. flox create - create new environments

$ flox create
created environment default (aarch64-linux)

With this command flox will automatically create an environment called default. You can specify a name for your environment using -e.

$ flox create -e empty
created environment empty (aarch64-linux)

Environments are also automatically created when installing to a non-existent environment.

$ flox install -e py2 python27                                                     \
Installed 'python27' package(s) into 'py2' environment.
created generation 1 # (1)!

$ flox install -e py3 python3
Installed 'python3' package(s) into 'py3' environment.
  1. Generation represents the version of the environment.

    To list all generations look at the last section of this tutorial.

2. flox destroy - delete environments

$ flox destroy -e empty
WARNING: you are about to delete the following:
 - the aarch64-linux.empty branch in /home/rok/.cache/flox/meta/local
Are you sure? (y/N) y
Deleted branch aarch64-linux.empty (was b89b112).

3. flox environments - list environments

$ flox environments # (1)!
local/default
    Alias     default
    System    aarch64-linux
    Path      /home/USER/.local/share/flox/environments/local/default
    Curr Gen  1

local/py2
    Alias     py2
    System    aarch64-linux
    Path      /home/USER/.local/share/flox/environments/local/py2
    Curr Gen  1

local/py3
    Alias     py3
    System    aarch64-linux
    Path      /home/USER/.local/share/flox/environments/local/py3
    Curr Gen  1
  1. You can also use flox envs alias.

4. flox activate - activate environments

So far we only briefly mentioned (here and here) how to use flox activate.

But there are other scenarios how to use flox activate:

4.1. In a subshell

When invoked from a controlling terminal the following command starts a subshell in which the environment is active.

$ flox activate -e py2
flox [py2 default] $ python --version
Python 2.7.18

flox [py2 default] $ flox activate -e py3
flox [py3 py2 default] $ python --version
Python 3.10.6

flox [py3 py2 default] $ exit
flox [py2 default] $ exit
$

Be sure to exit from the subshell before proceeding with examples.

4.2. In the current shell

When invoked without a controlling terminal flox activate prints the commands required to activate the environment using syntax according to the current value of the $SHELL environment variable, which can be used to activate an environment into the running shell.

Don't run this command!

If you do, all programs in the py2 environment will be available for the duration of this tutorial.

$ . <(flox activate -e py2)
flox [py2 default] $ python --version
Python 2.7.18

flox [py2 default] $ exit
$
Activate default environment automatically at login ...

... simply add the following to the relevant "dotfile" (e.g. ~/.bashrc):

~/.bashrc
. <(flox activate)

For direnv integration follow this tutorial.

4.3. For a single invocation

Environments can be activated for a single invocation by providing command arguments following a -- delimiter.

$ flox activate -e py2 -- python --version
Python 2.7.18

$ flox activate -e py3 -- python --version
Python 3.10.6

When invoking multiple commands you need to invoke a subshell.

Let's install an additional program to our py3 environment, and chain commands:

$ flox install -e py3 figlet
Installed 'figlet' package(s) into 'py3' environment.

$ flox activate -e py3 -- sh -c "python --version 2>&1 | figlet"
____        _   _                   _____  _  ___
|  _ \ _   _| |_| |__   ___  _ __   |___ / / |/ _ \ / /_
| |_) | | | | __| '_ \ / _ \| '_ \    |_ \ | | | | | '_ \
|  __/| |_| | |_| | | | (_) | | | |  ___) || | |_| | (_) |
|_|    \__, |\__|_| |_|\___/|_| |_| |____(_)_|\___(_)___/
       |___/

4.4. With multiple environments

All of the above methods can be used with multiple -e flags to activate multiple environments at the same time.

$ flox activate -e py2 -e py3 -- sh -c "python --version 2>&1| figlet"
 ____        _   _                   ____   _____ _  ___   ____
|  _ \ _   _| |_| |__   ___  _ __   |___ \ |___  / |( _ ) | ___|
| |_) | | | | __| '_ \ / _ \| '_ \    __) |   / /| |/ _ \ |___ \
|  __/| |_| | |_| | | | (_) | | | |  / __/ _ / /_| | (_) | ___) |
|_|    \__, |\__|_| |_|\___/|_| |_| |_____(_)_/(_)_|\___(_)____/
       |___/

Which shows the python 2 version, since the first environment passed will override the subsequent environments.

The figlet command exists in the py3 environment, and both are used together.

5. flox upgrade - keep environments up-to-date

Running flox upgrade synchronizes an environment wherever it is used. That means your laptop, desktop, and EC2 instance can all run the same software.

$ flox upgrade -e py3 # (1)!
No change! Environment 'py3' _not_ upgraded."
  1. Running flox upgrade on just a created environment will of course result with everything already being at the latest version.

6. flox generations - environment history

Every time you make a change flox creates a new version of the environment alongside the old one.

Each of these is referred to as a "generation" and you can view them with the flox generations command.

$ flox generations -e py3
Generation 1:
  Path:        /nix/store/ab6r118k58frbzf3cdbs9j30jb1msar8-profile
  Created:     2022-11-10T22:59:56Z
  Last active: 2022-11-10T23:00:09Z
  Log entries:
    Generation 1: USER installed stable.nixpkgs-flox.python3
Generation 2:
  Path:        /nix/store/jpq7m92wwi4zzqr3rn4v1339sdfzhhnx-profile
  Created:     2022-11-10T23:00:11Z
  Last active: 2022-11-10T23:00:09Z
  Log entries:
    Generation 1->2: USER installed stable.nixpkgs-flox.figlet

You can roll back to the previous generation with the flox rollback command.

$ flox rollback -e py3
Rolled back environment 'py3' from generation 2 to 1.

... or switch to any available generation by number with flox switch-generation <NUMBER>.

$ flox switch-generation -e py3 2
Switched environment 'py3' from generation 1 to 2.

Where to next?