mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-02-24 05:00:19 +00:00
gitea: enable and configure postgres service if selected as database
This commit is contained in:
parent
29d46471e0
commit
1ad75d0c50
@ -4,6 +4,8 @@ with lib;
|
|||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.services.gitea;
|
cfg = config.services.gitea;
|
||||||
|
pg = config.services.postgresql;
|
||||||
|
usePostgresql = cfg.database.type == "postgres";
|
||||||
configFile = pkgs.writeText "app.ini" ''
|
configFile = pkgs.writeText "app.ini" ''
|
||||||
APP_NAME = ${cfg.appName}
|
APP_NAME = ${cfg.appName}
|
||||||
RUN_USER = ${cfg.user}
|
RUN_USER = ${cfg.user}
|
||||||
@ -16,6 +18,9 @@ let
|
|||||||
USER = ${cfg.database.user}
|
USER = ${cfg.database.user}
|
||||||
PASSWD = #dbpass#
|
PASSWD = #dbpass#
|
||||||
PATH = ${cfg.database.path}
|
PATH = ${cfg.database.path}
|
||||||
|
${optionalString usePostgresql ''
|
||||||
|
SSL_MODE = disable
|
||||||
|
''}
|
||||||
|
|
||||||
[repository]
|
[repository]
|
||||||
ROOT = ${cfg.repositoryRoot}
|
ROOT = ${cfg.repositoryRoot}
|
||||||
@ -82,7 +87,7 @@ in
|
|||||||
|
|
||||||
port = mkOption {
|
port = mkOption {
|
||||||
type = types.int;
|
type = types.int;
|
||||||
default = 3306;
|
default = (if !usePostgresql then 3306 else pg.port);
|
||||||
description = "Database host port.";
|
description = "Database host port.";
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -123,6 +128,15 @@ in
|
|||||||
default = "${cfg.stateDir}/data/gitea.db";
|
default = "${cfg.stateDir}/data/gitea.db";
|
||||||
description = "Path to the sqlite3 database file.";
|
description = "Path to the sqlite3 database file.";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
createDatabase = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = true;
|
||||||
|
description = ''
|
||||||
|
Whether to create a local postgresql database automatically.
|
||||||
|
This only applies if database type "postgres" is selected.
|
||||||
|
'';
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
appName = mkOption {
|
appName = mkOption {
|
||||||
@ -186,10 +200,11 @@ in
|
|||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
|
services.postgresql.enable = mkIf usePostgresql (mkDefault true);
|
||||||
|
|
||||||
systemd.services.gitea = {
|
systemd.services.gitea = {
|
||||||
description = "gitea";
|
description = "gitea";
|
||||||
after = [ "network.target" ];
|
after = [ "network.target" "postgresql.service" ];
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
path = [ pkgs.gitea.bin ];
|
path = [ pkgs.gitea.bin ];
|
||||||
|
|
||||||
@ -231,12 +246,31 @@ in
|
|||||||
mkdir -p ${cfg.stateDir}/conf
|
mkdir -p ${cfg.stateDir}/conf
|
||||||
cp -r ${pkgs.gitea.out}/locale ${cfg.stateDir}/conf/locale
|
cp -r ${pkgs.gitea.out}/locale ${cfg.stateDir}/conf/locale
|
||||||
fi
|
fi
|
||||||
|
'' + optionalString (usePostgresql && cfg.database.createDatabase) ''
|
||||||
|
if ! test -e "${cfg.stateDir}/db-created"; then
|
||||||
|
echo "CREATE ROLE ${cfg.database.user}
|
||||||
|
WITH ENCRYPTED PASSWORD '$(head -n1 ${cfg.database.passwordFile})'
|
||||||
|
NOCREATEDB NOCREATEROLE LOGIN" |
|
||||||
|
${pkgs.sudo}/bin/sudo -u ${pg.superUser} ${pg.package}/bin/psql
|
||||||
|
${pkgs.sudo}/bin/sudo -u ${pg.superUser} \
|
||||||
|
${pg.package}/bin/createdb \
|
||||||
|
--owner=${cfg.database.user} \
|
||||||
|
--encoding=UTF8 \
|
||||||
|
--lc-collate=C \
|
||||||
|
--lc-ctype=C \
|
||||||
|
--template=template0 \
|
||||||
|
${cfg.database.name}
|
||||||
|
touch "${cfg.stateDir}/db-created"
|
||||||
|
fi
|
||||||
|
'' + ''
|
||||||
|
chown ${cfg.user} -R ${cfg.stateDir}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
User = cfg.user;
|
User = cfg.user;
|
||||||
WorkingDirectory = cfg.stateDir;
|
WorkingDirectory = cfg.stateDir;
|
||||||
|
PermissionsStartOnly = true;
|
||||||
ExecStart = "${pkgs.gitea.bin}/bin/gitea web";
|
ExecStart = "${pkgs.gitea.bin}/bin/gitea web";
|
||||||
Restart = "always";
|
Restart = "always";
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user