From f5f9595a29b11a4d6d7e1691504f592315e3d6a1 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 19 Nov 2026 19:06:58 +0000 Subject: continuum: add domain An instance with a domain gets an nginx virtual host passing requests on to its HTTP API, over a Unix socket in its runtime directory. The socket is the instance's group's (continuum-, made for it), which nginx's user joins; continuum makes it 0660, and the directory is 0750. The instance checks who users are itself, so requests pass on as they come. --- modules/continuum.nix | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/continuum.nix b/modules/continuum.nix index 4d8c063..484350b 100644 --- a/modules/continuum.nix +++ b/modules/continuum.nix @@ -20,6 +20,19 @@ // lib.optionalAttrs (settings.tor.enable or false) { tor.state-dir = "${dir}/tor"; }; + # The socket of an instance with a domain, where nginx reaches its HTTP API. + socket = name: "/run/continuum-${name}/api.sock"; + + # The settings as written: their own, the defaults under them, and the + # socket for nginx added to bind-addr. + settingsOf = name: instance: + lib.recursiveUpdate (stateFiles instance.settings) instance.settings + // lib.optionalAttrs (instance.domain != null) { + bind-addr = + lib.toList (instance.settings.bind-addr or []) ++ [ "unix:${socket name}" ]; + }; + + withDomain = lib.filterAttrs (_: instance: instance.domain != null) cfg.instances; in { options.hrm.continuum.instances = lib.mkOption { default = {}; @@ -38,6 +51,17 @@ in { example = { htpasswd = "/var/src/secrets/continuum.htpasswd"; }; type = lib.types.attrsOf mylib.types.absolute-pathname; }; + options.domain = lib.mkOption { + default = null; + description = '' + A name for an nginx virtual host passing requests on to the + instance's HTTP API, over a Unix socket only nginx and the instance + can use. The instance checks who users are itself (htpasswd), so + requests pass on as they come. + ''; + example = "kartei.continuum.ne.r"; + type = lib.types.nullOr lib.types.str; + }; options.user = lib.mkOption { default = null; description = '' @@ -81,8 +105,7 @@ in { after = [ "network-online.target" ]; serviceConfig = { ExecStart = "${pkgs.continuum}/bin/continuum --config ${ - format.generate "continuum-${name}.toml" - (lib.recursiveUpdate (stateFiles instance.settings) instance.settings) + format.generate "continuum-${name}.toml" (settingsOf name instance) }"; # continuum exits with status 0 when a transport stops, too. Restart = "always"; @@ -92,6 +115,10 @@ in { User = lib.mkIf (instance.user != null) instance.user; StateDirectory = "continuum-${name}"; StateDirectoryMode = "0700"; + # The socket's directory and group let nginx through. + RuntimeDirectory = lib.mkIf (instance.domain != null) "continuum-${name}"; + RuntimeDirectoryMode = lib.mkIf (instance.domain != null) "0750"; + Group = lib.mkIf (instance.domain != null) "continuum-${name}"; LoadCredential = lib.mapAttrsToList (name: path: "${name}:${path}") instance.credentials; @@ -107,4 +134,27 @@ in { }; } ) cfg.instances; + + config.users.groups = lib.mapAttrs' (name: _: + lib.nameValuePair "continuum-${name}" {} + ) withDomain; + + config.users.users = lib.mkIf (withDomain != {}) { + ${config.services.nginx.user}.extraGroups = + lib.mapAttrsToList (name: _: "continuum-${name}") withDomain; + }; + + config.services.nginx = lib.mkIf (withDomain != {}) { + enable = true; + virtualHosts = lib.mapAttrs' (name: instance: + lib.nameValuePair instance.domain { + locations."/" = { + proxyPass = "http://unix:${socket name}"; + extraConfig = '' + proxy_set_header X-Forwarded-Proto $scheme; + ''; + }; + } + ) withDomain; + }; } -- cgit v1.3.1