lenticular_cloud2/module.nix

123 lines
3.5 KiB
Nix
Raw Normal View History

2023-09-30 10:58:24 +00:00
{ config, pkgs, lib, ... }:
2023-03-18 11:00:49 +00:00
let
2023-09-30 10:58:24 +00:00
cfg = config.services.lenticular-cloud;
2023-10-09 19:58:44 +00:00
username = "lenticular_cloud";
data_folder = "/var/lib/${username}";
2023-09-30 10:58:24 +00:00
python = pkgs.python3;
2023-03-18 11:00:49 +00:00
in
{
options = with lib.options; {
2023-10-20 07:58:32 +00:00
services.lenticular-cloud = {
2023-03-18 11:00:49 +00:00
enable = mkEnableOption "lenticluar service enable";
2023-10-09 19:58:44 +00:00
domain = mkOption {
2023-11-13 14:15:39 +00:00
type = lib.types.str;
example = "example.com";
};
service_domain = mkOption {
2023-10-09 19:58:44 +00:00
type = lib.types.str;
example = "account.example.com";
};
settings = mkOption {
type = lib.types.attrs;
default = rec {
DOMAIN = cfg.domain;
DATA_FOLDER = data_folder;
PKI_PATH = "${DATA_FOLDER}/pki";
# SQLALCHEMY_DATABASE_URI = "sqlite:////${DATA_FOLDER}/db.sqlite";
SQLALCHEMY_DATABASE_URI = "postgresql://${username}@/${username}?host=/run/postgresql";
HYDRA_ADMIN_URL= "https://${config.services.ory-hydra.admin_domain}";
HYDRA_PUBLIC_URL= "https://${config.services.ory-hydra.public_domain}";
};
};
2023-03-18 11:00:49 +00:00
};
};
config = {
2023-09-30 10:58:24 +00:00
environment.systemPackages = [ pkgs.lenticular-cloud ];
nixpkgs.overlays = [
(import ./overlay.nix)
];
2023-03-18 11:00:49 +00:00
users = {
2023-10-09 19:58:44 +00:00
groups."${username}" = {
2023-03-18 11:00:49 +00:00
};
2023-10-09 19:58:44 +00:00
users."${username}" = {
2023-03-18 11:00:49 +00:00
createHome = true;
2023-10-09 19:58:44 +00:00
home = data_folder;
2023-03-18 11:00:49 +00:00
description = "web server";
extraGroups = [
2023-10-09 19:58:44 +00:00
# "ory-hydra"
2023-03-18 11:00:49 +00:00
];
2023-10-09 19:58:44 +00:00
group = username;
2023-03-18 11:00:49 +00:00
isSystemUser = true;
};
};
2023-10-09 19:58:44 +00:00
services.postgresql = {
enable = true;
ensureDatabases = [ username ];
ensureUsers = [
{
name = username;
ensurePermissions = {
"DATABASE ${username}" = "All PRIVILEGES";
};
}
];
identMap = ''
# ArbitraryMapName systemUser DBUser
superuser_map ${username} ${username}
'';
};
services.nginx.enable = true;
2023-11-13 14:15:39 +00:00
services.nginx.virtualHosts."${cfg.service_domain}" = {
2023-10-09 19:58:44 +00:00
addSSL = true;
enableACME = true;
2023-11-13 14:15:39 +00:00
serverName = cfg.service_domain;
2023-10-09 19:58:44 +00:00
locations."/" = {
recommendedProxySettings = true;
proxyPass = "http://unix:/run/${username}/web.sock";
};
};
users.users.nginx.extraGroups = [ username ];
2023-03-18 11:00:49 +00:00
systemd.services.lenticular-cloud = {
description = "lenticular account";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
2023-10-20 07:58:32 +00:00
requires = [ "ory-hydra.service" "postgresql.service" ];
2023-09-30 10:58:24 +00:00
enable = cfg.enable;
2023-03-18 11:00:49 +00:00
environment = let
2023-10-20 07:58:32 +00:00
python_path = with python.pkgs; makePythonPath [ pkgs.lenticular-cloud gevent setuptools ];
2023-03-18 11:00:49 +00:00
in {
2023-10-09 19:58:44 +00:00
# CONFIG_FILE = "/etc/lenticular_cloud/production.conf";
CONFIG_FILE = pkgs.writeText "lenticular-cloud.json" (builtins.toJSON cfg.settings);
2023-09-30 10:58:24 +00:00
PYTHONPATH = "${python_path}";
# PYTHONPATH = "${lenticular-pkg.pythonPath}:${lenticular-pkg}/lib/python3.10/site-packages:${python_path}";
2023-03-18 11:00:49 +00:00
};
2023-10-09 19:58:44 +00:00
preStart = ''
#cat > ${data_folder}/foobar.conf <<EOF
#SECRET_KEY=""
#EOF
${pkgs.lenticular-cloud}/bin/lenticular_cloud-cli db_upgrade
'';
2023-03-18 11:00:49 +00:00
serviceConfig = {
Type = "simple";
2023-10-09 19:58:44 +00:00
WorkingDirectory = data_folder;
User = username;
2023-09-30 10:58:24 +00:00
ExecStart = ''${python.pkgs.gunicorn}/bin/gunicorn lenticular_cloud.wsgi --name lenticular_cloud \
--workers 1 --log-level=info \
2023-10-09 19:58:44 +00:00
--bind=unix:/run/${username}/web.sock \
2023-03-18 11:00:49 +00:00
-k gevent'';
Restart = "on-failure";
2023-10-09 19:58:44 +00:00
RuntimeDirectory = username;
2023-03-18 11:00:49 +00:00
};
};
};
}