|
| 1 | +{withSystem, ...}: { |
| 2 | + flake.nixosModules.mcl-secrets = { |
| 3 | + config, |
| 4 | + options, |
| 5 | + lib, |
| 6 | + dirs, |
| 7 | + mcl-modules, |
| 8 | + ... |
| 9 | + }: let |
| 10 | + eachServiceCfg = config.mcl.secrets.services; |
| 11 | + isDebugVM = config.mcl.host-info.isDebugVM; |
| 12 | + |
| 13 | + sshKey = config.mcl.host-info.sshKey; |
| 14 | + |
| 15 | + ageSecretOpts = |
| 16 | + builtins.head |
| 17 | + (builtins.head |
| 18 | + options.age.secrets.type.nestedTypes.elemType.getSubModules) |
| 19 | + .imports; |
| 20 | + |
| 21 | + secretDir = let |
| 22 | + machineConfigPath = config.mcl.host-info.configPath; |
| 23 | + machineSecretDir = machineConfigPath + "/secrets"; |
| 24 | + vmConfig = dirs.modules + "/default-vm-config"; |
| 25 | + vmSecretDir = vmConfig + "/secrets"; |
| 26 | + in |
| 27 | + if isDebugVM |
| 28 | + then vmSecretDir |
| 29 | + else machineSecretDir; |
| 30 | + in { |
| 31 | + options.mcl.secrets = with lib; { |
| 32 | + services = mkOption { |
| 33 | + type = types.attrsOf (types.submodule ({config, ...}: let |
| 34 | + serviceName = config._module.args.name; |
| 35 | + in { |
| 36 | + options = { |
| 37 | + encryptedSecretDir = mkOption { |
| 38 | + type = types.path; |
| 39 | + default = secretDir; |
| 40 | + }; |
| 41 | + secrets = mkOption { |
| 42 | + default = {}; |
| 43 | + type = types.attrsOf (types.submoduleWith { |
| 44 | + modules = [ |
| 45 | + ageSecretOpts |
| 46 | + ({name, ...}: let |
| 47 | + secretName = name; |
| 48 | + in { |
| 49 | + config = { |
| 50 | + name = "${serviceName}/${secretName}"; |
| 51 | + file = |
| 52 | + lib.mkDefault (config.encryptedSecretDir + "/${serviceName}/${secretName}.age"); |
| 53 | + }; |
| 54 | + }) |
| 55 | + ]; |
| 56 | + }); |
| 57 | + }; |
| 58 | + extraGroups = mkOption { |
| 59 | + type = types.listOf types.str; |
| 60 | + default = []; |
| 61 | + example = ["devops" "secretsAccess"]; |
| 62 | + description = "Groups which have access to decrypt the secrets."; |
| 63 | + }; |
| 64 | + extraKeys = mkOption { |
| 65 | + type = types.listOf types.str; |
| 66 | + default = []; |
| 67 | + example = ["ssh-ed25519 AAAAC3Nza" "ssh-ed25519 AAAACSNss"]; |
| 68 | + description = "Groups which have access to decrypt the secrets."; |
| 69 | + }; |
| 70 | + nix-file = mkOption { |
| 71 | + default = |
| 72 | + if (pathIsRegularFile (config.encryptedSecretDir + "/${serviceName}/secrets.nix")) |
| 73 | + then config.encryptedSecretDir + "/${serviceName}/secrets.nix" |
| 74 | + else |
| 75 | + builtins.toFile "${serviceName}-secrets.nix" '' |
| 76 | + let |
| 77 | + hostKey = ["${sshKey}"]; |
| 78 | + groupsKeys = ["${concatStringsSep "\"\"" (mcl-modules.libs.utils.allUserKeysForGroup config.extraGroups)}"]; |
| 79 | + extraKeys = ["${concatStringsSep "\"\"" config.extraKeys}"]; |
| 80 | + in { |
| 81 | + ${concatMapStringsSep "\n" |
| 82 | + (n: "\"${n}\".publicKeys = hostKey ++ groupKeys ++ extraKeys;") |
| 83 | + (builtins.attrNames config.secrets)} |
| 84 | + } |
| 85 | + ''; |
| 86 | + type = types.path; |
| 87 | + }; |
| 88 | + }; |
| 89 | + })); |
| 90 | + default = {}; |
| 91 | + example = { |
| 92 | + service1.secrets.secretA = {}; |
| 93 | + service1.secrets.secretB = {}; |
| 94 | + service2.secrets.secretC = {}; |
| 95 | + cachix-deploy.secrets.token = { |
| 96 | + path = "/etc/cachix-agent.token"; |
| 97 | + }; |
| 98 | + }; |
| 99 | + description = mdDoc "Per-service attrset of encryptedSecretDir and secrets"; |
| 100 | + }; |
| 101 | + }; |
| 102 | + |
| 103 | + config = lib.mkIf (eachServiceCfg != {}) { |
| 104 | + age.secrets = lib.pipe eachServiceCfg [ |
| 105 | + (lib.mapAttrsToList (serviceName: service: |
| 106 | + lib.mapAttrsToList ( |
| 107 | + secretName: config: |
| 108 | + lib.nameValuePair "${serviceName}/${secretName}" config |
| 109 | + ) |
| 110 | + service.secrets)) |
| 111 | + lib.concatLists |
| 112 | + lib.listToAttrs |
| 113 | + ]; |
| 114 | + }; |
| 115 | + }; |
| 116 | +} |
0 commit comments