-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules): Add
secrets
as nixosModules
- Loading branch information
1 parent
1b9dc78
commit 23ba5a7
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
./grafana-agent-flow | ||
./pyroscope | ||
./host-info.nix | ||
./secrets.nix | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
{withSystem, ...}: { | ||
flake.nixosModules.mcl-secrets = { | ||
config, | ||
options, | ||
lib, | ||
dirs, | ||
... | ||
}: let | ||
eachServiceCfg = config.mcl.secrets.services; | ||
isDebugVM = config.mcl.host-info.isDebugVM; | ||
|
||
sshKey = config.mcl.host-info.sshKey; | ||
|
||
ageSecretOpts = | ||
builtins.head | ||
(builtins.head | ||
options.age.secrets.type.nestedTypes.elemType.getSubModules) | ||
.imports; | ||
|
||
secretDir = let | ||
machineConfigPath = config.mcl.host-info.configPath; | ||
machineSecretDir = machineConfigPath + "/secrets"; | ||
vmConfig = dirs.modules + "/default-vm-config"; | ||
vmSecretDir = vmConfig + "/secrets"; | ||
in | ||
if isDebugVM | ||
then vmSecretDir | ||
else machineSecretDir; | ||
in { | ||
options.mcl.secrets = with lib; { | ||
services = mkOption { | ||
type = types.attrsOf (types.submodule ({config, ...}: let | ||
serviceName = config._module.args.name; | ||
in { | ||
options = { | ||
encryptedSecretDir = mkOption { | ||
type = types.path; | ||
default = secretDir; | ||
}; | ||
secrets = mkOption { | ||
default = {}; | ||
type = types.attrsOf (types.submoduleWith { | ||
modules = [ | ||
ageSecretOpts | ||
({name, ...}: let | ||
secretName = name; | ||
in { | ||
config = { | ||
name = "${serviceName}/${secretName}"; | ||
file = | ||
lib.mkDefault (config.encryptedSecretDir + "/${serviceName}/${secretName}.age"); | ||
}; | ||
}) | ||
]; | ||
}); | ||
}; | ||
extraGroups = mkOption { | ||
type = types.listOf types.str; | ||
default = []; | ||
example = ["devops" "secretsAccess"]; | ||
description = "Groups which have access to decrypt the secrets."; | ||
}; | ||
extraKeys = mkOption { | ||
type = types.listOf types.str; | ||
default = []; | ||
example = ["ssh-ed25519 AAAAC3Nza" "ssh-ed25519 AAAACSNss"]; | ||
description = "Groups which have access to decrypt the secrets."; | ||
}; | ||
nix-file = mkOption { | ||
default = | ||
if (pathIsRegularFile (config.encryptedSecretDir + "/${serviceName}/secrets.nix")) | ||
then config.encryptedSecretDir + "/${serviceName}/secrets.nix" | ||
else | ||
builtins.toFile "${serviceName}-secrets.nix" '' | ||
let | ||
hostKey = ["${sshKey}"]; | ||
extraKeys = ["${concatStringsSep "\"\"" config.extraKeys}"]; | ||
in { | ||
${concatMapStringsSep "\n" | ||
(n: "\"${n}.age\".publicKeys = hostKey ++ extraKeys;") | ||
(builtins.attrNames config.secrets)} | ||
} | ||
''; | ||
|
||
# groupsKeys = ["${concatStringsSep "\"\"" (mcl-modules.libs.utils.allUserKeysForGroup config.extraGroups)}"]; | ||
|
||
type = types.path; | ||
}; | ||
}; | ||
})); | ||
default = {}; | ||
example = { | ||
service1.secrets.secretA = {}; | ||
service1.secrets.secretB = {}; | ||
service2.secrets.secretC = {}; | ||
cachix-deploy.secrets.token = { | ||
path = "/etc/cachix-agent.token"; | ||
}; | ||
}; | ||
description = mdDoc "Per-service attrset of encryptedSecretDir and secrets"; | ||
}; | ||
}; | ||
|
||
config = lib.mkIf (eachServiceCfg != {}) { | ||
age.secrets = lib.pipe eachServiceCfg [ | ||
(lib.mapAttrsToList (serviceName: service: | ||
lib.mapAttrsToList ( | ||
secretName: config: | ||
lib.nameValuePair "${serviceName}/${secretName}" config | ||
) | ||
service.secrets)) | ||
lib.concatLists | ||
lib.listToAttrs | ||
]; | ||
}; | ||
}; | ||
} |