2024-05-20 16:27:01 +00:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
2024-06-24 15:38:28 +00:00
|
|
|
cfg = config.aux.system.services.forgejo;
|
2024-05-20 16:27:01 +00:00
|
|
|
cli-cfg = config.services.forgejo;
|
|
|
|
|
|
|
|
forgejo-cli = pkgs.writeScriptBin "forgejo-cli" ''
|
|
|
|
#!${pkgs.runtimeShell}
|
|
|
|
cd ${cli-cfg.stateDir}
|
|
|
|
sudo=exec
|
|
|
|
if [[ "$USER" != forgejo ]]; then
|
|
|
|
sudo='exec /run/wrappers/bin/sudo -u ${cli-cfg.user} -g ${cli-cfg.group} --preserve-env=GITEA_WORK_DIR --preserve-env=GITEA_CUSTOM'
|
|
|
|
fi
|
|
|
|
# Note that these variable names will change
|
|
|
|
export GITEA_WORK_DIR=${cli-cfg.stateDir}
|
|
|
|
export GITEA_CUSTOM=${cli-cfg.customDir}
|
|
|
|
$sudo ${lib.getExe cli-cfg.package} "$@"
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-06-24 15:38:28 +00:00
|
|
|
aux.system.services.forgejo = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables Forgejo Git hosting service.";
|
2024-05-20 16:27:01 +00:00
|
|
|
home = lib.mkOption {
|
2024-05-21 13:22:03 +00:00
|
|
|
default = "";
|
2024-05-20 16:27:01 +00:00
|
|
|
type = lib.types.str;
|
2024-05-21 00:52:57 +00:00
|
|
|
description = "Where to store Forgejo's files";
|
2024-06-04 18:18:45 +00:00
|
|
|
example = "/home/forgejo";
|
|
|
|
};
|
|
|
|
url = lib.mkOption {
|
|
|
|
default = "";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The complete URL where Forgejo is hosted.";
|
|
|
|
example = "https://forgejo.example.com";
|
|
|
|
};
|
|
|
|
actions = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables a local Forgejo Actions runner.";
|
2024-06-04 18:18:45 +00:00
|
|
|
token = lib.mkOption {
|
|
|
|
default = "";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Token used to authenticate the runner with Forgejo.";
|
|
|
|
};
|
2024-05-20 16:27:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
environment.systemPackages = [
|
|
|
|
forgejo-cli
|
|
|
|
pkgs.podman-tui
|
|
|
|
];
|
|
|
|
services = {
|
|
|
|
forgejo = {
|
|
|
|
enable = true;
|
|
|
|
settings.server = {
|
2024-09-09 02:34:29 +00:00
|
|
|
DOMAIN =
|
|
|
|
let
|
|
|
|
parsedURL = (lib.strings.splitString "." cfg.url);
|
|
|
|
in
|
|
|
|
builtins.concatStringsSep "." [
|
|
|
|
(builtins.elemAt parsedURL 1)
|
|
|
|
(builtins.elemAt parsedURL 2)
|
|
|
|
];
|
2024-07-02 20:19:15 +00:00
|
|
|
ROOT_URL = cfg.url;
|
|
|
|
HTTP_PORT = 3000;
|
|
|
|
};
|
|
|
|
useWizard = true;
|
|
|
|
} // lib.optionalAttrs (cfg.home != null) { stateDir = cfg.home; };
|
2024-05-27 16:32:24 +00:00
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
nginx.virtualHosts."${cfg.url}" = {
|
2024-09-09 03:47:53 +00:00
|
|
|
useACMEHost = pkgs.util.getDomainFromURL cfg.url;
|
2024-07-02 20:19:15 +00:00
|
|
|
forceSSL = true;
|
|
|
|
locations."/" = {
|
|
|
|
proxyPass = "http://127.0.0.1:3000";
|
|
|
|
proxyWebsockets = true;
|
|
|
|
extraConfig = "proxy_ssl_server_name on;"; # required when the target is also TLS server with multiple hosts
|
2024-06-25 18:13:15 +00:00
|
|
|
};
|
2024-07-02 20:19:15 +00:00
|
|
|
};
|
2024-06-25 18:13:15 +00:00
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
# Enable runner for CI actions
|
|
|
|
gitea-actions-runner = lib.mkIf cfg.actions.enable {
|
|
|
|
package = pkgs.forgejo-actions-runner;
|
|
|
|
instances.default = {
|
|
|
|
enable = true;
|
|
|
|
name = config.networking.hostName;
|
2024-08-22 20:26:30 +00:00
|
|
|
url = "https://${cfg.url}";
|
2024-07-02 20:19:15 +00:00
|
|
|
token = cfg.actions.token;
|
|
|
|
labels = [
|
|
|
|
"nix:docker://nixos/nix" # Shoutout to Icewind 1991 for this syntax: https://icewind.nl/entry/gitea-actions-nix/
|
|
|
|
"debian:docker://node:20-bullseye"
|
|
|
|
"ubuntu-latest:docker://ubuntu:latest"
|
|
|
|
];
|
|
|
|
settings = {
|
|
|
|
# For an example of configuring in Nix: https://git.clan.lol/clan/clan-infra/src/branch/main/modules/web01/gitea/actions-runner.nix
|
|
|
|
# For an example of the different options available: https://gitea.com/gitea/act_runner/src/branch/main/internal/pkg/config/config.example.yaml
|
|
|
|
container.options = "-v /nix:/nix";
|
|
|
|
container.validVolumes = [ "/nix" ];
|
2024-05-27 17:30:16 +00:00
|
|
|
};
|
2024-05-27 16:32:24 +00:00
|
|
|
};
|
|
|
|
};
|
2024-07-02 20:19:15 +00:00
|
|
|
};
|
2024-05-27 16:32:24 +00:00
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
# Enable Podman for running...uh, runners.
|
|
|
|
virtualisation = lib.mkIf cfg.actions.enable {
|
|
|
|
containers.enable = true;
|
|
|
|
podman = {
|
|
|
|
enable = true;
|
2024-05-27 16:32:24 +00:00
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
# Create a `docker` alias for podman, to use it as a drop-in replacement
|
|
|
|
dockerCompat = true;
|
2024-05-27 16:32:24 +00:00
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
# Required for containers under podman-compose to be able to talk to each other.
|
|
|
|
defaultNetwork.settings.dns_enabled = true;
|
2024-05-27 16:32:24 +00:00
|
|
|
};
|
2024-07-02 20:19:15 +00:00
|
|
|
};
|
2024-05-20 16:27:01 +00:00
|
|
|
|
2024-07-02 20:19:15 +00:00
|
|
|
# Allow containers to make DNS queries (https://www.reddit.com/r/NixOS/comments/199f16j/why_dont_my_podman_containers_have_internet_access/)
|
|
|
|
networking.firewall.interfaces.podman4 = lib.mkIf cfg.actions.enable {
|
|
|
|
allowedTCPPorts = [ 53 ];
|
|
|
|
allowedUDPPorts = [ 53 ];
|
|
|
|
};
|
2024-08-29 15:59:28 +00:00
|
|
|
|
|
|
|
systemd.services = {
|
2024-09-07 17:44:14 +00:00
|
|
|
forgejo.unitConfig.RequiresMountsFor = cfg.home;
|
2024-08-29 15:59:28 +00:00
|
|
|
nginx.wants = [ config.systemd.services.forgejo.name ];
|
|
|
|
};
|
2024-07-02 20:19:15 +00:00
|
|
|
};
|
2024-05-20 16:27:01 +00:00
|
|
|
}
|