lenticular_cloud2/module.nix

161 lines
4.6 KiB
Nix

{ config, pkgs, lib, ... }:
let
cfg = config.services.lenticular-cloud;
python = pkgs.python3;
format = pkgs.formats.json {};
types = lib.types;
in
{
options = with lib.options; {
services.lenticular-cloud = {
enable = mkEnableOption "lenticluar service enable";
domain = mkOption {
type = lib.types.str;
example = "example.com";
};
username = mkOption {
type = lib.types.str;
description = mdDoc "user to run the service";
example = "lenticular_cloud";
};
service_domain = mkOption {
type = lib.types.str;
example = "account.example.com";
};
settings = mkOption {
description = mdDoc ''
Lenticular cloud settings
'';
default = { };
type = types.submodule {
freeformType = format.type;
options = {
DOMAIN = mkOption {
type = types.str;
description = mdDoc "Top level Domain of the service";
default = cfg.domain;
};
PUBLIC_URL = mkOption {
type = types.str;
description = mdDoc "public service url";
default = "https://${cfg.service_domain}";
};
DATA_FOLDER = mkOption {
type = types.str;
default = "/var/lib/${cfg.username}";
};
PKI_PATH = mkOption {
type = types.str;
default = "${cfg.settings.DATA_FOLDER}/pki";
};
SQLALCHEMY_DATABASE_URI = mkOption {
type = types.str;
default = "postgresql://${username}@/${username}?host=/run/postgresql";
};
HYDRA_ADMIN_URL = mkOption {
type = types.str;
default = "https://${config.services.ory-hydra.admin_domain}";
};
HYDRA_PUBLIC_URL = mkOption {
type = types.str;
default = "https://${config.services.ory-hydra.public_domain}";
};
};
};
};
};
};
config = {
environment.systemPackages = [ pkgs.lenticular-cloud ];
nixpkgs.overlays = [
(import ./overlay.nix)
];
users = {
groups."${username}" = {
};
users."${username}" = {
createHome = true;
home = "/var/lib/${username}";
description = "web server";
extraGroups = [
# "ory-hydra"
];
group = username;
isSystemUser = true;
};
};
services.postgresql = {
enable = true;
ensureDatabases = [ username ];
ensureUsers = [
{
name = username;
ensureDBOwnership = true;
}
];
};
services.ory-hydra.settings = {
urls = {
login = "${cfg.settings.PUBLIC_URL}/auth/login";
logout = "${cfg.settings.PUBLIC_URL}/auth/logout";
consent = "${cfg.settings.PUBLIC_URL}/auth/consent";
error = "${cfg.settings.PUBLIC_URL}/auth/error";
};
};
services.nginx.enable = true;
services.nginx.virtualHosts."${cfg.service_domain}" = {
addSSL = true;
enableACME = true;
serverName = cfg.service_domain;
locations."/" = {
recommendedProxySettings = true;
proxyPass = "http://unix:/run/${username}/web.sock";
};
};
users.users.nginx.extraGroups = [ username ];
systemd.services.lenticular-cloud = {
description = "lenticular account";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
requires = [ "ory-hydra.service" "postgresql.service" ];
enable = cfg.enable;
environment = let
python_path = with python.pkgs; makePythonPath [ pkgs.lenticular-cloud gevent setuptools ];
in {
# CONFIG_FILE = "/etc/lenticular_cloud/production.conf";
CONFIG_FILE = format.generate "lenticular-cloud.json" cfg.settings;
PYTHONPATH = "${python_path}";
# PYTHONPATH = "${lenticular-pkg.pythonPath}:${lenticular-pkg}/lib/python3.10/site-packages:${python_path}";
};
preStart = ''
#cat > ${data_folder}/foobar.conf <<EOF
#SECRET_KEY=""
#EOF
${pkgs.lenticular-cloud}/bin/lenticular_cloud-cli db_upgrade
'';
serviceConfig = {
Type = "simple";
WorkingDirectory = data_folder;
User = username;
ExecStart = ''${python.pkgs.gunicorn}/bin/gunicorn lenticular_cloud.wsgi --name lenticular_cloud \
--workers 1 --log-level=info \
--bind=unix:/run/${username}/web.sock \
-k gevent'';
Restart = "on-failure";
RuntimeDirectory = username;
};
};
};
}