git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #55413 from msteen/bitwarden_rs

bitwarden_rs: init at 1.8.0

commit508fd8f13372b131f5c549765086e0f76b60d3da authorSilvan Mosberger <infinisil@icloud.com> dateWed 24 Apr 2019, 00:25 (8 years ago) parentb561092ef1a430
6 files changed+200
nixos/modules/module-list.nix+1 nixos/modules/services/security/bitwarden_rs/backup.sh+17 nixos/modules/services/security/bitwarden_rs/default.nix+126 pkgs/tools/security/bitwarden_rs/default.nix+27 pkgs/tools/security/bitwarden_rs/vault.nix+25 pkgs/top-level/all-packages.nix+4
nixos/modules/module-list.nix+1
@@ -707,6 +707,7 @@
707707 ./services/search/hound.nix
708708 ./services/search/kibana.nix
709709 ./services/search/solr.nix
710+ ./services/security/bitwarden_rs/default.nix
710711 ./services/security/certmgr.nix
711712 ./services/security/cfssl.nix
712713 ./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+}
pkgs/tools/security/bitwarden_rs/default.nixnew file+27
@@ -0,0 +1,27 @@
1+{ lib, rustPlatform, fetchFromGitHub, pkgconfig, openssl }:
2+
3+rustPlatform.buildRustPackage rec {
4+ pname = "bitwarden_rs";
5+ version = "1.8.0";
6+
7+ src = fetchFromGitHub {
8+ owner = "dani-garcia";
9+ repo = pname;
10+ rev = version;
11+ sha256 = "0jz9r6ck6sfz4ig95x0ja6g5ikyq6z0xw1zn9zf4kxha4klqqbkx";
12+ };
13+
14+ buildInputs = [ pkgconfig openssl ];
15+
16+ RUSTC_BOOTSTRAP = 1;
17+
18+ cargoSha256 = "0bzid5wrpcrghazv5652ghyv4amp298p5kfridswv175kmr9gg0x";
19+
20+ meta = with lib; {
21+ description = "An unofficial lightweight implementation of the Bitwarden server API using Rust and SQLite";
22+ homepage = https://github.com/dani-garcia/bitwarden_rs;
23+ license = licenses.gpl3;
24+ maintainers = with maintainers; [ msteen ];
25+ platforms = platforms.all;
26+ };
27+}
pkgs/tools/security/bitwarden_rs/vault.nixnew file+25
@@ -0,0 +1,25 @@
1+{ stdenv, fetchurl }:
2+
3+stdenv.mkDerivation rec {
4+ pname = "bitwarden_rs-vault";
5+ version = "2.9.0";
6+
7+ src = fetchurl {
8+ url = "https://github.com/dani-garcia/bw_web_builds/releases/download/v${version}/bw_web_v${version}.tar.gz";
9+ sha256 = "0kh7nqd688ilw73n4pw3s6fahghwbhiql548js6cdwsp4car3vbb";
10+ };
11+
12+ buildCommand = ''
13+ mkdir -p $out/share/bitwarden_rs/vault
14+ cd $out/share/bitwarden_rs/vault
15+ tar xf $src
16+ '';
17+
18+ meta = with stdenv.lib; {
19+ description = "Integrates the web vault into bitwarden_rs";
20+ homepage = https://github.com/dani-garcia/bw_web_builds;
21+ platforms = platforms.all;
22+ license = licenses.gpl3;
23+ maintainers = with maintainers; [ msteen ];
24+ };
25+}
pkgs/top-level/all-packages.nix+4
@@ -666,6 +666,10 @@ in
666666
667667 bcachefs-tools = callPackage ../tools/filesystems/bcachefs-tools { };
668668
669+ bitwarden_rs = callPackage ../tools/security/bitwarden_rs { };
670+
671+ bitwarden_rs-vault = callPackage ../tools/security/bitwarden_rs/vault.nix { };
672+
669673 bitwarden-cli = callPackage ../tools/security/bitwarden-cli { };
670674
671675 bmap-tools = callPackage ../tools/misc/bmap-tools { };