2 description = "Secrets handling";
5 nixosModule = { config, lib, pkgs, ... }: {
6 options.secrets = with lib; {
8 type = types.listOf types.unspecified;
10 description = "Keys to upload to server";
13 type = types.listOf types.path;
15 description = "GPG public keys files to encrypt to";
18 type = types.listOf types.str;
20 description = "AGE keys to encrypt to";
22 decryptKey = mkOption {
24 default = "/etc/ssh/ssh_host_ed25519_key";
25 description = "ed25519 key used to decrypt with AGE";
29 default = "/var/secrets";
30 description = "Location where to put the keys";
32 secretsVars = mkOption {
34 description = "Location where the secrets variables are defined, to be used to fill the templates in secrets";
36 deleteSecretsVars = mkOption {
39 description = "Delete secrets file after deployment";
42 fullPaths = mkOption {
43 type = types.attrsOf types.path;
44 default = builtins.listToAttrs
45 (map (v: { name = v.dest; value = "${config.secrets.location}/${v.dest}"; }) config.secrets.keys);
47 description = "set of full paths to secrets";
52 location = config.secrets.location;
53 keys = config.secrets.keys;
54 empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done";
55 fpath = v: "secrets/${v.dest}${lib.optionalString (v.isTemplated or true) ".gucci.tpl"}";
57 if v.isDir or false then
59 mkdir -p secrets/${v.dest}
61 ${v.user or "root"} ${v.group or "root"} ${v.permissions or "0600"} secrets/${v.dest}
65 mkdir -p secrets/$(dirname ${v.dest})
66 echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v}
68 ${v.user or "root"} ${v.group or "root"} ${v.permissions or "0600"} ${fpath v}
71 secrets = pkgs.runCommand "secrets.tar.enc" {
72 buildInputs = [ pkgs.gnupg pkgs.sops ];
75 tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done
76 ${builtins.concatStringsSep "\n" (map dumpKey keys)}
77 cat mods | while read u g p k; do
78 tar --no-recursion --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k"
82 for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do
83 gpg --import $key 2>/dev/null
84 fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1)
87 sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null
91 dirs = builtins.filter (v: v.isDir or false) keys;
92 exclPath = builtins.concatStringsSep " -o " (map (d: " -path $TMP/${d.dest}") dirs);
94 lib.optionalString (builtins.length dirs > 0) " -not \\( ${exclPath} \\) ";
95 in lib.mkIf (builtins.length keys > 0) {
96 system.activationScripts.secrets = {
97 deps = [ "users" "wrappers" ];
99 install -m0750 -o root -g keys -d ${location}
100 TMP=$(${pkgs.coreutils}/bin/mktemp -d)
101 TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d)
102 chmod go-rwx $TMPWORK
103 if [ -n "$TMP" -a -n "$TMPWORK" ]; then
104 install -m0750 -o root -g keys -d $TMP
105 ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt
106 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x
107 if [ -f ${config.secrets.secretsVars} ]; then
108 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml
110 if [ -f $TMPWORK/vars.yml ]; then
111 find $TMP -name "*.gucci.tpl" -exec \
112 /bin/sh -c 'f="{}"; ${pkgs.gucci}/bin/gucci -f '$TMPWORK'/vars.yml "$f" > "''${f%.gucci.tpl}"; touch --reference "$f" ''${f%.gucci.tpl} ; chmod --reference="$f" ''${f%.gucci.tpl} ; chown --reference="$f" ''${f%.gucci.tpl}' \;
114 find $TMP -type d ${pathChmodExcl}-exec chown root:keys {} \; -exec chmod o-rx {} \;
115 ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location}
116 rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars}