You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Preferences provides a simple package configuration store; packages can
store arbitrary configurations into `Dict` objects that get serialized
into their active `Project.toml`. Depot-wide preferences can also be
stored within the `prefs` folder of a Julia depot, allowing for default
values to be passed down to new environments from the system admin.
Pkg's preferences API requires at least Julia 1.6.
5
+
6
+
`Pkg` Preferences support embedding a simple `Dict` of metadata for a package on a per-project or per-depot basis. These preferences allow for packages to set simple, persistent pieces of data that the user has selected, that can persist across multiple versions of a package.
7
+
8
+
## API Overview
9
+
10
+
Usage is performed primarily through the `@load_preferences`, `@save_preferences` and `@modify_preferences` macros. These macros will auto-detect the UUID of the calling package, (throwing an error if the calling module does not belong to a package) the function forms can be used to load, save or modify preferences belonging to another package.
11
+
12
+
Example usage:
13
+
14
+
```julia
15
+
using Pkg.Preferences
16
+
17
+
functionget_preferred_backend()
18
+
prefs =@load_preferences()
19
+
returnget(prefs, "backend", "native")
20
+
end
21
+
22
+
functionset_backend(new_backend)
23
+
@modify_preferences!() do prefs
24
+
prefs["backend"] = new_backend
25
+
end
26
+
end
27
+
```
28
+
29
+
By default, preferences are stored within the `Project.toml` file of the currently-active project, and as such all new projects will start from a blank state, with all preferences being un-set.
30
+
Package authors that wish to have a default value set for their preferences should use the `get(prefs, key, default)` pattern as shown in the code example above.
31
+
If a system administrator wishes to provide a default value for new environments on a machine, they may create a depot-wide default value by saving preferences for a particular UUID targeting a particular depot:
32
+
33
+
```julia
34
+
using Pkg.Preferences, Foo
35
+
# We want Foo to default to a certain library on this machine,
36
+
# save that as a depot-wide preference to our `~/.julia` depot
Depot-wide preferences are overridden by preferences stored wtihin `Project.toml` files, and all preferences (including those inherited from depot-wide preferences) are stored concretely within `Project.toml` files.
44
+
This means that depot-wide preferences will serve to provide default values for new projects/environments, but once a project has
45
+
saved its preferences at all, they are effectively decoupled.
46
+
This is an intentional design choice to maximize reproducibility and to continue to support the `Project.toml` as an independent archive.
47
+
48
+
For a full listing of docstrings and methods, see the [Preferences Reference](@ref) section.
0 commit comments