From d0647343ec05a8ed967fb886501c2880530e31ab Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Sun, 13 Oct 2024 19:32:31 +0200 Subject: [PATCH] vm: serve media directory from virtiofs This required a serveMediaUsingNginx argument, because nginx fails to serve media files correctly from the virtiofs filesystem. (It works with /nix, because that's mounted using a cache flag, but we can't do that for the media directory because we want to reflect file changes.) --- nix/infra.nix | 3 +++ nix/services/kn/django.nix | 30 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/nix/infra.nix b/nix/infra.nix index 87e0385b..02e97853 100644 --- a/nix/infra.nix +++ b/nix/infra.nix @@ -256,6 +256,8 @@ in rec { ''; }; kn.django.package = "/kninfra-pub"; + kn.django.serveMediaUsingNginx = false; + kn.settings.INFRA_REPO = "/kninfra-pub"; kn.mailserver.hostname = "mail.local"; programs.bash.shellAliases."vm.stop" = "poweroff"; system.fsPackages = [ pkgs.bindfs ]; @@ -268,6 +270,7 @@ in rec { sharedDirectories.kninfra = { source = "\${PRJ_ROOT:?please run this inside the devshell}"; target = "/kninfra"; + securityModel = "none"; }; # use bindfs to make it world-readable fileSystems."/kninfra-pub" = { diff --git a/nix/services/kn/django.nix b/nix/services/kn/django.nix index 8a47329b..fe51b537 100644 --- a/nix/services/kn/django.nix +++ b/nix/services/kn/django.nix @@ -38,19 +38,33 @@ in { defaultText = "pkgs.kninfra"; type = types.path; }; + serveMediaUsingNginx = mkOption { + # doesn't work in VM :( + description = '' + Use nginx to serve media directory. + ''; + default = true; + type = types.bool; + }; }; config = lib.mkIf cfg.enable { kn.shared.enable = true; services.nginx = { enable = true; - virtualHosts.kn = { - serverName = config.kn.settings.DOMAINNAME; - locations."/djmedia/".alias = "${pkgs.kninfra}/media/"; - locations."/".extraConfig = '' - include ${pkgs.nginx}/conf/uwsgi_params; - uwsgi_pass unix:${config.kn.django.socket}; - ''; - }; + virtualHosts.kn = lib.mkMerge [ + { + serverName = config.kn.settings.DOMAINNAME; + locations."/".extraConfig = '' + include ${pkgs.nginx}/conf/uwsgi_params; + uwsgi_pass unix:${config.kn.django.socket}; + ''; + } + { + locations = lib.mkIf cfg.serveMediaUsingNginx { + "/djmedia/".alias = "${cfg.package}/media/"; + }; + } + ]; }; systemd.sockets.kndjango = { wantedBy = [ "sockets.target" ];