refactor: renames some options to be a little clearer, and fixes up the module

This commit is contained in:
Lewis Wynne 2026-04-09 18:57:58 +01:00
parent 0534b315a3
commit 726fe55eb8
5 changed files with 212 additions and 199 deletions

View file

@ -30,124 +30,6 @@ in
description = "Site title shown in nav and page title.";
};
telegramChatId = mkOption {
type = types.int;
description = "Telegram chat ID for moderation messages.";
};
telegramBotTokenFile = mkOption {
type = types.path;
description = "Path to a file containing the Telegram bot token.";
};
honeypot = mkOption {
type = types.bool;
default = true;
description = "Enable honeypot field for spam prevention.";
};
maxNameLength = mkOption {
type = types.int;
default = 50;
description = "Maximum length for names. 0 for unlimited.";
};
maxMessageLength = mkOption {
type = types.int;
default = 1000;
description = "Maximum length for messages. 0 for unlimited.";
};
maxWebsiteLength = mkOption {
type = types.int;
default = 100;
description = "Maximum length for website URLs. 0 for unlimited.";
};
openRegistration = mkOption {
type = types.bool;
default = true;
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
};
enableWebsiteField = mkOption {
type = types.bool;
default = true;
description = "Show website field in submission form. When false, the input is hidden and submitted values are ignored.";
};
allowHtmlInjection = mkOption {
type = types.bool;
default = true;
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
};
separator = mkOption {
type = types.str;
default = "------------------------------------------------------------";
description = "Separator between guestbook entries.";
};
style = mkOption {
type = types.str;
default = "";
description = "Custom CSS injected into a style tag. Use class names: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input, .guestbook-textarea, .guestbook-button, .entry-header, .entry-name, .entry-website, .entry-body, .entry-separator";
};
styleFile = mkOption {
type = types.nullOr types.path;
default = null;
description = "Path to a CSS file. Takes precedence over style.";
};
formPrompt = mkOption {
type = types.str;
default = "If you visited my site, please sign my guestbook!";
description = "Text shown above the form.";
};
buttonText = mkOption {
type = types.str;
default = "sign";
description = "Submit button text.";
};
labelName = mkOption {
type = types.str;
default = "Your name:";
description = "Label for the name field.";
};
labelWebsite = mkOption {
type = types.str;
default = "Your website (optional):";
description = "Label for the website field.";
};
labelMessage = mkOption {
type = types.str;
default = "Your message:";
description = "Label for the message field.";
};
textareaRows = mkOption {
type = types.int;
default = 8;
description = "Number of rows for the message textarea.";
};
textareaCols = mkOption {
type = types.int;
default = 60;
description = "Number of columns for the message textarea.";
};
templateFile = mkOption {
type = types.nullOr types.path;
default = null;
description = "Custom HTML template file with {{title}}, {{form}}, and {{entries}} placeholders. Uses built-in default if null.";
};
user = mkOption {
type = types.str;
default = "guestbook";
@ -168,6 +50,136 @@ in
description = "Domain for the Caddy virtual host.";
};
};
security = {
enableSubmissions = mkOption {
type = types.bool;
default = true;
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
};
enableHtmlInjection = mkOption {
type = types.bool;
default = true;
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
};
enableWebsiteLinks = mkOption {
type = types.bool;
default = true;
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
};
enableHoneypot = mkOption {
type = types.bool;
default = true;
description = "Enable honeypot field for spam prevention.";
};
};
telegram = {
botTokenFile = mkOption {
type = types.path;
description = "Path to a file containing the Telegram bot token.";
};
chatId = mkOption {
type = types.int;
description = "Telegram chat ID for moderation messages.";
};
};
limits = {
name = mkOption {
type = types.int;
default = 50;
description = "Maximum length for names. 0 for unlimited.";
};
message = mkOption {
type = types.int;
default = 1000;
description = "Maximum length for messages. 0 for unlimited.";
};
website = mkOption {
type = types.int;
default = 100;
description = "Maximum length for website URLs. 0 for unlimited.";
};
};
styles = {
css = mkOption {
type = types.str;
default = "";
description = "Custom CSS injected into a style tag. Use class names: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input, .guestbook-textarea, .guestbook-button, .entry-header, .entry-name, .entry-website, .entry-body, .entry-separator";
};
cssFile = mkOption {
type = types.nullOr types.path;
default = null;
description = "Path to a CSS file. Takes precedence over css.";
};
templateFile = mkOption {
type = types.nullOr types.path;
default = null;
description = "Custom HTML template file with {{title}}, {{form}}, {{entries}}, and {{style}} placeholders. Uses built-in default if null.";
};
separator = mkOption {
type = types.str;
default = "------------------------------------------------------------";
description = "Separator between guestbook entries.";
};
greeting = mkOption {
type = types.str;
default = "If you visited my site, please sign my guestbook!";
description = "Text shown above the form.";
};
labels = {
submit = mkOption {
type = types.str;
default = "sign";
description = "Submit button text.";
};
name = mkOption {
type = types.str;
default = "Your name:";
description = "Label for the name field.";
};
website = mkOption {
type = types.str;
default = "Your website (optional):";
description = "Label for the website field.";
};
message = mkOption {
type = types.str;
default = "Your message:";
description = "Label for the message field.";
};
};
message = {
rows = mkOption {
type = types.int;
default = 8;
description = "Number of rows for the message textarea.";
};
cols = mkOption {
type = types.int;
default = 60;
description = "Number of columns for the message textarea.";
};
};
};
};
config = mkIf cfg.enable (mkMerge [
@ -182,27 +194,27 @@ in
BOOK_DATA_DIR = cfg.dataDir;
BOOK_SITE_TITLE = cfg.siteTitle;
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegramChatId;
BOOK_HONEYPOT = if cfg.honeypot then "true" else "false";
BOOK_MAX_NAME_LENGTH = toString cfg.maxNameLength;
BOOK_MAX_MESSAGE_LENGTH = toString cfg.maxMessageLength;
BOOK_MAX_WEBSITE_LENGTH = toString cfg.maxWebsiteLength;
BOOK_OPEN_REGISTRATION = if cfg.openRegistration then "true" else "false";
BOOK_ENABLE_WEBSITE_FIELD = if cfg.enableWebsiteField then "true" else "false";
BOOK_ALLOW_HTML_INJECTION = if cfg.allowHtmlInjection then "true" else "false";
BOOK_SEPARATOR = cfg.separator;
BOOK_STYLE = cfg.style;
} // lib.optionalAttrs (cfg.styleFile != null) {
BOOK_STYLE_FILE = cfg.styleFile;
BOOK_FORM_PROMPT = cfg.formPrompt;
BOOK_BUTTON_TEXT = cfg.buttonText;
BOOK_LABEL_NAME = cfg.labelName;
BOOK_LABEL_WEBSITE = cfg.labelWebsite;
BOOK_LABEL_MESSAGE = cfg.labelMessage;
BOOK_TEXTAREA_ROWS = toString cfg.textareaRows;
BOOK_TEXTAREA_COLS = toString cfg.textareaCols;
} // lib.optionalAttrs (cfg.templateFile != null) {
BOOK_TEMPLATE = cfg.templateFile;
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegram.chatId;
BOOK_ENABLE_HONEYPOT = if cfg.security.enableHoneypot then "true" else "false";
BOOK_ENABLE_SUBMISSIONS = if cfg.security.enableSubmissions then "true" else "false";
BOOK_ENABLE_HTML_INJECTION = if cfg.security.enableHtmlInjection then "true" else "false";
BOOK_ENABLE_WEBSITE_LINKS = if cfg.security.enableWebsiteLinks then "true" else "false";
BOOK_MAX_NAME_LENGTH = toString cfg.limits.name;
BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message;
BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website;
BOOK_SEPARATOR = cfg.styles.separator;
BOOK_STYLE = cfg.styles.css;
BOOK_FORM_PROMPT = cfg.styles.greeting;
BOOK_BUTTON_TEXT = cfg.styles.labels.submit;
BOOK_LABEL_NAME = cfg.styles.labels.name;
BOOK_LABEL_WEBSITE = cfg.styles.labels.website;
BOOK_LABEL_MESSAGE = cfg.styles.labels.message;
BOOK_TEXTAREA_ROWS = toString cfg.styles.message.rows;
BOOK_TEXTAREA_COLS = toString cfg.styles.message.cols;
} // lib.optionalAttrs (cfg.styles.cssFile != null) {
BOOK_STYLE_FILE = cfg.styles.cssFile;
} // lib.optionalAttrs (cfg.styles.templateFile != null) {
BOOK_TEMPLATE = cfg.styles.templateFile;
};
serviceConfig = {
Type = "simple";
@ -216,7 +228,7 @@ in
ReadWritePaths = [ cfg.dataDir ];
};
script = ''
export BOOK_TELEGRAM_BOT_TOKEN="$(< "${cfg.telegramBotTokenFile}")"
export BOOK_TELEGRAM_BOT_TOKEN="$(< "${cfg.telegram.botTokenFile}")"
exec ${cfg.package}/bin/guestbook
'';
};