stockholm/krebs/3modules/nginx.nix

82 lines
1.7 KiB
Nix
Raw Normal View History

2015-07-11 14:55:22 +00:00
{ config, pkgs, lib, ... }:
with builtins;
with lib;
let
2015-07-24 09:50:23 +00:00
cfg = config.krebs.nginx;
2015-07-11 14:55:22 +00:00
out = {
2015-07-24 09:50:23 +00:00
options.krebs.nginx = api;
2015-07-11 14:55:22 +00:00
config = mkIf cfg.enable imp;
};
api = {
2015-07-24 09:50:23 +00:00
enable = mkEnableOption "krebs.nginx";
2015-07-11 14:55:22 +00:00
servers = mkOption {
type = with types; attrsOf optionSet;
options = singleton {
server-names = mkOption {
type = with types; listOf str;
2015-07-24 09:50:23 +00:00
# TODO use identity
default = [
"${config.networking.hostName}"
"${config.networking.hostName}.retiolum"
];
};
2015-10-19 18:42:18 +00:00
listen = mkOption {
type = with types; str;
default = "80";
};
locations = mkOption {
type = with types; listOf (attrsOf str);
};
extraConfig = mkOption {
type = with types; str;
default = "";
};
};
default = {};
2015-07-11 14:55:22 +00:00
};
};
imp = {
services.nginx = {
enable = true;
httpConfig = ''
include ${pkgs.nginx}/conf/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80 default_server;
server_name _;
return 404;
}
${concatStrings (mapAttrsToList (_: to-server) cfg.servers)}
'';
};
2015-07-11 14:55:22 +00:00
};
indent = replaceChars ["\n"] ["\n "];
to-location = { name, value }: ''
location ${name} {
${indent value}
}
'';
2015-10-19 18:42:18 +00:00
to-server = { server-names, listen, locations, extraConfig, ... }: ''
server {
2015-10-19 18:42:18 +00:00
listen ${listen};
server_name ${toString server-names};
${extraConfig}
${indent (concatStrings (map to-location locations))}
}
'';
2015-07-11 14:55:22 +00:00
in
out