8 cfg = config.services.bitwarden-directory-connector-cli;
10 options.services.bitwarden-directory-connector-cli = {
11 enable = mkEnableOption "Bitwarden Directory Connector";
13 package = mkPackageOption pkgs "bitwarden-directory-connector-cli" {};
17 description = "The domain the Bitwarden/Vaultwarden is accessible on.";
18 example = "https://vaultwarden.example.com";
23 description = "User to run the program.";
29 default = "*:0,15,30,45";
30 description = "The interval when to run the connector. This uses systemd's OnCalendar syntax.";
35 Options to configure the LDAP connection.
36 If you used the desktop application to test the configuration you can find the settings by searching for `ldap` in `~/.config/Bitwarden\ Directory\ Connector/data.json`.
39 type = types.submodule ({
44 freeformType = types.attrsOf (pkgs.formats.json {}).type;
46 config.finalJSON = builtins.toJSON (removeAttrs config (filter (x: x == "finalJSON" || ! options.${x}.isDefined or false) (attrNames options)));
49 finalJSON = mkOption {
50 type = (pkgs.formats.json {}).type;
59 description = "Whether to use TLS.";
64 description = "Whether to use STARTTLS.";
69 description = "The host the LDAP is accessible on.";
70 example = "ldap.example.com";
76 description = "Port LDAP is accessible on.";
82 description = "Whether the LDAP Server is an Active Directory.";
85 pagedSearch = mkOption {
88 description = "Whether the LDAP server paginates search results.";
93 description = "Root path for LDAP.";
94 example = "dc=example,dc=com";
99 description = "The user to authenticate as.";
100 example = "cn=admin,dc=example,dc=com";
108 Options to configure what gets synced.
109 If you used the desktop application to test the configuration you can find the settings by searching for `sync` in `~/.config/Bitwarden\ Directory\ Connector/data.json`.
112 type = types.submodule ({
117 freeformType = types.attrsOf (pkgs.formats.json {}).type;
119 config.finalJSON = builtins.toJSON (removeAttrs config (filter (x: x == "finalJSON" || ! options.${x}.isDefined or false) (attrNames options)));
122 finalJSON = mkOption {
123 type = (pkgs.formats.json {}).type;
129 removeDisabled = mkOption {
132 description = "Remove users from bitwarden groups if no longer in the ldap group.";
135 overwriteExisting = mkOption {
138 description = "Remove and re-add users/groups, See https://bitwarden.com/help/user-group-filters/#overwriting-syncs for more details.";
141 largeImport = mkOption {
144 description = "Enable if you are syncing more than 2000 users/groups.";
147 memberAttribute = mkOption {
149 description = "Attribute that lists members in a LDAP group.";
150 example = "uniqueMember";
153 creationDateAttribute = mkOption {
155 description = "Attribute that lists a user's creation date.";
156 example = "whenCreated";
159 useEmailPrefixSuffix = mkOption {
162 description = "If a user has no email address, combine a username prefix with a suffix value to form an email.";
164 emailPrefixAttribute = mkOption {
166 description = "The attribute that contains the users username.";
167 example = "accountName";
169 emailSuffix = mkOption {
171 description = "Suffix for the email, normally @example.com.";
172 example = "@example.com";
178 description = "Sync users.";
180 userPath = mkOption {
182 description = "User directory, relative to root.";
183 default = "ou=users";
185 userObjectClass = mkOption {
187 description = "Class that users must have.";
188 default = "inetOrgPerson";
190 userEmailAttribute = mkOption {
192 description = "Attribute for a users email.";
195 userFilter = mkOption {
197 description = "LDAP filter for users.";
198 example = "(memberOf=cn=sales,ou=groups,dc=example,dc=com)";
205 description = "Whether to sync ldap groups into BitWarden.";
207 groupPath = mkOption {
209 description = "Group directory, relative to root.";
210 default = "ou=groups";
212 groupObjectClass = mkOption {
214 description = "A class that groups will have.";
215 default = "groupOfNames";
217 groupNameAttribute = mkOption {
219 description = "Attribute for a name of group.";
222 groupFilter = mkOption {
224 description = "LDAP filter for groups.";
225 example = "(cn=sales)";
235 description = "Path to file that contains LDAP password for user in {option}`ldap.username";
239 client_path_id = mkOption {
241 description = "Path to file that contains Client ID.";
243 client_path_secret = mkOption {
245 description = "Path to file that contains Client Secret.";
251 config = mkIf cfg.enable {
252 users.groups."${cfg.user}" = {};
253 users.users."${cfg.user}" = {
259 timers.bitwarden-directory-connector-cli = {
260 description = "Sync timer for Bitwarden Directory Connector";
261 wantedBy = ["timers.target"];
262 after = ["network-online.target"];
263 wants = ["network-online.target"];
265 OnCalendar = cfg.interval;
266 Unit = "bitwarden-directory-connector-cli.service";
271 services.bitwarden-directory-connector-cli = {
272 description = "Main process for Bitwarden Directory Connector";
276 BITWARDENCLI_CONNECTOR_APPDATA_DIR = "/tmp";
277 BITWARDENCLI_CONNECTOR_PLAINTEXT_SECRETS = "true";
283 # create the config file
284 ${lib.getExe cfg.package} data-file
285 touch /tmp/data.json.tmp
286 chmod 600 /tmp/data.json{,.tmp}
288 ${lib.getExe cfg.package} config server ${cfg.domain}
290 # now login to set credentials
291 export BW_CLIENTID="$(< ${escapeShellArg cfg.secrets.bitwarden.client_path_id})"
292 export BW_CLIENTSECRET="$(< ${escapeShellArg cfg.secrets.bitwarden.client_path_secret})"
293 ${lib.getExe cfg.package} login
295 jq '.authenticatedAccounts[0] as $account
296 | .[$account].directoryConfigurations.ldap |= $ldap_data
297 | .[$account].directorySettings.organizationId |= $orgID
298 | .[$account].directorySettings.sync |= $sync_data' \
299 --argjson ldap_data ${escapeShellArg cfg.ldap.finalJSON} \
300 --arg orgID "''${BW_CLIENTID//organization.}" \
301 --argjson sync_data ${escapeShellArg cfg.sync.finalJSON} \
305 mv -f /tmp/data.json.tmp /tmp/data.json
308 ${lib.getExe cfg.package} config directory 0
309 ${lib.getExe cfg.package} config ldap.password --secretfile ${cfg.secrets.ldap}
314 User = "${cfg.user}";
316 ExecStart = "${lib.getExe cfg.package} sync";
322 meta.maintainers = with maintainers; [Silver-Golden];