make module instances

This commit is contained in:
Sierra 2024-04-18 21:47:07 -05:00
parent 7c606576fb
commit 4a7a6c3321

View file

@ -1,60 +1,36 @@
self: { config, lib, options, ... }:
with lib;
let cfg = config.casuallyblue.services.webring; in
{
options = {
casuallyblue.services.webring = {
enable = mkEnableOption "webring.casuallyblue.dev service";
port = mkOption {
type = types.port;
default = 33242;
example = 9001;
description = "The http port to listen on";
};
hostname = mkOption {
type = types.str;
default = "casuallyblue.dev";
description = "The hostname to proxy to the server";
};
acmeEmail = mkOption {
type = types.str;
default = "amylarane@gmail.com";
description = "The email to send certbot renewals to";
let
cfg = config.casuallyblue.services.webring;
instance = name: { port, vhost, ... }: let
user-name = if user == null then "webring-${name}" else user;
in {
users = {
${if user != null then null else user-name} = {
isSystemUser = true;
group = "nginx";
createHome = true;
home = "/var/lib/webring/${name}";
};
};
};
config = mkIf cfg.enable {
users.users.webring = {
createHome = true;
description = "site";
isSystemUser = true;
group = "users";
home = "/var/lib/webring";
};
services.nginx.virtualHosts."${cfg.hostname}" = {
vhosts.${vhost} = {
forceSSL = true;
enableACME = true;
locations."/" = {
proxyPass = "http://127.0.0.1:${builtins.toString cfg.port}";
proxyPass = "http://127.0.0.1:${builtins.toString port}";
proxyWebsockets = true;
};
};
security.acme.certs."${cfg.hostname}".email = cfg.acmeEmail;
systemd.services."casuallyblue-webring" = {
services."webring-${name}" = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = "webring";
User = "${user-name}";
Group = "users";
Restart = "on-failure";
WorkingDirectory = config.users.users.webring.home;
WorkingDirectory = config.users.users.${user-name}.home;
RestartSec = "30s";
Type = "simple";
};
@ -66,8 +42,47 @@ let cfg = config.casuallyblue.services.webring; in
in
''
cd ${static-files}
exec ${package}/bin/webring ${builtins.toString cfg.port} /var/lib/webring/sites.dhall
exec ${package}/bin/webring ${builtins.toString port} /var/lib/webring/${name}/sites.dhall
'';
};
};
in {
options = {
casuallyblue.services.webring = {
default = { };
description = ''
Instances of the webring to run.
'';
type = types.attrsOf ( types.submodule {
port = mkOption {
type = types.port;
default = 33242;
example = 9001;
description = "The http port to listen on";
};
vhost = mkOption {
type = types.str;
default = "webring.casuallyblue.dev";
description = "The hostname to proxy to the server";
};
user = mkOption {
default = null;
type = types.nullOr types.str;
};
});
};
};
config = let
c = mapAttrsToList instance cfg;
vhosts = catAttrs "vhosts" c;
in mkIf (cfg != { }) {
users.users = mkmerge (catAttrs "users" c);
services.nginx.virtualHosts = mkMerge vhosts;
systemd.services = mkMerge services;
};
}