Repository navigation
contracts: init - #506343
Draft
KiaraGrouwstra wants to merge 30 commits into
Draft
contracts: init#506343KiaraGrouwstra wants to merge 30 commits into
KiaraGrouwstra wants to merge 30 commits into
Conversation
KiaraGrouwstra
marked this pull request as ready for review
April 3, 2026 16:53
3 of 13 tasks
KiaraGrouwstra
force-pushed
the
contracts-automated
branch
2 times, most recently
from
April 3, 2026 17:23
47917e6 to
2d43014
Compare
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
KiaraGrouwstra
force-pushed
the
contracts-automated
branch
from
April 3, 2026 17:39
2d43014 to
262c8a1
Compare
KiaraGrouwstra
force-pushed
the
contracts-automated
branch
from
April 3, 2026 17:49
262c8a1 to
1c0ed60
Compare
Contributor
Author
|
KiaraGrouwstra
force-pushed
the
contracts-automated
branch
from
April 4, 2026 06:17
1c0ed60 to
c80a599
Compare
1 of 13 tasks
KiaraGrouwstra
force-pushed
the
contracts-automated
branch
5 times, most recently
from
April 4, 2026 09:45
331e0da to
47d0440
Compare
The provider wrote each secret in an activation script. An activation script can run before the owner of the secret exists. With `systemd-sysusers` or `userborn`, the users are created after activation, thus `chown` fails with `invalid user`. With the Perl user creation, the script ran after `users` only because activation scripts without dependencies run in alphabetical order. Now a oneshot service writes each secret. The service runs after `systemd-sysusers.service` and `userborn.service`, and before `sysinit.target`. Thus each secret exists before all services that have default dependencies, such as the consumers of the secret. The test now uses an owner and group that are not `root`. A new test does the same check with `userborn`. Without this change, the new test fails. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
The contract change renamed these options to `jwtSecretKey` and `sessionStoreKey`. Without an alias, configurations that set the old names fail to evaluate. Add `mkRenamedOptionModule` for both. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01C1cyFmsXJdW4oEaqPAdXBz
`types.path` accepts strings such as `"/run/secrets/stash"`, but `lib.isPath` is false for strings. Thus the setup script read `.output.path` from a string and evaluation failed. Select on `builtins.isAttrs` in a shared `secretPath` helper. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01C1cyFmsXJdW4oEaqPAdXBz
…sponse` `input` and `output` do not tell which side sets the value. With `request` and `response`, the consumer requests the file permissions and the provider responds with the file path. The same names apply on the consumer and on the provider, so the wiring reads as: ```nix testing.hardcoded-secret."stash-password".request = config.services.stash.passwordFile.request; services.stash.passwordFile.response = config.testing.hardcoded-secret."stash-password".response; ``` Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01C1cyFmsXJdW4oEaqPAdXBz
The stash consumer and the hardcoded-secret provider each declared the same `request` and `response` options. Move these declarations into the `interface` module of `lib/contracts/definitions/file-secrets.nix`, available as `lib.contracts.fileSecrets`. Each side imports that module into its submodule type and adds only its `default`, `defaultText` and `readOnly` values through option declaration merging. Thus a change to the contract occurs in one location, and the two sides cannot have different options. The contract type is in `lib` and not in `nixos`, so that each module system can use it. Next to `interface`, the file has `meta` with a description and the maintainers of the contract type. The `interface` module sets `_file`. Without it, the module system gives the options the location of the module that imports the interface. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01C1cyFmsXJdW4oEaqPAdXBz Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Before, each attribute of `testing.hardcoded-secret` was a secret. Thus the provider could not have options that apply to all of its secrets. Now the secrets are in `testing.hardcoded-secret.fileSecrets.<name>`. The new option `testing.hardcoded-secret.directory` sets the directory of the secret files. Its default is `/run/hardcodedsecrets`, which is the same directory as before. Also correct the example: it set `request.user`, which is not an option of the request. The example now sets `owner` and `group`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01C1cyFmsXJdW4oEaqPAdXBz
The options `request` and `response` come from the file secrets contract. The option `content` is specific to this provider. Put it in the `providerOptions` namespace, so that the options of the contract and the options of the provider are separate. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01C1cyFmsXJdW4oEaqPAdXBz
The description of `fileSecrets` did not tell when and how the provider writes each secret. Add this to the description, so that the option documentation has the behavior of the provider. Assisted-by: Claude:claude-opus-5-5
Add a chapter about contracts to the development part of the manual. The chapter tells about the file secrets contract, how consumers and providers use its interface, and how a user connects them. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
5 of 13 tasks
Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
The user can connect each consumer option to a provider entry manually: set the `request` on the provider, and set the `response` on the consumer. To change the provider, the user must change these definitions for each option. Add `lib.contract.module` (`lib/contracts/module.nix`). It declares `contractTypes` and `contracts`. For each contract type, `contracts.<type>` has these options: - `want`: the requests from consumers. Each entry imports the `interface` module of the contract type. - `providers.<provider>`: the option of the provider, for example `options.testing.hardcoded-secret.fileSecrets`. The value of this option has an entry with a `response` for each request. - `defaultProvider`: the provider that gets the requests, one of `providers`. - `instances.<name>`: the provider that gets the entry `<name>` of `want`. The default is `defaultProvider`. Set it to a different provider to override `defaultProvider` for one entry, or to `null` to connect the entry manually. - `providerRequests.<provider>`: the requests that a provider must fulfill. Routing compares the `loc` of the options, thus a provider can read its own entry. - `results`: the `response` from the provider for each entry in `want`. The module does not use NixOS options, so other module systems can import it. `nixos/modules/contracts/default.nix` imports `lib.contract.module` and adds `lib.contracts` to `contractTypes`. The hardcoded-secret provider now reads its requests from `providerRequests.hardcoded-secret` and registers its option in `providers`. Definitions from the user, such as `providerOptions.content`, merge with these requests. The contracts chapter of the manual now tells how to route requests, and shows a consumer module that sends its own requests. Routing is opt-in: the user selects a provider and sets the `response` of each consumer option from `results`. The chapter keeps the manual connection, and tells how `instances` overrides `defaultProvider`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Each entry of `contracts.<type>.providers` has two options: - `module`: the options of the provider. - `contract`: the path of the provider option in `module`. The default is `[ "<type>" ]`. The routing module finds the provider option from these two options. With `module`, a tool can find the other options of the provider, for example to make a configuration form for a provider. With `contract`, a provider can put its provider option at a different path. The tests make sure that the routing module finds the provider option in each of these forms: at the default path, at a different path, as `module` itself, and in the value of `module`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Each secret option of stash that uses the contract now sends its request to `contracts.fileSecrets.want."stash-<option>"`. Routing is opt-in. The secret options do not get a default `response` from `contracts.fileSecrets.results`, because a provider such as `testing.hardcoded-secret` is not safe for production. The user sets `contracts.fileSecrets.defaultProvider`, sets the `response` of each secret option from `results`, and sets the options of the provider. The test now uses this. The consumer module example in the contracts chapter is now a simplified version of stash. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Add `lib.contract.isInstance`. It tells if a value is a contract, that is an attribute set with a `response`. Use it when an option accepts a contract or a value without a contract, for example `types.oneOf [ types.path contractType ]`. The stash module uses it in `secretPath` and for its `want` entries. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
The file secrets behavior test wrote the request directly into the options of the provider and read the response from there. Thus the test did not use the contracts module, and it worked only with a provider that has the same option layout as `hardcoded-secret`. Now the test takes the name of a provider. It sets this provider as `defaultProvider`, sends the request through `contracts.fileSecrets.want` and reads the response from `contracts.fileSecrets.results`. Thus the test also checks the routing, and it can test each provider that registers in `contracts.fileSecrets.providers`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
A contract type can now get a new name with `lib.mkRenamedOptionModule` in `contracts`, as for other options:
contracts = { ... }: {
imports = [ (lib.mkRenamedOptionModule [ "number" ] [ "count" ]) ];
};
The entries of the old name go to `want` of the new name, and consumers read `results` of the new name.
Consumers of the old name do not change.
The new option `contracts.warnings` contains the warning of `lib.mkRenamedOptionModule`.
The NixOS module sends `contracts.warnings` to `warnings`.
Thus a contract type cannot have the name `warnings`.
The tests also make sure that the old name throws `no provider` when the new name has no provider.
The manual tells how to rename a contract type.
Assisted-by: Claude:claude-opus-5-5
Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Add `lib.types.nestedAttrsOf`, as a nested attribute set of a given type. Further add functions in `lib.attrsets` to process values of this type: - `isNestedAttrsLeaf` - `mapNestedAttrsWith` - `mapNestedAttrs'` - `concatMapNestedAttrsWith` - `concatMapNestedAttrs'` Note that recursion over this type may either require all or any of the leaf types to be present. While this type and its helpers are generic in function, these will later be used for abstracting over variable-depth nested attrsets used in name-spacing for contract instances. In that particular use-case, we may only presume for part of the leaf type's keys to be present. For an element type without options, `nestedAttrsOf` uses `elemType.check` to find the leaves, as `isNestedAttrsLeaf` does. Its `check` accepts properties such as `mkDefault` at each level, and checks their content, because `merge` applies the properties for each attribute. Signed-off-by: cinereal <[email protected]> Assisted-by: Claude:claude-opus-5-5
Before this change, each name in `contracts.<type>.want` had to be unique for all consumers, for example `stash-passwordFile`.
Now `want` is a `nestedAttrsOf`. Each consumer puts its entries in an attribute set with its own name, for example `want.stash.passwordFile`, and can use more levels, for example `want.<consumer>.db.primary`.
`requests`, `instances`, `providerRequests` and `results` have the same paths as `want`.
An attribute set that contains only names of interface options, such as `request`, is an entry. Other attribute sets are namespaces.
Empty namespaces, for example from `mkIf false`, do not go to a provider, because a provider reads `{ }` as an entry.
`testing.hardcoded-secret` uses the path of each entry, and not only its name, for the path of the secret file. Thus, two consumers can use the same option name.
Flat names at depth 1 continue to work.
Assisted-by: Claude:claude-opus-5-5
Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
The secrets of `services.stash` go in `contracts.fileSecrets.want.stash.<option>`, and not in `want.stash-<option>`. With `testing.hardcoded-secret`, the secret files are in `/run/hardcodedsecrets/stash/`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Add `mkContract` to each contract type. It makes the submodule type of a consumer from the `interface` of the contract type and a set of overrides, for example `request.owner.default`. This replaces the option declarations that a consumer must otherwise write to add a `default` or `readOnly` to an option of the interface. Move the options of a contract type to `lib.contract.definitionType`. `lib.contracts` uses this type, so that `lib.contracts.<type>.mkContract` is available outside of a module system. `mkContract` is read-only, thus NixOS sets only `meta` and `interface` in `contractTypes`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Use `lib.contracts.fileSecrets.mkContract` to make the type of the secret options. The type is the same as before, but the module does not declare the `request` submodule itself. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
`contracts.<type>.results` gives each entry the `request` from `want` and the `response` from the provider in one attribute set. A `response` attribute takes precedence over a `request` attribute with the same name. Thus a consumer reads a request value from the same place as the response, and a contract type does not need a `response` option that repeats a `request` option. If the code that writes an entry in `want` changes the request, for example to add a prefix to a name, the consumer gets the changed value. In the type from `mkContract` and in the entries of `want`, `response` also has the options of `request`. The new internal option `consumerInterface` of a contract type adds them. Thus a consumer can set its option from `results` and send that option to `want`. These options do not have the `default` and `apply` of the `request` options, because their value comes from the request in `want`. The provider type does not change, so a provider gives only the `response`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Add the function `mkProviderType` to each contract type. It makes the type of the option in which a provider gets its requests: a `nestedAttrsOf` submodule that imports the interface. - `overrides` adds attributes to the options of the interface, as in `mkContract`. - `providerOptions` declares the options of `providerOptions`. - `fulfill` makes the `response` from the `request`. `fulfill'` also gets the location `loc`, the last attribute name `name` and the configuration `instance` of the entry. Each attribute of the response from `fulfill` has `mkDefault` priority, thus a definition of a `response` option replaces it. The priority is on each attribute and not on the full response, because the module system ignores a `mkDefault` definition of a submodule if there is a definition with a higher priority. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
Use `lib.contracts.fileSecrets.mkProviderType` to make the type of `testing.hardcoded-secret.fileSecrets`. `fulfill'` makes the path of each secret file from the location of its entry. The configuration of the VM tests does not change. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
A contract type can now rename an option of `request`. The new option `requestAliases` contains modules that add aliases to `request`, for example `lib.mkRenamedOptionModule [ "oldValue" ] [ "value" ]`. The internal option `consumerInterface` now also has these aliases in `request`. `mkContract` and the entries in `want` use it, thus consumers can set an alias. The `request` submodule of `consumerInterface` has an internal option `warnings`, thus `lib.mkRenamedOptionModule` can add its warning there. The new read-only option `contracts.<type>.warnings` collects these warnings from the entries in `want`. `contracts.warnings` also contains them, thus in NixOS they go to `warnings`. The value of `request` keeps only the `request` options of `interface` and `warnings`. Thus a consumer can forward its `request` to `want`, and the aliases do not apply a second time. `requests`, `providerRequests` and `results` also remove `warnings`, thus providers get only the `request` options of `interface`, and the `request` in `results` does not have the aliases. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
A provider of one contract type can also be a consumer of a different contract type. The test defines a `databaseConnection` contract type. `pgProvider` fulfills its requests, and requests a credential file with the `fileSecrets` contract. The connection string contains the path of the credential file. The test reads the file at that path, thus it checks the full chain at run time. The manual now describes chaining, and tells that a configuration can define a contract type in `contractTypes`. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
`contracts.<type>.defaultProvider` is a reference to the option of a provider, for example `config.contracts.fileSecrets.providers.hardcoded-secret`. The new option `contracts.<type>.defaultProviderName` selects the provider by its name in `providers`, for example `"hardcoded-secret"`, and sets the default of `defaultProvider`. Its type is an enum of the names in `providers`, thus an incorrect name gives an error. Assisted-by: Claude:claude-opus-5-5
Each NixOS node has its own `contracts`. Tell how to connect a consumer on one node to a provider on a different node: the provider node gets the `requests` of the consumer node in `want`, and the consumer node uses the default provider of the provider node as its default provider. The consumer module and the provider module do not change. Add a test in `lib/tests/modules.sh` that connects two module evaluations in this way. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_016rruTfmDuCSj848uMGiiYW
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements contracts (NixOS/rfcs#189). This work builds on the work of @ibizaman (and earlier work by @fricklerhandwerk).
Closes NixOS/rfcs#189.
For more information, see the manual chapter or the options reference.
Supersedes: #432529, #485453, #495303, #500287.
Disclaimer: I used a coding agent to get the work to this state.
Use cases
Contents
A contract lets a consumer request a resource, for example a secret file, without a dependency on one specific provider. The consumer sends a
request, and the provider sends back aresponse.lib.contracts: the contract types of nixpkgs. This PR adds one contract type,fileSecrets(request:mode,owner,group;response:path). Each contract type hasmetaand aninterfacemodule. It also hasmkContract(the option type for a consumer),mkProviderType(the option type for a provider) andrequestAliases(to renamerequestoptions).lib.contract.module: the routing module. It can be imported into each module system. For each contract type it has these options:contracts.<type>.want: the requests of the consumers, atwant.<consumer>.<option>, nested to any depth.contracts.<type>.providers.<name>: where to find the provider option of each provider:module(the options of the provider) andcontract(the path of the provider option inmodule, by default the name of the contract type). Withmodule, a tool can also find the other options of the provider, for example to make a configuration form.contracts.<type>.defaultProviderorcontracts.<type>.defaultProviderName: the provider that gets the requests.contracts.<type>.instances: the provider of each request. This replacesdefaultProviderfor one request.contracts.<type>.providerRequests: the requests that each provider must fulfill.contracts.<type>.results: therequestand theresponseof each entry inwant.nixos/modules/contracts: imports the routing module into NixOS.nixos/modules/testing/hardcoded-secret.nix: afileSecretsprovider for tests. It is not safe for production.services.stash: the first consumer. Its secret options accept a path or a contract. Contracts are opt-in: the user sets theresponsefromresults.lib.types.nestedAttrsOfand nested helpers inlib.attrsets, for the nestedwantentries.lib/tests/modules.sh, and NixOS tests innixosTests.contracts. One module test shows contracts across two nodes.Differences from the RFC
consumer.provider = config.services.restic...), and the provider links back to the consumer (provider.consumer = config.services.nextcloud...). Here, consumers send their requests tocontracts.<type>.want, and providers register incontracts.<type>.providers.defaultProviderselects the provider for all requests, andinstancescan select a different provider for one request. Thus the links go in one direction only, and the "dual link" problem of the RFC does not occur.lib/contracts/module.nixdoes not use NixOS options, so that other systems can use contracts, for example home-manager, nix-darwin or NixBSD.contracts.<name>withdeferredModuletypes. Here, the contract types (contractTypes) are separate from the requests (contracts). The contract types of nixpkgs are inlib.contracts, thus documentation builds in the sandbox can use them. Projects outside nixpkgs can add contract types inconfig.contractTypes(without this documentation).inputandoutput. This PR usesrequestandresponse. These names show the direction: a consumer requests, and a provider responds.requestis also the name in SelfHostBlocks.Tests to run on changes
bash lib/tests/modules.shnix-build -A nixosTests.contractsnix-build -A stash.passthru.tests.stashnix-build nixos/release.nix -A manualHTML.x86_64-linuxNext steps
These changes are not breaking, thus they can come after this PR:
Things done
passthru.tests.nixpkgs-reviewon this PR. See nixpkgs-review usage../result/bin/.Assisted-by: Claude:claude-opus-5-5