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