about summary refs log tree commit diff
path: root/modules/nixos/vaultwarden.nix
blob: 7d51667e77fed34764310e1aa945e4e99386fcda (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
{
  config,
  inputs,
  lib,
  ...
}:
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 {
      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;
            };
            locations."/notifications/hub" = {
              proxyPass = "http://vaultwarden_websocket";
              proxyWebsockets = true;
            };
            locations."/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;
              ensurePermissions."DATABASE \"${db}\"" = "ALL";
            }
          ];
        };

        fail2ban.jails = mkIf config.nixfiles.modules.fail2ban.enable {
          vaultwarden = ''
            enabled = true
            filter = vaultwarden
            port = http,https
          '';
          vaultwarden-admin = ''
            enabled = true
            filter = vaultwarden-admin
            port = http,https
          '';
        };
      };

      environment.etc = mkIf config.nixfiles.modules.fail2ban.enable {
        "fail2ban/filter.d/vaultwarden.conf".text = ''
          [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 = ''
          [Definition]
          failregex = ^.*Invalid admin token\. IP: <ADDR>.*$
          ignoreregex =
          journalmatch = _SYSTEMD_UNIT=vaultwarden.service
        '';
      };
    };
}

Consider giving Nix/NixOS a try! <3