Files
nixos/modules/services/chhoto-url/default.nix
JakeHillion 9ceba231a0
commit-validation / validate-single-commit (pull_request) Successful in 8s
buildbot/nix-eval Build done. (652 warnings)
buildbot/nix-build Build done.
buildbot/nix-effects Build done.
chhoto-url: serve short links from jhlink.uk on iceman
Add Chhoto URL as a short link service on iceman, with random UID slugs,
optional custom names, per-link expiry and an Argon2-hashed admin password.

The public jhlink.uk vhost only passes GET and HEAD for single-segment
paths and the static assets of the 404 page, so it serves redirects
alone. The admin UI and API are on jhlink.neb.jakehillion.me over
Nebula. Redirects are temporary so that expiry and edits take effect
for clients that have already followed a link.

The service runs as a fixed UID with its state directory persisted. A
consistent copy of the SQLite database is taken with sqlite3 .backup
before each restic run to mig29.

The upstream module's try_longer_slugs option sets a variable Chhoto
does not read, so try_longer_slug is set through the freeform settings
instead.

Test plan:
- CI
- Ran chhoto-url and the public Caddy matcher locally: custom and random
  links redirect with 307, expired links 404, the root, /api/* and the
  frontend 404 publicly, and creating links needs a login
- Checked an Argon2 password loaded from a systemd EnvironmentFile
  accepts the right password and rejects a wrong one
- Checked the sqlite3 .backup copy contains the links
2026-11-18 23:14:32 +00:00

108 lines
2.7 KiB
Nix

{ config, lib, pkgs, ... }:
let
cfg = config.custom.services.chhoto-url;
port = 4567;
backupDir = "/run/restic-backups-chhoto-url";
in
{
options.custom.services.chhoto-url = {
enable = lib.mkEnableOption "chhoto-url";
domain = lib.mkOption {
type = lib.types.str;
default = "jhlink.uk";
description = "Public domain the short links are served from";
};
backup = lib.mkOption {
default = true;
type = lib.types.bool;
};
};
config = lib.mkIf cfg.enable {
age.secrets = {
"chhoto-url/environment".rekeyFile = ./environment.age;
"backups/chhoto-url/restic/mig29" = lib.mkIf cfg.backup {
rekeyFile = ../../../secrets/restic/mig29.age;
};
};
users.users.chhoto-url = {
uid = config.ids.uids.chhoto-url;
isSystemUser = true;
group = "chhoto-url";
};
users.groups.chhoto-url = {
gid = config.ids.gids.chhoto-url;
};
systemd.services.chhoto-url.serviceConfig.DynamicUser = lib.mkForce false;
custom.impermanence.extraDirs = lib.mkIf config.custom.impermanence.enable
[ "/var/lib/chhoto-url" ];
services.chhoto-url = {
enable = true;
environmentFiles = [ config.age.secrets."chhoto-url/environment".path ];
settings = {
inherit port;
listen_address = "127.0.0.1";
site_url = "https://${cfg.domain}";
hash_algorithm = "Argon2";
redirect_method = "TEMPORARY";
slug_style = "UID";
slug_length = 6;
try_longer_slug = true;
use_wal_mode = true;
};
};
# Only the redirects are public. The admin UI and API are reachable over
# Nebula alone.
services.caddy = {
enable = true;
virtualHosts.${cfg.domain}.extraConfig = ''
@public {
method GET HEAD
path_regexp ^/([^/]+|(assets|static)/.+)$
}
handle @public {
reverse_proxy http://127.0.0.1:${toString port}
}
handle {
respond 404
}
'';
};
custom.www.nebula = {
enable = true;
virtualHosts."jhlink.${config.ogygia.domain}".extraConfig = ''
reverse_proxy http://127.0.0.1:${toString port}
'';
};
services.restic.backups."chhoto-url" = lib.mkIf cfg.backup {
user = "root";
timerConfig = {
OnCalendar = "03:00";
RandomizedDelaySec = "60m";
};
repository = "rest:https://restic.${config.ogygia.domain}/mig29";
passwordFile = config.age.secrets."backups/chhoto-url/restic/mig29".path;
backupPrepareCommand = ''
${lib.getExe pkgs.sqlite} ${config.services.chhoto-url.settings.db_url} ".backup '${backupDir}/urls.sqlite'"
'';
paths = [ "${backupDir}/urls.sqlite" ];
};
};
}