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

Added Common Secrets Flag #21

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
80 changes: 42 additions & 38 deletions nixos-modules/secrets.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
{ config, lib, pkgs, flake, ... }:
with builtins; with lib;
let
sharedDir = "${flake}/secrets";
hostDir = "${flake}/hosts/${config.system.name}/secrets";

commonAssets = findAssets sharedDir;
hostAssets = findAssets hostDir;
allAssets = commonAssets ++ hostAssets;

findAssets = path: if pathExists path then map (file: removePrefix "${path}/" file) (find "" path) else [ ];
findAssetSource = name: (if elem name hostAssets then "${hostDir}" else "${sharedDir}") + "/${name}";
in
{
options = with types; {
basement.enableAgenix = mkOption {
Expand All @@ -21,40 +10,55 @@ in
secrets = mkOption {
# type = attrsOf str;
};
basement.useCommonSecrets = mkOption {
type = bool;
default = true;
};
};

config = {
config =
let
sharedDir = "${flake}/secrets";
hostDir = "${flake}/hosts/${config.system.name}/secrets";

secrets = mapListToAttrs
(file:
let
file' = removeSuffix ".age" (unsafeDiscardStringContext file);
in
nameValuePair
file'
(
if config.basement.enableAgenix && hasSuffix ".age" file
then config.age.secrets.${file'}.path
else findAssetSource file'
)
)
allAssets;
commonAssets = findAssets sharedDir;
hostAssets = findAssets hostDir;
allAssets = if config.basement.useCommonSecrets then commonAssets ++ hostAssets else hostAssets;

age = mkIf config.basement.enableAgenix {
identityPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
findAssets = path: if pathExists path then map (file: removePrefix "${path}/" file) (find "" path) else [ ];
findAssetSource = name: (if elem name hostAssets then "${hostDir}" else "${sharedDir}") + "/${name}";
in
{
secrets = mapListToAttrs
(file:
nameValuePair'
(removeSuffix ".age" file)
{ file = findAssetSource file; }
let
file' = removeSuffix ".age" (unsafeDiscardStringContext file);
in
nameValuePair
file'
(
if config.basement.enableAgenix && hasSuffix ".age" file
then config.age.secrets.${file'}.path
else findAssetSource file'
)
)
(
filter
(name: hasSuffix ".age" name)
allAssets
);
};
allAssets;

};
age = mkIf config.basement.enableAgenix {
identityPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
secrets = mapListToAttrs
(file:
nameValuePair'
(removeSuffix ".age" file)
{ file = findAssetSource file; }
)
(
filter
(name: hasSuffix ".age" name)
allAssets
);
};

};
}