commit 74429b9cbe17a60ecfca6decb58dceb2b98d5ef1
parent 749e09b2d8ecc8e6b8eb4a0abf9723209de19961
Author: mtmn <miro@haravara.org>
Date: Sun, 3 May 2026 20:54:17 +0200
chore: simplify static.nix
Diffstat:
3 files changed, 39 insertions(+), 69 deletions(-)
diff --git a/flake.lock b/flake.lock
@@ -200,11 +200,11 @@
]
},
"locked": {
- "lastModified": 1777760447,
- "narHash": "sha256-6Tq5I0u85ROgesFWvFUrWsJvPtIn51nsPU+EgXAMl/0=",
+ "lastModified": 1777815398,
+ "narHash": "sha256-MrIhEoqXc4YsHEUfH4rDU/K09XnWcKntNhCjs7n7zi8=",
"owner": "nix-community",
"repo": "home-manager",
- "rev": "561bd674646db26ebfccc79b4fbef89f335505db",
+ "rev": "b5e86c1b19f178a8ee10f7cb747325e02e3d3991",
"type": "github"
},
"original": {
@@ -242,11 +242,11 @@
]
},
"locked": {
- "lastModified": 1777181277,
- "narHash": "sha256-yVJbd07ortDRAttDFmDV5p220aOLTHgVAx//0nW/xW8=",
+ "lastModified": 1777787189,
+ "narHash": "sha256-2KUbS/HhzWW3kkkY1+RiWj9mJ76VEXw8lBJzcCFKzfY=",
"owner": "nix-community",
"repo": "nix-index-database",
- "rev": "b8eb7acee0f7604fe1bf6a5b3dcf5254369180fa",
+ "rev": "2dea2b920e7127b3afa8506713f23536651de312",
"type": "github"
},
"original": {
diff --git a/hosts/nixaran/configuration.nix b/hosts/nixaran/configuration.nix
Binary files differ.
diff --git a/modules/services/static.nix b/modules/services/static.nix
@@ -4,89 +4,59 @@
...
}: let
cfg = config.services.haravara.static;
-
- staticFiles = ../../static;
-
- localDomains = lib.filterAttrs (_: opt: !opt.s3.enable) cfg.domains;
in {
options.services.haravara.static = {
enable = lib.mkEnableOption "Static site assets deployment";
domains = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options = {
- dir = lib.mkOption {
+ bucket = lib.mkOption {
type = lib.types.str;
- default = "";
- description = "Directory name in static folder (not needed when s3.enable = true)";
+ description = "S3 bucket name";
};
- hasRedirect = lib.mkOption {
- type = lib.types.bool;
- default = false;
- description = "Whether to redirect / to the base domain";
+ endpoint = lib.mkOption {
+ type = lib.types.str;
+ description = "S3 upstream host";
};
extraConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = "Extra Caddy config appended to the virtual host";
};
- s3 = {
- enable = lib.mkEnableOption "Serve from S3 bucket";
- endpoint = lib.mkOption {
- type = lib.types.str;
- description = "S3 upstream host; Caddy rewrites to /<bucket>{uri} and proxies here";
- };
- bucket = lib.mkOption {
- type = lib.types.str;
- default = "";
- description = "Bucket name; defaults to the domain name when empty";
- };
- };
};
});
};
};
config = lib.mkIf cfg.enable {
- users.users.caddy.extraGroups = ["storage-users"];
-
- systemd.tmpfiles.rules = lib.concatLists [
- ["d /var/lib/haravara/static 0755 caddy caddy -"]
- (lib.mapAttrsToList (
- _: opt: "L+ /var/lib/haravara/static/${opt.dir} - - - - ${staticFiles}/${opt.dir}"
- )
- localDomains)
- ];
+ services.caddy.virtualHosts =
+ lib.mapAttrs' (
+ domain: opt:
+ lib.nameValuePair domain {
+ extraConfig =
+ ''
+ handle_errors {
+ redir / 302
+ }
- services.caddy.virtualHosts = lib.mapAttrs (domain: opt:
- if opt.s3.enable
- then let
- bucket =
- if opt.s3.bucket != ""
- then opt.s3.bucket
- else domain;
- in {
- extraConfig =
- ''
- rewrite * /${bucket}{uri}
- reverse_proxy https://${opt.s3.endpoint} {
- header_up Host {upstream_hostport}
- }
- ''
- + opt.extraConfig;
- }
- else {
- extraConfig =
- ''
- handle {
- root * /var/lib/haravara/static/${opt.dir}
- file_server
- }
- ''
- + lib.optionalString opt.hasRedirect ''
- redir / https://${lib.concatStringsSep "." (lib.drop 1 (lib.splitString "." domain))}
- ''
- + opt.extraConfig;
- })
- cfg.domains;
+ handle / {
+ route {
+ rewrite * /${opt.bucket}/index.html
+ reverse_proxy https://${opt.endpoint}
+ }
+ }
+ ''
+ + opt.extraConfig
+ + ''
+ handle {
+ route {
+ rewrite * /${opt.bucket}{http.request.uri.path}
+ reverse_proxy https://${opt.endpoint}
+ }
+ }
+ '';
+ }
+ )
+ cfg.domains;
};
}