Changes to how lemmy image is generated

This commit is contained in:
niten 2023-07-16 10:48:07 -07:00
parent 0e42173d73
commit 9474fbd06a
2 changed files with 78 additions and 82 deletions

View File

@ -1,12 +1,17 @@
{ {
description = "Lemmy via Docker Compose on NixOS"; description = "Lemmy via Docker Compose on NixOS";
inputs = { nixpkgs.url = "nixpkgs/nixos-23.05"; }; inputs = {
nixpkgs.url = "nixpkgs/nixos-23.05";
arion.url = "github:hercules-ci/arion";
};
outputs = { self, nixpkgs, ... }: { outputs = { self, nixpkgs, arion, ... }: {
nixosModules = rec { nixosModules = rec {
default = lemmyDocker; default = lemmyDocker;
lemmyDocker = import ./lemmy-docker.nix; lemmyDocker = { ... }: {
imports = [ arion.nixosModules.arion ./lemmy-docker.nix ];
};
}; };
}; };
} }

View File

@ -1,5 +1,3 @@
arion:
{ config, lib, pkgs, ... }@toplevel: { config, lib, pkgs, ... }@toplevel:
with lib; with lib;
@ -8,72 +6,54 @@ let
hostSecrets = config.fudo.secrets.host-secrets."${config.instance.hostname}"; hostSecrets = config.fudo.secrets.host-secrets."${config.instance.hostname}";
lemmyImage = { hostname, port, lemmyCfgFile, nginxCfgFile, postgresCfgFile makeEnvFile = envVars:
, lemmyDockerImage, lemmyUiDockerImage, pictrsDockerImage let envLines = mapAttrsToList (var: val: ''${val}="${val}"'') envVars;
, postgresDockerImage, stateDirectory, ... }: in pkgs.writeText "envFile" (concatStringsSep "\n" envLines);
makeLemmyImage = { port, stateDirectory, proxyCfg, lemmyCfg, lemmyUiCfg
, pictrsCfg, postgresCfg, ... }:
{ pkgs, ... }: { { pkgs, ... }: {
project.name = "lemmy"; project.name = "lemmy";
services = { services = {
proxy = { proxy = {
service = { service = {
image = "nginx:1-alpine"; image = proxyCfg.image;
ports = [ "${port}:8536" ]; ports = [ "${port}:8536" ];
volumes = [ "${nginxCfgFile}:/etc/nginx/nginx.conf:ro,Z" ]; volumes = [ "${proxyCfg.configFile}:/etc/nginx/nginx.conf:ro,Z" ];
depends_on = [ "pictrs" "lemmy-ui" ]; depends_on = [ "pictrs" "lemmy-ui" ];
}; };
}; };
lemmy = { lemmy = {
service = { service = {
image = lemmyDockerImage; image = lemmyCfg.image;
hostname = "lemmy"; hostname = "lemmy";
environment.RUST_LOG = "warn"; env_file = lemmyCfg.envFile;
volumes = [ "${lemmyCfgFile}:/config/config.hjson:ro,Z" ]; volumes = [ "${lemmyCfg.configFile}:/config/config.hjson:ro,Z" ];
depends_on = [ "postgres" "pictrs" ]; depends_on = [ "postgres" "pictrs" ];
}; };
}; };
lemmy-ui = { lemmy-ui = {
service = { service = {
image = lemmyUiDockerImage; image = lemmyUiCfg.image;
hostname = "lemmy-ui"; hostname = "lemmy-ui";
environment = {
LEMMY_UI_LEMMY_INTERNAL_HOST = "lemmy:8536";
LEMMY_UI_LEMMY_EXTERNAL_HOST = hostname;
LEMMY_UI_HTTPS = true;
};
depends_on = [ "lemmy" ]; depends_on = [ "lemmy" ];
}; };
}; };
pictrs = { pictrs = {
service = { service = {
image = pictrsDockerImage; image = pictrsCfg.image;
hostname = "pictrs"; hostname = "pictrs";
environment = {
PICTRS_OPENTELEMETRY_URL = "http://otel:4137";
PICTRS__API_KEY = "pictrsApiKey";
RUST_LOG = "debug";
RUST_BACKTRACE = "full";
PICTRS__MEDIA__VIDEO_CODEC = "vp9";
PICTRS__MEDIA__GIF__MAX_WIDTH = "256";
PICTRS__MEDIA__GIF__MAX_HEIGHT = "256";
PICTRS__MEDIA__GIF__MAX_AREA = "65536";
PICTRS__MEDIA__GIF__MAX_FRAME_COUNT = "400";
};
volumes = [ "${stateDirectory}/pictrs:/mnt:Z" ]; volumes = [ "${stateDirectory}/pictrs:/mnt:Z" ];
service.user = "991:991"; service.user = "991:991";
}; };
}; };
postgres = { postgres = {
service = { service = {
image = postgresDockerImage; image = postgresCfg.image;
hostname = "postgres"; hostname = "postgres";
environment = {
POSTGRES_USER = "lemmy";
POSTGRES_PASSWORD = postgresPassword;
POSTGRES_DB = "lemmy";
};
volumes = [ volumes = [
"${stateDirectory}/postgres:/var/lib/postgresql/data:Z" "${stateDirectory}/postgres:/var/lib/postgresql/data:Z"
"${postgresCfg}:/etc/postgresql.conf" "${postgresCfg.configFile}:/etc/postgresql.conf"
]; ];
}; };
}; };
@ -162,7 +142,7 @@ let
}; };
}); });
postgresCfg = pkgs.writeText "lemmy-postgres.conf" '' postgresCfgFile = pkgs.writeText "lemmy-postgres.conf" ''
# DB Version: 15 # DB Version: 15
# OS Type: linux # OS Type: linux
# DB Type: web # DB Type: web
@ -315,8 +295,6 @@ in {
}; };
}; };
imports = [ arion.nixosModule.arion ];
config = mkIf cfg.enable (let config = mkIf cfg.enable (let
postgresPasswd = postgresPasswd =
readFile (pkgs.lib.passwd.random-passwd-file "lemmy-postgres-passwd" 30); readFile (pkgs.lib.passwd.random-passwd-file "lemmy-postgres-passwd" 30);
@ -324,55 +302,68 @@ in {
readFile (pkgs.lib.passwd.random-passwd-file "lemmy-pictrs-api-key" 30); readFile (pkgs.lib.passwd.random-passwd-file "lemmy-pictrs-api-key" 30);
in { in {
fudo.secrets.host-secrets."${config.instance.hostname}" = { fudo.secrets.host-secrets."${config.instance.hostname}" = {
lemmyDockerEnv = { lemmyPictrsEnv = {
source-file = pkgs.writeText "lemmy-docker-env" '' source-file = makeEnvFile {
PICTRS__API_KEY=\"${pictrsApiKey}\" PICTRS_OPENTELEMETRY_URL = "http://otel:4137";
POSTGRES_PASSWORD=\"${postgresPasswd}\" PICTRS__MEDIA__VIDEO_CODEC = "vp9";
''; PICTRS__MEDIA__GIF__MAX_WIDTH = "256";
target-file = "/run/lemmy-docker/env"; PICTRS__MEDIA__GIF__MAX_HEIGHT = "256";
PICTRS__MEDIA__GIF__MAX_AREA = "65536";
PICTRS__MEDIA__GIF__MAX_FRAME_COUNT = "400";
PICTRS__API_KEY = pictrsApiKey;
RUST_LOG = "debug";
};
target-file = "/run/lemmy/pictrs.env";
};
lemmyPostgresEnv = {
source-file = makeEnvFile {
POSTGRES_USER = "lemmy";
POSTGRES_PASSWORD = postgresPasswd;
POSTGRES_DB = "lemmy";
};
target-file = "/run/lemmy/postgres.env";
}; };
}; };
virtualisation = { virtualisation = {
arion = { arion = {
backend = "podman-socket"; backend = "podman-socket";
projects.lemmy.settings = { projects.lemmy.settings = let
lemmyImage = makeLemmyImage {
}; port = cfg.port;
};
oci-containers.containers.lemmy = {
# Not sure what the image should be...
image = "lemmy/lemmy";
imageFile = let
image = lemmyDockerImage {
inherit (cfg) hostname port;
lemmyDockerImage = cfg.docker-images.lemmy;
lemmyUiDockerImage = cfg.docker-images.lemmy-ui;
pictrsDockerImage = cfg.docker-images.pictrs;
postgresDockerImage = cfg.docker-images.postgres;
stateDirectory = cfg.state-directory; stateDirectory = cfg.state-directory;
smtpServer = cfg.smtp-server; proxyCfg = {
inherit postgresPasswd pictrsApiKey nginxCfgFile postgresCfg; image = "nginx:1-alpine";
configFile = nginxCfgFile;
}; };
in "${image}"; lemmyCfg = {
autoStart = true; image = cfg.docker-images.lemmy;
environment = { configFile = makeLemmyImage {
inherit (cfg) hostname;
inherit postgresPasswd pictrsApiKey;
smtpServer = cfg.smtp-server;
};
envFile = makeEnvFile { RUST_LOG = "warn"; };
};
lemmyUiCfg = {
image = cfg.docker-images.lemmy-ui;
envFile = mkEnvFile {
LEMMY_UI_LEMMY_INTERNAL_HOST = "lemmy:8536"; LEMMY_UI_LEMMY_INTERNAL_HOST = "lemmy:8536";
LEMMY_UI_LEMMY_EXTERNAL_HOST = cfg.hostname; LEMMY_UI_LEMMY_EXTERNAL_HOST = cfg.hostname;
LEMMY_UI_HTTPS = "false"; LEMMY_UI_HTTPS = true;
PICTRS_OPENTELEMETRY_URL = "http://otel:4137";
RUST_LOG = "debug";
RUST_BACKTRACE = "full";
PICTRS__MEDIA__VIDEO_CODEC = "vp9";
PICTRS__MEDIA__GIF__MAX_WIDTH = "256";
PICTRS__MEDIA__GIF__MAX_HEIGHT = "256";
PICTRS__MEDIA__GIF__MAX_AREA = "65536";
PICTRS__MEDIA__GIF__MAX_FRAME_COUNT = "400";
POSTGRES_USER = "lemmy";
POSTGRES_DB = "lemmy";
}; };
environmentFiles = [ hostSecrets.lemmyDockerEnv.target-file ]; };
pictrsCfg = {
image = cfg.docker-images.pictrs;
envFile = host-secrets.lemmy-pictrs-env-file.target-file;
};
postgresCfg = {
image = cfg.docker-images.postgres;
envFile = hostSecrets.lemmy-postgres-env-file.target-file;
configFile = postgresCfgFile;
};
};
in { imports = [ lemmyImage ]; };
}; };
}; };