diff --git a/hosts/lab/sites.nix b/hosts/lab/sites.nix index d1a5310..4db697b 100644 --- a/hosts/lab/sites.nix +++ b/hosts/lab/sites.nix @@ -2,7 +2,9 @@ # # domain — primary domain (required) # repo — git repository URL (required) -# port — Node.js server port (required) +# static — serve build output as static files, no Node server (default: false) +# port — Node.js server port (required when static = false) +# buildOutputDir — build output directory relative to repo root (default: "dist") # redirectDomains — domains that 301 to the primary domain (default: []) # branch — git branch to track (default: "main") # packageManager — "npm" or "pnpm" (default: "pnpm") @@ -36,6 +38,6 @@ in services.site.penfield = { domain = "penfield2.ily.rs"; repo = "https://git.ily.rs/lew/penfield"; - port = 4324; + static = true; }; } diff --git a/modules/site.nix b/modules/site.nix index 5bf62a9..73ba234 100644 --- a/modules/site.nix +++ b/modules/site.nix @@ -26,9 +26,22 @@ let default = "main"; }; + static = mkOption { + type = types.bool; + default = false; + description = "Serve build output as static files instead of running a Node.js server."; + }; + port = mkOption { - type = types.port; - description = "Port the Node.js server listens on."; + type = types.nullOr types.port; + default = null; + description = "Port the Node.js server listens on. Required when static = false."; + }; + + buildOutputDir = mkOption { + type = types.str; + default = "dist"; + description = "Build output directory relative to repo root (used for static sites)."; }; packageManager = mkOption { @@ -100,7 +113,12 @@ in config = { services.caddy.virtualHosts = mkMerge (mapAttrsToList (name: site: { - ${site.domain}.extraConfig = '' + ${site.domain}.extraConfig = if site.static then '' + root * ${site.dataDir}/repo/${site.buildOutputDir} + encode zstd gzip + try_files {path} /index.html + file_server + '' else '' reverse_proxy localhost:${toString site.port} encode zstd gzip ''; @@ -114,23 +132,6 @@ in systemd.services = mkMerge ((mapAttrsToList (name: site: let h = siteHelpers name site; in { - ${name} = { - description = site.domain; - environment = { - HOST = "127.0.0.1"; - PORT = toString site.port; - } // site.environment; - serviceConfig = { - Type = "simple"; - WorkingDirectory = "${h.dataDir}/repo"; - ExecStart = "${pkgs.nodejs}/bin/node ${site.entryPoint}"; - Restart = "on-failure"; - User = name; - Group = name; - ReadWritePaths = site.readWritePaths; - }; - }; - "${name}-rebuild" = { description = "Clone/pull and build ${site.domain}"; after = [ "network-online.target" ] ++ site.afterServices; @@ -157,11 +158,29 @@ in ${h.installCmd} ${h.pmBin} run build ''; - ExecStartPost = "+/run/current-system/sw/bin/systemctl restart ${name}"; + ExecStartPost = lib.mkIf (!site.static) + "+/run/current-system/sw/bin/systemctl restart ${name}"; User = name; Group = name; }; }; + } // lib.optionalAttrs (!site.static) { + ${name} = { + description = site.domain; + environment = { + HOST = "127.0.0.1"; + PORT = toString site.port; + } // site.environment; + serviceConfig = { + Type = "simple"; + WorkingDirectory = "${h.dataDir}/repo"; + ExecStart = "${pkgs.nodejs}/bin/node ${site.entryPoint}"; + Restart = "on-failure"; + User = name; + Group = name; + ReadWritePaths = site.readWritePaths; + }; + }; } ) cfg) ++ [{ site-webhook = mkIf (cfg != {}) {