2022-03-31 10:19:43 +02:00
|
|
|
{ lib, config , ... }:
|
2022-03-31 09:36:51 +02:00
|
|
|
with lib;
|
|
|
|
let
|
2022-04-03 01:53:19 +02:00
|
|
|
eachSite = config.services.staticWebsites.sites;
|
2022-03-31 10:19:43 +02:00
|
|
|
website = { name, ... }: {
|
2022-03-31 09:36:51 +02:00
|
|
|
options = {
|
|
|
|
root = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/nginx/static/${name}";
|
|
|
|
description = "Static files path for the website";
|
|
|
|
};
|
|
|
|
hostname = mkOption {
|
2022-03-31 10:19:43 +02:00
|
|
|
type = types.str;
|
2022-03-31 09:36:51 +02:00
|
|
|
default = name;
|
|
|
|
description = "Website hostname";
|
|
|
|
};
|
2022-04-03 01:53:19 +02:00
|
|
|
location = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Add a location rule if not null";
|
|
|
|
};
|
2022-03-31 09:36:51 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2022-04-03 01:27:30 +02:00
|
|
|
options.services.staticWebsites = {
|
|
|
|
sites = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule website);
|
|
|
|
description = "Specification of one or more static websites to serve";
|
|
|
|
};
|
|
|
|
debug = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2022-03-31 09:36:51 +02:00
|
|
|
};
|
2022-03-31 10:19:43 +02:00
|
|
|
config = mkIf (eachSite != {}) {
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
2022-04-03 19:26:31 +02:00
|
|
|
virtualHosts = mapAttrs' ( hostname: conf: {
|
|
|
|
name = conf.hostname;
|
|
|
|
value = (mkMerge [
|
|
|
|
{
|
|
|
|
serverName = conf.hostname;
|
|
|
|
forceSSL = if config.services.staticWebsites.debug then false else true;
|
|
|
|
enableACME = if config.services.staticWebsites.debug then false else true;
|
|
|
|
}
|
|
|
|
|
|
|
|
(mkIf (conf.location == null) {
|
|
|
|
root = conf.root;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (conf.location != null) {
|
|
|
|
locations = {
|
|
|
|
"${conf.location}/" = {
|
|
|
|
alias = "${conf.root}/";
|
|
|
|
};
|
2022-04-03 01:53:19 +02:00
|
|
|
};
|
2022-04-03 19:26:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
]);
|
|
|
|
}) eachSite;
|
2022-03-31 10:19:43 +02:00
|
|
|
};
|
2022-03-31 09:36:51 +02:00
|
|
|
};
|
|
|
|
}
|