Skip to content

modular services: porting legacy modules consisting of multiple services #490688

Open

Description

@KiaraGrouwstra

our modular services implementations have so far ported over service modules that consisted of a single systemd service.
many of our existing service modules however are not restricted to this, instead making further use of ancilliary services, such as reverse proxies (say services.nginx) or databases (say services.postgresql).
as such, to scale out or concept of modular services to cover, or even end up superseding, our incumbent service modules, we would need a method to elegantly handle such multi-service service modules as well.


to get feedback on my current train of thought there, i think we could go about this by giving each used service (e.g. an application's services.postgresql) its own modular service, so that e.g. the default modular service is the application itself, its postgresql modular service handles the services.postgresql bits, while a compatibility layer (see #475372) could wrap these all together, by default.

now, the challenge here is that modular services have options at the service level, whereas traditional service modules have a shared option set handling anything required (main service, ancilliary services, other NixOS configuration).

while we could attempt to explicitly configure everything for each service even accepting options duplicated across sibling services, the problem here is that some of the info normally coordinated across services may be calculated in non-trivial ways, whereas in case info flows in both directions we may also need to deal with preventing infinite recursion.

prior art on addressing scenarios like that seems using contracts (#506343).
(also works across nodes.)

contracts offer a mechanism that, from the perspective of the application service making a request:

  • takes input (request parameters)
  • yields outputs (result)
  • may generate configuration on the side of the provider, rendering it effectful (unlike nix functions, which yield outputs from inputs without causing such side-effects for our purposes here)
  • .. all without automatically invoking things

note that, while contracts' original motivation involved abstracting over providers (e.g. have a reverse proxy contract rather than something specific to just e.g. nginx), solving our current challenge i think would not require such abstractions yet necessarily.

further, while the contracts PR uses a dedicated folder to gather contract implementations, i think in my idea here, in case of services offering multiple systemd services specific to the service (say a package offering an API service and a web service with some interaction in configuration), we could end up with contracts specific to an application, in which case it could be sensible to bundle such code with the modular rather than with general-use contracts.

as an extra note here to inform alternative ideas, contracts' round-trip mechanism should be overkill when not each of input/output/configuration are needed:

  • don't need configuration: should be able to settle for a nix function
  • don't need both input and output: should be able to settle for module system (possibly with custom types)

Activity

  1. KiaraGrouwstra commented on Feb 19, 2026

    @KiaraGrouwstra
    ContributorAuthor

    a way to map from traditional to modular services would be maintaining the old cross-service option set to coordinate what each of them need and instantiate the modular services.
    that might make for lower-hanging fruit for compatibility, though eventually supporting a distributed setting could require contracts still.

  2. KiaraGrouwstra commented on Feb 19, 2026

    @KiaraGrouwstra
    ContributorAuthor

    previous work on coordinating information across distributed services also includes clan's exports

  3. nixos-discourse commented on Feb 24, 2026

    @nixos-discourse
  4. KiaraGrouwstra commented on Feb 24, 2026

    @KiaraGrouwstra
    ContributorAuthor

    one simple idea might be to request external modules like:

    # a modular service gets a metadata thing like this
    serviceRequests = {
      # match name as used in service equivalent like services.postgresql
      postgresql = {
        # let's say we may make multiple (instance?) requests, and this is one
        default = {
          # match the structure of services.postgresql
          ensureDatabases = {
            # ...
          };
        };
      };
    };

    the idea would be that this in itself would not yet do anything, instead exposing relevant info to the service consumer so that they might load such requested services as desired (same environment, different environment, ...).

  5. KiaraGrouwstra commented on Apr 3, 2026

    @KiaraGrouwstra
    ContributorAuthor

    i added a test nixos/tests/contracts/database-modular-services.nix demonstrating this principle at #506343.

  6. KiaraGrouwstra commented on Apr 28, 2026

    @KiaraGrouwstra
    ContributorAuthor

    from matrix:

    kiara:

    a modular service is expected to actually be scoped to one (systemd) service, which doesn't fit with our previous setting of like 'web service A can trigger db service B'

    roberth:

    doesn't have to be. they can be nested groupings of services as well. This nesting could be used as a "poor man's dependency wiring system", as a module that defines multiple has the capability to set up those dependencies, but an alternative like contracts is still desirable, so that e.g. unique db per app does not become a requirement

  7. added
    6.topic: package configuration modules
    on Jul 23, 2026
  8. KiaraGrouwstra commented on Jul 23, 2026

    @KiaraGrouwstra
    ContributorAuthor

    (earlier i posted this on matrix:)

    i might have mentioned i considered modular services' child services feature something i had mixed feelings on, figuring that at face value i considered it closer to nixos services' way of doing things than to keeping distributing services across nodes a first-class citizen the way i'd like. i now managed to do a PoC on defining services on the nixpkgs side in a somewhat more logistics-agnostics way, such that the consumer could kind of rearrange things the way they prefer. i'll admit this builds upon contracts (this wasn't a 1-commit branch), but at least i now maybe got something tangible to show for how i generally felt on direction there, i guess.
    KiaraGrouwstra@c01bd01

    i considered having the rearrangements generated as well, but module system wanted separate evals to like prevent some infinite recursions. so maybe room for improvement still, but contracts-wise, the idea would be to have services client-style indicate what they'd want from lower-level services they'd consume. for the sake of simplicity, this example sorta cheated there by pretending the whole db config (i.e. also 'server side') is wholesale configured as part of that as well (on top of.. not being a real db).
    but yeah, i hope the idea sorta came across.

  9. nixos-discourse commented on Jul 24, 2026

    @nixos-discourse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    6.topic: package configuration modules

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions