From a7b34850f421e002bd0926e798f4de516eddd43e Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Thu, 16 Jan 2025 18:50:44 +0100 Subject: [PATCH] feat(django-apps): Finish the daphne-specific setup --- modules/nixos/django-apps/default.nix | 56 ++++++++++++++------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/modules/nixos/django-apps/default.nix b/modules/nixos/django-apps/default.nix index c13de69..a605412 100644 --- a/modules/nixos/django-apps/default.nix +++ b/modules/nixos/django-apps/default.nix @@ -264,6 +264,7 @@ in ps: [ (config.django ps) ] ++ (optional (config.application.type != "daphne") ps.gunicorn) + ++ (optional (config.application.type == "daphne") ps.daphne) ++ (optional (config.application.type == "asgi") ps.uvicorn) ++ (optional (config.dbType == "postgresql") ps.psycopg) ++ (config.dependencies ps) @@ -547,6 +548,7 @@ in virtualHosts = mapAttrs' ( name: { + application, domain, nginx, serveMedia, @@ -555,7 +557,11 @@ in nameValuePair domain ( recursiveUpdate { locations = { - "/".proxyPass = "http://unix:/run/django-apps/${name}.sock"; + "/".proxyPass = + if application.type == "daphne" then + "http://unix:/run/django-apps/${name}/socket" + else + "http://unix:/run/django-apps/${name}.sock"; "/static/".root = "/run/django-apps/${name}"; "/media/".root = mkIf serveMedia "/run/django-apps/${name}"; }; @@ -600,7 +606,7 @@ in SocketUser = config'.services.nginx.user; }; } - ) cfg.sites; + ) (filterAttrs (_: { application, ... }: application.type != "daphne") cfg.sites); mounts = concatLists ( mapAttrsToList ( @@ -704,9 +710,15 @@ in mkdir -p ${config.mediaDirectory} ${config.staticDirectory} ''; - requires = [ "dj-${name}.socket" ]; + requires = optional (config.application.type != "daphne") "dj-${name}.socket"; wantedBy = [ "multi-user.target" ]; + script = mkIf (config.application.type == "daphne") '' + cd source && ${getExe' config.djangoEnv "daphne"} \ + -u /run/django-apps/${name}/socket \ + ${config.application.module}.asgi:application + ''; + serviceConfig = { inherit Group @@ -719,31 +731,23 @@ in ; DynamicUser = true; - ExecStart = escapeSystemdExecArgs ( - if (config.application.type == "daphne") then + ExecStart = mkIf (config.application.type != "daphne") ( + escapeSystemdExecArgs ( [ - (getExe' config.djangoEnv "daphne") - "-u" - "/run/django-apps/${name}.sock" - "${config.application.module}.asgi:${config.application.channelLayer}" + (getExe' config.djangoEnv "gunicorn") + "--workers" + config.application.workers + "--bind" + "unix:/run/django-apps/${name}.sock" + "--pythonpath" + "source" ] - else - ( - [ - (getExe' config.djangoEnv "gunicorn") - "--workers" - config.application.workers - "--bind" - "unix:/run/django-apps/${name}.sock" - "--pythonpath" - "source" - ] - ++ (optionals (config.application.type == "asgi") [ - "--worker-class" - "uvicorn.workers.UvicornWorker" - ]) - ++ [ "${config.application.module}.${config.application.type}" ] - ) + ++ (optionals (config.application.type == "asgi") [ + "--worker-class" + "uvicorn.workers.UvicornWorker" + ]) + ++ [ "${config.application.module}.${config.application.type}" ] + ) ); ExecReload = "${getExe' pkgs.coreutils "kill"} -s HUP $MAINPID"; KillMode = "mixed";