8 cfg = config.services.anki-sync-server;
9 name = "anki-sync-server";
10 specEscape = replaceStrings ["%"] ["%%"];
12 lists.imap1 (i: user: {
17 usersWithIndexesFile = filter (x: x.user.passwordFile != null) usersWithIndexes;
18 usersWithIndexesNoFile = filter (x: x.user.passwordFile == null && x.user.password != null) usersWithIndexes;
19 anki-sync-server-run = pkgs.writeShellScriptBin "anki-sync-server-run" ''
20 # When services.anki-sync-server.users.passwordFile is set,
21 # each password file is passed as a systemd credential, which is mounted in
22 # a file system exposed to the service. Here we read the passwords from
23 # the credential files to pass them as environment variables to the Anki
28 (x: ''export SYNC_USER${toString x.i}=${escapeShellArg x.user.username}:"''$(cat "''${CREDENTIALS_DIRECTORY}/"${escapeShellArg x.user.username})"'')
31 # For users where services.anki-sync-server.users.password isn't set,
32 # export passwords in environment variables in plaintext.
36 (x: ''export SYNC_USER${toString x.i}=${escapeShellArg x.user.username}:${escapeShellArg x.user.password}'')
37 usersWithIndexesNoFile
39 exec ${cfg.package}/bin/anki-sync-server
42 options.services.anki-sync-server = {
43 enable = mkEnableOption "anki-sync-server";
45 package = mkPackageOption pkgs "anki-sync-server" { };
51 IP address anki-sync-server listens to.
52 Note host names are not resolved.
59 description = "Port number anki-sync-server listens to.";
62 openFirewall = mkOption {
65 description = "Whether to open the firewall for the specified port.";
74 description = "User name accepted by anki-sync-server.";
80 Password accepted by anki-sync-server for the associated username.
81 **WARNING**: This option is **not secure**. This password will
82 be stored in *plaintext* and will be visible to *all users*.
83 See {option}`services.anki-sync-server.users.passwordFile` for
87 passwordFile = mkOption {
91 File containing the password accepted by anki-sync-server for
92 the associated username. Make sure to make readable only by
98 description = "List of user-password pairs to provide to the sync server.";
102 config = mkIf cfg.enable {
105 assertion = (builtins.length usersWithIndexesFile) + (builtins.length usersWithIndexesNoFile) > 0;
106 message = "At least one username-password pair must be set.";
109 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [cfg.port];
111 systemd.services.anki-sync-server = {
112 description = "anki-sync-server: Anki sync server built into Anki";
113 after = ["network.target"];
114 wantedBy = ["multi-user.target"];
115 path = [cfg.package];
118 SYNC_HOST = specEscape cfg.address;
119 SYNC_PORT = toString cfg.port;
125 StateDirectory = name;
126 ExecStart = "${anki-sync-server-run}/bin/anki-sync-server-run";
130 (x: "${specEscape x.user.username}:${specEscape (toString x.user.passwordFile)}")
131 usersWithIndexesFile;
137 maintainers = with maintainers; [telotortium];
138 doc = ./anki-sync-server.md;