Skip to content

Commit ecbc3e9

Browse files
authored
docs: add new docs page for build logs and env var json (#2030)
1 parent 9fbbc47 commit ecbc3e9

File tree

5 files changed

+729
-14
lines changed

5 files changed

+729
-14
lines changed

docs/index.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ self-contained.
123123

124124
There is a GitHub Action for `rattler-build`. It can be used to install `rattler-build` in CI/CD workflows and run a build command. Please check out the [GitHub Action documentation](https://github.com/prefix-dev/rattler-build-action) for more information.
125125

126-
### The recipe format
126+
## The Recipe Format
127127

128128
> **Note** You can find all examples below in the [`examples`](https://github.com/prefix-dev/rattler-build/tree/main/examples)
129129
> folder in the codebase and run them with `rattler-build`.
130130
131131
A simple example recipe for the `xtensor` header-only C++ library:
132132

133-
```yaml
133+
```yaml title="recipe.yaml"
134134
context:
135135
name: xtensor
136136
version: 0.24.6
@@ -198,7 +198,7 @@ extra:
198198
199199
A recipe for the `rich` Python package (using `noarch`):
200200

201-
```yaml
201+
```yaml title="recipe.yaml"
202202
context:
203203
version: "13.4.2"
204204
@@ -251,7 +251,7 @@ about:
251251
252252
A recipe for the `curl` library:
253253

254-
```yaml
254+
```yaml title="recipe.yaml"
255255
context:
256256
version: "8.0.1"
257257
@@ -298,9 +298,7 @@ about:
298298

299299
For the `curl` library recipe, two additional script files (`build.sh` and `build.bat`) are needed.
300300

301-
**`build.sh`**
302-
303-
```bash
301+
```bash title="build.sh"
304302
#!/bin/bash
305303
306304
# Get an updated config.sub and config.guess
@@ -326,9 +324,9 @@ make install
326324
rm -rf "${PREFIX}/share"
327325
```
328326

329-
**`build.bat`**
327+
Or on Windows:
330328

331-
```cmd
329+
```cmd title="build.bat"
332330
mkdir build
333331
334332
cmake -GNinja ^

docs/special_files.md

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@ The activation scripts are executed when the environment containing the package
1010

1111
The scripts are located in special folders:
1212

13-
- `etc/conda/activate.d/{script.sh/bat}` - scripts in this folder are executed before the environment is activated
14-
- `etc/conda/deactivate.d/{script.sh/bat}` - scripts in this folder are executed when the environment is deactivated
13+
- `etc/conda/activate.d/{script.*}` - scripts in this folder are executed when the environment is activated
14+
- `etc/conda/deactivate.d/{script.*}` - scripts in this folder are executed when the environment is deactivated
1515

1616
The scripts are executed in lexicographical order, so you can prefix them with numbers to control the order of execution.
1717

18+
### Shell-specific file extensions
19+
20+
Different shells require different script file extensions. The activation system will only execute scripts that match the current shell:
21+
22+
| Shell | Extension |
23+
|------------|-----------|
24+
| Bash | `.sh` |
25+
| Zsh | `.sh` |
26+
| Fish | `.fish` |
27+
| Xonsh | `.xsh` or `.sh` |
28+
| PowerShell | `.ps1` |
29+
| Cmd.exe | `.bat` |
30+
| NuShell | `.nu` |
31+
1832
To add a script to the package, just make sure that you install the file in this folder. For
1933
example, on Linux:
2034

@@ -26,6 +40,128 @@ mkdir -p $PREFIX/etc/conda/deactivate.d
2640
cp deactivate-mypkg.sh $PREFIX/etc/conda/deactivate.d/10-deactivate-mypkg.sh
2741
```
2842

43+
For cross-platform packages that need to support multiple shells, you should provide scripts for each shell type you want to support:
44+
45+
```sh
46+
# Unix shells (Bash, Zsh)
47+
mkdir -p $PREFIX/etc/conda/activate.d
48+
cp activate-mypkg.sh $PREFIX/etc/conda/activate.d/10-activate-mypkg.sh
49+
50+
# Windows Cmd.exe
51+
cp activate-mypkg.bat $PREFIX/etc/conda/activate.d/10-activate-mypkg.bat
52+
53+
# Windows PowerShell
54+
cp activate-mypkg.ps1 $PREFIX/etc/conda/activate.d/10-activate-mypkg.ps1
55+
```
56+
57+
## Activation environment variables
58+
59+
If you only need to set environment variables when an environment is activated (rather than running arbitrary shell code), you can use JSON files in the `etc/conda/env_vars.d/` directory. This is more efficient and portable than using activation scripts.
60+
61+
### Package-specific environment variables
62+
63+
To set environment variables from your package, create a JSON file in `etc/conda/env_vars.d/`:
64+
65+
```text
66+
<prefix>/etc/conda/env_vars.d/<package_name>.json
67+
```
68+
69+
The JSON file should contain a simple object with string key-value pairs:
70+
71+
```json
72+
{
73+
"MY_PACKAGE_HOME": "/path/to/data",
74+
"MY_PACKAGE_CONFIG": "default",
75+
"SOME_API_ENDPOINT": "https://api.example.com"
76+
}
77+
```
78+
79+
To include this in your package, add to your build script:
80+
81+
```sh
82+
mkdir -p $PREFIX/etc/conda/env_vars.d
83+
cat > $PREFIX/etc/conda/env_vars.d/mypkg.json << 'EOF'
84+
{
85+
"MY_PKG_VAR": "some_value",
86+
"ANOTHER_VAR": "another_value"
87+
}
88+
EOF
89+
```
90+
91+
Or copy a pre-existing file:
92+
93+
```sh
94+
mkdir -p $PREFIX/etc/conda/env_vars.d
95+
cp $RECIPE_DIR/env_vars.json $PREFIX/etc/conda/env_vars.d/mypkg.json
96+
```
97+
98+
### Using the $PREFIX path in environment variables
99+
100+
Since JSON files contain static values, you need to expand the `$PREFIX` environment variable when creating the file during the build. Use a heredoc **without** quotes to allow bash variable expansion:
101+
102+
```sh
103+
mkdir -p $PREFIX/etc/conda/env_vars.d
104+
cat > $PREFIX/etc/conda/env_vars.d/mypkg.json << EOF
105+
{
106+
"MY_PKG_DATA_DIR": "$PREFIX/share/mypkg",
107+
"MY_PKG_CONFIG": "$PREFIX/etc/mypkg.conf"
108+
}
109+
EOF
110+
```
111+
112+
This will write the actual prefix path (e.g., `/home/user/host_env_placehold_plachold...`) into the JSON file, which be replaced at installation time.
113+
114+
On Windows (in `bld.bat`), use `%PREFIX%`:
115+
116+
```batch
117+
mkdir %PREFIX%\etc\conda\env_vars.d
118+
echo {"MY_PKG_DATA_DIR": "%PREFIX%\\share\\mypkg"} > %PREFIX%\etc\conda\env_vars.d\mypkg.json
119+
```
120+
121+
!!! tip "Quoted vs unquoted heredocs"
122+
Note the difference: `<< 'EOF'` (quoted) prevents variable expansion, while `<< EOF` (unquoted) allows bash to expand `$PREFIX` before writing the file.
123+
124+
### Complete directory structure
125+
126+
Here's the complete structure of activation-related files in a conda environment:
127+
128+
```text
129+
<conda-prefix>/
130+
├── bin/ # Executables (Unix)
131+
├── Scripts/ # Executables (Windows)
132+
├── etc/conda/
133+
│ ├── activate.d/ # Activation scripts (shell-specific)
134+
│ │ ├── 10-pkg1-activate.sh # Bash/Zsh script
135+
│ │ ├── 10-pkg1-activate.bat # Cmd.exe script
136+
│ │ └── 10-pkg1-activate.ps1 # PowerShell script
137+
│ ├── deactivate.d/ # Deactivation scripts
138+
│ │ ├── 10-pkg1-deactivate.sh
139+
│ │ └── 10-pkg1-deactivate.bat
140+
│ └── env_vars.d/ # Package environment variables (JSON)
141+
│ ├── pkg1.json
142+
│ ├── pkg2.json
143+
│ └── zzz-override.json # Loaded last due to filename
144+
└── conda-meta/
145+
├── pkg1-1.0.0-h1234.json # Package metadata
146+
├── pkg2-2.0.0-h5678.json
147+
└── state # Environment-level state (JSON)
148+
```
149+
150+
### Processing order
151+
152+
When an environment is activated, the activation system:
153+
154+
1. Reads all `.json` files from `etc/conda/env_vars.d/` in **lexicographical order**
155+
2. Reads the `conda-meta/state` file (if it exists)
156+
3. Merges all variables, with later files overriding earlier ones
157+
158+
This means:
159+
160+
- If multiple packages define the same variable, the package whose filename comes later alphabetically will win
161+
- You can prefix filenames with numbers (e.g., `00-base.json`, `50-mypkg.json`) to control priority
162+
- The `conda-meta/state` file always has the highest priority
163+
164+
29165
## Post-link and pre-unlink scripts
30166

31167
The `post-link` and `pre-unlink` scripts are executed when the package is installed or uninstalled. They are both heavily discouraged but implemented for compatibility with conda in `rattler-build` since version 0.17.

0 commit comments

Comments
 (0)