Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added support for substituting secrets into files #290

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions modules/age-home.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ with lib; let
''}
'';

substituteSecret = secretType:
builtins.concatStringsSep "\n" (builtins.map (file: ''
${pkgs.gnused}/bin/sed -i "s#@${secretType.name}@#$(cat ${secretType.path})#" ${file}
'')
secretType.substitutions);

testIdentities =
map
(path: ''
Expand All @@ -91,6 +97,11 @@ with lib; let
++ [cleanupAndLink]
);

substituteSecrets = builtins.concatStringsSep "\n" (
["echo '[agenix] substituting secrets...'"]
++ (map substituteSecret (builtins.attrValues cfg.secrets))
);

secretType = types.submodule ({
config,
name,
Expand All @@ -117,6 +128,14 @@ with lib; let
Path where the decrypted secret is installed.
'';
};
substitutions = mkOption {
type = types.listOf types.str;
default = [];
description = ''
List of files to substitute the secret into.
WARNING: It is recommended to set `force = true` for files managed through home-manager.
'';
};
mode = mkOption {
type = types.str;
default = "0400";
Expand All @@ -135,6 +154,7 @@ with lib; let
text = ''
${newGeneration}
${installSecrets}
${substituteSecrets}
exit 0
'';
};
Expand Down Expand Up @@ -233,5 +253,9 @@ in {
StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/agenix/stderr";
};
};

home.activation.agenix = lib.mkIf pkgs.stdenv.hostPlatform.isLinux (
lib.hm.dag.entryAfter ["linkGeneration"] mountingScript
);
};
}
24 changes: 24 additions & 0 deletions modules/age.nix
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ with lib; let
++ (map chownSecret (builtins.attrValues cfg.secrets))
);

substituteSecret = secretType:
builtins.concatStringsSep "\n" (builtins.map (file: ''
${pkgs.gnused}/bin/sed -i "s#@${secretType.name}@#$(cat ${secretType.path})#" ${file}
'')
secretType.substitutions);

substituteSecrets = builtins.concatStringsSep "\n" (
["echo '[agenix] substituting secrets...'"]
++ (map substituteSecret (builtins.attrValues cfg.secrets))
);

secretType = types.submodule ({config, ...}: {
options = {
name = mkOption {
Expand All @@ -155,6 +166,13 @@ with lib; let
Path where the decrypted secret is installed.
'';
};
substitutions = mkOption {
type = types.listOf types.str;
default = [];
description = ''
List of files to substitute the secret into.
'';
};
mode = mkOption {
type = types.str;
default = "0400";
Expand Down Expand Up @@ -298,6 +316,12 @@ in {
text = "";
deps = ["agenixChown"];
};

# Substitute secrets into files.
system.activationScripts.agenixSubstitute = {
text = substituteSecrets;
deps = ["agenix" "etc"];
};
})
(optionalAttrs isDarwin {
launchd.daemons.activate-agenix = {
Expand Down
Loading