Skip to content

Commit 1dc7195

Browse files
authored
Merge branch 'main' into vendoring-dependencies-flox-build-tutorial
2 parents 977e487 + c56dc1d commit 1dc7195

File tree

9 files changed

+469
-28
lines changed

9 files changed

+469
-28
lines changed

.github/actions/common-setup/action.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ runs:
3030
using: "composite"
3131
steps:
3232
- name: "Setup Tailscale"
33-
uses: "tailscale/github-action@v4"
33+
uses: tailscale/github-action@306e68a486fd2350f2bfc3b19fcd143891a4a2d8 # v4
34+
3435
if: ${{ inputs.TAILSCALE_AUTH_KEY }}
3536
with:
3637
oauth-secret: "${{ inputs.TAILSCALE_AUTH_KEY }}"
@@ -39,10 +40,12 @@ runs:
3940
use-cache: true
4041

4142
- name: "Install newer Nix"
42-
uses: "cachix/install-nix-action@v24"
43+
uses: cachix/install-nix-action@7ac1ec25491415c381d9b62f0657c7a028df52a7 # v24
44+
4345

4446
- name: "Configure Nix"
45-
uses: "flox/configure-nix-action@main"
47+
uses: flox/configure-nix-action@1ee58d63463cb1870ab73d9df0acfd9d8ba671f8 # main
48+
4649
with:
4750
github-access-token: "${{ inputs.GITHUB_ACCESS_TOKEN }}"
4851
substituter: "${{ inputs.SUBSTITUTER }}"

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ updates:
2626
all:
2727
patterns:
2828
- "*"
29+
30+
31+
- package-ecosystem: "github-actions"
32+
directory: "/.github/actions/common-setup"
33+
schedule:
34+
interval: "weekly"

.github/workflows/auto-label.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ jobs:
1414
issues: write
1515
steps:
1616
- name: Add label automatically to new issues and PRs
17-
uses: actions-ecosystem/action-add-labels@v1
17+
uses: actions-ecosystem/action-add-labels@18f1af5e3544586314bbe15c0273249c770b2daf # v1
18+
1819
with:
1920
github_token: "${{ secrets.GITHUB_TOKEN }}"
2021
labels: "documentation"

.github/workflows/ci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ jobs:
3232

3333
steps:
3434
- name: "Checkout"
35-
uses: "actions/checkout@v6"
35+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
36+
3637
with:
3738
fetch-depth: 0
3839

3940
- name: "Install Flox"
40-
uses: "flox/install-flox-action@main"
41+
uses: flox/install-flox-action@c94e7e1ab56ae14fe98bae4fd84384fd135f0c2a # main
42+
4143

4244
- name: "Build"
43-
uses: "flox/activate-action@main"
45+
uses: flox/activate-action@d2c0d305d166520ed1cc2485bf10cd83cd859674 # main
46+
4447
with:
4548
command: |
4649
mkdocs build
@@ -50,19 +53,22 @@ jobs:
5053
cp netlify.toml ./public/
5154
5255
- name: "Check markdown lint"
53-
uses: "flox/activate-action@main"
56+
uses: flox/activate-action@d2c0d305d166520ed1cc2485bf10cd83cd859674 # main
57+
5458
with:
5559
command: "markdownlint-cli2"
5660

5761
- name: "Check external links"
5862
if: ${{ github.event_name == 'pull_request' }}
59-
uses: "flox/activate-action@main"
63+
uses: flox/activate-action@d2c0d305d166520ed1cc2485bf10cd83cd859674 # main
64+
6065
with:
6166
command: "./check_links.sh"
6267

6368
- name: "Publish to Netlify"
6469
if: ${{ github.repository_owner == 'flox' }}
65-
uses: "nwtgck/actions-netlify@v3.0"
70+
uses: nwtgck/actions-netlify@4cbaf4c08f1a7bfa537d6113472ef4424e4eb654 # v3.0
71+
6672
env:
6773
NETLIFY_AUTH_TOKEN: "${{ secrets.MANAGED_NETLIFY_TOKEN }}"
6874
NETLIFY_SITE_ID: "${{ vars.MANAGED_NETLIFY_FLOXDOCS_ID }}"

.github/workflows/update.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ jobs:
1717

1818
steps:
1919
- name: "Checkout"
20-
uses: "actions/checkout@v6"
20+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
21+
2122

2223
- name: "Install flox"
23-
uses: "flox/install-flox-action@main"
24+
uses: flox/install-flox-action@c94e7e1ab56ae14fe98bae4fd84384fd135f0c2a # main
25+
2426

2527
- name: "Run upgrade"
2628
run: "flox -vvv upgrade"
2729

2830
- name: "Create Pull Request"
29-
uses: "peter-evans/create-pull-request@v8"
31+
uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8
32+
3033
with:
3134
token: "${{ secrets.MANAGED_FLOXBOT_GITHUB_ACCESS_TOKEN_REPO_SCOPE }}"
3235
add-paths: ".flox"

docs/tutorials/default-environment.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ in order to install packages system-wide.
1818
This has a number of drawbacks:
1919

2020
- You often only have a single package version to choose from.
21-
- You often can't install multiple versions side-by-side.
21+
- You often can't install multiple versions of a package side-by-side.
2222
- You can't ensure that multiple machines get the exact same version.
2323
- You may not be able to back up the list of installed packages.
2424

@@ -29,9 +29,10 @@ so let's take a look at how to set it up.
2929

3030
At the most basic level, the `default` environment is simply an environment
3131
called `default`.
32-
`default` environments are typically [shared via FloxHub][floxhub-env];
33-
We refer to the one associated with your account,
34-
as _your_ `default` environment.
32+
`default` environments are typically [shared via FloxHub][floxhub-env], but
33+
you can also manage environments with git.
34+
We refer to the environment associated with your user account as _your_
35+
`default` environment.
3536

3637
In some cases Flox will prompt to set up your `default` environment for you.
3738
To create the `default` environment yourself,
@@ -46,11 +47,9 @@ flox auth status || flox auth login
4647
flox init -r <youruser>/default
4748
```
4849

49-
Once the environment has been created,
50-
you'll want to configure your shell to activate the environment with every new
51-
shell.
52-
This can be done as part of the automatic setup,
53-
or you can add a single line to your shell's RC file:
50+
Once the environment has been created, configure your shell to activate the
51+
environment with every new shell. This can be done as part of the automatic
52+
setup, or you can add a single line to your shell's RC file:
5453

5554
=== "Bash"
5655

@@ -95,9 +94,9 @@ or you can add a single line to your shell's RC file:
9594
Once you've added that line to your shell,
9695
you'll need to restart your shell (or open a new one) for the changes to
9796
take effect.
98-
If you don't want to activate it automatically, the default
99-
environment can simply be activated using `-d` parameter of the Flox CLI
100-
like so:
97+
If you don't want to activate it automatically from your shell
98+
initialization scripts, you can activate the default environment
99+
explicitly when needed:
101100

102101
```{ .bash .copy }
103102
flox activate -r <your username>/default
@@ -125,8 +124,6 @@ $ flox install hello
125124
✅ 'hello' installed to environment 'default'
126125
```
127126

128-
It worked (though you shouldn't be surprised; Flox is awesome)!
129-
130127
## Installing packages to the default environment from another Flox environment
131128

132129
If you're in a project directory with an existing Flox environment,
@@ -138,7 +135,7 @@ environment.
138135
All you need to do is pass the `-r` argument to the `install` command, like so:
139136

140137
```{ .bash .copy }
141-
flox install -r <your username>/default ~ hello
138+
flox install -r <your username>/default hello
142139
```
143140

144141
When you do this, you should see the following output, indicating success:

docs/tutorials/flox-and-systemd.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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

Comments
 (0)