|
| 1 | +{ |
| 2 | + inputs', |
| 3 | + pkgs, |
| 4 | + ... |
| 5 | +}: |
| 6 | +let |
| 7 | + agenix = inputs'.agenix.packages.agenix.override { ageBin = "${pkgs.rage}/bin/rage"; }; |
| 8 | +in |
| 9 | +pkgs.writeShellApplication { |
| 10 | + name = "secret"; |
| 11 | + text = '' |
| 12 | + #!/usr/bin/env bash |
| 13 | + set -euo pipefail |
| 14 | +
|
| 15 | + machine="" |
| 16 | + service="" |
| 17 | + secret="" |
| 18 | + vm="" |
| 19 | + export RULES="" |
| 20 | + secretsFolder="" |
| 21 | +
|
| 22 | + while [[ $# -gt 0 ]]; do |
| 23 | + case "$1" in |
| 24 | + --machine=*) |
| 25 | + machine="''${1#*=}" |
| 26 | + ;; |
| 27 | + --secrets-folder=*) |
| 28 | + secretsFolder="''${1#*=}" |
| 29 | + ;; |
| 30 | + --service=*) |
| 31 | + service="''${1#*=}" |
| 32 | + ;; |
| 33 | + --secret=*) |
| 34 | + secret="''${1#*=}" |
| 35 | + ;; |
| 36 | + --vm) |
| 37 | + vm="true" |
| 38 | + ;; |
| 39 | + --help) |
| 40 | + echo -e "NAME\n\ |
| 41 | + secret\n\n\ |
| 42 | + SYNOPSIS\n\ |
| 43 | + secret [OPTION]\n\n\ |
| 44 | + EXAMPLE\n\ |
| 45 | + secret --machine=mymachine --service=myservice --secret=mysecret\n\n\ |
| 46 | + DESCRIPTION\n\ |
| 47 | + Secret is the command made for nix repos to get rid of the secret.nix when\n\ |
| 48 | + you are using agenix. Secret must be used with mcl-secrets and mcl-host-info\n\ |
| 49 | + modules from nixos-modules repository to work properly.\n\n\ |
| 50 | + OPTIONS\n\ |
| 51 | + --secrets-folder - pecifies the location where secrets are saved.\n\ |
| 52 | + By default, secrets are stored in /(folder of the machine)/secrets/service/\n\ |
| 53 | + if this directory exists, unless otherwise specified. |
| 54 | + --machine - Machine for which you want to create a secret.\n\ |
| 55 | + --service - Service for which you want to create a secret.\n\ |
| 56 | + --secret - Secret you want to encrypt.\n\ |
| 57 | + --vm - Make secret for the vmVariant." |
| 58 | + exit 0 |
| 59 | + ;; |
| 60 | + *) |
| 61 | + echo "Unknown option: $1" |
| 62 | + exit 1 |
| 63 | + ;; |
| 64 | + esac |
| 65 | + shift |
| 66 | + done |
| 67 | +
|
| 68 | + if [[ -z "$machine" || -z "$service" || -z "$secret" ]]; then |
| 69 | + echo "You must specify machine, service, and secret" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + machineFolder="$(nix eval ".#nixosConfigurations.$machine.config.mcl.host-info.configPath" | sed 's|^\([^/]*/\)\{4\}||')" |
| 74 | +
|
| 75 | + if [ "$secretsFolder" == "" ]; then |
| 76 | + secretsFolder="$machineFolder/secrets/$service" |
| 77 | + fi |
| 78 | +
|
| 79 | + if [ "$vm" = "true" ]; then |
| 80 | + RULES="$(nix eval --raw ".#nixosConfigurations.$machine.config.virtualisation.vmVariant.mcl.secrets.services.$service.nix-file")" |
| 81 | + else |
| 82 | + RULES="$(nix eval --raw ".#nixosConfigurations.$machine.config.mcl.secrets.services.$service.nix-file")" |
| 83 | + fi |
| 84 | +
|
| 85 | + ( |
| 86 | + cd "$secretsFolder" |
| 87 | + "${agenix}/bin/agenix" -e "$secret.age" |
| 88 | + ) |
| 89 | + ''; |
| 90 | +} |
0 commit comments