Jump to content

Vaultwarden: Difference between revisions

From Official NixOS Wiki
LukESC (talk | contribs)
m mention agenix as a secrets management solution
Phobos (talk | contribs)
m fixed formatting
 
Line 2: Line 2:


== Example Configuration with SQlite ==
== Example Configuration with SQlite ==
<syntaxhighlight lang="nix">
{{File|name=vaultwarden.nix|3={...}: {
services.vaultwarden = {
  services.vaultwarden = {
     enable = true;
     enable = true;
     backupDir = "/var/local/vaultwarden/backup";
     backupDir = "/var/local/vaultwarden/backup";
Line 10: Line 10:
     environmentFile = "/var/lib/vaultwarden/vaultwarden.env";
     environmentFile = "/var/lib/vaultwarden/vaultwarden.env";
     config = {
     config = {
        # Refer to https://github.com/dani-garcia/vaultwarden/blob/main/.env.template
      # Refer to https://github.com/dani-garcia/vaultwarden/blob/main/.env.template
        DOMAIN = "https://bitwarden.example.com";
      DOMAIN = "https://bitwarden.example.com";
        SIGNUPS_ALLOWED = false;
      SIGNUPS_ALLOWED = false;


        ROCKET_ADDRESS = "127.0.0.1";
      ROCKET_ADDRESS = "127.0.0.1";
        ROCKET_PORT = 8222;
      ROCKET_PORT = 8222;
        ROCKET_LOG = "critical";
      ROCKET_LOG = "critical";


        # This example assumes a mailserver running on localhost,
      # This example assumes a mailserver running on localhost,
        # thus without transport encryption.
      # thus without transport encryption.
        # If you use an external mail server, follow:
      # If you use an external mail server, follow:
        #  https://github.com/dani-garcia/vaultwarden/wiki/SMTP-configuration
      #  https://github.com/dani-garcia/vaultwarden/wiki/SMTP-configuration
        SMTP_HOST = "127.0.0.1";
      SMTP_HOST = "127.0.0.1";
        SMTP_PORT = 25;
      SMTP_PORT = 25;
        SMTP_SECURITY = off;
      SMTP_SECURITY = "off";


        SMTP_FROM = "admin@bitwarden.example.com";
      SMTP_FROM = "admin@bitwarden.example.com";
        SMTP_FROM_NAME = "example.com Bitwarden server";
      SMTP_FROM_NAME = "example.com Bitwarden server";
     };
     };
};
  };
</syntaxhighlight>
}|lang=nix}}


== Example Configuration with Postgresql (recommended for production) ==
== Example Configuration with Postgresql (recommended for production) ==
<syntaxhighlight lang="nix">
{{File|name=vaultwarden.nix|3={pkgs, ...}: {
services.vaultwarden = {
  services.vaultwarden = {
     enable = true;
     enable = true;
     # Needed to enable postgresql
     # Needed to enable postgresql
Line 43: Line 43:
     configureNginx = true;
     configureNginx = true;
     configurePostgres = true;
     configurePostgres = true;
     domain = bitwarden.example.com;
     domain = "bitwarden.example.com";


     # in order to avoid having ADMIN_TOKEN and SMTP_PASSWORD in the nix store, it can also be set with the help of an environment file
     # in order to avoid having ADMIN_TOKEN and SMTP_PASSWORD in the nix store, it can also be set with the help of an environment file
Line 49: Line 49:
     environmentFile = "/var/lib/vaultwarden/vaultwarden.env";
     environmentFile = "/var/lib/vaultwarden/vaultwarden.env";
     config = {
     config = {
        # Refer to https://github.com/dani-garcia/vaultwarden/blob/main/.env.template
      # Refer to https://github.com/dani-garcia/vaultwarden/blob/main/.env.template
        SIGNUPS_ALLOWED = false;
      SIGNUPS_ALLOWED = false;


        ROCKET_ADDRESS = "127.0.0.1";
      ROCKET_ADDRESS = "127.0.0.1";
        ROCKET_PORT = 8222;
      ROCKET_PORT = 8222;
        ROCKET_LOG = "critical";
      ROCKET_LOG = "critical";


        # This example assumes an external mailserver, for more information see:
      # This example assumes an external mailserver, for more information see:
        #  https://github.com/dani-garcia/vaultwarden/wiki/SMTP-configuration
      #  https://github.com/dani-garcia/vaultwarden/wiki/SMTP-configuration
        # Note: don't forget to add SMTP_PASSWORD in your env file
      # Note: don't forget to add SMTP_PASSWORD in your env file
        SMTP_HOST = "smtp.example.com";
      SMTP_HOST = "smtp.example.com";
        SMTP_SECURITY = "force_tls";
      SMTP_SECURITY = "force_tls";
        SMTP_FROM = "bitwarden@example.com";
      SMTP_FROM = "bitwarden@example.com";
        SMTP_FROM_NAME = "example.com Bitwarden server";
      SMTP_FROM_NAME = "example.com Bitwarden server";
     };
     };
};
  };
</syntaxhighlight>
}|lang=nix}}
 
To enable proper SSL certificate look at ACME wiki entry:
To enable proper SSL certificate look at ACME wiki entry:
https://wiki.nixos.org/wiki/ACME
https://wiki.nixos.org/wiki/ACME

Latest revision as of 18:19, 4 November 2026

Vaultwarden is an alternative server implementation of the Bitwarden Client API, written in Rust and compatible with official Bitwarden clients, allowing you to self-host your own password manager backend.

Example Configuration with SQlite

❄︎ vaultwarden.nix
{...}: {
  services.vaultwarden = {
    enable = true;
    backupDir = "/var/local/vaultwarden/backup";
    # in order to avoid having  ADMIN_TOKEN in the nix store it can be also set with the help of an environment file
    # be aware that this file must be created by hand (or via secrets management like sops or agenix)
    environmentFile = "/var/lib/vaultwarden/vaultwarden.env";
    config = {
      # Refer to https://github.com/dani-garcia/vaultwarden/blob/main/.env.template
      DOMAIN = "https://bitwarden.example.com";
      SIGNUPS_ALLOWED = false;

      ROCKET_ADDRESS = "127.0.0.1";
      ROCKET_PORT = 8222;
      ROCKET_LOG = "critical";

      # This example assumes a mailserver running on localhost,
      # thus without transport encryption.
      # If you use an external mail server, follow:
      #   https://github.com/dani-garcia/vaultwarden/wiki/SMTP-configuration
      SMTP_HOST = "127.0.0.1";
      SMTP_PORT = 25;
      SMTP_SECURITY = "off";

      SMTP_FROM = "admin@bitwarden.example.com";
      SMTP_FROM_NAME = "example.com Bitwarden server";
    };
  };
}

Example Configuration with Postgresql (recommended for production)

❄︎ vaultwarden.nix
{pkgs, ...}: {
  services.vaultwarden = {
    enable = true;
    # Needed to enable postgresql
    package = pkgs.vaultwarden-postgresql;
    dbBackend = "postgresql";

    # automatically inject Nginx and Postgres config
    configureNginx = true;
    configurePostgres = true;
    domain = "bitwarden.example.com";

    # in order to avoid having ADMIN_TOKEN and SMTP_PASSWORD in the nix store, it can also be set with the help of an environment file
    # be aware that this file must be created by hand (or via secrets management like sops)
    environmentFile = "/var/lib/vaultwarden/vaultwarden.env";
    config = {
      # Refer to https://github.com/dani-garcia/vaultwarden/blob/main/.env.template
      SIGNUPS_ALLOWED = false;

      ROCKET_ADDRESS = "127.0.0.1";
      ROCKET_PORT = 8222;
      ROCKET_LOG = "critical";

      # This example assumes an external mailserver, for more information see:
      #   https://github.com/dani-garcia/vaultwarden/wiki/SMTP-configuration
      # Note: don't forget to add SMTP_PASSWORD in your env file
      SMTP_HOST = "smtp.example.com";
      SMTP_SECURITY = "force_tls";
      SMTP_FROM = "bitwarden@example.com";
      SMTP_FROM_NAME = "example.com Bitwarden server";
    };
  };
}

To enable proper SSL certificate look at ACME wiki entry: https://wiki.nixos.org/wiki/ACME

Reverse Proxy Setup (only needed if `configureNginx` is not set)

Caddy

services.caddy.virtualHosts."bitwarden.example.com".extraConfig = ''
    encode zstd gzip

    reverse_proxy :${toString config.services.vaultwarden.config.ROCKET_PORT} {
        header_up X-Real-IP {remote_host}
    }
'';

Nginx

services.nginx.virtualHosts."bitwarden.example.com" = {
    enableACME = true;
    forceSSL = true;
    locations."/" = {
        proxyPass = "http://127.0.0.1:${toString config.services.vaultwarden.config.ROCKET_PORT}";
        proxyWebsockets = true;
    };
};

Access your server

Set the ADMIN_TOKEN in your env file to be able to create your first user using the `/admin` endpoint. For example: https://bitwarden.example.com/admin And use ADMIN_TOKEN to login and create your first admin user. Then you can remove the token from the env file and use your admin user directly.