hackens-org-configurations/hosts/hackens-org/modules/staticWebsites.nix

65 lines
1.6 KiB
Nix
Raw Permalink Normal View History

2022-03-31 10:19:43 +02:00
{ lib, config , ... }:
2022-03-31 09:36:51 +02:00
with lib;
let
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";
};
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;
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;
2022-03-31 10:19:43 +02:00
};
2022-03-31 09:36:51 +02:00
};
}