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
|
|
|
|
2015-07-16 16:07:20 +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
|
2015-07-16 16:07:20 +00:00
|
|
|
default = [
|
|
|
|
"${config.networking.hostName}"
|
|
|
|
"${config.networking.hostName}.retiolum"
|
|
|
|
];
|
|
|
|
};
|
2015-10-19 18:42:18 +00:00
|
|
|
listen = mkOption {
|
2015-10-20 16:53:11 +00:00
|
|
|
type = with types; either str (listOf str);
|
2015-10-19 18:42:18 +00:00
|
|
|
default = "80";
|
2015-10-20 16:53:11 +00:00
|
|
|
apply = x:
|
|
|
|
if typeOf x != "list"
|
|
|
|
then [x]
|
|
|
|
else x;
|
2015-10-19 18:36:09 +00:00
|
|
|
};
|
2015-07-16 16:07:20 +00:00
|
|
|
locations = mkOption {
|
|
|
|
type = with types; listOf (attrsOf str);
|
|
|
|
};
|
2015-10-19 18:36:09 +00:00
|
|
|
extraConfig = mkOption {
|
2015-10-21 16:44:51 +00:00
|
|
|
type = with types; string;
|
2015-10-19 18:36:09 +00:00
|
|
|
default = "";
|
|
|
|
};
|
2015-07-16 16:07:20 +00:00
|
|
|
};
|
|
|
|
default = {};
|
2015-07-11 14:55:22 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
imp = {
|
2015-07-16 16:07:20 +00:00
|
|
|
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, ... }: ''
|
2015-07-16 16:07:20 +00:00
|
|
|
server {
|
2015-10-20 16:53:11 +00:00
|
|
|
${concatMapStringsSep "\n" (x: "listen ${x};") listen}
|
2015-07-16 16:07:20 +00:00
|
|
|
server_name ${toString server-names};
|
2015-10-19 18:36:09 +00:00
|
|
|
${extraConfig}
|
2015-07-16 16:07:20 +00:00
|
|
|
${indent (concatStrings (map to-location locations))}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2015-07-11 14:55:22 +00:00
|
|
|
in
|
|
|
|
out
|