regalade/nixos/default.nix

187 lines
5 KiB
Nix
Raw Normal View History

2023-07-28 00:05:34 +02:00
self: {
pkgs,
lib,
config,
2023-07-29 10:25:22 +02:00
options,
2023-07-28 00:05:34 +02:00
...
}:
with lib; {
options.services.regalade = {
enable = mkEnableOption "regalade, a recipe manager";
package = mkOption {
type = types.package;
default = self.packages.${config.nixpkgs.system}.server;
};
2023-07-29 10:25:22 +02:00
settings = {
jwtSecret = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The JWT secret to be used by the application. Should be passed through environmentFile,
with REGALADE_JWT_SECRET.
'';
};
host = mkOption {
type = types.str;
default = "0.0.0.0";
description = "The listen address";
};
port = mkOption {
type = types.port;
default = 8085;
};
apiAllowed = mkOption {
type = types.nullOr types.str;
default = null;
description = "The CORS Access-Control-Allow-Origin value";
};
databaseUrl = mkOption {
type = types.str;
default = "postgres://${config.services.regalade.user}/regalade?host=/var/run/postgresql";
};
sqlxLogging = mkEnableOption "logging of all SQL operations";
oidc = {
enable = mkEnableOption "authentication using an OIDC provider";
url = mkOption {
type = types.str;
description = "The base URL of the provider (directory where the .well-known is)";
};
id = mkOption {
type = types.str;
description = "Client ID";
};
secret = mkOption {
type = types.nullOr types.str;
2023-07-29 11:07:40 +02:00
default = null;
2023-07-29 10:25:22 +02:00
description = "The environment variable REGALADE_OIDC__SECRET should be preferred";
};
scopes = mkOption {
type = types.listOf types.str;
default = ["openid" "profile" "email"];
};
domain = mkOption {
type = types.str;
description = "URL at which to redirect, where the application is deployed";
};
};
2023-07-28 00:05:34 +02:00
};
user = mkOption {
type = types.str;
default = "regalade";
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
};
frontend = {
enable = mkEnableOption "the frontend for regalade";
package = mkOption {
type = types.package;
default = self.packages.${config.nixpkgs.system}.frontend;
};
};
};
config = let
cfg = config.services.regalade;
in
mkIf cfg.enable {
systemd.services.regalade = {
description = "regalade";
after = ["network.target" "postgresql.service"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "simple";
User = cfg.user;
ExecStart = "${cfg.package}/bin/regalade";
EnvironmentFile = optional (cfg.environmentFile != null) cfg.environmentFile;
# Security
NoNewPrivileges = true;
# Sandboxing
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
RestrictAddressFamilies = ["AF_UNIX AF_INET AF_INET6"];
LockPersonality = true;
MemoryDenyWriteExecute = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
PrivateMounts = true;
};
2023-07-29 10:25:22 +02:00
environment =
2023-07-28 00:05:34 +02:00
{
2023-07-29 10:25:22 +02:00
REGALADE_DATABASE_URL = cfg.settings.databaseUrl;
REGALADE_JWT_SECRET = cfg.settings.jwtSecret;
REGALADE_HOST = cfg.settings.host;
REGALADE_PORT = toString cfg.settings.port;
REGALADE_API_ALLOWED = cfg.settings.apiAllowed;
REGALADE_SQLX_LOGGING =
if cfg.settings.sqlxLogging
then "true"
else "false";
2023-07-28 00:05:34 +02:00
}
2023-07-29 10:25:22 +02:00
// (
if cfg.settings.oidc.enable
then let
inherit (cfg.settings) oidc;
in {
REGALADE_OIDC__URL = oidc.url;
REGALADE_OIDC__ID = oidc.id;
REGALADE_OIDC__SECRET = oidc.secret;
REGALADE_OIDC__DOMAIN = oidc.domain;
REGALADE_OIDC__SCOPES = lib.strings.concatStringsSep "," oidc.scopes;
}
else {}
);
2023-07-28 00:05:34 +02:00
};
2023-07-29 10:25:22 +02:00
services.postgresql =
mkIf (cfg.settings.databaseUrl == options.services.regalade.settings.databaseUrl.default)
{
ensureUsers = [
{
name = cfg.user;
ensurePermissions = {"DATABASE \"regalade\"" = "ALL PRIVILEGES";};
}
];
ensureDatabases = ["regalade"];
};
2023-07-28 00:05:34 +02:00
users = mkIf (cfg.user == "regalade") {
users.regalade = {
description = "Regalade API server";
group = "regalade";
isSystemUser = true;
};
groups.regalade = {};
};
};
}