Skip to content

Instantly share code, notes, and snippets.

@katyo
Last active December 17, 2016 07:33
Show Gist options
  • Save katyo/b8d91b415b6955cfdcf8dc264ea09f70 to your computer and use it in GitHub Desktop.
Save katyo/b8d91b415b6955cfdcf8dc264ea09f70 to your computer and use it in GitHub Desktop.
{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";
};
} // cfg.presets."${config.preset}".options; # <- infinite recursion occurs here
}));
};
};
## Implementation ##
config = {
environment.systemPackages = [ pkgs.libidn ];
services.nginx.virtualHosts = mapAttrs (serverName: opts@{preset, ...}:
(cfg.presets."${preset}".mkConfig opts)) 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";
root = "/var/www/example.com";
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment