about summary refs log tree commit diff
path: root/modules/vaultwarden.nix
blob: edce8e57e84090afb63aa183ec2422adaa4cd2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
  config,
  inputs,
  lib,
  this,
  ...
}:
with lib;
let
  cfg = config.nixfiles.modules.vaultwarden;
in
{
  options.nixfiles.modules.vaultwarden = {
    enable = mkEnableOption "Vaultwarden";

    domain = mkOption {
      description = "Domain name sans protocol scheme.";
      type = with types; str;
      default = "vaultwarden.${config.networking.domain}";
    };
  };

  config =
    let
      db = "vaultwarden";
    in
    mkIf cfg.enable {
      ark.directories = [ "/var/lib/bitwarden_rs" ];

      secrets.vaultwarden-environment = {
        file = "${inputs.self}/secrets/vaultwarden-environment";
        owner = "vaultwarden";
        group = "vaultwarden";
      };

      nixfiles.modules = {
        nginx = {
          enable = true;
          upstreams = with config.services.vaultwarden.config; {
            vaultwarden_rocket.servers."${ROCKET_ADDRESS}:${toString ROCKET_PORT}" = { };
            vaultwarden_websocket.servers."${WEBSOCKET_ADDRESS}:${toString WEBSOCKET_PORT}" = { };
          };
          virtualHosts.${cfg.domain}.locations = {
            "/" = {
              proxyPass = "http://vaultwarden_rocket";
              proxyWebsockets = true;
            };
            "/notifications/hub" = {
              proxyPass = "http://vaultwarden_websocket";
              proxyWebsockets = true;
            };
            "/notifications/hub/negotiate" = {
              proxyPass = "http://vaultwarden_rocket";
              proxyWebsockets = true;
            };
          };
        };
        postgresql = {
          enable = true;
          extraPostStart = [
            ''
              $PSQL "${db}" -tAc 'GRANT ALL ON SCHEMA "public" TO "${db}"'
            ''
          ];
        };
      };

      services = {
        vaultwarden = {
          enable = true;
          config = {
            TZ = config.time.timeZone;

            WEB_VAULT_ENABLED = true;

            DOMAIN = optionalString (cfg.domain != null) "http://${cfg.domain}";

            SIGNUPS_ALLOWED = false;
            INVITATIONS_ALLOWED = false;

            ORG_CREATION_USERS = "none";

            PASSWORD_HINTS_ALLOWED = false;
            SHOW_PASSWORD_HINT = false;

            ROCKET_ADDRESS = "127.0.0.1";
            ROCKET_PORT = 8812;

            WEBSOCKET_ENABLED = true;
            WEBSOCKET_ADDRESS = "127.0.0.1";
            WEBSOCKET_PORT = 8813;

            LOG_LEVEL = "error";

            DATABASE_URL = "postgresql://${db}@/${db}";
          };
          dbBackend = "postgresql";
          environmentFile = config.secrets.vaultwarden-environment.path;
        };

        postgresql = {
          ensureDatabases = [ db ];
          ensureUsers = [
            {
              name = db;
              ensureDBOwnership = true;
            }
          ];
        };

        fail2ban.jails = {
          vaultwarden = {
            enabled = true;
            settings = {
              filter = "vaultwarden";
              port = "http,https";
            };
          };
          vaultwarden-admin = {
            enabled = true;
            settings = {
              filter = "vaultwarden-admin";
              port = "http,https";
            };
          };
        };
      };

      environment.etc = {
        "fail2ban/filter.d/vaultwarden.conf".text = generators.toINI { } {
          Definition = {
            failregex = "^.*Username or password is incorrect\. Try again\. IP: <ADDR>\. Username:.*$";
            ignoreregex = "";
            journalmatch = "_SYSTEMD_UNIT=vaultwarden.service";
          };
        };
        "fail2ban/filter.d/vaultwarden-admin.conf".text = generators.toINI { } {
          Definition = {
            failregex = "^.*Invalid admin token\. IP: <ADDR>.*$";
            ignoreregex = "";
            journalmatch = "_SYSTEMD_UNIT=vaultwarden.service";
          };
        };
      };

      topology = with cfg; {
        nodes.${this.hostname}.services.vaultwarden = {
          info = mkForce domain;
        };
      };
    };
}

Consider giving Nix/NixOS a try! <3