2024-02-11 07:16:25 +00:00
|
|
|
# A library specifically for environments with sops-nix.
|
2024-03-03 08:39:32 +00:00
|
|
|
{ pkgs, lib, self }:
|
2024-02-11 07:16:25 +00:00
|
|
|
|
2025-02-28 06:52:11 +00:00
|
|
|
let
|
|
|
|
inferFormat = sopsFile:
|
|
|
|
let endsWith = ext: lib.hasSuffix ext sopsFile;
|
|
|
|
in
|
|
|
|
if (endsWith ".env") then "dotenv"
|
|
|
|
else if (endsWith ".yaml") then "yaml"
|
|
|
|
else if (endsWith ".json") then "json"
|
|
|
|
else if (endsWith ".ini") then "ini"
|
|
|
|
else if (endsWith ".bin") then "binary"
|
|
|
|
else "yaml";
|
|
|
|
in
|
|
|
|
rec {
|
2024-02-11 07:16:25 +00:00
|
|
|
/* Get the secrets from a given sops file. This will set the individual
|
|
|
|
attributes `sopsFile` with the given file to not interrupt as much as
|
|
|
|
possible with your own sops-nix workflow.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
lib.getSecrets ./sops.yaml {
|
|
|
|
ssh-key = { };
|
|
|
|
"borg/ssh-key" = { };
|
|
|
|
"wireguard/private-key" = {
|
|
|
|
group = config.users.users.systemd-network.group;
|
|
|
|
reloadUnits = [ "systemd-networkd.service" ];
|
|
|
|
mode = "0640";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
getSecrets = sopsFile: secrets:
|
2025-02-28 06:52:11 +00:00
|
|
|
let getKey = key: {
|
|
|
|
inherit key sopsFile;
|
|
|
|
format = inferFormat sopsFile;
|
|
|
|
};
|
2025-01-12 09:57:14 +00:00
|
|
|
in lib.mapAttrs (path: attrs: (getKey path) // attrs) secrets;
|
2024-02-11 07:16:25 +00:00
|
|
|
|
2025-02-28 06:52:11 +00:00
|
|
|
getAsOneSecret = sopsFile:
|
|
|
|
{
|
|
|
|
inherit sopsFile;
|
|
|
|
format = inferFormat sopsFile;
|
|
|
|
|
|
|
|
# This value basically means it's the whole file.
|
|
|
|
key = "";
|
|
|
|
};
|
|
|
|
|
2024-02-11 07:16:25 +00:00
|
|
|
/* Prepend a prefix for the given secrets. This allows a workflow for
|
|
|
|
separate sops file.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
lib.getSecrets ./sops.yaml {
|
|
|
|
ssh-key = { };
|
|
|
|
"borg/ssh-key" = { };
|
|
|
|
} //
|
|
|
|
(lib.getSecrets ./wireguard.yaml
|
|
|
|
(lib.attachSopsPathPrefix "wireguard" {
|
|
|
|
"private-key" = {
|
|
|
|
group = config.users.users.systemd-network.group;
|
|
|
|
reloadUnits = [ "systemd-networkd.service" ];
|
|
|
|
mode = "0640";
|
|
|
|
};
|
|
|
|
}))
|
|
|
|
*/
|
|
|
|
attachSopsPathPrefix = prefix: secrets:
|
2025-01-12 09:57:14 +00:00
|
|
|
lib.mapAttrs' (key: settings:
|
|
|
|
lib.nameValuePair "${prefix}/${key}" ({ inherit key; } // settings))
|
|
|
|
secrets;
|
2024-02-11 07:16:25 +00:00
|
|
|
}
|