Files
system-config/system/hosts/mcp/containers/lib.nix

99 lines
2.8 KiB
Nix

config:
let
havenisms = "havenisms.com";
blazestar = "blazestar.net";
hostRule = host: domain: "Host(`${host}.${domain}`)";
hostRuleHavenisms = host: hostRule host havenisms;
localNet = "192.168.0.0/16";
dockerNet = "10.88.0.0/16";
localNetRule = "(ClientIP(`${localNet}`) || ClientIP(`${dockerNet}`))";
localHostRule = host: domain: "${localNetRule} && ${hostRule host domain}";
localHostRuleHavenisms = host: localHostRule host havenisms;
in
{
inherit
hostRule
localHostRule
hostRuleHavenisms
localHostRuleHavenisms
havenisms
blazestar
;
mkContainer =
{
image,
hostName,
port,
homepageOpts,
dependsOn ? [ ],
domain ? havenisms,
ports ? [ ],
volumes ? [ ],
environment ? { },
environmentFiles ? [ ],
public ? false,
}:
let
routerRule = if public then hostRule hostName domain else localHostRule hostName domain;
in
{
inherit
image
dependsOn
volumes
environment
environmentFiles
ports
;
hostname = "${hostName}.${domain}";
autoStart = true;
extraOptions = [
"-l=traefik.enable=true"
"-l=traefik.http.routers.${hostName}.rule=${routerRule}"
"-l=traefik.http.services.${hostName}.loadbalancer.server.port=${toString port}"
"-l=homepage.group=${homepageOpts.group}"
"-l=homepage.name=${homepageOpts.name}"
"-l=homepage.icon=${homepageOpts.icon}"
"-l=homepage.href=https://${hostName}.${domain}"
"-l=homepage.description=${homepageOpts.description}"
];
};
# Creates a MariaDB container for a specific app. It should be safe to give
# it the same UID and GID as the app it is made for. The contaner will be
# named `${name}-mariadb`. The database name is the same as the database
# user.
#
# Note that this returns a _module_ so that it can be imported and provide many different config values.
mkMariaDbContainer =
{
name,
uid,
gid,
passwordSecret,
directory,
}:
{ config, ... }:
{
virtualisation.oci-containers.containers."${name}-mariadb" = {
image = "lscr.io/linuxserver/mariadb:latest";
autoStart = true;
ports = [ "3306:3306" ];
volumes = [
"${directory}:/config"
"${config.sops.secrets.mariadb_root_password.path}:/run/secrets/mariadb_root_password"
"${config.sops.secrets."${passwordSecret}".path}:/run/secrets/mariadb_password"
];
environment = {
PUID = "${toString uid}";
PGID = "${toString gid}";
MYSQL_USER = name;
MYSQL_DATABASE = name;
FILE__MYSQL_ROOT_PASSWORD = "/run/secrets/mariadb_root_password";
FILE__MYSQL_PASSWORD = "/run/secrets/mariadb_password";
};
};
};
}