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"; description = "The Secrert key for Phoenix";
}; };
dbUrl = mkOption { database = {
type = types.string; name = mkOption rec {
default = "postgresql://${config.services.postgresql.user}:${config.services.postgresql.password}@${config.services.postgresql.host}:${toString config.services.postgresql.port}/zoeyscomputer_prod"; type = types.str;
description = "Postgres Databaase URL"; 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 { enableServer = mkOption rec {
@ -97,6 +112,25 @@
locations."/".proxyPass = "http://127.0.0.1:${toString cfg.phx.port}"; 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 systemd.services."zoeyscomputer-phx" = let
release_name = "zoeyscomputer"; release_name = "zoeyscomputer";
working_directory = "/var/lib/zoeycomputer"; working_directory = "/var/lib/zoeycomputer";
@ -117,7 +151,6 @@
PORT = "${toString cfg.phx.port}"; PORT = "${toString cfg.phx.port}";
PHX_HOST = cfg.domain; PHX_HOST = cfg.domain;
PHX_SERVER = toString cfg.phx.enableServer; PHX_SERVER = toString cfg.phx.enableServer;
DATABASE_URL = cfg.phx.dbUrl;
}; };
serviceConfig = { serviceConfig = {
Type = "exec"; Type = "exec";
@ -127,6 +160,18 @@
PrivateTmp = true; PrivateTmp = true;
ExecStart = pkgs.writeShellScript "start-zoeycomputer" '' ExecStart = pkgs.writeShellScript "start-zoeycomputer" ''
export SECRET_KEY_BASE=$(cat ${cfg.phx.secret_key_file}) 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} eval "ZoeysComputer.Release.migrate"
${cfg.phx.package}/bin/${release_name} start ${cfg.phx.package}/bin/${release_name} start
''; '';