Skip to content

Instantly share code, notes, and snippets.

@datakurre
Last active December 7, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datakurre/23876f44a02c89d11d8db00cfae1de3a to your computer and use it in GitHub Desktop.
Save datakurre/23876f44a02c89d11d8db00cfae1de3a to your computer and use it in GitHub Desktop.
ZConfig generator for Nix
{}:
with builtins;
rec {
toZConfigValue = value:
if (isString value) then value else
if (isInt value) then toString value else
if (isBool value) && value then "on" else
if (isBool value) then "off" else
if (isList value) then (
let ret = foldl' (x: y: x + y + " ") "" value;
in substring 0 ((stringLength ret) - 1) ret
) else
throw "Unsupported value \"${value}\"";
toZConfigComponent = type: name: attrs: indentation:
let names = attrNames attrs; in
if (all (value: (isAttrs value)) (attrValues attrs)) then
concatLists
(map (key: toZConfigComponent type key (getAttr key attrs) indentation)
(sort lessThan names))
else [
(if (isNull name)
then "\n${indentation}<${type}>"
else "\n${indentation}<${type} ${name}>")
] ++ toZConfigLines attrs (indentation + " ") ++ [
"${indentation}</${type}>"
];
toZConfigLines = attrs: indentation:
let names = attrNames attrs; in
# key value pairs
(map (key: "${indentation}${key} ${toZConfigValue (getAttr key attrs)}")
(filter (key: let value = (getAttr key attrs);
in (isString value) ||
(isInt value) ||
(isBool value) ||
(isList value))
(sort lessThan names)))
# components
++
concatLists
(map (key: toZConfigComponent key null (getAttr key attrs) indentation)
(filter (key: let value = (getAttr key attrs);
in (isAttrs value))
(sort lessThan names)));
toZConfig = attrs:
let names = attrNames attrs; in
foldl' (x: y: x + y + "\n") "" (
# %define
(if hasAttr "%define" attrs then
map (key: "%define ${key} ${getAttr key attrs."%define"}")
(sort lessThan (attrNames attrs."%define"))
else [])
# %import
++
(if hasAttr "%import" attrs then
map (module: "%import ${module}")
(sort lessThan attrs."%import")
else [])
# separator
++ (if (elem "%define" names || elem "%import" names)
then [ "" ] else [])
# configuration
++
toZConfigLines (removeAttrs attrs [ "%define" "%import" ]) ""
);
}
{ pkgs ? import <nixpkgs> {}
, generators ? import ./generators.nix {}
, instancehome ? import ./instancehome.nix {}
, var ? "/tmp"
}:
let configuration = generators.toZConfig {
clienthome = "${var}";
debug-mode = false;
default-zpublisher-encoding = "utf-8";
enable-product-installation = false;
http-header-max-length = 8192;
instancehome = "${instancehome}";
lock-filename = "${var}/instance1.lock";
pid-filename = "${var}/instance1.pid";
python-check-interval = 1000;
security-policy-implementation = "C";
verbose-security = false;
zserver-threads = 2;
environment = {
CHAMELEON_CACHE = "/tmp";
zope_i18n_compile_mo_files = true;
zope_i18n_allowed_languages = ["en" "fi" "sv" "de"];
PTS_LANGUAGES = ["en" "fi" "sv" "de"];
PLONE_X_FRAME_OPTIONS = "";
TMP = "${var}";
};
warnfilter = {
action = "ignore";
category = "exceptions.DeprecationWarning";
};
eventlog = {
level = "INFO";
logfile = {
path = "${var}/instance1.log";
level = "INFO";
};
};
logger = {
access = {
level = "WARN";
logfile = {
path = "${var}/instance1-Z2.log";
format = "%(message)s";
};
};
};
http-server = {
address = 8080;
fast-listen = true;
};
zodb_db = {
main = {
cache-size = 40000;
mount-point = "/";
blobstorage = {
blob-dir = "${var}/blostorage";
filestorage = {
path = "${var}/filestorage/Data.fs";
};
};
};
temporary = {
temporarystorage = {
name = "temporary storage for sessioning";
};
mount-point = "/temp_folder";
container-class = "Products.TemporaryFolder.TemporaryContainer";
};
};
}; in
pkgs.stdenv.mkDerivation {
name = "zope.conf";
builder = builtins.toFile "builder.sh" ''
source $stdenv/setup
cat > $out << EOF
$configuration
EOF
'';
inherit configuration;
}
# Plone expects Zope2 to load ``./etc/site.zcml`` from ``instancehome``.
{ pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation {
name = "plone";
builder = builtins.toFile "builder.sh" ''
source $stdenv/setup
mkdir -p $out/etc
cat > $out/etc/site.zcml << EOF
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:meta="http://namespaces.zope.org/meta"
xmlns:five="http://namespaces.zope.org/five">
<include package="Products.Five" />
<meta:redefinePermission from="zope2.Public" to="zope.Public" />
<!-- Load the meta -->
<include files="package-includes/*-meta.zcml" />
<five:loadProducts file="meta.zcml"/>
<!-- Load the configuration -->
<include files="package-includes/*-configure.zcml" />
<five:loadProducts />
<!-- Load the configuration overrides-->
<includeOverrides files="package-includes/*-overrides.zcml" />
<five:loadProductsOverrides />
<securityPolicy
component="AccessControl.security.SecurityPolicy" />
</configure>
EOF
'';
}
{ pkgs ? import <nixpkgs> {}
, generators ? import ./generators.nix {}
, var ? "/tmp"
}:
let configuration = generators.toZConfig {
zeo = {
address = "127.0.0.1:8660";
read-only = false;
invalidation-queue-size = 100;
pid-filename = "${var}/zeoserver.pid";
};
filestorage = {
"1" = {
path = "${var}/filestorage/Data.fs";
blob-dir = "${var}/blostorage";
};
};
eventlog = {
level = "INFO";
logfile = {
path = "${var}/zeoserver.log";
format = "%(asctime)s %(message)s";
};
};
}; in
pkgs.stdenv.mkDerivation {
name = "zeo.conf";
builder = builtins.toFile "builder.sh" ''
source $stdenv/setup
cat > $out << EOF
$configuration
EOF
'';
inherit configuration;
}
{ pkgs ? import <nixpkgs> {}
, generators ? import ./generators.nix {}
, instancehome ? import ./instancehome.nix {}
, var ? "/tmp"
}:
let configuration = generators.toZConfig {
clienthome = "${var}";
debug-mode = false;
default-zpublisher-encoding = "utf-8";
enable-product-installation = false;
http-header-max-length = 8192;
instancehome = "${instancehome}";
lock-filename = "${var}/instance1.lock";
pid-filename = "${var}/instance1.pid";
python-check-interval = 1000;
security-policy-implementation = "C";
verbose-security = false;
zserver-threads = 2;
environment = {
CHAMELEON_CACHE = "/tmp";
zope_i18n_compile_mo_files = true;
zope_i18n_allowed_languages = ["en" "fi" "sv" "de"];
PTS_LANGUAGES = ["en" "fi" "sv" "de"];
PLONE_X_FRAME_OPTIONS = "";
TMP = "${var}";
};
warnfilter = {
action = "ignore";
category = "exceptions.DeprecationWarning";
};
eventlog = {
level = "INFO";
logfile = {
path = "${var}/instance1.log";
level = "INFO";
};
};
logger = {
access = {
level = "WARN";
logfile = {
path = "${var}/instance1-Z2.log";
format = "%(message)s";
};
};
};
http-server = {
address = 8080;
fast-listen = true;
};
zodb_db = {
main = {
cache-size = 40000;
mount-point = "/";
zeoclient = {
read-only = false;
read-only-fallback = false;
blob-dir = "${var}/blostorage";
shared-blob-dir = true;
server = "127.0.0.1:8660";
storage = 1;
name = "zeostorage";
var = "${var}";
cache-size = "128MB";
};
};
temporary = {
temporarystorage = {
name = "temporary storage for sessioning";
};
mount-point = "/temp_folder";
container-class = "Products.TemporaryFolder.TemporaryContainer";
};
};
}; in
pkgs.stdenv.mkDerivation {
name = "zope.conf";
builder = builtins.toFile "builder.sh" ''
source $stdenv/setup
cat > $out << EOF
$configuration
EOF
'';
inherit configuration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment