Skip to content

Commit 5a635ac

Browse files
committed
Added way to set Firefox preferences via environment variables.
1 parent 3b92cc2 commit 5a635ac

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Mozilla Firefox is a free and open-source web browser developed by Mozilla Found
3939
* [Shell Access](#shell-access)
4040
* [Increasing Shared Memory Size](#increasing-shared-memory-size)
4141
* [Sound Support](#sound-support)
42+
* [Setting Firefox Preferences Via Environment Variables](#setting-firefox-preferences-via-environment-variables)
4243
* [Troubleshooting](#troubleshooting)
4344
* [Crashes](#crashes)
4445
* [Support or Contact](#support-or-contact)
@@ -460,6 +461,42 @@ For Firefox to be able to use the audio device available on
460461
the host, `/dev/snd` must be exposed to the container by adding the
461462
`--device /dev/snd` parameter to the `docker run` command.
462463

464+
## Setting Firefox Preferences Via Environment Variables
465+
466+
Firefox preferences can be set via environment variables
467+
passed to the containter. During the startup, a script process all these
468+
variables and modify the preference file accordingly.
469+
470+
The name of the environment variable must start with `FF_PREF_`, followed by a
471+
string of your choice. For example, `FF_PREF_MY_PREF` is a valid name.
472+
473+
The content of the variable should be in the format `NAME=VAL`, where `NAME` is
474+
the name of the preference (as found in the `about:config` page) and `VAL` is
475+
its value. A value can be one of the following type:
476+
- string
477+
- integer
478+
- boolean
479+
It is important to note that a value of type `string` should be surrounded by
480+
double quotes. Other types don't need them.
481+
482+
For example, to set the `network.proxy.http` preference, one would pass the
483+
environment variable to the container by adding the following argument to the
484+
`docker run` command:
485+
486+
```
487+
-e "FF_PREF_HTTP_PROXY=network.proxy.http=\"proxy.example.com\""
488+
```
489+
490+
If a preference needs to be *removed*, its value should be set to `UNSET`. For
491+
example:
492+
493+
```
494+
-e "FF_PREF_HTTP_PROXY=network.proxy.http=UNSET
495+
```
496+
497+
**NOTE**: This is an advanced usage and it is recommended to set preferences
498+
via Firefox directly.
499+
463500
## Troubleshooting
464501

465502
### Crashes

appdefs.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@ the host, `/dev/snd` must be exposed to the container by adding the
4949
`--device /dev/snd` parameter to the `docker run` command.
5050
</content>
5151
</section>
52+
<section>
53+
<title level="2">Setting {{ defs.app.friendly_name }} Preferences Via Environment Variables</title>
54+
<content>
55+
{{ defs.app.friendly_name }} preferences can be set via environment variables
56+
passed to the containter. During the startup, a script process all these
57+
variables and modify the preference file accordingly.
58+
59+
The name of the environment variable must start with `FF_PREF_`, followed by a
60+
string of your choice. For example, `FF_PREF_MY_PREF` is a valid name.
61+
62+
The content of the variable should be in the format `NAME=VAL`, where `NAME` is
63+
the name of the preference (as found in the `about:config` page) and `VAL` is
64+
its value. A value can be one of the following type:
65+
- string
66+
- integer
67+
- boolean
68+
It is important to note that a value of type `string` should be surrounded by
69+
double quotes. Other types don't need them.
70+
71+
For example, to set the `network.proxy.http` preference, one would pass the
72+
environment variable to the container by adding the following argument to the
73+
`docker run` command:
74+
75+
```
76+
-e "FF_PREF_HTTP_PROXY=network.proxy.http=\"proxy.example.com\""
77+
```
78+
79+
If a preference needs to be *removed*, its value should be set to `UNSET`. For
80+
example:
81+
82+
```
83+
-e "FF_PREF_HTTP_PROXY=network.proxy.http=UNSET
84+
```
85+
86+
**NOTE**: This is an advanced usage and it is recommended to set preferences
87+
via {{ defs.app.friendly_name }} directly.
88+
</content>
89+
</section>
5290
<section>
5391
<title level="2">Troubleshooting</title>
5492
<content/>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/with-contenv sh
2+
3+
set -e
4+
5+
log() {
6+
echo "[cont-init.d] $(basename $0): $*"
7+
}
8+
9+
PREF_FILE="${1:-/config/profile/prefs.js}"
10+
11+
if [ -z "$PREF_FILE" ]; then
12+
log "ERROR: Preference file not set."
13+
exit 1
14+
fi
15+
16+
mkdir -p "$(dirname "$PREF_FILE")"
17+
[ -f "$PREF_FILE" ] || touch "$PREF_FILE"
18+
19+
env | grep "^FF_PREF_" | while read ENV
20+
do
21+
ENAME="$(echo "$ENV" | cut -d '=' -f1)"
22+
EVAL="$(echo "$ENV" | cut -d '=' -f2-)"
23+
24+
if [ -z "$EVAL" ]; then
25+
log "Skipping environment variable '$ENAME': no value set."
26+
continue
27+
fi
28+
29+
if echo "$EVAL" | grep -q "="; then
30+
PNAME="$(echo "$EVAL" | cut -d '=' -f1)"
31+
PVAL="$(echo "$EVAL" | cut -d '=' -f2-)"
32+
[ -n "$PVAL" ] || PVAL='""'
33+
else
34+
PNAME="$EVAL"
35+
PVAL='""'
36+
fi
37+
38+
if [ "$PVAL" = "UNSET" ]; then
39+
log "Removing preference '$PNAME'..."
40+
sed -i "/user_pref(\"$PNAME\",.*);/d" "$PREF_FILE"
41+
elif grep -q "user_pref(\"$PNAME\"," "$PREF_FILE"; then
42+
log "Setting preference '$PNAME'..."
43+
sed -i "s/user_pref(\"$PNAME\",.*);/user_pref(\"$PNAME\", $PVAL);/" "$PREF_FILE"
44+
else
45+
log "Setting new preference '$PNAME'..."
46+
echo "user_pref(\"$PNAME\", $PVAL);" >> "$PREF_FILE"
47+
fi
48+
done

0 commit comments

Comments
 (0)