refactor(secrets): simplify secrets

This commit is contained in:
2026-04-20 19:17:37 +02:00
parent b62f3c20ac
commit 72c3710a3c
9 changed files with 87 additions and 80 deletions

View File

@@ -13,10 +13,12 @@ let
standalone = osConfig == null; standalone = osConfig == null;
in in
lib.optionalAttrs standalone { lib.optionalAttrs standalone {
sops.secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" "anki" [ sops.secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null {
"sync-user" anki = [
"sync-key" "sync-user"
] { }; "sync-key"
];
};
} }
// { // {
warnings = lib.optional ( warnings = lib.optional (

View File

@@ -15,11 +15,13 @@ let
in in
lib.optionalAttrs standalone { lib.optionalAttrs standalone {
sops = { sops = {
secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" "taskwarrior" [ secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null {
"sync-server-url" taskwarrior = [
"sync-server-client-id" "sync-server-url"
"sync-encryption-secret" "sync-server-client-id"
] { }; "sync-encryption-secret"
];
};
templates."taskrc.d/sync" = { templates."taskrc.d/sync" = {
content = '' content = ''

View File

@@ -1,15 +1,14 @@
{ config, myUtils, ... }: { config, ... }:
let let
inherit (config.secrets) sopsDir;
inherit (config.host) username; inherit (config.host) username;
owner = config.users.users.${username}.name; inherit (config.secrets) owner;
in in
{ {
config.sops = { config = {
secrets = myUtils.mkSopsSecrets sopsDir "opencode" [ "api-key" ] { inherit owner; }; secrets.groups.opencode = [ "api-key" ];
templates."opencode/auth.json" = { sops.templates."opencode/auth.json" = {
inherit owner; inherit owner;
path = "/home/${username}/.local/share/opencode/auth.json"; path = "/home/${username}/.local/share/opencode/auth.json";
content = '' content = ''

View File

@@ -1,12 +1,6 @@
{ config, myUtils, ... }:
let
inherit (config.secrets) sopsDir;
inherit (config.host) username;
owner = config.users.users.${username}.name;
in
{ {
config.sops = { config.secrets.groups.anki = [
secrets = myUtils.mkSopsSecrets sopsDir "anki" [ "sync-user" "sync-key" ] { inherit owner; }; "sync-user"
}; "sync-key"
];
} }

View File

@@ -1,14 +1,11 @@
{ {
lib, lib,
config, config,
myUtils,
... ...
}: }:
let let
cfg = config.restic-backup; cfg = config.restic-backup;
inherit (config.secrets) sopsDir;
mkSopsSecrets = myUtils.mkSopsSecrets sopsDir;
host = config.networking.hostName; host = config.networking.hostName;
in in
{ {
@@ -27,21 +24,24 @@ in
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
sops = { secrets.groups = {
secrets = lib.mkMerge [ restic = [ "password" ];
(mkSopsSecrets "restic" [ "password" ] { }) backblaze-b2 = [
(mkSopsSecrets "backblaze-b2" [ "bucket-name" "account-id" "account-key" ] { }) "bucket-name"
"account-id"
"account-key"
]; ];
templates = { };
"restic/repo-${host}" = {
content = "b2:${config.sops.placeholder."backblaze-b2/bucket-name"}:${host}"; sops.templates = {
}; "restic/repo-${host}" = {
"restic/b2-env-${host}" = { content = "b2:${config.sops.placeholder."backblaze-b2/bucket-name"}:${host}";
content = '' };
B2_ACCOUNT_ID=${config.sops.placeholder."backblaze-b2/account-id"} "restic/b2-env-${host}" = {
B2_ACCOUNT_KEY=${config.sops.placeholder."backblaze-b2/account-key"} content = ''
''; B2_ACCOUNT_ID=${config.sops.placeholder."backblaze-b2/account-id"}
}; B2_ACCOUNT_KEY=${config.sops.placeholder."backblaze-b2/account-key"}
'';
}; };
}; };

View File

@@ -1,14 +1,13 @@
{ {
lib, lib,
config, config,
myUtils,
... ...
}: }:
let let
cfg = config.hcloud; cfg = config.hcloud;
inherit (config.host) username; inherit (config.host) username;
inherit (config.secrets) sopsDir; inherit (config.secrets) owner;
in in
{ {
options.hcloud = { options.hcloud = {
@@ -16,12 +15,10 @@ in
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
sops.secrets = myUtils.mkSopsSecrets sopsDir "hcloud" [ "api-token" ] { secrets.groups.hcloud = [ "api-token" ];
owner = config.users.users.${username}.name;
};
sops.templates."hcloud/cli.toml" = { sops.templates."hcloud/cli.toml" = {
owner = config.users.users.${username}.name; inherit owner;
path = "/home/${username}/.config/hcloud/cli.toml"; path = "/home/${username}/.config/hcloud/cli.toml";
content = '' content = ''
active_context = "server" active_context = "server"

View File

@@ -12,7 +12,6 @@ let
inherit (config.host) username; inherit (config.host) username;
inherit (cfg) sopsDir; inherit (cfg) sopsDir;
owner = config.users.users.${username}.name; owner = config.users.users.${username}.name;
mkSopsSecrets = myUtils.mkSopsSecrets sopsDir;
in in
{ {
imports = [ inputs.sops-nix.nixosModules.sops ]; imports = [ inputs.sops-nix.nixosModules.sops ];
@@ -24,6 +23,15 @@ in
default = "${toString inputs.nix-secrets}/secrets"; default = "${toString inputs.nix-secrets}/secrets";
}; };
groups = lib.mkOption {
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
default = { };
};
owner = lib.mkOption {
type = lib.types.unspecified;
};
nixSigningKey = { nixSigningKey = {
enable = lib.mkEnableOption "nix signing key configuration"; enable = lib.mkEnableOption "nix signing key configuration";
}; };
@@ -35,27 +43,28 @@ in
}; };
config = { config = {
secrets = {
inherit owner;
groups = {
email = [
"personal"
"work"
];
nix = lib.optional cfg.nixSigningKey.enable "signing-key";
};
};
sops = { sops = {
# for yubikey, generate as follows: # for yubikey, generate as follows:
# ``` # ```
# age-plugin-yubikey --identity > <keyfile-path> # age-plugin-yubikey --identity > <keyfile-path>
# ``` # ```
age.keyFile = "/home/${username}/.config/sops/age/keys.txt"; age.keyFile = "/home/${username}/.config/sops/age/keys.txt";
secrets = myUtils.mkSopsSecrets sopsDir owner cfg.groups;
secrets = lib.mkMerge [
(mkSopsSecrets "email" [ "personal" "work" ] { inherit owner; })
(lib.mkIf cfg.nixSigningKey.enable {
nix-signing-key = {
sopsFile = "${sopsDir}/nix.yaml";
key = "signing-key";
inherit owner;
};
})
];
}; };
nix.settings.secret-key-files = lib.mkIf cfg.nixSigningKey.enable [ nix.settings.secret-key-files = lib.mkIf cfg.nixSigningKey.enable [
config.sops.secrets.nix-signing-key.path config.sops.secrets."nix/signing-key".path
]; ];
services = { services = {

View File

@@ -1,19 +1,17 @@
{ config, myUtils, ... }: { config, ... }:
let let
inherit (config.secrets) sopsDir; inherit (config.secrets) owner;
inherit (config.host) username;
owner = config.users.users.${username}.name;
in in
{ {
config.sops = { config = {
secrets = myUtils.mkSopsSecrets sopsDir "taskwarrior" [ secrets.groups.taskwarrior = [
"sync-server-url" "sync-server-url"
"sync-server-client-id" "sync-server-client-id"
"sync-encryption-secret" "sync-encryption-secret"
] { inherit owner; }; ];
templates."taskrc.d/sync" = { sops.templates."taskrc.d/sync" = {
inherit owner; inherit owner;
content = '' content = ''
sync.server.url=${config.sops.placeholder."taskwarrior/sync-server-url"} sync.server.url=${config.sops.placeholder."taskwarrior/sync-server-url"}

View File

@@ -2,19 +2,25 @@
{ {
mkSopsSecrets = mkSopsSecrets =
sopsDir: group: names: extraOpts: sopsDir: owner: groups:
let let
file = "${group}.yaml"; opts = lib.optionalAttrs (owner != null) { inherit owner; };
mkGroup =
group: names:
let
file = "${group}.yaml";
in
lib.foldl' lib.mergeAttrs { } (
map (name: {
"${group}/${name}" = {
sopsFile = "${sopsDir}/${file}";
key = name;
}
// opts;
}) names
);
in in
lib.foldl' lib.mergeAttrs { } ( lib.foldl' lib.mergeAttrs { } (lib.mapAttrsToList mkGroup groups);
map (name: {
"${group}/${name}" = {
sopsFile = "${sopsDir}/${file}";
key = name;
}
// extraOpts;
}) names
);
sopsAvailability = sopsAvailability =
config: osConfig: config: osConfig: