nixos-config/modules/gitea.nix

117 lines
4.5 KiB
Nix
Raw Normal View History

2022-06-12 15:39:15 +00:00
{
config,
lib,
options,
pkgs,
...
}:
with lib; let
2022-02-18 13:17:35 +00:00
cfg = config.services.gitea;
2023-04-21 06:19:40 +00:00
opt = options.services.gitea;
exe = lib.getExe cfg.package;
pg = config.services.postgresql;
useMysql = cfg.database.type == "mysql";
usePostgresql = cfg.database.type == "postgres";
useSqlite = cfg.database.type == "sqlite3";
format = pkgs.formats.ini {};
2022-02-18 13:17:35 +00:00
configFile = pkgs.writeText "app.ini" ''
APP_NAME = ${cfg.appName}
RUN_USER = ${cfg.user}
RUN_MODE = prod
${generators.toINI {} cfg.settings}
${optionalString (cfg.extraConfig != null) cfg.extraConfig}
'';
2022-06-12 15:39:15 +00:00
in {
2022-02-18 13:17:35 +00:00
options = {
services.gitea = {
storageSecretFile = mkOption {
type = with types; str;
default = "";
description = ''
Storage secret to be inserted into the config at #STORAGE_SECRET#
'';
example = literalExpression ''"/run/secrets/gitea/storage-secret"'';
};
};
};
config = mkIf cfg.enable {
systemd.services.gitea = {
2022-07-06 10:50:25 +00:00
path = [pkgs.gnupg];
2022-07-04 09:37:19 +00:00
serviceConfig = {
2022-10-26 18:36:48 +00:00
SystemCallFilter = mkForce "~@clock @cpu-emulation @debug @module @mount @obsolete @raw-io @reboot @setuid @swap";
2023-04-08 13:17:26 +00:00
ReadWritePaths = ["/var/lib/gitea/.gnupg"];
2023-07-22 09:12:18 +00:00
TimeoutSec = "infinity";
2022-07-04 09:37:19 +00:00
};
2023-04-21 06:19:40 +00:00
2022-02-18 13:17:35 +00:00
# In older versions the secret naming for JWT was kind of confusing.
# The file jwt_secret hold the value for LFS_JWT_SECRET and JWT_SECRET
2023-04-21 06:19:40 +00:00
# wasn't persistent at all.
2022-02-18 13:17:35 +00:00
# To fix that, there is now the file oauth2_jwt_secret containing the
# values for JWT_SECRET and the file jwt_secret gets renamed to
# lfs_jwt_secret.
# We have to consider this to stay compatible with older installations.
2022-06-12 15:39:15 +00:00
preStart = let
2023-04-21 06:19:40 +00:00
runConfig = "${cfg.customDir}/conf/app.ini";
secretKey = "${cfg.customDir}/conf/secret_key";
oauth2JwtSecret = "${cfg.customDir}/conf/oauth2_jwt_secret";
oldLfsJwtSecret = "${cfg.customDir}/conf/jwt_secret"; # old file for LFS_JWT_SECRET
lfsJwtSecret = "${cfg.customDir}/conf/lfs_jwt_secret"; # new file for LFS_JWT_SECRET
internalToken = "${cfg.customDir}/conf/internal_token";
replaceSecretBin = "${pkgs.replace-secret}/bin/replace-secret";
2023-04-23 17:35:42 +00:00
in
lib.mkForce ''
# copy custom configuration and generate random secrets if needed
${optionalString (!cfg.useWizard) ''
function gitea_setup {
cp -f '${configFile}' '${runConfig}'
if [ ! -s '${secretKey}' ]; then
${exe} generate secret SECRET_KEY > '${secretKey}'
fi
# Migrate LFS_JWT_SECRET filename
if [[ -s '${oldLfsJwtSecret}' && ! -s '${lfsJwtSecret}' ]]; then
mv '${oldLfsJwtSecret}' '${lfsJwtSecret}'
fi
if [ ! -s '${oauth2JwtSecret}' ]; then
${exe} generate secret JWT_SECRET > '${oauth2JwtSecret}'
fi
${lib.optionalString cfg.lfs.enable ''
if [ ! -s '${lfsJwtSecret}' ]; then
${exe} generate secret LFS_JWT_SECRET > '${lfsJwtSecret}'
fi
''}
if [ ! -s '${internalToken}' ]; then
${exe} generate secret INTERNAL_TOKEN > '${internalToken}'
fi
chmod u+w '${runConfig}'
${replaceSecretBin} '#secretkey#' '${secretKey}' '${runConfig}'
${replaceSecretBin} '#dbpass#' '${cfg.database.passwordFile}' '${runConfig}'
${replaceSecretBin} '#oauth2jwtsecret#' '${oauth2JwtSecret}' '${runConfig}'
${replaceSecretBin} '#internaltoken#' '${internalToken}' '${runConfig}'
${lib.optionalString cfg.lfs.enable ''
${replaceSecretBin} '#lfsjwtsecret#' '${lfsJwtSecret}' '${runConfig}'
''}
${lib.optionalString (cfg.mailerPasswordFile != null) ''
${replaceSecretBin} '#mailerpass#' '${cfg.mailerPasswordFile}' '${runConfig}'
''}
${lib.optionalString (cfg.storageSecretFile != null) ''
${replaceSecretBin} '#storageSecret#' '${cfg.storageSecretFile}' '${runConfig}'
''}
chmod u-w '${runConfig}'
}
(umask 027; gitea_setup)
2023-04-21 06:19:40 +00:00
''}
2023-04-23 17:35:42 +00:00
# run migrations/init the database
${exe} migrate
# update all hooks' binary paths
${exe} admin regenerate hooks
# update command option in authorized_keys
if [ -r ${cfg.stateDir}/.ssh/authorized_keys ]
then
${exe} admin regenerate keys
fi
'';
2022-02-18 13:17:35 +00:00
};
};
}