Skip to content

Commit 518f57e

Browse files
committed
feat(chezmoi): add env_vars option for setting fixed environment variables
Adds a semicolon-separated KEY=VALUE option that appends export statements to /etc/bash.bashrc and /etc/zsh/zshenv at build time, following the same pattern as persist-shell-history.
1 parent 97d48e1 commit 518f57e

5 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/chezmoi/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This feature does not install Atuin, Starship, or shell themes. Those tools must
1717

1818
To persist Atuin history across rebuilds, combine this feature with [persist-shell-history](../persist-shell-history/).
1919

20+
If `env_vars` is set, the key-value pairs are exported as environment variables in every shell session (bash and zsh) via the system-wide shell config files.
21+
2022
## Example Usage
2123

2224
```json
@@ -27,6 +29,17 @@ To persist Atuin history across rebuilds, combine this feature with [persist-she
2729
}
2830
```
2931

32+
## Example With Environment Variables
33+
34+
```json
35+
"features": {
36+
"ghcr.io/ckagerer/devcontainer-features/chezmoi:1": {
37+
"dotfiles_repo": "twpayne/dotfiles",
38+
"env_vars": "MY_VAR=hello;OTHER_VAR=world"
39+
}
40+
}
41+
```
42+
3043
## Example With Atuin
3144

3245
```json

src/chezmoi/devcontainer-feature.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"type": "boolean",
3535
"default": false,
3636
"description": "Ignore errors during setup and continue execution."
37+
},
38+
"env_vars": {
39+
"type": "string",
40+
"default": "",
41+
"description": "Semicolon-separated KEY=VALUE pairs to export as environment variables (e.g. MY_VAR=hello;OTHER=world)."
3742
}
3843
},
3944
"postCreateCommand": "/usr/local/share/chezmoi-atuin-init.sh",

src/chezmoi/install.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,24 @@ sed -i \
165165

166166
chmod 755 "$INIT_ATUIN_SCRIPT_PATH"
167167

168+
apply_env_vars() {
169+
[ -z "${ENV_VARS:-}" ] && return 0
170+
171+
if [ -f /etc/bash.bashrc ] && ! grep -q "# chezmoi-env-vars" /etc/bash.bashrc; then
172+
printf "\n# chezmoi-env-vars\n" >>/etc/bash.bashrc
173+
printf '%s' "${ENV_VARS}" | tr ';' '\n' | while IFS= read -r pair; do
174+
[ -n "${pair}" ] && printf 'export %s\n' "${pair}" >>/etc/bash.bashrc
175+
done
176+
fi
177+
178+
if [ -d /etc/zsh ] && ! grep -q "# chezmoi-env-vars" /etc/zsh/zshenv 2>/dev/null; then
179+
printf "\n# chezmoi-env-vars\n" >>/etc/zsh/zshenv
180+
printf '%s' "${ENV_VARS}" | tr ';' '\n' | while IFS= read -r pair; do
181+
[ -n "${pair}" ] && printf 'export %s\n' "${pair}" >>/etc/zsh/zshenv
182+
done
183+
fi
184+
}
185+
186+
apply_env_vars
187+
168188
echo "Done"

test/chezmoi/env_vars.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
set -e
3+
4+
source dev-container-features-test-lib
5+
6+
check "CHEZMOI_TEST_VAR exported in bash" bash -c 'source /etc/bash.bashrc && [ "${CHEZMOI_TEST_VAR}" = "hello" ]'
7+
check "CHEZMOI_OTHER_VAR exported in bash" bash -c 'source /etc/bash.bashrc && [ "${CHEZMOI_OTHER_VAR}" = "world" ]'
8+
9+
reportResults

test/chezmoi/scenarios.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
{
2+
"env_vars": {
3+
"image": "ubuntu:jammy",
4+
"features": {
5+
"ghcr.io/devcontainers/features/common-utils:1": {
6+
"installZsh": false,
7+
"installOhMyZsh": false,
8+
"upgradePackages": false,
9+
"username": "octocat"
10+
},
11+
"chezmoi": {
12+
"dotfiles_repo": "twpayne/dotfiles",
13+
"env_vars": "CHEZMOI_TEST_VAR=hello;CHEZMOI_OTHER_VAR=world"
14+
}
15+
},
16+
"remoteUser": "octocat"
17+
},
218
"test": {
319
"image": "ubuntu:jammy",
420
"features": {

0 commit comments

Comments
 (0)