diff options
author | Samuel Leathers <disasm@gmail.com> | 2018-03-15 14:01:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-15 14:01:01 -0400 |
commit | 5931f463ad7ce027d20041a0e316275dbda6d77b (patch) | |
tree | 534df6b606901d92ec3b03441ec622033977aeda /nixos | |
parent | b269a687c0c12c45c6db3bd18b6229d7585b79f2 (diff) | |
parent | 1ad75d0c501fb757c7bceb346efe130bad48db1f (diff) | |
download | nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.tar nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.tar.gz nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.tar.bz2 nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.tar.lz nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.tar.xz nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.tar.zst nixlib-5931f463ad7ce027d20041a0e316275dbda6d77b.zip |
Merge pull request #36924 from etu/gitea-postgres-autocreate-database
Gitea postgres autocreate database
Diffstat (limited to 'nixos')
-rw-r--r-- | nixos/modules/services/misc/gitea.nix | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/nixos/modules/services/misc/gitea.nix b/nixos/modules/services/misc/gitea.nix index f0b44b7bedeb..458d0087dbda 100644 --- a/nixos/modules/services/misc/gitea.nix +++ b/nixos/modules/services/misc/gitea.nix @@ -4,6 +4,8 @@ with lib; let cfg = config.services.gitea; + pg = config.services.postgresql; + usePostgresql = cfg.database.type == "postgres"; configFile = pkgs.writeText "app.ini" '' APP_NAME = ${cfg.appName} RUN_USER = ${cfg.user} @@ -16,6 +18,9 @@ let USER = ${cfg.database.user} PASSWD = #dbpass# PATH = ${cfg.database.path} + ${optionalString usePostgresql '' + SSL_MODE = disable + ''} [repository] ROOT = ${cfg.repositoryRoot} @@ -82,7 +87,7 @@ in port = mkOption { type = types.int; - default = 3306; + default = (if !usePostgresql then 3306 else pg.port); description = "Database host port."; }; @@ -123,6 +128,15 @@ in default = "${cfg.stateDir}/data/gitea.db"; 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 { @@ -186,10 +200,11 @@ in }; config = mkIf cfg.enable { + services.postgresql.enable = mkIf usePostgresql (mkDefault true); systemd.services.gitea = { description = "gitea"; - after = [ "network.target" ]; + after = [ "network.target" "postgresql.service" ]; wantedBy = [ "multi-user.target" ]; path = [ pkgs.gitea.bin ]; @@ -231,12 +246,31 @@ in mkdir -p ${cfg.stateDir}/conf cp -r ${pkgs.gitea.out}/locale ${cfg.stateDir}/conf/locale 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 = { Type = "simple"; User = cfg.user; WorkingDirectory = cfg.stateDir; + PermissionsStartOnly = true; ExecStart = "${pkgs.gitea.bin}/bin/gitea web"; Restart = "always"; }; |