|
| 1 | +{ pkgs |
| 2 | +, lib |
| 3 | +, name |
| 4 | +, config |
| 5 | +, ... |
| 6 | +}: |
| 7 | +{ |
| 8 | + options = { |
| 9 | + package = lib.mkPackageOption pkgs "dynamodb-local" { }; |
| 10 | + |
| 11 | + port = lib.mkOption { |
| 12 | + type = lib.types.port; |
| 13 | + default = 8000; |
| 14 | + description = '' |
| 15 | + The port number that DynamoDB uses to communicate with your application. |
| 16 | + ''; |
| 17 | + }; |
| 18 | + |
| 19 | + dbPath = lib.mkOption { |
| 20 | + type = lib.types.nullOr lib.types.str; |
| 21 | + default = null; |
| 22 | + description = '' |
| 23 | + The directory where DynamoDB writes its database file. If you don't specify this option, the file |
| 24 | + is written to the current directory. |
| 25 | + ''; |
| 26 | + }; |
| 27 | + |
| 28 | + inMemory = lib.mkOption { |
| 29 | + type = lib.types.bool; |
| 30 | + default = false; |
| 31 | + description = '' |
| 32 | + DynamoDB runs in memory instead of using a database file. When you stop DynamoDB, none of the |
| 33 | + data is saved. |
| 34 | + ''; |
| 35 | + apply = |
| 36 | + v: |
| 37 | + lib.throwIf (config.dbPath != null) '' |
| 38 | + You can't specify both -dbPath and -inMemory at once. |
| 39 | + '' |
| 40 | + v; |
| 41 | + }; |
| 42 | + |
| 43 | + extraArgs = lib.mkOption { |
| 44 | + type = lib.types.listOf lib.types.str; |
| 45 | + default = [ ]; |
| 46 | + description = '' |
| 47 | + Extra args to pass down to DynamoDB. |
| 48 | + ''; |
| 49 | + }; |
| 50 | + |
| 51 | + disableTelemetry = lib.mkOption { |
| 52 | + type = lib.types.bool; |
| 53 | + default = true; |
| 54 | + description = '' |
| 55 | + Specify if DynamoDB local should send telemetry. |
| 56 | + ''; |
| 57 | + }; |
| 58 | + }; |
| 59 | + |
| 60 | + config.outputs.settings.processes.${name} = |
| 61 | + let |
| 62 | + startCommand = pkgs.writeShellApplication { |
| 63 | + name = "start-dynamodb"; |
| 64 | + runtimeInputs = [ config.package ]; |
| 65 | + text = '' |
| 66 | + ${lib.optionalString (config.dbPath != null) '' |
| 67 | + mkdir -p ${lib.escapeShellArg config.dbPath} |
| 68 | + ''} |
| 69 | + exec dynamodb-local \ |
| 70 | + -port ${toString config.port} \ |
| 71 | + ${lib.optionalString (config.dbPath != null) "-dbPath ${lib.escapeShellArg config.dbPath}"} \ |
| 72 | + ${lib.optionalString config.inMemory "-inMemory"} \ |
| 73 | + ${lib.optionalString config.disableTelemetry "-disableTelemetry"} \ |
| 74 | + ${lib.escapeShellArgs config.extraArgs} |
| 75 | + ''; |
| 76 | + }; |
| 77 | + in |
| 78 | + { |
| 79 | + command = startCommand; |
| 80 | + readiness_probe = { |
| 81 | + # There is no explicit healthcheck in DynamoDB. Instead, it's a common practice to |
| 82 | + # use `list-tables` opearation as a healthcheck. Inspired by Localstack and devenv |
| 83 | + # DynamoDB service. |
| 84 | + # DynamoDB Local doesn't have any credentials, but awscli expects them. |
| 85 | + exec.command = '' |
| 86 | + AWS_ACCESS_KEY_ID='fake' \ |
| 87 | + AWS_SECRET_ACCESS_KEY='fake' \ |
| 88 | + AWS_DEFAULT_REGION='us-east-1' \ |
| 89 | + ${lib.getExe pkgs.awscli2} dynamodb list-tables \ |
| 90 | + --endpoint-url http://127.0.0.1:${toString config.port} |
| 91 | + ''; |
| 92 | + initial_delay_seconds = 2; |
| 93 | + period_seconds = 10; |
| 94 | + timeout_seconds = 4; |
| 95 | + success_threshold = 1; |
| 96 | + failure_threshold = 5; |
| 97 | + }; |
| 98 | + # https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy |
| 99 | + availability = { |
| 100 | + restart = "on_failure"; |
| 101 | + max_restarts = 5; |
| 102 | + }; |
| 103 | + }; |
| 104 | +} |
0 commit comments