Compare commits

...
Author SHA1 Message Date
müde
608de57924 hive-forge: default to pkgs.forgejo (15.x), expose package option
nixpkgs's services.forgejo defaults to forgejo-lts (11.0.13 today);
LTS lags far enough behind that any prior non-LTS run against the
same state dir leaves the DB at a migration the LTS binary can't
read ('database newer than binary, refusing to start'). default to
the latest release line and let operators opt down to LTS by
overriding services.hive-forge.package.
2026-05-17 01:29:19 +02:00
müde
2192cb5148 forge-login: don't die on RO ~/.config/git/config
home-manager / nix-managed git configs ship the file from the nix
store, so `git config --global` errors out. catch the failure and
print the equivalent home-manager snippet instead of aborting — the
tea + netrc steps still want to run.
2026-05-17 01:22:31 +02:00
müde
33f7408ef1 scripts: forge-migrate.sh — run pending DB migrations + restart
for the 'table X has no column Y' class of schema-lag errors that
showed up generating an access token on a fresh 11.0.13 install.
2026-05-17 01:21:40 +02:00
müde
a1c4d37bc9 scripts: forge-login.sh + forge-create-token.sh
forge-create-token.sh mints an access token for an existing user
(prints to stdout — forgejo only shows it once). forge-login.sh
configures the operator's shell: git config --global user.name /
user.email, ~/.netrc entry for HTTP clones, and `tea login add`
when tea is on PATH. takes the token interactively (hidden input)
so it doesn't land in shell history.
2026-05-17 01:18:27 +02:00
4 changed files with 147 additions and 0 deletions

View file

@ -60,6 +60,21 @@ in
'';
};
package = lib.mkOption {
type = lib.types.package;
default = pkgs.forgejo;
defaultText = lib.literalExpression "pkgs.forgejo";
description = ''
Forgejo package to run inside the container. Defaults to
`pkgs.forgejo` (the latest release line) rather than the
nixpkgs-module default of `pkgs.forgejo-lts`, because LTS
lags far behind on schema and the DB easily ends up "newer
than the binary" if the operator ever ran a non-LTS forgejo
against the same state dir. Override to `pkgs.forgejo-lts`
if you actively want the slower release train.
'';
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = true;
@ -87,6 +102,7 @@ in
system.stateVersion = "25.11";
services.forgejo = {
enable = true;
package = cfg.package;
database.type = "sqlite3";
lfs.enable = true;
settings = {

35
scripts/forge-create-token.sh Executable file
View file

@ -0,0 +1,35 @@
#!/usr/bin/env bash
# Mint a Forgejo access token for an existing user.
#
# Usage: forge-create-token.sh <username> [--name <label>] [--scopes <csv>]
#
# Defaults:
# --name = local-<timestamp>
# --scopes = all
#
# Prints the token to stdout — feed it to `forge-login.sh` or paste
# into tea / .netrc. Forgejo only shows the token once, so capture it.
set -euo pipefail
if [ $# -lt 1 ]; then
echo "usage: $0 <username> [--name <label>] [--scopes <csv>]" >&2
exit 2
fi
username="$1"; shift
name="local-$(date +%s)"
scopes="all"
while [ $# -gt 0 ]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--scopes) scopes="$2"; shift 2 ;;
*) echo "unknown arg: $1" >&2; exit 2 ;;
esac
done
sudo nixos-container run hive-forge -- runuser -u forgejo -- \
forgejo --work-path /var/lib/forgejo admin user generate-access-token \
--username "$username" \
--token-name "$name" \
--scopes "$scopes"

84
scripts/forge-login.sh Executable file
View file

@ -0,0 +1,84 @@
#!/usr/bin/env bash
# Configure the current shell user's git + tea for the hive-forge.
#
# Sets:
# - git config --global user.name / user.email
# - tea login (if `tea` is on PATH)
# - ~/.netrc entry so `git clone http://...` works without prompting
#
# Usage: forge-login.sh <username> [--email <addr>] [--url <forge-url>]
#
# Prompts for an access token on stdin (paste-and-enter). Generate
# one first with `forge-create-token.sh <username>` or in the web UI
# under Settings → Applications → Generate New Token.
set -euo pipefail
if [ $# -lt 1 ]; then
echo "usage: $0 <username> [--email <addr>] [--url <forge-url>]" >&2
exit 2
fi
username="$1"; shift
email="${username}@hive.local"
forge_url="http://localhost:3000"
while [ $# -gt 0 ]; do
case "$1" in
--email) email="$2"; shift 2 ;;
--url) forge_url="$2"; shift 2 ;;
*) echo "unknown arg: $1" >&2; exit 2 ;;
esac
done
# Extract host:port for netrc.
host=$(printf '%s' "$forge_url" | sed -E 's|^https?://||; s|/.*$||; s|:.*$||')
read -r -s -p "forgejo access token for $username (input hidden): " token
echo
if [ -z "$token" ]; then
echo "no token entered; aborting" >&2
exit 1
fi
if git config --global user.name "$username" 2>/dev/null \
&& git config --global user.email "$email" 2>/dev/null; then
echo "git config: $username <$email>"
else
cat <<EOF
git config: --global write failed (read-only ~/.config/git/config?
home-manager / nix-managed setup). add this to your home-manager:
programs.git = {
enable = true;
userName = "$username";
userEmail = "$email";
};
EOF
fi
# netrc entry — git uses this for HTTP basic auth. 0600 because it
# contains the plaintext token.
netrc="$HOME/.netrc"
touch "$netrc"
chmod 600 "$netrc"
if grep -q "^machine $host" "$netrc" 2>/dev/null; then
# Remove the old block (machine line + the two following lines).
sed -i.bak "/^machine $host\$/,+2d" "$netrc"
fi
cat >>"$netrc" <<EOF
machine $host
login $username
password $token
EOF
echo "netrc: wrote $host entry"
if command -v tea >/dev/null 2>&1; then
mkdir -p "$HOME/.config/tea"
# tea refuses to add a login with a name that already exists; drop
# it first so re-running this script is idempotent.
tea login delete forge 2>/dev/null || true
tea login add --name forge --url "$forge_url" --token "$token"
echo "tea: configured 'forge' login"
else
echo "tea: not on PATH — install pkgs.tea if you want the CLI"
fi

12
scripts/forge-migrate.sh Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
# Run pending Forgejo DB migrations + restart the daemon.
#
# Use after upgrading the forgejo package, or when a CLI command
# errors with "table X has no column named Y" (schema lag).
set -euo pipefail
sudo nixos-container run hive-forge -- runuser -u forgejo -- \
forgejo --work-path /var/lib/forgejo migrate
sudo nixos-container run hive-forge -- systemctl restart forgejo
echo "forge: migrated + restarted"