2020-09-01 07:37:26 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.age;
|
|
|
|
users = config.users.users;
|
|
|
|
|
2020-09-02 00:29:37 +03:00
|
|
|
identities = builtins.concatStringsSep " " (map (path: "-i ${path}") cfg.sshKeyPaths);
|
2020-09-02 00:27:54 +03:00
|
|
|
installSecret = secretType: ''
|
2020-09-03 06:49:24 +03:00
|
|
|
TMP_FILE="${secretType.path}.tmp"
|
2020-09-02 00:27:54 +03:00
|
|
|
(umask 0400; ${pkgs.age}/bin/age --decrypt ${identities} -o "$TMP_FILE" "${secretType.file}")
|
2020-09-03 06:49:24 +03:00
|
|
|
chmod ${secretType.mode} "$TMP_FILE"
|
|
|
|
chown ${secretType.owner}:${secretType.group} "$TMP_FILE"
|
|
|
|
mv -f "$TMP_FILE" '${secretType.path}'
|
2020-09-02 00:27:54 +03:00
|
|
|
'';
|
2020-09-03 06:49:24 +03:00
|
|
|
installAllSecrets = builtins.concatStringsSep "\n" (map installSecret (builtins.attrValues cfg.secrets));
|
2020-09-01 07:37:26 +03:00
|
|
|
|
|
|
|
secretType = types.submodule ({ config, ... }: {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = config._module.args.name;
|
|
|
|
description = ''
|
|
|
|
Name of the file used in /run/secrets
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
file = mkOption {
|
2020-09-03 21:24:33 +03:00
|
|
|
type = types.path;
|
2020-09-01 07:37:26 +03:00
|
|
|
description = ''
|
|
|
|
Age file the secret is loaded from.
|
|
|
|
'';
|
|
|
|
};
|
2020-09-03 21:24:33 +03:00
|
|
|
path = mkOption {
|
2020-09-01 07:37:26 +03:00
|
|
|
type = types.str;
|
|
|
|
default = "/run/secrets/${config.name}";
|
|
|
|
description = ''
|
|
|
|
Path where secrets are symlinked to.
|
|
|
|
If the default is kept no symlink is created.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
mode = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0400";
|
|
|
|
description = ''
|
|
|
|
Permissions mode of the in octal.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
owner = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "root";
|
|
|
|
description = ''
|
|
|
|
User of the file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = users.${config.owner}.group;
|
|
|
|
description = ''
|
|
|
|
Group of the file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
in {
|
|
|
|
options.age = {
|
|
|
|
secrets = mkOption {
|
|
|
|
type = types.attrsOf secretType;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Attrset of secrets.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
sshKeyPaths = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
default = if config.services.openssh.enable then
|
|
|
|
map (e: e.path) (lib.filter (e: e.type == "rsa" || e.type == "ed25519") config.services.openssh.hostKeys)
|
|
|
|
else [];
|
|
|
|
description = ''
|
2020-09-03 21:24:33 +03:00
|
|
|
Path to SSH keys to be used as identities in age decryption.
|
2020-09-01 07:37:26 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf (cfg.secrets != {}) {
|
|
|
|
assertions = [{
|
|
|
|
assertion = cfg.sshKeyPaths != [];
|
2020-09-03 21:24:33 +03:00
|
|
|
message = "age.sshKeyPaths must be set.";
|
|
|
|
}];
|
2020-09-01 07:37:26 +03:00
|
|
|
|
|
|
|
system.activationScripts.setup-secrets = stringAfter [ "users" "groups" ] installAllSecrets;
|
|
|
|
};
|
|
|
|
}
|