|
| 1 | +--- |
| 2 | +title: Flox and systemd |
| 3 | +description: Run Flox environment services as persistent systemd units. |
| 4 | +--- |
| 5 | + |
| 6 | +# Flox and systemd |
| 7 | + |
| 8 | +Flox environments have a built-in concept of [services][services_concept]. |
| 9 | +Flox environment services are managed by invoking the `flox services` |
| 10 | +category of sub-commands such as [`flox services status`][flox_services_status]. |
| 11 | +In some scenarios, you may want to register Flox services to be run and managed |
| 12 | +by the operating system's systemd. |
| 13 | +For example, systemd can auto-start services when the host is booting |
| 14 | +or when a service crashes. |
| 15 | + |
| 16 | +This tutorial shows how to create and use systemd services with Flox |
| 17 | +by creating unit files manually. |
| 18 | +You will learn how to run a Flox environment service as both a |
| 19 | +**systemd user unit** and a **systemd system unit**. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- A Linux system with systemd support. |
| 24 | + This tutorial was tested on Ubuntu 24.04 and 26.04. |
| 25 | +- Flox installed in multi-user mode. |
| 26 | + This tutorial was tested on Flox 1.11.0. |
| 27 | + |
| 28 | +## Constraints |
| 29 | + |
| 30 | +- The systemd service that invokes Flox cannot run as root. |
| 31 | +- The service cannot listen on a port with a value less than 1024. |
| 32 | +- The UID for the user running the systemd service should be >= 1000 |
| 33 | + for logs to function properly. |
| 34 | + See [flox#2e789b5][uid_fix] for details. |
| 35 | +- Logs may not function properly if the process forks. |
| 36 | + See [flox#9b1e750][fork_fix] for details. |
| 37 | + |
| 38 | +## Run a Flox environment service as a systemd user unit |
| 39 | + |
| 40 | +In this section you will set up a Redis service from a FloxHub environment |
| 41 | +and run it as a systemd user unit. |
| 42 | + |
| 43 | +### Create the Redis environment locally |
| 44 | + |
| 45 | +Pull the `flox/redis` environment from FloxHub into your home directory: |
| 46 | + |
| 47 | +``` { .bash .copy } |
| 48 | +flox pull flox/redis -d ~/redis |
| 49 | +``` |
| 50 | + |
| 51 | +### Test the environment with Flox services |
| 52 | + |
| 53 | +Before creating the systemd unit, |
| 54 | +verify that the environment works: |
| 55 | + |
| 56 | +``` { .bash .copy } |
| 57 | +cd ~/redis |
| 58 | +flox activate -s |
| 59 | +flox services status |
| 60 | +redis-cli -p $REDIS_PORT ping |
| 61 | +exit |
| 62 | +``` |
| 63 | + |
| 64 | +### Create the systemd user service |
| 65 | + |
| 66 | +Create the systemd user unit file: |
| 67 | + |
| 68 | +``` { .bash .copy } |
| 69 | +mkdir -p ~/.config/systemd/user/ |
| 70 | +cat > ~/.config/systemd/user/redis.service << 'EOF' |
| 71 | +[Unit] |
| 72 | +Description=Redis Server (Flox) |
| 73 | +
|
| 74 | +[Service] |
| 75 | +ExecStart=flox activate -d %h/redis -c 'redis-server --bind $REDIS_HOST --port $REDIS_PORT --daemonize no --dir $REDIS_DATA' |
| 76 | +
|
| 77 | +[Install] |
| 78 | +WantedBy=default.target |
| 79 | +EOF |
| 80 | +``` |
| 81 | + |
| 82 | +By default, systemd user units only run while the user is logged in. |
| 83 | +Enabling **lingering** allows the service to start at boot without a login session. |
| 84 | +If you only need the service to run while you are logged in, |
| 85 | +you can skip this step. |
| 86 | + |
| 87 | +``` { .bash .copy } |
| 88 | +sudo loginctl enable-linger $USER |
| 89 | +``` |
| 90 | + |
| 91 | +Load, enable, and start the service: |
| 92 | + |
| 93 | +``` { .bash .copy } |
| 94 | +systemctl --user daemon-reload |
| 95 | +systemctl --user enable redis.service |
| 96 | +systemctl --user start redis.service |
| 97 | +``` |
| 98 | + |
| 99 | +Verify the service is running: |
| 100 | + |
| 101 | +``` { .bash .copy } |
| 102 | +systemctl --user status redis.service |
| 103 | +``` |
| 104 | + |
| 105 | +Verify Redis is responding: |
| 106 | + |
| 107 | +``` { .bash .copy } |
| 108 | +flox activate -d ~/redis -c 'redis-cli -p "$REDIS_PORT" ping' |
| 109 | +# should respond PONG |
| 110 | +``` |
| 111 | + |
| 112 | +### User unit cleanup |
| 113 | + |
| 114 | +To stop and fully remove the systemd user unit: |
| 115 | + |
| 116 | +``` { .bash .copy } |
| 117 | +systemctl --user stop redis.service |
| 118 | +systemctl --user disable redis.service |
| 119 | +rm ~/.config/systemd/user/redis.service |
| 120 | +systemctl --user daemon-reload |
| 121 | +``` |
| 122 | + |
| 123 | +## Run a Flox environment service as a systemd system unit |
| 124 | + |
| 125 | +For services that should run under a dedicated system user rather than |
| 126 | +your personal account, |
| 127 | +you can create a system-level systemd unit instead. |
| 128 | + |
| 129 | +### Create a dedicated Redis user and environment |
| 130 | + |
| 131 | +``` { .bash .copy } |
| 132 | +sudo useradd --system --create-home --shell /usr/sbin/nologin redis |
| 133 | +sudo -u redis flox pull flox/redis -d /home/redis/redis |
| 134 | +``` |
| 135 | + |
| 136 | +### Create the system unit file |
| 137 | + |
| 138 | +Since the `redis` user has no login session, |
| 139 | +user units will not work. |
| 140 | +Create a system unit instead: |
| 141 | + |
| 142 | +``` { .bash .copy } |
| 143 | +sudo tee /etc/systemd/system/redis.service << 'EOF' |
| 144 | +[Unit] |
| 145 | +Description=Redis Server (Flox) |
| 146 | +
|
| 147 | +[Service] |
| 148 | +User=redis |
| 149 | +ExecStart=flox activate -d /home/redis/redis -c 'redis-server --bind $REDIS_HOST --port $REDIS_PORT --daemonize no --dir $REDIS_DATA' |
| 150 | +
|
| 151 | +[Install] |
| 152 | +WantedBy=multi-user.target |
| 153 | +EOF |
| 154 | +``` |
| 155 | + |
| 156 | +!!! note "Note" |
| 157 | + Enable lingering is not needed for system units. |
| 158 | + System units start at boot automatically. |
| 159 | + |
| 160 | +### Load, enable, and start |
| 161 | + |
| 162 | +``` { .bash .copy } |
| 163 | +sudo systemctl daemon-reload |
| 164 | +sudo systemctl enable redis.service |
| 165 | +sudo systemctl start redis.service |
| 166 | +``` |
| 167 | + |
| 168 | +### Verify |
| 169 | + |
| 170 | +``` { .bash .copy } |
| 171 | +sudo systemctl status redis.service |
| 172 | +``` |
| 173 | + |
| 174 | +``` { .bash .copy } |
| 175 | +flox activate -d ~/redis -c 'redis-cli -p $REDIS_PORT ping' |
| 176 | +# should respond PONG |
| 177 | +``` |
| 178 | + |
| 179 | +!!! note "Note" |
| 180 | + Key differences from the user unit approach: |
| 181 | + the system unit goes in `/etc/systemd/system/`, |
| 182 | + uses `multi-user.target`, |
| 183 | + requires `sudo`, |
| 184 | + and no lingering is needed. |
| 185 | + |
| 186 | +### System unit cleanup |
| 187 | + |
| 188 | +To stop and fully remove the systemd system unit: |
| 189 | + |
| 190 | +``` { .bash .copy } |
| 191 | +sudo systemctl stop redis.service |
| 192 | +sudo systemctl disable redis.service |
| 193 | +sudo rm /etc/systemd/system/redis.service |
| 194 | +sudo systemctl daemon-reload |
| 195 | +``` |
| 196 | + |
| 197 | +## Where to next? |
| 198 | + |
| 199 | +- :simple-readme:{ .flox-purple .flox-heart } [Learn more about services][services_concept] |
| 200 | + |
| 201 | +- :simple-readme:{ .flox-purple .flox-heart } [Sharing environments][sharing_guide] |
| 202 | + |
| 203 | +[services_concept]: ../concepts/services.md |
| 204 | +[flox_services_status]: ../man/flox-services-status.md |
| 205 | +[sharing_guide]: ./sharing-environments.md |
| 206 | +[uid_fix]: https://github.com/flox/flox/commit/2e789b55de153b80a23367b236334ffbe84d6289 |
| 207 | +[fork_fix]: https://github.com/flox/flox/commit/9b1e7504fd5dd482d9afeda1e21ecfe8d1f86593 |
0 commit comments