Files
hearth/nixos/system/hectic-lab/whiteboard.nix
T
yukkop acd7612e7c
runner nix smoke / nix label and flake smoke (push) Failing after 1m49s
miro
2026-10-25 21:34:03 +00:00

82 lines
2.4 KiB
Nix

{
domain,
...
}: {
pkgs,
...
}: let
authDirectory = "/var/lib/whiteboard-auth";
authFile = "${authDirectory}/htpasswd";
passwordFile = "${authDirectory}/password";
whiteboardDomain = "whiteboard.${domain}";
in {
# Retrieve generated credential with:
# sudo cat /var/lib/whiteboard-auth/password
# Whiteboard state is persisted under /var/lib/whitebophir.
services.whitebophir = {
enable = true;
listenAddress = "127.0.0.1";
port = 5001;
};
systemd.tmpfiles.rules = [
"d ${authDirectory} 0750 root nginx -"
];
systemd.services.whiteboard-auth = {
description = "Prepare Whitebophir basic authentication";
requiredBy = [ "nginx.service" ];
before = [ "nginx.service" ];
after = [ "systemd-tmpfiles-setup.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = ''
set -eu
umask 077
if [ ! -s ${passwordFile} ]; then
password_tmp="$(${pkgs.coreutils}/bin/mktemp ${authDirectory}/.password.XXXXXX)"
trap '${pkgs.coreutils}/bin/rm -f "$password_tmp"' EXIT
${pkgs.openssl}/bin/openssl rand -base64 48 > "$password_tmp"
${pkgs.coreutils}/bin/chmod 0600 "$password_tmp"
${pkgs.coreutils}/bin/chown root:root "$password_tmp"
${pkgs.coreutils}/bin/mv "$password_tmp" ${passwordFile}
trap - EXIT
fi
if [ ! -s ${authFile} ]; then
auth_tmp="$(${pkgs.coreutils}/bin/mktemp ${authDirectory}/.htpasswd.XXXXXX)"
trap '${pkgs.coreutils}/bin/rm -f "$auth_tmp"' EXIT
hash="$(${pkgs.openssl}/bin/openssl passwd -6 -stdin < ${passwordFile})"
${pkgs.coreutils}/bin/printf 'whiteboard:%s\n' "$hash" > "$auth_tmp"
${pkgs.coreutils}/bin/chmod 0640 "$auth_tmp"
${pkgs.coreutils}/bin/chown root:nginx "$auth_tmp"
${pkgs.coreutils}/bin/mv "$auth_tmp" ${authFile}
trap - EXIT
fi
${pkgs.coreutils}/bin/chmod 0600 ${passwordFile}
${pkgs.coreutils}/bin/chown root:root ${passwordFile}
${pkgs.coreutils}/bin/chmod 0640 ${authFile}
${pkgs.coreutils}/bin/chown root:nginx ${authFile}
'';
};
services.nginx.virtualHosts.${whiteboardDomain} = {
enableACME = true;
forceSSL = true;
basicAuthFile = authFile;
locations."/" = {
proxyPass = "http://127.0.0.1:5001";
proxyWebsockets = true;
extraConfig = ''
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
'';
};
};
}