2023-12-03 08:29:01 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
secrets-filepath = "/etc/selfprivacy/secrets.json";
|
2023-12-18 11:33:09 +00:00
|
|
|
backup-dir = "/var/lib/bitwarden/backup";
|
2024-02-15 09:56:12 +00:00
|
|
|
cfg = sp.modules.bitwarden;
|
2023-12-03 08:29:01 +00:00
|
|
|
inherit (import ./common.nix config) bitwarden-env sp;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.selfprivacy.modules.bitwarden = {
|
|
|
|
enable = lib.mkOption {
|
|
|
|
default = false;
|
2023-12-28 08:54:59 +00:00
|
|
|
type = lib.types.bool;
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
|
|
|
location = lib.mkOption {
|
2023-12-28 08:54:59 +00:00
|
|
|
type = lib.types.str;
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
2024-02-15 09:56:12 +00:00
|
|
|
subdomain = lib.mkOption {
|
|
|
|
default = "password";
|
|
|
|
type = lib.types.strMatching "[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]";
|
|
|
|
};
|
2024-06-30 17:47:08 +00:00
|
|
|
signupsAllowed = lib.mkOption {
|
|
|
|
default = true;
|
|
|
|
type = lib.types.bool;
|
|
|
|
};
|
|
|
|
sendsAllowed = lib.mkOption {
|
|
|
|
default = true;
|
|
|
|
type = lib.types.bool;
|
|
|
|
};
|
|
|
|
emergencyAccessAllowed = lib.mkOption {
|
|
|
|
default = true;
|
|
|
|
type = lib.types.bool;
|
|
|
|
};
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf config.selfprivacy.modules.bitwarden.enable {
|
|
|
|
fileSystems = lib.mkIf sp.useBinds {
|
|
|
|
"/var/lib/bitwarden" = {
|
2024-02-15 09:56:12 +00:00
|
|
|
device = "/volumes/${cfg.location}/bitwarden";
|
2023-12-18 11:33:09 +00:00
|
|
|
options = [
|
|
|
|
"bind"
|
|
|
|
"x-systemd.required-by=bitwarden-secrets.service"
|
2023-12-18 18:16:40 +00:00
|
|
|
"x-systemd.required-by=backup-vaultwarden.service"
|
2023-12-18 11:33:09 +00:00
|
|
|
"x-systemd.required-by=vaultwarden.service"
|
2023-12-22 14:07:14 +00:00
|
|
|
"x-systemd.before=bitwarden-secrets.service"
|
|
|
|
"x-systemd.before=backup-vaultwarden.service"
|
|
|
|
"x-systemd.before=vaultwarden.service"
|
2023-12-18 11:33:09 +00:00
|
|
|
];
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
|
|
|
"/var/lib/bitwarden_rs" = {
|
2024-02-15 09:56:12 +00:00
|
|
|
device = "/volumes/${cfg.location}/bitwarden_rs";
|
2023-12-18 11:33:09 +00:00
|
|
|
options = [
|
|
|
|
"bind"
|
|
|
|
"x-systemd.required-by=bitwarden-secrets.service"
|
2023-12-18 18:16:40 +00:00
|
|
|
"x-systemd.required-by=backup-vaultwarden.service"
|
2023-12-18 11:33:09 +00:00
|
|
|
"x-systemd.required-by=vaultwarden.service"
|
2023-12-22 14:07:14 +00:00
|
|
|
"x-systemd.before=bitwarden-secrets.service"
|
|
|
|
"x-systemd.before=backup-vaultwarden.service"
|
|
|
|
"x-systemd.before=vaultwarden.service"
|
2023-12-18 11:33:09 +00:00
|
|
|
];
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
services.vaultwarden = {
|
|
|
|
enable = true;
|
|
|
|
dbBackend = "sqlite";
|
2023-12-18 11:33:09 +00:00
|
|
|
backupDir = backup-dir;
|
2023-12-03 08:29:01 +00:00
|
|
|
environmentFile = "${bitwarden-env}";
|
|
|
|
config = {
|
2024-06-30 17:47:08 +00:00
|
|
|
DOMAIN = "https://${cfg.subdomain}.${sp.domain}/";
|
|
|
|
SIGNUPS_ALLOWED = cfg.signupsAllowed;
|
|
|
|
ROCKET_PORT = 8222;
|
|
|
|
SENDS_ALLOWED = cfg.sendsAllowed;
|
|
|
|
EMERGENCY_ACCESS_ALLOWED = cfg.emergencyAccessAllowed;
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.services.bitwarden-secrets = {
|
2023-12-18 19:40:15 +00:00
|
|
|
before = [ "vaultwarden.service" ];
|
|
|
|
requiredBy = [ "vaultwarden.service" ];
|
2023-12-03 08:29:01 +00:00
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
path = with pkgs; [ coreutils jq ];
|
|
|
|
script = ''
|
|
|
|
set -o nounset
|
|
|
|
|
|
|
|
token="$(jq -r '.bitwarden.adminToken' ${secrets-filepath})"
|
|
|
|
if [ "$token" == "null" ]; then
|
|
|
|
# If it's null, empty the contents of the file
|
|
|
|
bitwarden_env=""
|
|
|
|
else
|
|
|
|
bitwarden_env="ADMIN_TOKEN=$token"
|
|
|
|
fi
|
2023-12-18 11:33:09 +00:00
|
|
|
|
|
|
|
install -C -m 0700 -o vaultwarden -g vaultwarden \
|
|
|
|
-d /var/lib/bitwarden
|
|
|
|
|
|
|
|
install -C -m 0600 -o vaultwarden -g vaultwarden -DT \
|
2023-12-03 08:29:01 +00:00
|
|
|
<(printf "%s" "$bitwarden_env") ${bitwarden-env}
|
|
|
|
'';
|
|
|
|
};
|
2024-02-15 09:56:12 +00:00
|
|
|
services.nginx.virtualHosts."${cfg.subdomain}.${sp.domain}" = {
|
2023-12-22 15:57:48 +00:00
|
|
|
useACMEHost = sp.domain;
|
2023-12-18 15:02:54 +00:00
|
|
|
forceSSL = true;
|
|
|
|
extraConfig = ''
|
|
|
|
add_header Strict-Transport-Security $hsts_header;
|
|
|
|
#add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
|
|
|
|
add_header 'Referrer-Policy' 'origin-when-cross-origin';
|
2024-02-19 12:23:25 +00:00
|
|
|
add_header X-Frame-Options SAMEORIGIN;
|
2023-12-18 15:02:54 +00:00
|
|
|
add_header X-Content-Type-Options nosniff;
|
|
|
|
add_header X-XSS-Protection "1; mode=block";
|
|
|
|
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
|
|
|
|
'';
|
|
|
|
locations = {
|
|
|
|
"/" = {
|
|
|
|
proxyPass = "http://127.0.0.1:8222";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-12-18 19:40:15 +00:00
|
|
|
# NixOS upstream bug? Otherwise, backup-vaultwarden cannot find sqlite DB.
|
2023-12-18 20:57:12 +00:00
|
|
|
systemd.services.backup-vaultwarden.unitConfig.ConditionPathExists =
|
|
|
|
"/var/lib/bitwarden_rs/db.sqlite3";
|
2023-12-03 08:29:01 +00:00
|
|
|
};
|
|
|
|
}
|