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"; description = "NixOS modules for matrix related services";
outputs = { self }: { inputs = {
nixosModules = { nixpkgs-lib.url = github:nix-community/nixpkgs.lib;
synapse = import ./synapse-module;
}; };
outputs = { self, nixpkgs-lib }: {
nixosModules = {
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, ... }: { pkgs, lib, config, ... }:
let let
cfg = config.services.matrix-synapse-next; cfg = config.services.matrix-synapse-next;
@ -31,7 +32,7 @@ in
imports = [ imports = [
./nginx.nix ./nginx.nix
(import ./workers.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-synapse-common-config,
matrix-lib,
pluginsEnv, pluginsEnv,
throw', throw',
format format
@ -22,15 +23,7 @@
genAttrs' = items: f: g: builtins.listToAttrs (map (i: lib.nameValuePair (f i) (g i)) items); 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; mainReplicationListener = matrix-lib.firstListenerOfType "replication" cfg.settings.listeners;
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;
in { in {
# See https://github.com/matrix-org/synapse/blob/develop/docs/workers.md for more info # See https://github.com/matrix-org/synapse/blob/develop/docs/workers.md for more info
options.services.matrix-synapse-next.workers = let options.services.matrix-synapse-next.workers = let
@ -183,17 +176,20 @@ in {
in { in {
mainReplicationHost = mkOption { mainReplicationHost = mkOption {
type = types.str; type = types.str;
default = default = let
if builtins.elem (listenerHost mainReplicationListener) [ "0.0.0.0" "::" ] 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" then "127.0.0.1"
else listenerHost mainReplicationListener; else host;
# TODO: add defaultText # TODO: add defaultText
description = "Host of the main synapse instance's replication listener"; description = "Host of the main synapse instance's replication listener";
}; };
mainReplicationPort = mkOption { mainReplicationPort = mkOption {
type = types.port; type = types.port;
default = listenerPort mainReplicationListener; default = mainReplicationListener.port;
# TODO: add defaultText # TODO: add defaultText
description = "Port for the main synapse instance's replication listener"; description = "Port for the main synapse instance's replication listener";
}; };
@ -258,11 +254,8 @@ in {
instance_map = genAttrs' (lib.lists.range 1 wcfg.eventPersisters) instance_map = genAttrs' (lib.lists.range 1 wcfg.eventPersisters)
(i: "auto-event-persist${toString i}") (i: "auto-event-persist${toString i}")
(i: let (i: let
wRL = firstListenerOfType "replication" wcfg.instances."auto-event-persist${toString i}".settings.worker_listeners; wRL = matrix-lib.firstListenerOfType "replication" wcfg.instances."auto-event-persist${toString i}".settings.worker_listeners;
in { in matrix-lib.connectionInfo wRL);
host = listenerHost wRL;
port = listenerPort wRL;
});
stream_writers.events = stream_writers.events =
mkIf (wcfg.eventPersisters > 0) mkIf (wcfg.eventPersisters > 0)