about summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/lemmy.nix
diff options
context:
space:
mode:
authorBrian Leung <leungbk@posteo.net>2022-09-21 00:48:02 -0700
committerYt <happysalada@proton.me>2022-09-21 09:00:52 -0400
commite5f798f3b98ad370c9b7c8380de54c86549bbf18 (patch)
treec748857f3f246ce286e580d533300a104ce6f0a0 /nixos/modules/services/web-apps/lemmy.nix
parent3de898f26272c261b4e186bc8744080e22019068 (diff)
downloadnixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.tar
nixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.tar.gz
nixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.tar.bz2
nixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.tar.lz
nixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.tar.xz
nixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.tar.zst
nixlib-e5f798f3b98ad370c9b7c8380de54c86549bbf18.zip
nixos/lemmy: use PostgreSQL module to ensure database/user existence
Co-authored-by: Shahar Dawn Or <mightyiampresence@gmail.com>
Co-authored-by: a-kenji <aks.kenji@protonmail.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
Co-authored-by: Ilan Joselevich <personal@ilanjoselevich.com>
Diffstat (limited to 'nixos/modules/services/web-apps/lemmy.nix')
-rw-r--r--nixos/modules/services/web-apps/lemmy.nix32
1 files changed, 8 insertions, 24 deletions
diff --git a/nixos/modules/services/web-apps/lemmy.nix b/nixos/modules/services/web-apps/lemmy.nix
index 24ba91df6148..267584dd0ca7 100644
--- a/nixos/modules/services/web-apps/lemmy.nix
+++ b/nixos/modules/services/web-apps/lemmy.nix
@@ -98,7 +98,12 @@ in
       });
 
       services.postgresql = mkIf cfg.database.createLocally {
-        enable = mkDefault true;
+        enable = true;
+        ensureDatabases = [ cfg.settings.database.database ];
+        ensureUsers = [{
+          name = cfg.settings.database.user;
+          ensurePermissions."DATABASE ${cfg.settings.database.database}" = "ALL PRIVILEGES";
+        }];
       };
 
       services.pict-rs.enable = true;
@@ -159,9 +164,9 @@ in
 
         wantedBy = [ "multi-user.target" ];
 
-        after = [ "pict-rs.service" ] ++ lib.optionals cfg.database.createLocally [ "lemmy-postgresql.service" ];
+        after = [ "pict-rs.service" ] ++ lib.optionals cfg.database.createLocally [ "postgresql.service" ];
 
-        requires = lib.optionals cfg.database.createLocally [ "lemmy-postgresql.service" ];
+        requires = lib.optionals cfg.database.createLocally [ "postgresql.service" ];
 
         serviceConfig = {
           DynamicUser = true;
@@ -198,27 +203,6 @@ in
           ExecStart = "${pkgs.nodejs}/bin/node ${pkgs.lemmy-ui}/dist/js/server.js";
         };
       };
-
-      systemd.services.lemmy-postgresql = mkIf cfg.database.createLocally {
-        description = "Lemmy postgresql db";
-        after = [ "postgresql.service" ];
-        partOf = [ "lemmy.service" ];
-        script = with cfg.settings.database; ''
-          PSQL() {
-            ${config.services.postgresql.package}/bin/psql --port=${toString cfg.settings.database.port} "$@"
-          }
-          # check if the database already exists
-          if ! PSQL -lqt | ${pkgs.coreutils}/bin/cut -d \| -f 1 | ${pkgs.gnugrep}/bin/grep -qw ${database} ; then
-            PSQL -tAc "CREATE ROLE ${user} WITH LOGIN;"
-            PSQL -tAc "CREATE DATABASE ${database} WITH OWNER ${user};"
-          fi
-        '';
-        serviceConfig = {
-          User = config.services.postgresql.superUser;
-          Type = "oneshot";
-          RemainAfterExit = true;
-        };
-      };
     };
 
 }