blob: 7ccc3a10e023f622fccbddc151d5664caee1874d [file] [log] [blame]
Skyler Grey22428b02023-11-19 13:20:56 +00001{ base, pkgs, lib, config, ... }:
2lib.recursiveUpdate {
TheCodedProfb6184602023-06-13 17:04:59 -04003 environment.systemPackages = with pkgs; [ vaultwarden ];
TheCodedProfaec8c452023-06-12 18:26:46 -04004
TheCodedProfb6184602023-06-13 17:04:59 -04005 services.vaultwarden.enable = true;
6 services.vaultwarden.dbBackend = "postgresql";
TheCodedProfaec8c452023-06-12 18:26:46 -04007
Skyler Greyfe1740c2023-10-21 01:24:18 +00008 sops.secrets = lib.pipe [
9 "ADMIN_TOKEN"
10 "SMTP_PASSWORD"
11 "YUBICO_SECRET_KEY"
12 "HIBP_API_KEY"
13 ] [
Skyler Grey87a11552023-06-14 23:02:25 +020014 (map (name: {
Skyler Greyfe1740c2023-10-21 01:24:18 +000015 inherit name;
16 value = {
17 mode = "0400";
18 owner = config.users.users.root.name;
Skyler Greybcb46d32023-11-10 20:48:38 +000019 group = config.users.users.root.group;
Samuel Shuertf68685d2023-10-28 20:07:56 -040020 sopsFile = ../../secrets/vaultwarden.json;
Skyler Greyfe1740c2023-10-21 01:24:18 +000021 format = "json";
22 };
Skyler Grey87a11552023-06-14 23:02:25 +020023 }))
TheCodedProfb6184602023-06-13 17:04:59 -040024 builtins.listToAttrs
25 ];
Skyler Greyfe1740c2023-10-21 01:24:18 +000026} (let isDerived = base != null;
27in if isDerived
28# We cannot use mkIf as both sides are evaluated no matter the condition value
29# Given we use base as an attrset, mkIf will error if base is null in here
30then
31 with lib;
32 let
33 cfg = config.services.vaultwarden;
34
35 vaultwarden_config = {
36 # Server Settings
Skyler Grey22428b02023-11-19 13:20:56 +000037 DOMAIN = "https://vaultwarden.clicks.codes";
38 ROCKET_ADDRESS = "127.0.0.255";
39 ROCKET_PORT = 1028;
Skyler Greyfe1740c2023-10-21 01:24:18 +000040
41 # General Settings
42 SIGNUPS_ALLOWED = false;
43 INVITATIONS_ALLOWED = true;
44 SIGNUPS_DOMAINS_WHITELIST =
45 "clicks.codes,coded.codes,thecoded.prof,starrysky.fyi,hopescaramels.com,pinea.dev,trans.gg";
46 SIGNUPS_VERIFY = true;
47
Skyler Greyfe1740c2023-10-21 01:24:18 +000048 DISABLE_2FA_REMEMBER = true;
49
50 # Admin Account
51 ADMIN_TOKEN = "!!ADMIN_TOKEN!!";
52
53 # Database Settings
54 DATABASE_URL =
Skyler Grey22428b02023-11-19 13:20:56 +000055 "postgresql://vaultwarden:!!clicks_vaultwarden_db_secret!!@127.0.0.1:${
Skyler Greyfe1740c2023-10-21 01:24:18 +000056 toString config.services.postgresql.port
57 }/vaultwarden";
58
59 # Mail Settings
60 SMTP_HOST = "mail.clicks.codes";
Skyler Grey22428b02023-11-19 13:20:56 +000061 SMTP_FROM = "vaultwarden@clicks.codes";
62 SMTP_FROM_NAME = "Clicks vaultwarden";
Skyler Greyfe1740c2023-10-21 01:24:18 +000063 SMTP_SECURITY = "starttls";
64 SMTP_PORT = 587;
65
Skyler Grey22428b02023-11-19 13:20:56 +000066 SMTP_USERNAME = "vaultwarden@clicks.codes";
Skyler Greyfe1740c2023-10-21 01:24:18 +000067 SMTP_PASSWORD = "!!SMTP_PASSWORD!!";
68
69 REQUIRE_DEVICE_EMAIL = true;
70
71 IP_HEADER = "X-Forwarded-For";
72
73 # YubiKey Settings
74 YUBICO_CLIENT_ID = "89788";
75 YUBICO_SECRET_KEY = "!!YUBICO_SECRET_KEY!!";
76
Skyler Greyfe1740c2023-10-21 01:24:18 +000077 # HIBP Settings
Skyler Grey22428b02023-11-19 13:20:56 +000078 HIBP_API_KEY="!!HIBP_API_KEY!!";
Skyler Greyfe1740c2023-10-21 01:24:18 +000079
80 ORG_ENABLE_GROUPS = true;
81 # I have looked at the risks. They seem relatively small in comparison to the utility
82 # (stuff like sync issues if you don't refresh your page)
83 # Also a general lack of real-world testing. Which, honestly, doesn't
84 # seem too bad. Please contact me *immediately* upon noticing issues
85 # as I want to make sure that as little as possible is lost if we need
86 # to restore from backups (although I doubt it'll come to that)
87 };
88
89 nameToEnvVar = name:
Skyler Grey31618512023-08-20 21:54:44 +000090 let
Skyler Greyfe1740c2023-10-21 01:24:18 +000091 parts = builtins.split "([A-Z0-9]+)" name;
92 partsToEnvVar = parts:
93 foldl' (key: x:
94 let last = stringLength key - 1;
95 in if isList x then
96 key
97 + optionalString (key != "" && substring last 1 key != "_") "_"
98 + head x
99 else if key != "" && elem (substring 0 1 x)
100 lowerChars then # to handle e.g. [ "disable" [ "2FAR" ] "emember" ]
101 substring 0 last key
102 + optionalString (substring (last - 1) 1 key != "_") "_"
103 + substring last 1 key + toUpper x
104 else
105 key + toUpper x) "" parts;
106 in if builtins.match "[A-Z0-9_]+" name != null then
107 name
108 else
109 partsToEnvVar parts;
TheCodedProfb6184602023-06-13 17:04:59 -0400110
Skyler Greyfe1740c2023-10-21 01:24:18 +0000111 # Due to the different naming schemes allowed for config keys,
112 # we can only check for values consistently after converting them to their corresponding environment variable name.
113 configEnv = let
114 configEnv = concatMapAttrs (name: value:
115 optionalAttrs (value != null) {
116 ${nameToEnvVar name} =
117 if isBool value then boolToString value else toString value;
118 }) vaultwarden_config;
119 in {
120 DATA_FOLDER = "/var/lib/bitwarden_rs";
121 } // optionalAttrs (!(configEnv ? WEB_VAULT_ENABLED)
122 || configEnv.WEB_VAULT_ENABLED == "true") {
123 WEB_VAULT_FOLDER = "${cfg.webVaultPackage}/share/vaultwarden/vault";
124 } // configEnv;
Skyler Grey31618512023-08-20 21:54:44 +0000125
Skyler Greyfe1740c2023-10-21 01:24:18 +0000126 configFile = pkgs.writeText "vaultwarden.env" (concatStrings (mapAttrsToList
127 (name: value: ''
128 ${name}=${value}
129 '') configEnv));
130 in {
131 scalpel.trafos."vaultwarden.env" = {
132 source = toString configFile;
133 matchers."ADMIN_TOKEN".secret = config.sops.secrets.ADMIN_TOKEN.path;
134 matchers."SMTP_PASSWORD".secret = config.sops.secrets.SMTP_PASSWORD.path;
135 matchers."YUBICO_SECRET_KEY".secret =
136 config.sops.secrets.YUBICO_SECRET_KEY.path;
137 matchers."HIBP_API_KEY".secret = config.sops.secrets.HIBP_API_KEY.path;
Skyler Grey22428b02023-11-19 13:20:56 +0000138 matchers."clicks_vaultwarden_db_secret".secret =
139 config.sops.secrets.clicks_vaultwarden_db_password.path;
Skyler Greyfe1740c2023-10-21 01:24:18 +0000140 owner = config.users.users.vaultwarden.name;
141 group = config.users.groups.vaultwarden.name;
142 mode = "0400";
143 };
Skyler Grey31618512023-08-20 21:54:44 +0000144
Skyler Greyfe1740c2023-10-21 01:24:18 +0000145 services.vaultwarden.environmentFile =
146 config.scalpel.trafos."vaultwarden.env".destination;
147 }
148else
149 { })