1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.services.litestream; 7 settingsFormat = pkgs.formats.yaml {}; 8in 9{ 10 options.services.litestream = { 11 enable = mkEnableOption (lib.mdDoc "litestream"); 12 13 package = mkOption { 14 description = lib.mdDoc "Package to use."; 15 default = pkgs.litestream; 16 defaultText = literalExpression "pkgs.litestream"; 17 type = types.package; 18 }; 19 20 settings = mkOption { 21 description = lib.mdDoc '' 22 See the [documentation](https://litestream.io/reference/config/). 23 ''; 24 type = settingsFormat.type; 25 example = { 26 dbs = [ 27 { 28 path = "/var/lib/db1"; 29 replicas = [ 30 { 31 url = "s3://mybkt.litestream.io/db1"; 32 } 33 ]; 34 } 35 ]; 36 }; 37 }; 38 39 environmentFile = mkOption { 40 type = types.nullOr types.path; 41 default = null; 42 example = "/run/secrets/litestream"; 43 description = lib.mdDoc '' 44 Environment file as defined in {manpage}`systemd.exec(5)`. 45 46 Secrets may be passed to the service without adding them to the 47 world-readable Nix store, by specifying placeholder variables as 48 the option value in Nix and setting these variables accordingly in the 49 environment file. 50 51 By default, Litestream will perform environment variable expansion 52 within the config file before reading it. Any references to ''$VAR or 53 ''${VAR} formatted variables will be replaced with their environment 54 variable values. If no value is set then it will be replaced with an 55 empty string. 56 57 ``` 58 # Content of the environment file 59 LITESTREAM_ACCESS_KEY_ID=AKIAxxxxxxxxxxxxxxxx 60 LITESTREAM_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxx 61 ``` 62 63 Note that this file needs to be available on the host on which 64 this exporter is running. 65 ''; 66 }; 67 }; 68 69 config = mkIf cfg.enable { 70 environment.systemPackages = [ cfg.package ]; 71 environment.etc = { 72 "litestream.yml" = { 73 source = settingsFormat.generate "litestream-config.yaml" cfg.settings; 74 }; 75 }; 76 77 systemd.services.litestream = { 78 description = "Litestream"; 79 wantedBy = [ "multi-user.target" ]; 80 after = [ "networking.target" ]; 81 serviceConfig = { 82 EnvironmentFile = mkIf (cfg.environmentFile != null) cfg.environmentFile; 83 ExecStart = "${cfg.package}/bin/litestream replicate"; 84 Restart = "always"; 85 User = "litestream"; 86 Group = "litestream"; 87 }; 88 }; 89 90 users.users.litestream = { 91 description = "Litestream user"; 92 group = "litestream"; 93 isSystemUser = true; 94 }; 95 users.groups.litestream = {}; 96 }; 97 98 meta.doc = ./default.md; 99}