nixos/lasuite-docs: init (#401798)

This commit is contained in:
Martin Weinelt 2025-05-24 18:43:49 +02:00 committed by GitHub
commit c49cc6a396
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1179 additions and 0 deletions

View file

@ -12,6 +12,8 @@
- [gtklock](https://github.com/jovanlanik/gtklock), a GTK-based lockscreen for Wayland. Available as [programs.gtklock](#opt-programs.gtklock.enable).
- [SuiteNumérique Docs](https://github.com/suitenumerique/docs), a collaborative note taking, wiki and documentation web platform and alternative to Notion or Outline. Available as [services.lasuite-docs](#opt-services.lasuite-docs.enable).
## Backward Incompatibilities {#sec-release-25.11-incompatibilities}
<!-- To avoid merge conflicts, consider adding your item at an arbitrary place in the list instead. -->

View file

@ -1580,6 +1580,7 @@
./services/web-apps/kimai.nix
./services/web-apps/komga.nix
./services/web-apps/lanraragi.nix
./services/web-apps/lasuite-docs.nix
./services/web-apps/lemmy.nix
./services/web-apps/limesurvey.nix
./services/web-apps/mainsail.nix

View file

@ -0,0 +1,514 @@
{
config,
lib,
pkgs,
utils,
...
}:
let
inherit (lib)
getExe
mapAttrs
mkEnableOption
mkIf
mkPackageOption
mkOption
types
optional
optionalString
;
cfg = config.services.lasuite-docs;
pythonEnvironment = mapAttrs (
_: value:
if value == null then
"None"
else if value == true then
"True"
else if value == false then
"False"
else
toString value
) cfg.settings;
commonServiceConfig = {
RuntimeDirectory = "lasuite-docs";
StateDirectory = "lasuite-docs";
WorkingDirectory = "/var/lib/lasuite-docs";
User = "lasuite-docs";
DynamicUser = true;
SupplementaryGroups = mkIf cfg.redis.createLocally [
config.services.redis.servers.lasuite-docs.group
];
# hardening
AmbientCapabilities = "";
CapabilityBoundingSet = [ "" ];
DevicePolicy = "closed";
LockPersonality = true;
NoNewPrivileges = true;
PrivateDevices = true;
PrivateTmp = true;
PrivateUsers = true;
ProcSubset = "pid";
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "invisible";
ProtectSystem = "strict";
RemoveIPC = true;
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
UMask = "0077";
};
in
{
options.services.lasuite-docs = {
enable = mkEnableOption "SuiteNumérique Docs";
backendPackage = mkPackageOption pkgs "lasuite-docs" { };
frontendPackage = mkPackageOption pkgs "lasuite-docs-frontend" { };
bind = mkOption {
type = types.str;
default = "unix:/run/lasuite-docs/gunicorn.sock";
example = "127.0.0.1:8000";
description = ''
The path, host/port or file descriptior to bind the gunicorn socket to.
See <https://docs.gunicorn.org/en/stable/settings.html#bind> for possible options.
'';
};
enableNginx = mkEnableOption "enable and configure Nginx for reverse proxying" // {
default = true;
};
secretKeyPath = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to the Django secret key.
The key can be generated using:
```
python3 -c 'import secrets; print(secrets.token_hex())'
```
If not set, the secret key will be automatically generated.
'';
};
s3Url = mkOption {
type = types.str;
description = ''
URL of the S3 bucket.
'';
};
postgresql = {
createLocally = mkOption {
type = types.bool;
default = false;
description = ''
Configure local PostgreSQL database server for docs.
'';
};
};
redis = {
createLocally = mkOption {
type = types.bool;
default = false;
description = ''
Configure local Redis cache server for docs.
'';
};
};
collaborationServer = {
package = mkPackageOption pkgs "lasuite-docs-collaboration-server" { };
port = mkOption {
type = types.port;
default = 4444;
description = ''
Port used by the collaboration server to listen.
'';
};
settings = mkOption {
type = types.submodule {
freeformType = types.attrsOf (
types.oneOf [
types.str
types.bool
]
);
options = {
PORT = mkOption {
type = types.str;
default = toString cfg.collaborationServer.port;
readOnly = true;
description = "Port used by collaboration server to listen to";
};
COLLABORATION_BACKEND_BASE_URL = mkOption {
type = types.str;
default = "https://${cfg.domain}";
defaultText = lib.literalExpression "https://\${cfg.domain}";
description = "URL to the backend server base";
};
};
};
default = { };
example = ''
{
COLLABORATION_LOGGING = true;
}
'';
description = ''
Configuration options of collaboration server.
See https://github.com/suitenumerique/docs/blob/v${cfg.collaborationServer.package.version}/docs/env.md
'';
};
};
gunicorn = {
extraArgs = mkOption {
type = types.listOf types.str;
default = [
"--name=impress"
"--workers=3"
];
description = ''
Extra arguments to pass to the gunicorn process.
'';
};
};
celery = {
extraArgs = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Extra arguments to pass to the celery process.
'';
};
};
domain = mkOption {
type = types.str;
description = ''
Domain name of the docs instance.
'';
};
settings = mkOption {
type = types.submodule {
freeformType = types.attrsOf (
types.nullOr (
types.oneOf [
types.str
types.bool
types.path
types.int
]
)
);
options = {
DJANGO_CONFIGURATION = mkOption {
type = types.str;
internal = true;
default = "Production";
description = "The configuration that Django will use";
};
DJANGO_SETTINGS_MODULE = mkOption {
type = types.str;
internal = true;
default = "impress.settings";
description = "The configuration module that Django will use";
};
DJANGO_SECRET_KEY_FILE = mkOption {
type = types.path;
default =
if cfg.secretKeyPath == null then "/var/lib/lasuite-docs/django_secret_key" else cfg.secretKeyPath;
description = "The path to the file containing Django's secret key";
};
DATA_DIR = mkOption {
type = types.path;
default = "/var/lib/lasuite-docs";
description = "Path to the data directory";
};
DJANGO_ALLOWED_HOSTS = mkOption {
type = types.str;
default = if cfg.enableNginx then "localhost,127.0.0.1,${cfg.domain}" else "";
defaultText = lib.literalExpression ''
if cfg.enableNginx then "localhost,127.0.0.1,$${cfg.domain}" else ""
'';
description = "Comma-separated list of hosts that are able to connect to the server";
};
DB_NAME = mkOption {
type = types.str;
default = "lasuite-docs";
description = "Name of the database";
};
DB_USER = mkOption {
type = types.str;
default = "lasuite-docs";
description = "User of the database";
};
DB_HOST = mkOption {
type = types.nullOr types.str;
default = if cfg.postgresql.createLocally then "/run/postgresql" else null;
description = "Host of the database";
};
REDIS_URL = mkOption {
type = types.nullOr types.str;
default =
if cfg.redis.createLocally then
"unix://${config.services.redis.servers.lasuite-docs.unixSocket}?db=0"
else
null;
description = "URL of the redis backend";
};
CELERY_BROKER_URL = mkOption {
type = types.nullOr types.str;
default =
if cfg.redis.createLocally then
"redis+socket://${config.services.redis.servers.lasuite-docs.unixSocket}?db=1"
else
null;
description = "URL of the redis backend for celery";
};
};
};
default = { };
example = ''
{
DJANGO_ALLOWED_HOSTS = "*";
}
'';
description = ''
Configuration options of docs.
See https://github.com/suitenumerique/docs/blob/v${cfg.backendPackage.version}/docs/env.md
`REDIS_URL` and `CELERY_BROKER_URL` are set if `services.lasuite-docs.redis.createLocally` is true.
`DB_HOST` is set if `services.lasuite-docs.postgresql.createLocally` is true.
'';
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to environment file.
This can be useful to pass secrets to docs via tools like `agenix` or `sops`.
'';
};
};
config = mkIf cfg.enable {
systemd.services.lasuite-docs = {
description = "Docs from SuiteNumérique";
after =
[ "network.target" ]
++ (optional cfg.postgresql.createLocally "postgresql.service")
++ (optional cfg.redis.createLocally "redis-lasuite-docs.service");
wants =
(optional cfg.postgresql.createLocally "postgresql.service")
++ (optional cfg.redis.createLocally "redis-lasuite-docs.service");
wantedBy = [ "multi-user.target" ];
preStart = ''
ln -sfT ${cfg.backendPackage}/share/static /var/lib/lasuite-docs/static
if [ ! -f .version ]; then
touch .version
fi
if [ "${cfg.backendPackage.version}" != "$(cat .version)" ]; then
${getExe cfg.backendPackage} migrate
echo -n "${cfg.backendPackage.version}" > .version
fi
${optionalString (cfg.secretKeyPath == null) ''
if [[ ! -f /var/lib/lasuite-docs/django_secret_key ]]; then
(
umask 0377
tr -dc A-Za-z0-9 < /dev/urandom | head -c64 | ${pkgs.moreutils}/bin/sponge /var/lib/lasuite-docs/django_secret_key
)
fi
''}
'';
environment = pythonEnvironment;
serviceConfig = {
ExecStart = utils.escapeSystemdExecArgs (
[
(lib.getExe' cfg.backendPackage "gunicorn")
"--bind=${cfg.bind}"
]
++ cfg.gunicorn.extraArgs
++ [ "impress.wsgi:application" ]
);
EnvironmentFile = optional (cfg.environmentFile != null) cfg.environmentFile;
MemoryDenyWriteExecute = true;
} // commonServiceConfig;
};
systemd.services.lasuite-docs-celery = {
description = "Docs Celery broker from SuiteNumérique";
after =
[ "network.target" ]
++ (optional cfg.postgresql.createLocally "postgresql.service")
++ (optional cfg.redis.createLocally "redis-lasuite-docs.service");
wants =
(optional cfg.postgresql.createLocally "postgresql.service")
++ (optional cfg.redis.createLocally "redis-lasuite-docs.service");
wantedBy = [ "multi-user.target" ];
environment = pythonEnvironment;
serviceConfig = {
ExecStart = utils.escapeSystemdExecArgs (
[
(lib.getExe' cfg.backendPackage "celery")
]
++ cfg.celery.extraArgs
++ [
"--app=impress.celery_app"
"worker"
]
);
EnvironmentFile = optional (cfg.environmentFile != null) cfg.environmentFile;
MemoryDenyWriteExecute = true;
} // commonServiceConfig;
};
systemd.services.lasuite-docs-collaboration-server = {
description = "Docs Collaboration Server from SuiteNumérique";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
environment = cfg.collaborationServer.settings;
serviceConfig = {
ExecStart = getExe cfg.collaborationServer.package;
} // commonServiceConfig;
};
services.postgresql = mkIf cfg.postgresql.createLocally {
enable = true;
ensureDatabases = [ "lasuite-docs" ];
ensureUsers = [
{
name = "lasuite-docs";
ensureDBOwnership = true;
}
];
};
services.redis.servers.lasuite-docs = mkIf cfg.redis.createLocally { enable = true; };
services.nginx = mkIf cfg.enableNginx {
enable = true;
virtualHosts.${cfg.domain} = {
extraConfig = ''
error_page 401 /401;
error_page 403 /403;
error_page 404 /404;
'';
root = cfg.frontendPackage;
locations."~ '^/docs/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/?$'" = {
tryFiles = "$uri /docs/[id]/index.html";
};
locations."/api" = {
proxyPass = "http://${cfg.bind}";
recommendedProxySettings = true;
};
locations."/admin" = {
proxyPass = "http://${cfg.bind}";
recommendedProxySettings = true;
};
locations."/collaboration/ws/" = {
proxyPass = "http://localhost:${toString cfg.collaborationServer.port}";
recommendedProxySettings = true;
proxyWebsockets = true;
};
locations."/collaboration/api/" = {
proxyPass = "http://localhost:${toString cfg.collaborationServer.port}";
recommendedProxySettings = true;
};
locations."/media-auth" = {
proxyPass = "http://${cfg.bind}";
recommendedProxySettings = true;
extraConfig = ''
rewrite $/(.*)^ /api/v1.0/documents/$1 break;
proxy_set_header X-Original-URL $request_uri;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-Method $request_method;
'';
};
locations."/media/" = {
proxyPass = cfg.s3Url;
recommendedProxySettings = true;
extraConfig = ''
auth_request /media-auth;
auth_request_set $authHeader $upstream_http_authorization;
auth_request_set $authDate $upstream_http_x_amz_date;
auth_request_set $authContentSha256 $upstream_http_x_amz_content_sha256;
proxy_set_header Authorization $authHeader;
proxy_set_header X-Amz-Date $authDate;
proxy_set_header X-Amz-Content-SHA256 $authContentSha256;
add_header Content-Security-Policy "default-src 'none'" always;
'';
};
};
};
};
meta = {
buildDocsInSandbox = false;
maintainers = [ lib.maintainers.soyouzpanda ];
};
}

View file

@ -711,6 +711,7 @@ in
languagetool = handleTest ./languagetool.nix { };
lanraragi = handleTest ./lanraragi.nix { };
latestKernel.login = handleTest ./login.nix { latestKernel = true; };
lasuite-docs = runTest ./web-apps/lasuite-docs.nix;
lavalink = runTest ./lavalink.nix;
leaps = handleTest ./leaps.nix { };
lemmy = handleTest ./lemmy.nix { };

View file

@ -0,0 +1,168 @@
{ lib, ... }:
let
domain = "docs.local";
oidcDomain = "127.0.0.1:8080";
s3Domain = "127.0.0.1:9000";
minioAccessKey = "a8dff633d164068418a5";
minioSecretKey = "d546ea5f9c9bfdcf83755a7c09f2f7fb";
in
{
name = "lasuite-docs";
meta.maintainers = with lib.maintainers; [
soyouzpanda
];
nodes.machine =
{ pkgs, ... }:
{
virtualisation.diskSize = 4 * 1024;
virtualisation.memorySize = 4 * 1024;
networking.hosts."127.0.0.1" = [ domain ];
environment.systemPackages = with pkgs; [
jq
minio-client
];
services.lasuite-docs = {
enable = true;
enableNginx = true;
redis.createLocally = true;
postgresql.createLocally = true;
inherit domain;
s3Url = "http://${s3Domain}/lasuite-docs";
settings = {
DJANGO_SECRET_KEY_FILE = pkgs.writeText "django-secret-file" ''
8540db59c03943d48c3ed1a0f96ce3b560e0f45274f120f7ee4dace3cc366a6b
'';
OIDC_OP_JWKS_ENDPOINT = "http://${oidcDomain}/dex/keys";
OIDC_OP_AUTHORIZATION_ENDPOINT = "http://${oidcDomain}/dex/auth/mock";
OIDC_OP_TOKEN_ENDPOINT = "http://${oidcDomain}/dex/token";
OIDC_OP_USER_ENDPOINT = "http://${oidcDomain}/dex/userinfo";
OIDC_RP_CLIENT_ID = "lasuite-docs";
OIDC_RP_SIGN_ALGO = "RS256";
OIDC_RP_SCOPES = "openid email";
OIDC_RP_CLIENT_SECRET = "lasuitedocsclientsecret";
LOGIN_REDIRECT_URL = "http://${domain}";
LOGIN_REDIRECT_URL_FAILURE = "http://${domain}";
LOGOUT_REDIRECT_URL = "http://${domain}";
AWS_S3_ENDPOINT_URL = "http://${s3Domain}";
AWS_S3_ACCESS_KEY_ID = minioAccessKey;
AWS_S3_SECRET_ACCESS_KEY = minioSecretKey;
AWS_STORAGE_BUCKET_NAME = "lasuite-docs";
MEDIA_BASE_URL = "http://${domain}";
# Disable HTTPS feature in tests because we're running on a HTTP connection
DJANGO_SECURE_PROXY_SSL_HEADER = "";
DJANGO_SECURE_SSL_REDIRECT = false;
DJANGO_CSRF_COOKIE_SECURE = false;
DJANGO_SESSION_COOKIE_SECURE = false;
DJANGO_CSRF_TRUSTED_ORIGINS = "http://*";
};
};
services.dex = {
enable = true;
settings = {
issuer = "http://${oidcDomain}/dex";
storage = {
type = "postgres";
config.host = "/var/run/postgresql";
};
web.http = "127.0.0.1:8080";
oauth2.skipApprovalScreen = true;
staticClients = [
{
id = "lasuite-docs";
name = "Docs";
redirectURIs = [ "http://${domain}/api/v1.0/callback/" ];
secretFile = "/etc/dex/lasuite-docs";
}
];
connectors = [
{
type = "mockPassword";
id = "mock";
name = "Example";
config = {
username = "admin";
password = "password";
};
}
];
};
};
services.minio = {
enable = true;
rootCredentialsFile = "/etc/minio/minio-root-credentials";
};
environment.etc."dex/lasuite-docs" = {
mode = "0400";
user = "dex";
text = "lasuitedocsclientsecret";
};
environment.etc."minio/minio-root-credentials" = {
mode = "0400";
text = ''
MINIO_ROOT_USER=${minioAccessKey}
MINIO_ROOT_PASSWORD=${minioSecretKey}
'';
};
services.postgresql = {
enable = true;
ensureDatabases = [ "dex" ];
ensureUsers = [
{
name = "dex";
ensureDBOwnership = true;
}
];
};
};
testScript = ''
with subtest("Wait for units to start"):
machine.wait_for_unit("dex.service")
machine.wait_for_unit("minio.service")
machine.wait_for_unit("lasuite-docs.service")
machine.wait_for_unit("lasuite-docs-celery.service")
machine.wait_for_unit("lasuite-docs-collaboration-server.service")
with subtest("Create S3 bucket"):
machine.succeed("mc config host add minio http://${s3Domain} ${minioAccessKey} ${minioSecretKey} --api s3v4")
machine.succeed("mc mb lasuite-docs")
with subtest("Wait for web servers to start"):
machine.wait_until_succeeds("curl -fs 'http://${domain}/api/v1.0/authenticate/'", timeout=120)
machine.wait_until_succeeds("curl -fs '${oidcDomain}/dex/auth/mock?client_id=lasuite-docs&response_type=code&redirect_uri=http://${domain}/api/v1.0/callback/&scope=openid'", timeout=120)
with subtest("Login"):
state, nonce = machine.succeed("curl -fs -c cjar 'http://${domain}/api/v1.0/authenticate/' -w '%{redirect_url}' | sed -n 's/.*state=\\(.*\\)&nonce=\\(.*\\)/\\1 \\2/p'").strip().split(' ')
oidc_state = machine.succeed(f"curl -fs '${oidcDomain}/dex/auth/mock?client_id=lasuite-docs&response_type=code&redirect_uri=http://${domain}/api/v1.0/callback/&scope=openid+email&state={state}&nonce={nonce}' | sed -n 's/.*state=\\(.*\\)\">.*/\\1/p'").strip()
code = machine.succeed(f"curl -fs '${oidcDomain}/dex/auth/mock/login?back=&state={oidc_state}' -d 'login=admin&password=password' -w '%{{redirect_url}}' | sed -n 's/.*code=\\(.*\\)&.*/\\1/p'").strip()
print(f"Got approval code {code}")
machine.succeed(f"curl -fs -c cjar -b cjar 'http://${domain}/api/v1.0/callback/?code={code}&state={state}'")
with subtest("Create a document"):
csrf_token = machine.succeed("grep csrftoken cjar | cut -f 7 | tr -d '\n'")
document_id = machine.succeed(f"curl -fs -c cjar -b cjar 'http://${domain}/api/v1.0/documents/' -X POST -H 'X-CSRFToken: {csrf_token}' -H 'Referer: http://${domain}' | jq .id -r").strip()
print(f"Created document with id {document_id}")
'';
}