Created
December 17, 2016 07:25
-
-
Save katyo/7dfb40d138769121d84534ea30543c24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{options, config, lib, pkgs, ...}: | |
with lib; | |
let | |
cfg = config.webapp; | |
in { | |
## Interface ## | |
options.webapp = { | |
presets = mkOption { | |
default = {}; | |
example = literalExample '' | |
{ | |
name = { | |
options: { <additional options> } | |
mkConfig: config: virtualHost | |
}; | |
} | |
''; | |
description = "Presets"; | |
type = types.attrsOf (types.submodule ({config, ...}: { | |
options = { | |
options = mkOption { | |
type = types.attrs; | |
default = {}; | |
description = "Preset specific options"; | |
}; | |
mkConfig = mkOption { | |
type = mkOptionType { | |
name = "function"; | |
check = f: (builtins.typeOf f) == "lambda"; | |
}; | |
default = opts: {}; | |
description = "Preset config function"; | |
}; | |
}; | |
})); | |
}; | |
applications = mkOption { | |
default = {}; | |
example = ''{ | |
"example.com" = { | |
preset = "static"; | |
# preset specific options | |
root = "/var/www/example_com"; | |
} | |
}''; | |
description = "Applications"; | |
type = types.attrsOf (types.submodule ({config, ...}: { | |
options = { | |
preset = mkOption { | |
example = "static"; | |
type = types.enum (attrNames cfg.presets); | |
description = "The preset for application."; | |
default = "base"; | |
}; | |
presetConfig = mkOption { | |
type = types.submodule ({...}: { | |
options = cfg.presets."${config.preset}".options; | |
}); | |
description = "The preset config options."; | |
default = {}; | |
}; | |
}; | |
})); | |
}; | |
}; | |
## Implementation ## | |
config = { | |
environment.systemPackages = [ pkgs.libidn ]; | |
services.nginx.virtualHosts = mapAttrs (serverName: defs@{preset, presetConfig, ...}: | |
(cfg.presets."${preset}".mkConfig presetConfig)) cfg.applications; | |
webapp.presets.static = { | |
options = { | |
root = mkOption { | |
type = types.str; | |
description = "Root directory of application"; | |
}; | |
}; | |
mkConfig = {root, ...}: { | |
locations."/".root = root; | |
}; | |
}; | |
## Test app config ## | |
webapp.applications."example.com" = { | |
preset = "static"; | |
presetConfig = { | |
root = "/var/www/example.com"; | |
}; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment