2024-12-12 14:41:43 +01:00
|
|
|
# SPDX-FileCopyrightText: 2024 Maurice Debray <maurice.debray@dgnum.eu>
|
|
|
|
# SPDX-FileCopyrightText: 2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: EUPL-1.2
|
|
|
|
|
2024-04-23 22:02:04 +02:00
|
|
|
{ config, lib, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib) mkOption;
|
|
|
|
|
2024-09-30 20:11:56 +02:00
|
|
|
inherit (lib.types)
|
|
|
|
attrsOf
|
|
|
|
ints
|
|
|
|
listOf
|
|
|
|
str
|
|
|
|
submodule
|
|
|
|
;
|
2024-04-23 22:02:04 +02:00
|
|
|
|
|
|
|
mkRetired =
|
|
|
|
hosts:
|
|
|
|
builtins.listToAttrs (
|
|
|
|
builtins.map (name: {
|
|
|
|
inherit name;
|
|
|
|
value = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
locations."/".return = "301 https://${cfg.retiredHost}/${name}";
|
|
|
|
};
|
|
|
|
}) hosts
|
|
|
|
);
|
|
|
|
|
2024-09-30 20:11:56 +02:00
|
|
|
mkPermanent = _: globalRedirect: {
|
2024-04-23 22:02:04 +02:00
|
|
|
inherit globalRedirect;
|
|
|
|
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
};
|
|
|
|
|
2024-09-30 20:11:56 +02:00
|
|
|
mkTemporary =
|
|
|
|
_:
|
|
|
|
{
|
|
|
|
to,
|
|
|
|
code,
|
|
|
|
location,
|
|
|
|
}:
|
|
|
|
{
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
|
|
|
|
locations.${location}.return = "${toString code} ${to}";
|
|
|
|
};
|
|
|
|
|
2024-04-23 22:02:04 +02:00
|
|
|
cfg = config.dgn-redirections;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options.dgn-redirections = {
|
2024-09-30 20:11:56 +02:00
|
|
|
permanent = mkOption {
|
2024-04-23 22:02:04 +02:00
|
|
|
type = attrsOf str;
|
|
|
|
default = { };
|
|
|
|
description = ''
|
|
|
|
Attribute set of redirections, for:
|
|
|
|
{ a = b; },
|
|
|
|
a redirection from a to b will be made.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-30 20:11:56 +02:00
|
|
|
temporary = mkOption {
|
|
|
|
type = attrsOf (submodule {
|
|
|
|
options = {
|
|
|
|
to = mkOption {
|
|
|
|
type = str;
|
|
|
|
description = "Target of the redirection";
|
|
|
|
};
|
|
|
|
code = mkOption {
|
|
|
|
type = ints.between 300 399;
|
|
|
|
default = 302;
|
|
|
|
example = 308;
|
|
|
|
description = ''
|
|
|
|
HTTP status used by the redirection. Possible usecases
|
|
|
|
include temporary (302, 307) redirects, keeping the request method and
|
|
|
|
body (307, 308), or explicitly resetting the method to GET (303).
|
|
|
|
See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
location = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "/";
|
|
|
|
description = "nginx-style location for the source of the redirection";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
"source.dgnum.eu" = {
|
|
|
|
to = "https://target.dgnum.eu/path_to_page";
|
|
|
|
code = 307;
|
|
|
|
location = "/subpath/";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Attribute set of temporary redirections. The attribute is the source
|
|
|
|
domain.
|
|
|
|
|
|
|
|
For:
|
|
|
|
```
|
|
|
|
{
|
|
|
|
"source.dgnum.eu" = {
|
|
|
|
to = "https://target.dgnum.eu/path_to_page";
|
|
|
|
code = 307;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
a 307 redirect from all the urls within the domain `source.dgnum.eu` to
|
|
|
|
`https://target.dgnum.eu/path_to_page` will be made.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-23 22:02:04 +02:00
|
|
|
retired = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
List of retired domains, they will we redirected to `retired.dgnum.eu/$host`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
retiredHost = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "retired.dgnum.eu";
|
|
|
|
description = ''
|
|
|
|
Host used for the redirections of retired services.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
services.nginx.virtualHosts =
|
2024-09-30 20:11:56 +02:00
|
|
|
(builtins.mapAttrs mkPermanent cfg.permanent // builtins.mapAttrs mkTemporary cfg.temporary)
|
|
|
|
// (mkRetired cfg.retired);
|
2024-04-23 22:02:04 +02:00
|
|
|
};
|
|
|
|
}
|