git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/bitwarden_rs: init
commitef1a43030bb11d5435a027bea2deb176133ddadf authorMatthijs Steen <emailmatthijs@gmail.com> dateTue 23 Apr 2019, 21:27 (8 years ago) parent8a0a971
3 files changed+144
nixos/modules/module-list.nix+1 nixos/modules/services/security/bitwarden_rs/backup.sh+17 nixos/modules/services/security/bitwarden_rs/default.nix+126
nixos/modules/module-list.nix+1
@@ -705,6 +705,7 @@
705705 ./services/search/hound.nix
706706 ./services/search/kibana.nix
707707 ./services/search/solr.nix
708+ ./services/security/bitwarden_rs/default.nix
708709 ./services/security/certmgr.nix
709710 ./services/security/cfssl.nix
710711 ./services/security/clamav.nix
nixos/modules/services/security/bitwarden_rs/backup.shnew file+17
@@ -0,0 +1,17 @@
1+#!/usr/bin/env bash
2+
3+# Based on: https://github.com/dani-garcia/bitwarden_rs/wiki/Backing-up-your-vault
4+if ! mkdir -p "$BACKUP_FOLDER"; then
5+ echo "Could not create backup folder '$BACKUP_FOLDER'" >&2
6+ exit 1
7+fi
8+
9+if [[ ! -f "$DATA_FOLDER"/db.sqlite3 ]]; then
10+ echo "Could not find SQLite database file '$DATA_FOLDER/db.sqlite3'" >&2
11+ exit 1
12+fi
13+
14+sqlite3 "$DATA_FOLDER"/db.sqlite3 ".backup '$BACKUP_FOLDER/db.sqlite3'"
15+cp "$DATA_FOLDER"/rsa_key.{der,pem,pub.der} "$BACKUP_FOLDER"
16+cp -r "$DATA_FOLDER"/attachments "$BACKUP_FOLDER"
17+cp -r "$DATA_FOLDER"/icon_cache "$BACKUP_FOLDER"
nixos/modules/services/security/bitwarden_rs/default.nixnew file+126
@@ -0,0 +1,126 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.bitwarden_rs;
7+ user = config.users.users.bitwarden_rs.name;
8+ group = config.users.groups.bitwarden_rs.name;
9+
10+ # Convert name from camel case (e.g. disable2FARemember) to upper case snake case (e.g. DISABLE_2FA_REMEMBER).
11+ nameToEnvVar = name:
12+ let
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;
20+
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 []
24+ ) cfg.config))));
25+
26+in {
27+ options.services.bitwarden_rs = with types; {
28+ enable = mkEnableOption "bitwarden_rs";
29+
30+ backupDir = mkOption {
31+ type = nullOr str;
32+ default = null;
33+ description = ''
34+ The directory under which bitwarden_rs will backup its persistent data.
35+ '';
36+ };
37+
38+ config = mkOption {
39+ type = attrsOf (nullOr (either (either bool int) str));
40+ default = {};
41+ example = literalExample ''
42+ {
43+ domain = https://bw.domain.tld:8443;
44+ signupsAllowed = true;
45+ rocketPort = 8222;
46+ rocketLog = "critical";
47+ }
48+ '';
49+ description = ''
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.
58+
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.
61+
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>.
64+ '';
65+ apply = config: optionalAttrs config.webVaultEnabled {
66+ webVaultFolder = "${pkgs.bitwarden_rs-vault}/share/bitwarden_rs/vault";
67+ } // config;
68+ };
69+ };
70+
71+ config = mkIf cfg.enable {
72+ services.bitwarden_rs.config = {
73+ dataFolder = "/var/lib/bitwarden_rs";
74+ webVaultEnabled = mkDefault true;
75+ };
76+
77+ users.users.bitwarden_rs = { inherit group; };
78+ users.groups.bitwarden_rs = { };
79+
80+ systemd.services.bitwarden_rs = {
81+ after = [ "network.target" ];
82+ path = with pkgs; [ openssl ];
83+ serviceConfig = {
84+ User = user;
85+ Group = group;
86+ EnvironmentFile = configFile;
87+ ExecStart = "${pkgs.bitwarden_rs}/bin/bitwarden_rs";
88+ LimitNOFILE = "1048576";
89+ LimitNPROC = "64";
90+ PrivateTmp = "true";
91+ PrivateDevices = "true";
92+ ProtectHome = "true";
93+ ProtectSystem = "strict";
94+ AmbientCapabilities = "CAP_NET_BIND_SERVICE";
95+ StateDirectory = "bitwarden_rs";
96+ };
97+ wantedBy = [ "multi-user.target" ];
98+ };
99+
100+ systemd.services.backup-bitwarden_rs = mkIf (cfg.backupDir != null) {
101+ description = "Backup bitwarden_rs";
102+ environment = {
103+ DATA_FOLDER = "/var/lib/bitwarden_rs";
104+ BACKUP_FOLDER = cfg.backupDir;
105+ };
106+ path = with pkgs; [ sqlite ];
107+ serviceConfig = {
108+ SyslogIdentifier = "backup-bitwarden_rs";
109+ User = mkDefault user;
110+ Group = mkDefault group;
111+ ExecStart = "${pkgs.bash}/bin/bash ${./backup.sh}";
112+ };
113+ wantedBy = [ "multi-user.target" ];
114+ };
115+
116+ systemd.timers.backup-bitwarden_rs = mkIf (cfg.backupDir != null) {
117+ description = "Backup bitwarden_rs on time";
118+ timerConfig = {
119+ OnCalendar = mkDefault "23:00";
120+ Persistent = "true";
121+ Unit = "backup-bitwarden_rs.service";
122+ };
123+ wantedBy = [ "multi-user.target" ];
124+ };
125+ };
126+}