64 lines
1.6 KiB
Nix
64 lines
1.6 KiB
Nix
{ lib, config , ... }:
|
|
with lib;
|
|
let
|
|
eachSite = config.services.staticWebsites.sites;
|
|
website = { name, ... }: {
|
|
options = {
|
|
root = mkOption {
|
|
type = types.path;
|
|
default = "/var/lib/nginx/static/${name}";
|
|
description = "Static files path for the website";
|
|
};
|
|
hostname = mkOption {
|
|
type = types.str;
|
|
default = name;
|
|
description = "Website hostname";
|
|
};
|
|
location = mkOption {
|
|
type = types.nullOr types.str;
|
|
default = null;
|
|
description = "Add a location rule if not null";
|
|
};
|
|
};
|
|
};
|
|
in
|
|
{
|
|
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;
|
|
};
|
|
};
|
|
config = mkIf (eachSite != {}) {
|
|
services.nginx = {
|
|
enable = true;
|
|
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}/";
|
|
};
|
|
};
|
|
})
|
|
|
|
]);
|
|
}) eachSite;
|
|
};
|
|
};
|
|
}
|