change db

This commit is contained in:
zack 2024-10-21 20:07:13 -04:00
parent 610db6ab0c
commit 69fd778120
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35

View file

@ -75,10 +75,25 @@
description = "The Secrert key for Phoenix";
};
dbUrl = mkOption {
type = types.string;
default = "postgresql://${config.services.postgresql.user}:${config.services.postgresql.password}@${config.services.postgresql.host}:${toString config.services.postgresql.port}/zoeyscomputer_prod";
description = "Postgres Databaase URL";
database = {
name = mkOption rec {
type = types.str;
default = "zoeyscomputer_prod";
example = default;
description = "Database name";
};
user = mkOption rec {
type = types.str;
default = "zoeyscomputer";
example = default;
description = "Database user";
};
passwordFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/run/secrets/db_password";
description = "File containing the database password";
};
};
enableServer = mkOption rec {
@ -97,6 +112,25 @@
locations."/".proxyPass = "http://127.0.0.1:${toString cfg.phx.port}";
};
# Ensure database exists
services.postgresql.ensureDatabases = [
cfg.phx.database.name
];
# Ensure database user exists
services.postgresql.ensureUsers = let
userAttrs = {
name = cfg.phx.database.user;
ensureDBOwnership = true;
};
userWithPassword =
if cfg.phx.database.passwordFile != null
then userAttrs // {passwordFile = cfg.phx.database.passwordFile;}
else userAttrs;
in [
userWithPassword
];
systemd.services."zoeyscomputer-phx" = let
release_name = "zoeyscomputer";
working_directory = "/var/lib/zoeycomputer";
@ -117,7 +151,6 @@
PORT = "${toString cfg.phx.port}";
PHX_HOST = cfg.domain;
PHX_SERVER = toString cfg.phx.enableServer;
DATABASE_URL = cfg.phx.dbUrl;
};
serviceConfig = {
Type = "exec";
@ -127,6 +160,18 @@
PrivateTmp = true;
ExecStart = pkgs.writeShellScript "start-zoeycomputer" ''
export SECRET_KEY_BASE=$(cat ${cfg.phx.secret_key_file})
${
if cfg.phx.database.passwordFile != null
then ''
DB_PASSWORD=$(cat ${cfg.phx.database.passwordFile})
export DATABASE_URL="postgresql://${cfg.phx.database.user}:$DB_PASSWORD@${cfg.phx.database.host}/${cfg.phx.database.name}"
''
else ''
export DATABASE_URL="postgresql://${cfg.phx.database.user}@${cfg.phx.database.host}/${cfg.phx.database.name}"
''
}
${cfg.phx.package}/bin/${release_name} eval "ZoeysComputer.Release.migrate"
${cfg.phx.package}/bin/${release_name} start
'';