docs(inventory): improve extendSettings docs

This commit is contained in:
Johannes Kirschbauer
2025-04-08 15:46:13 +02:00
parent ce84c4cddb
commit 1b3ce7ebd4

View File

@@ -281,26 +281,29 @@ Next we need to define the settings and the behavior of these distinct roles.
} }
``` ```
## Vendoring settings for a machine ## Using values from a NixOS machine inside the module
!!! Example "Experimental Status" !!! Example "Experimental Status"
This feature is experimental and should be used with care. This feature is experimental and should be used with care.
Sometimes the *default* value depends on something within a machines `config`. Sometimes a settings value depends on something within a machines `config`.
Since the `interface` is defined completely machine-agnostic this means default values from a machine cannot be set in the traditional way. Since the `interface` is defined completely machine-agnostic this means values from a machine cannot be set in the traditional way.
The following example shows how to create a local instance of machine specific settings. The following example shows how to create a local instance of machine specific settings.
```nix title="someservice.nix" ```nix title="someservice.nix"
{ {
# Maps over all instances and produces one result per instance. # Maps over all instances and produces one result per instance.
perInstance = { instanceName, settings, machine, roles, ... }: { perInstance = { instanceName, extendSettings, machine, roles, ... }: {
nixosModule = { config, ... }: nixosModule = { config, ... }:
let let
# Calling settings via function application # Create new settings with
# will extend the underlying module # 'ipRanges' defaulting to 'config.network.ip.range' from this machine
localSettings = settings { ipRanges = lib.mkDefault config.network.ip.range; }; # This only works if there is no 'default' already.
localSettings = extendSettings {
ipRanges = lib.mkDefault config.network.ip.range;
};
in in
{ {
# ... # ...
@@ -311,4 +314,6 @@ The following example shows how to create a local instance of machine specific s
!!! Danger !!! Danger
`localSettings` are a local attribute. Other machines cannot access it. `localSettings` are a local attribute. Other machines cannot access it.
If settings vendoring is done. Accessing that settings attribute of another machine via the `settings` argument is considered **unsafe**. If calling extendSettings is done that doesn't change the original `settings` this means if a different machine tries to access i.e `roles.client.settings` it would *NOT* contain your changes.
Exposing the changed settings to other machines would come with a huge performance penalty, thats why we don't want to offer it.