introduce matrix-lib

This commit is contained in:
Daniel Lovbrotte Olsen 2023-01-20 07:53:01 +01:00
parent 5ef8873997
commit 07e95170e8
5 changed files with 66 additions and 21 deletions

26
flake.lock Normal file
View File

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs-lib": {
"locked": {
"lastModified": 1673743903,
"narHash": "sha256-sloY6KYyVOozJ1CkbgJPpZ99TKIjIvM+04V48C04sMQ=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "7555e2dfcbac1533f047021f1744ac8871150f9f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
}
}
},
"root": "root",
"version": 7
}

View File

@ -1,9 +1,14 @@
{
description = "NixOS modules for matrix related services";
outputs = { self }: {
inputs = {
nixpkgs-lib.url = github:nix-community/nixpkgs.lib;
};
outputs = { self, nixpkgs-lib }: {
nixosModules = {
synapse = import ./synapse-module;
synapse = import ./synapse-module { matrix-lib = self.lib; };
};
lib = import ./lib.nix { lib = nixpkgs-lib.lib; };
};
}

20
lib.nix Normal file
View File

@ -0,0 +1,20 @@
{ lib }:
rec {
# checks if given listener configuration has type as a resource
isListenerType = type: l: lib.any (r: lib.any (n: n == type) r.names) l.resources;
# Get the first listener that includes the given resource from worker
firstListenerOfType = type: ls: lib.lists.findFirst (isListenerType type)
(lib.throw "No listener with resource: ${type} configured")
ls;
# Get an attrset of the host and port from a listener
connectionInfo = l: {
host = lib.head l.bind_addresses;
port = l.port;
};
# Get an attrset of the host and port from a worker given a type
workerConnectionResource = r: w: let
l = firstListenerOfType r w.settings.worker_listeners;
in connectionInfo l;
}

View File

@ -1,3 +1,4 @@
{ matrix-lib }:
{ pkgs, lib, config, ... }:
let
cfg = config.services.matrix-synapse-next;
@ -31,7 +32,7 @@ in
imports = [
./nginx.nix
(import ./workers.nix {
inherit throw' format matrix-synapse-common-config pluginsEnv;
inherit matrix-lib throw' format matrix-synapse-common-config pluginsEnv;
})
];

View File

@ -1,4 +1,5 @@
{ matrix-synapse-common-config,
matrix-lib,
pluginsEnv,
throw',
format
@ -22,15 +23,7 @@
genAttrs' = items: f: g: builtins.listToAttrs (map (i: lib.nameValuePair (f i) (g i)) items);
isListenerType = type: l: lib.any (r: lib.any (n: n == type) r.names) l.resources;
firstListenerOfType = type: w: lib.lists.findFirst (isListenerType type)
(throw' "No listener with resource: ${type} configured")
w.settings.listeners;
listenerHost = l: builtins.head l.bind_addresses;
listenerPort = l: l.port;
socketAddressOfType = type: w: let l = firstListenerOfType type w; in "${listenerHost l}:${listenerPort l}";
mainReplicationListener = firstListenerOfType "replication" cfg;
mainReplicationListener = matrix-lib.firstListenerOfType "replication" cfg.settings.listeners;
in {
# See https://github.com/matrix-org/synapse/blob/develop/docs/workers.md for more info
options.services.matrix-synapse-next.workers = let
@ -183,17 +176,20 @@ in {
in {
mainReplicationHost = mkOption {
type = types.str;
default =
if builtins.elem (listenerHost mainReplicationListener) [ "0.0.0.0" "::" ]
default = let
host = (matrix-lib.connectionInfo mainReplicationListener).host;
in
# To avoid connecting to 0.0.0.0 and so on
if builtins.elem host [ "0.0.0.0" "::" ]
then "127.0.0.1"
else listenerHost mainReplicationListener;
else host;
# TODO: add defaultText
description = "Host of the main synapse instance's replication listener";
};
mainReplicationPort = mkOption {
type = types.port;
default = listenerPort mainReplicationListener;
default = mainReplicationListener.port;
# TODO: add defaultText
description = "Port for the main synapse instance's replication listener";
};
@ -258,11 +254,8 @@ in {
instance_map = genAttrs' (lib.lists.range 1 wcfg.eventPersisters)
(i: "auto-event-persist${toString i}")
(i: let
wRL = firstListenerOfType "replication" wcfg.instances."auto-event-persist${toString i}".settings.worker_listeners;
in {
host = listenerHost wRL;
port = listenerPort wRL;
});
wRL = matrix-lib.firstListenerOfType "replication" wcfg.instances."auto-event-persist${toString i}".settings.worker_listeners;
in matrix-lib.connectionInfo wRL);
stream_writers.events =
mkIf (wcfg.eventPersisters > 0)