|
| 1 | +{ config, lib, pkgs, ... }: |
| 2 | +let |
| 3 | + cfg = config.services.tigerbeetle; |
| 4 | +in |
| 5 | +{ |
| 6 | + meta = { |
| 7 | + maintainers = with lib.maintainers; [ danielsidhion ]; |
| 8 | + doc = ./tigerbeetle.md; |
| 9 | + buildDocsInSandbox = true; |
| 10 | + }; |
| 11 | + |
| 12 | + options = { |
| 13 | + services.tigerbeetle = with lib; { |
| 14 | + enable = mkEnableOption (mdDoc "TigerBeetle server"); |
| 15 | + |
| 16 | + package = mkPackageOption pkgs "tigerbeetle" { }; |
| 17 | + |
| 18 | + clusterId = mkOption { |
| 19 | + type = types.either types.ints.unsigned (types.strMatching "[0-9]+"); |
| 20 | + default = 0; |
| 21 | + description = lib.mdDoc '' |
| 22 | + The 128-bit cluster ID used to create the replica data file (if needed). |
| 23 | + Since Nix only supports integers up to 64 bits, you need to pass a string to this if the cluster ID can't fit in 64 bits. |
| 24 | + Otherwise, you can pass the cluster ID as either an integer or a string. |
| 25 | + ''; |
| 26 | + }; |
| 27 | + |
| 28 | + replicaIndex = mkOption { |
| 29 | + type = types.ints.unsigned; |
| 30 | + default = 0; |
| 31 | + description = lib.mdDoc '' |
| 32 | + The index (starting at 0) of the replica in the cluster. |
| 33 | + ''; |
| 34 | + }; |
| 35 | + |
| 36 | + replicaCount = mkOption { |
| 37 | + type = types.ints.unsigned; |
| 38 | + default = 1; |
| 39 | + description = lib.mdDoc '' |
| 40 | + The number of replicas participating in replication of the cluster. |
| 41 | + ''; |
| 42 | + }; |
| 43 | + |
| 44 | + cacheGridSize = mkOption { |
| 45 | + type = types.strMatching "[0-9]+(K|M|G)B"; |
| 46 | + default = "1GB"; |
| 47 | + description = lib.mdDoc '' |
| 48 | + The grid cache size. |
| 49 | + The grid cache acts like a page cache for TigerBeetle. |
| 50 | + It is recommended to set this as large as possible. |
| 51 | + ''; |
| 52 | + }; |
| 53 | + |
| 54 | + addresses = mkOption { |
| 55 | + type = types.listOf types.nonEmptyStr; |
| 56 | + default = [ "3001" ]; |
| 57 | + description = lib.mdDoc '' |
| 58 | + The addresses of all replicas in the cluster. |
| 59 | + This should be a list of IPv4/IPv6 addresses with port numbers. |
| 60 | + Either the address or port number (but not both) may be omitted, in which case a default of 127.0.0.1 or 3001 will be used. |
| 61 | + The first address in the list corresponds to the address for replica 0, the second address for replica 1, and so on. |
| 62 | + ''; |
| 63 | + }; |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | + config = lib.mkIf cfg.enable { |
| 68 | + assertions = |
| 69 | + let |
| 70 | + numAddresses = builtins.length cfg.addresses; |
| 71 | + in |
| 72 | + [ |
| 73 | + { |
| 74 | + assertion = cfg.replicaIndex < cfg.replicaCount; |
| 75 | + message = "the TigerBeetle replica index must fit the configured replica count"; |
| 76 | + } |
| 77 | + { |
| 78 | + assertion = cfg.replicaCount == numAddresses; |
| 79 | + message = if cfg.replicaCount < numAddresses then "TigerBeetle must not have more addresses than the configured number of replicas" else "TigerBeetle must be configured with the addresses of all replicas"; |
| 80 | + } |
| 81 | + ]; |
| 82 | + |
| 83 | + systemd.services.tigerbeetle = |
| 84 | + let |
| 85 | + replicaDataPath = "/var/lib/tigerbeetle/${builtins.toString cfg.clusterId}_${builtins.toString cfg.replicaIndex}.tigerbeetle"; |
| 86 | + in |
| 87 | + { |
| 88 | + description = "TigerBeetle server"; |
| 89 | + |
| 90 | + wantedBy = [ "multi-user.target" ]; |
| 91 | + after = [ "network.target" ]; |
| 92 | + |
| 93 | + preStart = '' |
| 94 | + if ! test -e "${replicaDataPath}"; then |
| 95 | + ${lib.getExe cfg.package} format --cluster="${builtins.toString cfg.clusterId}" --replica="${builtins.toString cfg.replicaIndex}" --replica-count="${builtins.toString cfg.replicaCount}" "${replicaDataPath}" |
| 96 | + fi |
| 97 | + ''; |
| 98 | + |
| 99 | + serviceConfig = { |
| 100 | + Type = "exec"; |
| 101 | + |
| 102 | + DynamicUser = true; |
| 103 | + ProtectHome = true; |
| 104 | + DevicePolicy = "closed"; |
| 105 | + |
| 106 | + StateDirectory = "tigerbeetle"; |
| 107 | + StateDirectoryMode = 700; |
| 108 | + |
| 109 | + ExecStart = "${lib.getExe cfg.package} start --cache-grid=${cfg.cacheGridSize} --addresses=${lib.escapeShellArg (builtins.concatStringsSep "," cfg.addresses)} ${replicaDataPath}"; |
| 110 | + }; |
| 111 | + }; |
| 112 | + |
| 113 | + environment.systemPackages = [ cfg.package ]; |
| 114 | + }; |
| 115 | +} |
0 commit comments