···
1
+
{ config, lib, pkgs, ... }:
6
+
cfg = config.services.bitwarden_rs;
7
+
user = config.users.users.bitwarden_rs.name;
8
+
group = config.users.groups.bitwarden_rs.name;
10
+
# Convert name from camel case (e.g. disable2FARemember) to upper case snake case (e.g. DISABLE_2FA_REMEMBER).
11
+
nameToEnvVar = name:
13
+
parts = builtins.split "([A-Z0-9]+)" name;
14
+
partsToEnvVar = parts: foldl' (key: x: let last = stringLength key - 1; in
15
+
if isList x then key + optionalString (key != "" && substring last 1 key != "_") "_" + head x
16
+
else if key != "" && elem (substring 0 1 x) lowerChars then # to handle e.g. [ "disable" [ "2FAR" ] "emember" ]
17
+
substring 0 last key + optionalString (substring (last - 1) 1 key != "_") "_" + substring last 1 key + toUpper x
18
+
else key + toUpper x) "" parts;
19
+
in if builtins.match "[A-Z0-9_]+" name != null then name else partsToEnvVar parts;
21
+
configFile = pkgs.writeText "bitwarden_rs.env" (concatMapStrings (s: s + "\n") (
22
+
(concatLists (mapAttrsToList (name: value:
23
+
if value != null then [ "${nameToEnvVar name}=${if isBool value then boolToString value else toString value}" ] else []
27
+
options.services.bitwarden_rs = with types; {
28
+
enable = mkEnableOption "bitwarden_rs";
30
+
backupDir = mkOption {
34
+
The directory under which bitwarden_rs will backup its persistent data.
39
+
type = attrsOf (nullOr (either (either bool int) str));
41
+
example = literalExample ''
43
+
domain = https://bw.domain.tld:8443;
44
+
signupsAllowed = true;
46
+
rocketLog = "critical";
50
+
The configuration of bitwarden_rs is done through environment variables,
51
+
therefore the names are converted from camel case (e.g. disable2FARemember)
52
+
to upper case snake case (e.g. DISABLE_2FA_REMEMBER).
53
+
In this conversion digits (0-9) are handled just like upper case characters,
54
+
so foo2 would be converted to FOO_2.
55
+
Names already in this format remain unchanged, so FOO2 remains FOO2 if passed as such,
56
+
even though foo2 would have been converted to FOO_2.
57
+
This allows working around any potential future conflicting naming conventions.
59
+
Based on the attributes passed to this config option a environment file will be generated
60
+
that is passed to bitwarden_rs's systemd service.
62
+
The available configuration options can be found in
63
+
<link xlink:href="https://github.com/dani-garcia/bitwarden_rs/blob/1.8.0/.env.template">the environment template file</link>.
65
+
apply = config: optionalAttrs config.webVaultEnabled {
66
+
webVaultFolder = "${pkgs.bitwarden_rs-vault}/share/bitwarden_rs/vault";
71
+
config = mkIf cfg.enable {
72
+
services.bitwarden_rs.config = {
73
+
dataFolder = "/var/lib/bitwarden_rs";
74
+
webVaultEnabled = mkDefault true;
77
+
users.users.bitwarden_rs = { inherit group; };
78
+
users.groups.bitwarden_rs = { };
80
+
systemd.services.bitwarden_rs = {
81
+
after = [ "network.target" ];
82
+
path = with pkgs; [ openssl ];
86
+
EnvironmentFile = configFile;
87
+
ExecStart = "${pkgs.bitwarden_rs}/bin/bitwarden_rs";
88
+
LimitNOFILE = "1048576";
90
+
PrivateTmp = "true";
91
+
PrivateDevices = "true";
92
+
ProtectHome = "true";
93
+
ProtectSystem = "strict";
94
+
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
95
+
StateDirectory = "bitwarden_rs";
97
+
wantedBy = [ "multi-user.target" ];
100
+
systemd.services.backup-bitwarden_rs = mkIf (cfg.backupDir != null) {
101
+
description = "Backup bitwarden_rs";
103
+
DATA_FOLDER = "/var/lib/bitwarden_rs";
104
+
BACKUP_FOLDER = cfg.backupDir;
106
+
path = with pkgs; [ sqlite ];
108
+
SyslogIdentifier = "backup-bitwarden_rs";
109
+
User = mkDefault user;
110
+
Group = mkDefault group;
111
+
ExecStart = "${pkgs.bash}/bin/bash ${./backup.sh}";
113
+
wantedBy = [ "multi-user.target" ];
116
+
systemd.timers.backup-bitwarden_rs = mkIf (cfg.backupDir != null) {
117
+
description = "Backup bitwarden_rs on time";
119
+
OnCalendar = mkDefault "23:00";
120
+
Persistent = "true";
121
+
Unit = "backup-bitwarden_rs.service";
123
+
wantedBy = [ "multi-user.target" ];