···
+
{ config, options, lib, pkgs, ... }:
cfg = config.services.neo4j;
+
certDirOpt = options.services.neo4j.directories.certificates;
+
isDefaultPathOption = opt: isOption opt && opt.type == types.path && opt.highestPrio >= 1500;
+
sslPolicies = mapAttrsToList (
+
dbms.ssl.policy.${name}.allow_key_generation=${boolToString conf.allowKeyGeneration}
+
dbms.ssl.policy.${name}.base_directory=${conf.baseDirectory}
+
${optionalString (conf.ciphers != null) ''
+
dbms.ssl.policy.${name}.ciphers=${concatStringsSep "," conf.ciphers}
+
dbms.ssl.policy.${name}.client_auth=${conf.clientAuth}
+
${if length (splitString "/" conf.privateKey) > 1 then
+
''dbms.ssl.policy.${name}.private_key=${conf.privateKey}''
+
''dbms.ssl.policy.${name}.private_key=${conf.baseDirectory}/${conf.privateKey}''
+
${if length (splitString "/" conf.privateKey) > 1 then
+
''dbms.ssl.policy.${name}.public_certificate=${conf.publicCertificate}''
+
''dbms.ssl.policy.${name}.public_certificate=${conf.baseDirectory}/${conf.publicCertificate}''
+
dbms.ssl.policy.${name}.revoked_dir=${conf.revokedDir}
+
dbms.ssl.policy.${name}.tls_versions=${concatStringsSep "," conf.tlsVersions}
+
dbms.ssl.policy.${name}.trust_all=${boolToString conf.trustAll}
+
dbms.ssl.policy.${name}.trusted_dir=${conf.trustedDir}
serverConfig = pkgs.writeText "neo4j.conf" ''
+
dbms.allow_upgrade=${boolToString cfg.allowUpgrade}
+
dbms.connectors.default_listen_address=${cfg.defaultListenAddress}
+
dbms.read_only=${boolToString cfg.readOnly}
+
${optionalString (cfg.workerCount > 0) ''
+
dbms.threads.worker_count=${toString cfg.workerCount}
+
dbms.directories.certificates=${cfg.directories.certificates}
+
dbms.directories.data=${cfg.directories.data}
+
dbms.directories.logs=${cfg.directories.home}/logs
+
dbms.directories.plugins=${cfg.directories.plugins}
+
${optionalString (cfg.constrainLoadCsv) ''
+
dbms.directories.import=${cfg.directories.imports}
+
${optionalString (cfg.http.enable) ''
+
dbms.connector.http.enabled=${boolToString cfg.http.enable}
+
dbms.connector.http.listen_address=${cfg.http.listenAddress}
+
${optionalString (!cfg.http.enable) ''
+
# It is not possible to disable the HTTP connector. To fully prevent
+
# clients from connecting to HTTP, block the HTTP port (7474 by default)
+
# via firewall. listen_address is set to the loopback interface to
+
# prevent remote clients from connecting.
+
dbms.connector.http.listen_address=127.0.0.1
+
dbms.connector.https.enabled=${boolToString cfg.https.enable}
+
dbms.connector.https.listen_address=${cfg.https.listenAddress}
+
https.ssl_policy=${cfg.https.sslPolicy}
+
dbms.connector.bolt.enabled=${boolToString cfg.bolt.enable}
+
dbms.connector.bolt.listen_address=${cfg.bolt.listenAddress}
+
bolt.ssl_policy=${cfg.bolt.sslPolicy}
+
dbms.connector.bolt.tls_level=${cfg.bolt.tlsLevel}
+
dbms.shell.enabled=${boolToString cfg.shell.enable}
+
${concatStringsSep "\n" sslPolicies}
+
# Default retention policy from neo4j.conf
+
dbms.tx_log.rotation.retention_policy=1 days
# Default JVM parameters from neo4j.conf
dbms.jvm.additional=-XX:+UseG1GC
···
dbms.jvm.additional=-XX:+TrustFinalNonStaticFields
dbms.jvm.additional=-XX:+DisableExplicitGC
dbms.jvm.additional=-Djdk.tls.ephemeralDHKeySize=2048
+
dbms.jvm.additional=-Djdk.tls.rejectClientInitiatedRenegotiation=true
+
dbms.jvm.additional=-Dunsupported.dbms.udc.source=tarball
+
dbms.udc.enabled=${boolToString cfg.udc.enable}
+
${cfg.extraServerConfig}
···
options.services.neo4j = {
+
Whether to enable Neo4j Community Edition.
+
allowUpgrade = mkOption {
+
Allow upgrade of Neo4j database files from an older version.
+
constrainLoadCsv = mkOption {
+
Sets the root directory for file URLs used with the Cypher
+
<literal>LOAD CSV</literal> clause to be that defined by
+
<option>directories.imports</option>. It restricts
+
access to only those files within that directory and its
+
Setting this option to <literal>false</literal> introduces
+
possible security problems.
+
defaultListenAddress = mkOption {
+
Default network interface to listen for incoming connections. To
+
listen for connections on all interfaces, use "0.0.0.0".
+
Specifies the default IP address and address part of connector
+
specific <option>listenAddress</option> options. To bind specific
+
connectors to a specific network interfaces, specify the entire
+
<option>listenAddress</option> option for that connector.
+
extraServerConfig = mkOption {
+
Extra configuration for Neo4j Community server. Refer to the
+
<link xlink:href="https://neo4j.com/docs/operations-manual/current/reference/configuration-settings/">complete reference</link>
+
of Neo4j configuration settings.
defaultText = "pkgs.neo4j";
+
Only allow read operations from this Neo4j instance.
+
workerCount = mkOption {
+
type = types.ints.between 0 44738;
+
Number of Neo4j worker threads, where the default of
+
<literal>0</literal> indicates a worker count equal to the number of
+
Enable the BOLT connector for Neo4j. Setting this option to
+
<literal>false</literal> will stop Neo4j from listening for incoming
+
connections on the BOLT port (7687 by default).
+
listenAddress = mkOption {
+
Neo4j listen address for BOLT traffic. The listen address is
+
expressed in the format <literal><ip-address>:<port-number></literal>.
+
Neo4j SSL policy for BOLT traffic.
+
The legacy policy is a special policy which is not defined in
+
the policy configuration section, but rather derives from
+
<option>directories.certificates</option> and
+
associated files (by default: <filename>neo4j.key</filename> and
+
<filename>neo4j.cert</filename>). Its use will be deprecated.
+
Note: This connector must be configured to support/require
+
SSL/TLS for the legacy policy to actually be utilized. See
+
<option>bolt.tlsLevel</option>.
+
type = types.enum [ "REQUIRED" "OPTIONAL" "DISABLED" ];
+
SSL/TSL requirement level for BOLT traffic.
+
certificates = mkOption {
+
default = "${cfg.directories.home}/certificates";
+
Directory for storing certificates to be used by Neo4j for
+
When setting this directory to something other than its default,
+
ensure the directory's existence, and that read/write permissions are
+
given to the Neo4j daemon user <literal>neo4j</literal>.
+
Note that changing this directory from its default will prevent
+
the directory structure required for each SSL policy from being
+
automatically generated. A policy's directory structure as defined by
+
its <option>baseDirectory</option>,<option>revokedDir</option> and
+
<option>trustedDir</option> must then be setup manually. The
+
existence of these directories is mandatory, as well as the presence
+
of the certificate file and the private key. Ensure the correct
+
permissions are set on these directories and files.
+
default = "${cfg.directories.home}/data";
+
Path of the data directory. You must not configure more than one
+
Neo4j installation to use the same data directory.
+
When setting this directory to something other than its default,
+
ensure the directory's existence, and that read/write permissions are
+
given to the Neo4j daemon user <literal>neo4j</literal>.
+
default = "/var/lib/neo4j";
+
Path of the Neo4j home directory. Other default directories are
+
subdirectories of this path. This directory will be created if
+
non-existent, and its ownership will be <command>chown</command> to
+
the Neo4j daemon user <literal>neo4j</literal>.
+
default = "${cfg.directories.home}/import";
+
The root directory for file URLs used with the Cypher
+
<literal>LOAD CSV</literal> clause. Only meaningful when
+
<option>constrainLoadCvs</option> is set to
+
<literal>true</literal>.
+
When setting this directory to something other than its default,
+
ensure the directory's existence, and that read permission is
+
given to the Neo4j daemon user <literal>neo4j</literal>.
+
default = "${cfg.directories.home}/plugins";
+
Path of the database plugin directory. Compiled Java JAR files that
+
contain database procedures will be loaded if they are placed in
+
When setting this directory to something other than its default,
+
ensure the directory's existence, and that read permission is
+
given to the Neo4j daemon user <literal>neo4j</literal>.
+
The HTTP connector is required for Neo4j, and cannot be disabled.
+
Setting this option to <literal>false</literal> will force the HTTP
+
connector's <option>listenAddress</option> to the loopback
+
interface to prevent connection of remote clients. To prevent all
+
clients from connecting, block the HTTP port (7474 by default) by
+
listenAddress = mkOption {
+
Neo4j listen address for HTTP traffic. The listen address is
+
expressed in the format <literal><ip-address>:<port-number></literal>.
+
Enable the HTTPS connector for Neo4j. Setting this option to
+
<literal>false</literal> will stop Neo4j from listening for incoming
+
connections on the HTTPS port (7473 by default).
+
listenAddress = mkOption {
+
Neo4j listen address for HTTPS traffic. The listen address is
+
expressed in the format <literal><ip-address>:<port-number></literal>.
+
Neo4j SSL policy for HTTPS traffic.
+
The legacy policy is a special policy which is not defined in the
+
policy configuration section, but rather derives from
+
<option>directories.certificates</option> and
+
associated files (by default: <filename>neo4j.key</filename> and
+
<filename>neo4j.cert</filename>). Its use will be deprecated.
+
Enable a remote shell server which Neo4j Shell clients can log in to.
+
Only applicable to <command>neo4j-shell</command>.
+
ssl.policies = mkOption {
+
type = with types; attrsOf (submodule ({ name, config, options, ... }: {
+
allowKeyGeneration = mkOption {
+
Allows the generation of a private key and associated self-signed
+
certificate. Only performed when both objects cannot be found for
+
this policy. It is recommended to turn this off again after keys
+
The public certificate is required to be duplicated to the
+
directory holding trusted certificates as defined by the
+
<option>trustedDir</option> option.
+
Keys should in general be generated and distributed offline by a
+
trusted certificate authority and not by utilizing this mode.
+
baseDirectory = mkOption {
+
default = "${cfg.directories.certificates}/${name}";
+
The mandatory base directory for cryptographic objects of this
+
policy. This path is only automatically generated when this
+
option as well as <option>directories.certificates</option> are
+
left at their default. Ensure read/write permissions are given
+
to the Neo4j daemon user <literal>neo4j</literal>.
+
It is also possible to override each individual
+
configuration with absolute paths. See the
+
<option>privateKey</option> and <option>publicCertificate</option>
+
type = types.nullOr (types.listOf types.str);
+
Restrict the allowed ciphers of this policy to those defined
+
here. The default ciphers are those of the JVM platform.
+
clientAuth = mkOption {
+
type = types.enum [ "NONE" "OPTIONAL" "REQUIRE" ];
+
The client authentication stance for this policy.
+
privateKey = mkOption {
+
default = "private.key";
+
The name of private PKCS #8 key file for this policy to be found
+
in the <option>baseDirectory</option>, or the absolute path to
+
the key file. It is mandatory that a key can be found or generated.
+
publicCertificate = mkOption {
+
default = "public.crt";
+
The name of public X.509 certificate (chain) file in PEM format
+
for this policy to be found in the <option>baseDirectory</option>,
+
or the absolute path to the certificate file. It is mandatory
+
that a certificate can be found or generated.
+
The public certificate is required to be duplicated to the
+
directory holding trusted certificates as defined by the
+
<option>trustedDir</option> option.
+
revokedDir = mkOption {
+
default = "${config.baseDirectory}/revoked";
+
Path to directory of CRLs (Certificate Revocation Lists) in
+
PEM format. Must be an absolute path. The existence of this
+
directory is mandatory and will need to be created manually when:
+
setting this option to something other than its default; setting
+
either this policy's <option>baseDirectory</option> or
+
<option>directories.certificates</option> to something other than
+
their default. Ensure read/write permissions are given to the
+
Neo4j daemon user <literal>neo4j</literal>.
+
tlsVersions = mkOption {
+
type = types.listOf types.str;
+
default = [ "TLSv1.2" ];
+
Restrict the TLS protocol versions of this policy to those
+
Makes this policy trust all remote parties. Enabling this is not
+
recommended and the policy's trusted directory will be ignored.
+
Use of this mode is discouraged. It would offer encryption but
+
trustedDir = mkOption {
+
default = "${config.baseDirectory}/trusted";
+
Path to directory of X.509 certificates in PEM format for
+
trusted parties. Must be an absolute path. The existence of this
+
directory is mandatory and will need to be created manually when:
+
setting this option to something other than its default; setting
+
either this policy's <option>baseDirectory</option> or
+
<option>directories.certificates</option> to something other than
+
their default. Ensure read/write permissions are given to the
+
Neo4j daemon user <literal>neo4j</literal>.
+
The public certificate as defined by
+
<option>publicCertificate</option> is required to be duplicated
+
directoriesToCreate = mkOption {
+
type = types.listOf types.path;
+
Directories of this policy that will be created automatically
+
when the certificates directory is left at its default value.
+
This includes all options of type path that are left at their
+
config.directoriesToCreate = optionals
+
(certDirOpt.highestPrio >= 1500 && options.baseDirectory.highestPrio >= 1500)
+
(map (opt: opt.value) (filter isDefaultPathOption (attrValues options)));
+
Defines the SSL policies for use with Neo4j connectors. Each attribute
+
of this set defines a policy, with the attribute name defining the name
+
of the policy and its namespace. Refer to the operations manual section
+
<link xlink:href="https://neo4j.com/docs/operations-manual/current/security/ssl-framework/">SSL Framework</link>
+
Enable the Usage Data Collector which Neo4j uses to collect usage
+
data. Refer to the operations manual section on the
+
<link xlink:href="https://neo4j.com/docs/operations-manual/current/configuration/usage-data-collector/">Usage Data Collector</link>
+
policyNameList = attrNames cfg.ssl.policies;
+
validPolicyNameList = [ "legacy" ] ++ policyNameList;
+
validPolicyNameString = concatStringsSep ", " validPolicyNameList;
+
# Capture various directories left at their default so they can be created.
+
defaultDirectoriesToCreate = map (opt: opt.value) (filter isDefaultPathOption (attrValues options.services.neo4j.directories));
+
policyDirectoriesToCreate = concatMap (pol: pol.directoriesToCreate) (attrValues cfg.ssl.policies);
+
{ assertion = !elem "legacy" policyNameList;
+
message = "The policy 'legacy' is special to Neo4j, and its name is reserved."; }
+
{ assertion = elem cfg.bolt.sslPolicy validPolicyNameList;
+
message = "Invalid policy assigned: `services.neo4j.bolt.sslPolicy = \"${cfg.bolt.sslPolicy}\"`, defined policies are: ${validPolicyNameString}"; }
+
{ assertion = elem cfg.https.sslPolicy validPolicyNameList;
+
message = "Invalid policy assigned: `services.neo4j.https.sslPolicy = \"${cfg.https.sslPolicy}\"`, defined policies are: ${validPolicyNameString}"; }
+
systemd.services.neo4j = {
+
description = "Neo4j Daemon";
+
wantedBy = [ "multi-user.target" ];
+
after = [ "network.target" ];
+
NEO4J_HOME = "${cfg.package}/share/neo4j";
+
NEO4J_CONF = "${cfg.directories.home}/conf";
+
ExecStart = "${cfg.package}/bin/neo4j console";
+
PermissionsStartOnly = true;
+
# Always ensure home exists with nested conf, logs directories.
+
mkdir -m 0700 -p ${cfg.directories.home}/{conf,logs}
+
# Create other sub-directories and policy directories that have been left at their default.
+
${concatMapStringsSep "\n" (
+
mkdir -m 0700 -p ${dir}
+
'') (defaultDirectoriesToCreate ++ policyDirectoriesToCreate)}
+
# Place the configuration where Neo4j can find it.
+
ln -fs ${serverConfig} ${cfg.directories.home}/conf/neo4j.conf
+
# Ensure neo4j user ownership
+
chown -R neo4j ${cfg.directories.home}
+
environment.systemPackages = [ cfg.package ];
+
users.users = singleton {
+
uid = config.ids.uids.neo4j;
+
description = "Neo4j daemon user";
+
home = cfg.directories.home;
+
maintainers = with lib.maintainers; [ patternspandemic ];