1 { config, pkgs, lib, ... }:
3 cfg = config.services.dgraph;
4 settingsFormat = pkgs.formats.json {};
5 configFile = settingsFormat.generate "config.json" cfg.settings;
6 dgraphWithNode = pkgs.runCommand "dgraph" {
7 nativeBuildInputs = [ pkgs.makeWrapper ];
11 makeWrapper ${cfg.package}/bin/dgraph $out/bin/dgraph \
12 --prefix PATH : "${lib.makeBinPath [ pkgs.nodejs ]}" \
15 NoNewPrivileges = true;
17 AmbientCapabilities = "";
18 CapabilityBoundingSet = "";
22 LockPersonality = true;
25 PrivateDevices = true;
29 ProtectControlGroups = true;
30 ProtectHostname = true;
31 ProtectKernelLogs = true;
32 ProtectKernelModules = true;
33 ProtectKernelTunables = true;
37 RestrictNamespaces = true;
38 RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
39 RestrictRealtime = true;
40 RestrictSUIDSGID = true;
42 SystemCallArchitectures = "native";
43 SystemCallErrorNumber = "EPERM";
46 "~@cpu-emulation" "~@debug" "~@keyring" "~@memlock" "~@obsolete" "~@privileged" "~@setuid"
53 enable = lib.mkEnableOption "Dgraph native GraphQL database with a graph backend";
55 package = lib.mkPackageOption pkgs "dgraph" { };
57 settings = lib.mkOption {
58 type = settingsFormat.type;
61 Contents of the dgraph config. For more details see https://dgraph.io/docs/deploy/config
68 default = "localhost";
70 The host which dgraph alpha will be run on.
74 type = lib.types.port;
77 The port which to run dgraph alpha on.
86 default = "localhost";
88 The host which dgraph zero will be run on.
92 type = lib.types.port;
95 The port which to run dgraph zero on.
103 config = lib.mkIf cfg.enable {
104 services.dgraph.settings = {
105 badger.compression = lib.mkDefault "zstd:3";
108 systemd.services.dgraph-zero = {
109 description = "Dgraph native GraphQL database with a graph backend. Zero controls node clustering";
110 after = [ "network.target" ];
111 wantedBy = [ "multi-user.target" ];
114 StateDirectory = "dgraph-zero";
115 WorkingDirectory = "/var/lib/dgraph-zero";
117 ExecStart = "${cfg.package}/bin/dgraph zero --my ${cfg.zero.host}:${toString cfg.zero.port}";
118 Restart = "on-failure";
119 } // securityOptions;
122 systemd.services.dgraph-alpha = {
123 description = "Dgraph native GraphQL database with a graph backend. Alpha serves data";
124 after = [ "network.target" "dgraph-zero.service" ];
125 requires = [ "dgraph-zero.service" ];
126 wantedBy = [ "multi-user.target" ];
129 StateDirectory = "dgraph-alpha";
130 WorkingDirectory = "/var/lib/dgraph-alpha";
132 ExecStart = "${dgraphWithNode}/bin/dgraph alpha --config ${configFile} --my ${cfg.alpha.host}:${toString cfg.alpha.port} --zero ${cfg.zero.host}:${toString cfg.zero.port}";
134 ${pkgs.curl}/bin/curl --data "mutation { shutdown { response { message code } } }" \
135 --header 'Content-Type: application/graphql' \
137 http://localhost:8080/admin
139 Restart = "on-failure";
140 } // securityOptions;
144 meta.maintainers = with lib.maintainers; [ happysalada ];