Flox and systemd
Flox environments have a built-in concept of services.
Flox environment services are managed by invoking the flox services
category of sub-commands such as flox services status.
This tutorial shows how to create and use systemd services with Flox by creating unit files manually. You will learn how to run a Flox environment service as both a systemd user unit and a systemd system unit.
Prerequisites
- A Linux system with systemd support. This tutorial was tested on Ubuntu 24.04.
- Flox installed in multi-user mode. This tutorial was tested on Flox 1.10.0.
Run a Flox environment service as a systemd user unit
In this section you will set up a Redis service from a FloxHub environment and run it as a systemd user unit.
Create the Redis environment locally
Create a directory for the environment and pull the flox/redis environment
from FloxHub:
Test the environment with Flox services
Before creating the systemd unit, verify that the environment works with Flox services commands:
Once verified, stop the services and exit the environment:
Create the systemd user service
Note
The linger configuration is only required if you want the service to start on boot without a login. Otherwise the user must be logged in before the systemd service attempts to start.
Create the systemd user unit file:
cat > ~/.config/systemd/user/redis.service << 'EOF'
[Unit]
Description=Redis Server (Flox)
[Service]
ExecStart=flox activate -d /home/ubuntu/redis -- bash -c 'redis-server "$REDISCONFIG" --daemonize no --dir "$REDISDATA"'
[Install]
WantedBy=default.target
EOF
Note
Update the path /home/ubuntu/redis in the ExecStart line to match
the location where you created the environment.
Enable lingering so the service starts at boot without login:
Load, enable, and start the service:
systemctl --user daemon-reload
systemctl --user enable redis.service
systemctl --user start redis.service
Verify the service is running:
Verify Redis is responding:
Run a Flox environment service as a systemd system unit
For services that should run under a dedicated system user rather than your personal account, you can create a system-level systemd unit instead.
Create a dedicated Redis user
Create the environment and set ownership
sudo mkdir -p /home/redis
sudo flox pull flox/redis -d /home/redis/redis
sudo chown -R redis:redis /home/redis
Create the system unit file
Since the redis user has no login session,
user units will not work.
Create a system unit instead:
sudo tee /etc/systemd/system/redis.service << 'EOF'
[Unit]
Description=Redis Server (Flox)
[Service]
User=redis
ExecStart=flox activate -d /home/redis/redis -- bash -c 'redis-server "$REDISCONFIG" --daemonize no --dir "$REDISDATA"'
[Install]
WantedBy=multi-user.target
EOF
Note
Enable lingering is not needed for system units. System units start at boot automatically.
Load, enable, and start
Verify
Note
Key differences from the user unit approach:
the system unit goes in /etc/systemd/system/,
uses multi-user.target,
requires sudo,
and no lingering is needed.