2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.vault;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.vault;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
configFile = pkgs.writeText "vault.hcl" ''
|
2022-07-14 12:49:19 +00:00
|
|
|
# vault in dev mode will refuse to start if its configuration sets listener
|
|
|
|
${lib.optionalString (!cfg.dev) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
listener "tcp" {
|
|
|
|
address = "${cfg.address}"
|
|
|
|
${if (cfg.tlsCertFile == null || cfg.tlsKeyFile == null) then ''
|
|
|
|
tls_disable = "true"
|
|
|
|
'' else ''
|
|
|
|
tls_cert_file = "${cfg.tlsCertFile}"
|
|
|
|
tls_key_file = "${cfg.tlsKeyFile}"
|
|
|
|
''}
|
|
|
|
${cfg.listenerExtraConfig}
|
|
|
|
}
|
2022-07-14 12:49:19 +00:00
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
storage "${cfg.storageBackend}" {
|
|
|
|
${optionalString (cfg.storagePath != null) ''path = "${cfg.storagePath}"''}
|
|
|
|
${optionalString (cfg.storageConfig != null) cfg.storageConfig}
|
|
|
|
}
|
|
|
|
${optionalString (cfg.telemetryConfig != "") ''
|
|
|
|
telemetry {
|
|
|
|
${cfg.telemetryConfig}
|
|
|
|
}
|
|
|
|
''}
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
2021-02-05 17:12:51 +00:00
|
|
|
|
|
|
|
allConfigPaths = [configFile] ++ cfg.extraSettingsPaths;
|
2022-07-14 12:49:19 +00:00
|
|
|
configOptions = escapeShellArgs
|
|
|
|
(lib.optional cfg.dev "-dev" ++
|
|
|
|
lib.optional (cfg.dev && cfg.devRootTokenID != null) "-dev-root-token-id=${cfg.devRootTokenID}"
|
|
|
|
++ (concatMap (p: ["-config" p]) allConfigPaths));
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.vault = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "Vault daemon";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "vault" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-07-14 12:49:19 +00:00
|
|
|
dev = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-07-14 12:49:19 +00:00
|
|
|
In this mode, Vault runs in-memory and starts unsealed. This option is not meant production but for development and testing i.e. for nixos tests.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
devRootTokenID = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Initial root token. This only applies when {option}`services.vault.dev` is true
|
2022-07-14 12:49:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1:8200";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The name of the ip interface to listen to";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tlsCertFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "/path/to/your/cert.pem";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "TLS certificate file. TLS will be disabled unless this option is set";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tlsKeyFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "/path/to/your/key.pem";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "TLS private key file. TLS will be disabled unless this option is set";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
listenerExtraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = ''
|
|
|
|
tls_min_version = "tls12"
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Extra text appended to the listener section.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
storageBackend = mkOption {
|
|
|
|
type = types.enum [ "inmem" "file" "consul" "zookeeper" "s3" "azure" "dynamodb" "etcd" "mssql" "mysql" "postgresql" "swift" "gcs" "raft" ];
|
|
|
|
default = "inmem";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The name of the type of storage backend";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
storagePath = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
2022-08-21 13:32:41 +00:00
|
|
|
default = if cfg.storageBackend == "file" || cfg.storageBackend == "raft" then "/var/lib/vault" else null;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression ''
|
2022-08-21 13:32:41 +00:00
|
|
|
if config.${opt.storageBackend} == "file" || cfg.storageBackend == "raft"
|
2021-12-19 01:06:50 +00:00
|
|
|
then "/var/lib/vault"
|
|
|
|
else null
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Data directory for file backend";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
storageConfig = mkOption {
|
|
|
|
type = types.nullOr types.lines;
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-02-05 17:12:51 +00:00
|
|
|
HCL configuration to insert in the storageBackend section.
|
|
|
|
|
|
|
|
Confidential values should not be specified here because this option's
|
|
|
|
value is written to the Nix store, which is publicly readable.
|
|
|
|
Provide credentials and such in a separate file using
|
2022-08-12 12:06:08 +00:00
|
|
|
[](#opt-services.vault.extraSettingsPaths).
|
2021-02-05 17:12:51 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
telemetryConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Telemetry configuration";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Extra text appended to {file}`vault.hcl`.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2021-02-05 17:12:51 +00:00
|
|
|
|
|
|
|
extraSettingsPaths = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
default = [];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Configuration files to load besides the immutable one defined by the NixOS module.
|
|
|
|
This can be used to avoid putting credentials in the Nix store, which can be read by any user.
|
|
|
|
|
|
|
|
Each path can point to a JSON- or HCL-formatted file, or a directory
|
2022-09-09 14:08:57 +00:00
|
|
|
to be scanned for files with `.hcl` or
|
|
|
|
`.json` extensions.
|
2021-02-05 17:12:51 +00:00
|
|
|
|
|
|
|
To upload the confidential file with NixOps, use for example:
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
```
|
2021-02-05 17:12:51 +00:00
|
|
|
# https://releases.nixos.org/nixops/latest/manual/manual.html#opt-deployment.keys
|
|
|
|
deployment.keys."vault.hcl" = let db = import ./db-credentials.nix; in {
|
|
|
|
text = ${"''"}
|
|
|
|
storage "postgresql" {
|
|
|
|
connection_url = "postgres://''${db.username}:''${db.password}@host.example.com/exampledb?sslmode=verify-ca"
|
|
|
|
}
|
|
|
|
${"''"};
|
|
|
|
user = "vault";
|
|
|
|
};
|
|
|
|
services.vault.extraSettingsPaths = ["/run/keys/vault.hcl"];
|
|
|
|
services.vault.storageBackend = "postgresql";
|
|
|
|
users.users.vault.extraGroups = ["keys"];
|
2022-09-09 14:08:57 +00:00
|
|
|
```
|
2021-02-05 17:12:51 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
2022-08-21 13:32:41 +00:00
|
|
|
{
|
|
|
|
assertion = cfg.storageBackend == "inmem" -> (cfg.storagePath == null && cfg.storageConfig == null);
|
2020-04-24 23:36:52 +00:00
|
|
|
message = ''The "inmem" storage expects no services.vault.storagePath nor services.vault.storageConfig'';
|
|
|
|
}
|
2022-08-21 13:32:41 +00:00
|
|
|
{
|
|
|
|
assertion = (
|
|
|
|
(cfg.storageBackend == "file" -> (cfg.storagePath != null && cfg.storageConfig == null)) &&
|
|
|
|
(cfg.storagePath != null -> (cfg.storageBackend == "file" || cfg.storageBackend == "raft"))
|
|
|
|
);
|
|
|
|
message = ''You must set services.vault.storagePath only when using the "file" or "raft" backend'';
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.vault = {
|
|
|
|
name = "vault";
|
|
|
|
group = "vault";
|
|
|
|
uid = config.ids.uids.vault;
|
|
|
|
description = "Vault daemon user";
|
|
|
|
};
|
|
|
|
users.groups.vault.gid = config.ids.gids.vault;
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = optional (cfg.storagePath != null)
|
|
|
|
"d '${cfg.storagePath}' 0700 vault vault - -";
|
|
|
|
|
|
|
|
systemd.services.vault = {
|
|
|
|
description = "Vault server daemon";
|
|
|
|
|
|
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
after = [ "network.target" ]
|
|
|
|
++ optional (config.services.consul.enable && cfg.storageBackend == "consul") "consul.service";
|
|
|
|
|
|
|
|
restartIfChanged = false; # do not restart on "nixos-rebuild switch". It would seal the storage and disrupt the clients.
|
|
|
|
|
2020-11-03 02:18:15 +00:00
|
|
|
startLimitIntervalSec = 60;
|
|
|
|
startLimitBurst = 3;
|
2020-04-24 23:36:52 +00:00
|
|
|
serviceConfig = {
|
|
|
|
User = "vault";
|
|
|
|
Group = "vault";
|
2021-02-05 17:12:51 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/vault server ${configOptions}";
|
2020-04-24 23:36:52 +00:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -SIGHUP $MAINPID";
|
2022-07-14 12:49:19 +00:00
|
|
|
StateDirectory = "vault";
|
|
|
|
# In `dev` mode vault will put its token here
|
|
|
|
Environment = lib.optional (cfg.dev) "HOME=/var/lib/vault";
|
2020-04-24 23:36:52 +00:00
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
ProtectHome = "read-only";
|
|
|
|
AmbientCapabilities = "cap_ipc_lock";
|
|
|
|
NoNewPrivileges = true;
|
2023-07-15 17:15:38 +00:00
|
|
|
LimitCORE = 0;
|
2020-04-24 23:36:52 +00:00
|
|
|
KillSignal = "SIGINT";
|
|
|
|
TimeoutStopSec = "30s";
|
|
|
|
Restart = "on-failure";
|
|
|
|
};
|
|
|
|
|
|
|
|
unitConfig.RequiresMountsFor = optional (cfg.storagePath != null) cfg.storagePath;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|