diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e8d4a00 --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix index dd26c2f..39d57d3 100644 --- a/flake.nix +++ b/flake.nix @@ -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; }; }; } diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..217accd --- /dev/null +++ b/lib.nix @@ -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; + +} diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 14405c3..3c7c497 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -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; }) ]; diff --git a/synapse-module/workers.nix b/synapse-module/workers.nix index 6cdbd9a..0f17558 100644 --- a/synapse-module/workers.nix +++ b/synapse-module/workers.nix @@ -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)