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

62 lines
1.5 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: (mkMerge [
{
serverName = conf.hostname;
forceSSL = if debug then false else true;
enableACME = if debug then false else true;
}
(mkIf (conf.location == null) {
root = conf.root;
})
(mkIf (conf.location != null) {
location = {
"~ ^${conf.location}" = {
alias = conf.root;
};
};
})
])
) eachSite;
};
};
}