2018-05-18 00:56:58 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.meguca;
|
|
|
|
postgres = config.services.postgresql;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.meguca = {
|
|
|
|
enable = mkEnableOption "meguca";
|
|
|
|
|
|
|
|
baseDir = mkOption {
|
|
|
|
type = types.path;
|
2018-05-21 01:15:23 +01:00
|
|
|
default = "/run/meguca";
|
2018-05-18 00:56:58 +01:00
|
|
|
description = "Location where meguca stores it's database and links.";
|
|
|
|
};
|
|
|
|
|
|
|
|
password = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "meguca";
|
|
|
|
description = "Password for the meguca database.";
|
|
|
|
};
|
|
|
|
|
2018-05-21 01:15:23 +01:00
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/run/keys/meguca-password-file";
|
|
|
|
description = "Password file for the meguca database.";
|
|
|
|
};
|
|
|
|
|
2018-05-18 00:56:58 +01:00
|
|
|
reverseProxy = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Reverse proxy IP.";
|
|
|
|
};
|
|
|
|
|
|
|
|
sslCertificate = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Path to the SSL certificate.";
|
|
|
|
};
|
|
|
|
|
|
|
|
listenAddress = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Listen on a specific IP address and port.";
|
|
|
|
};
|
|
|
|
|
|
|
|
cacheSize = mkOption {
|
2018-05-21 01:15:23 +01:00
|
|
|
type = types.nullOr types.int;
|
2018-05-18 00:56:58 +01:00
|
|
|
default = null;
|
|
|
|
description = "Cache size in MB.";
|
|
|
|
};
|
|
|
|
|
|
|
|
postgresArgs = mkOption {
|
2018-05-21 01:15:23 +01:00
|
|
|
type = types.str;
|
|
|
|
default = "user=meguca password=" + cfg.password + " dbname=meguca sslmode=disable";
|
2018-05-18 00:56:58 +01:00
|
|
|
description = "Postgresql connection arguments.";
|
|
|
|
};
|
|
|
|
|
2018-05-21 01:15:23 +01:00
|
|
|
postgresArgsFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/run/keys/meguca-postgres-args";
|
|
|
|
description = "Postgresql connection arguments file.";
|
|
|
|
};
|
|
|
|
|
2018-05-18 00:56:58 +01:00
|
|
|
compressTraffic = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Compress all traffic with gzip.";
|
|
|
|
};
|
|
|
|
|
|
|
|
assumeReverseProxy = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Assume the server is behind a reverse proxy, when resolving client IPs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
httpsOnly = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Serve and listen only through HTTPS.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
security.sudo.enable = cfg.enable == true;
|
|
|
|
services.postgresql.enable = cfg.enable == true;
|
|
|
|
|
2018-05-21 01:15:23 +01:00
|
|
|
services.meguca.passwordFile = mkDefault (toString (pkgs.writeTextFile {
|
|
|
|
name = "meguca-password-file";
|
|
|
|
text = cfg.password;
|
|
|
|
}));
|
|
|
|
|
|
|
|
services.meguca.postgresArgsFile = mkDefault (toString (pkgs.writeTextFile {
|
|
|
|
name = "meguca-postgres-args";
|
|
|
|
text = cfg.postgresArgs;
|
|
|
|
}));
|
|
|
|
|
2018-05-18 00:56:58 +01:00
|
|
|
systemd.services.meguca = {
|
|
|
|
description = "meguca";
|
|
|
|
after = [ "network.target" "postgresql.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
# Ensure folder exists and links are correct or create them
|
|
|
|
mkdir -p ${cfg.baseDir}
|
2018-06-10 04:49:27 +01:00
|
|
|
chmod 750 ${cfg.baseDir}
|
2018-05-18 00:56:58 +01:00
|
|
|
ln -sf ${pkgs.meguca}/share/meguca/www ${cfg.baseDir}
|
|
|
|
|
|
|
|
# Ensure the database is correct or create it
|
2018-05-21 01:15:23 +01:00
|
|
|
${pkgs.sudo}/bin/sudo -u ${postgres.superUser} ${postgres.package}/bin/createuser \
|
|
|
|
-SDR meguca || true
|
|
|
|
${pkgs.sudo}/bin/sudo -u ${postgres.superUser} ${postgres.package}/bin/createdb \
|
|
|
|
-T template0 -E UTF8 -O meguca meguca || true
|
2018-06-10 04:49:27 +01:00
|
|
|
${pkgs.sudo}/bin/sudo -u meguca ${postgres.package}/bin/psql \
|
|
|
|
-c "ALTER ROLE meguca WITH PASSWORD '$(cat ${cfg.passwordFile})';" || true
|
2018-05-18 00:56:58 +01:00
|
|
|
'';
|
|
|
|
|
2018-05-21 01:15:23 +01:00
|
|
|
script = ''
|
|
|
|
cd ${cfg.baseDir}
|
|
|
|
|
|
|
|
${pkgs.meguca}/bin/meguca -d "$(cat ${cfg.postgresArgsFile})"\
|
|
|
|
${optionalString (cfg.reverseProxy != null) " -R ${cfg.reverseProxy}"}\
|
|
|
|
${optionalString (cfg.sslCertificate != null) " -S ${cfg.sslCertificate}"}\
|
|
|
|
${optionalString (cfg.listenAddress != null) " -a ${cfg.listenAddress}"}\
|
|
|
|
${optionalString (cfg.cacheSize != null) " -c ${toString cfg.cacheSize}"}\
|
|
|
|
${optionalString (cfg.compressTraffic) " -g"}\
|
|
|
|
${optionalString (cfg.assumeReverseProxy) " -r"}\
|
|
|
|
${optionalString (cfg.httpsOnly) " -s"} start
|
|
|
|
'';
|
|
|
|
|
2018-05-18 00:56:58 +01:00
|
|
|
serviceConfig = {
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
Type = "forking";
|
|
|
|
User = "meguca";
|
|
|
|
Group = "meguca";
|
2018-05-21 01:15:23 +01:00
|
|
|
RuntimeDirectory = "meguca";
|
2018-05-18 00:56:58 +01:00
|
|
|
ExecStop = "${pkgs.meguca}/bin/meguca stop";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users = {
|
2018-06-30 00:58:35 +01:00
|
|
|
users.meguca = {
|
2018-05-18 00:56:58 +01:00
|
|
|
description = "meguca server service user";
|
2018-05-21 01:15:23 +01:00
|
|
|
home = cfg.baseDir;
|
2018-05-18 00:56:58 +01:00
|
|
|
createHome = true;
|
|
|
|
group = "meguca";
|
|
|
|
uid = config.ids.uids.meguca;
|
|
|
|
};
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
groups.meguca = {
|
2018-05-18 00:56:58 +01:00
|
|
|
gid = config.ids.gids.meguca;
|
|
|
|
members = [ "meguca" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-05-21 01:15:23 +01:00
|
|
|
meta.maintainers = with maintainers; [ chiiruno ];
|
2018-05-18 00:56:58 +01:00
|
|
|
}
|