2 description = "Secrets handling";
5 nixosModule = { config, lib, pkgs, ... }: {
6 options.secrets = with lib; {
8 type = types.attrsOf (types.submodule {
10 isTemplated = mkOption {
13 description = "If the file is a gucci template that needs to be resolved";
18 description = "If the entry is a directory";
23 description = "Group to associate to the entry";
28 description = "User to associate to the entry";
30 permissions = mkOption {
33 description = "Permissions to associate to the entry";
37 description = "Content of the entry";
42 description = "Keys attrs to upload to the server";
43 apply = lib.mapAttrsToList (dest: v: v // { inherit dest; });
46 type = types.listOf types.path;
48 description = "GPG public keys files to encrypt to";
51 type = types.listOf types.str;
53 description = "AGE keys to encrypt to";
55 decryptKey = mkOption {
57 default = "/etc/ssh/ssh_host_ed25519_key";
58 description = "ed25519 key used to decrypt with AGE";
62 default = "/var/secrets";
63 description = "Location where to put the keys";
65 secretsVars = mkOption {
67 description = "Location where the secrets variables are defined, to be used to fill the templates in secrets";
69 deleteSecretsVars = mkOption {
72 description = "Delete secrets file after deployment";
75 fullPaths = mkOption {
76 type = types.attrsOf types.path;
77 default = builtins.listToAttrs
78 (map (v: { name = v.dest; value = "${config.secrets.location}/${v.dest}"; }) config.secrets.keys);
80 description = "set of full paths to secrets";
85 location = config.secrets.location;
86 keys = config.secrets.keys;
87 empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done";
88 fpath = v: "secrets/${v.dest}${lib.optionalString v.isTemplated ".gucci.tpl"}";
92 mkdir -p secrets/${v.dest}
94 ${v.user} ${v.group} ${v.permissions} secrets/${v.dest}
98 mkdir -p secrets/$(dirname ${v.dest})
99 echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v}
101 ${v.user} ${v.group} ${v.permissions} ${fpath v}
104 secrets = pkgs.runCommand "secrets.tar.enc" {
105 buildInputs = [ pkgs.gnupg pkgs.sops ];
108 tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done
109 ${builtins.concatStringsSep "\n" (map dumpKey keys)}
110 cat mods | while read u g p k; do
111 tar --no-recursion --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k"
115 for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do
116 gpg --import $key 2>/dev/null
117 fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1)
120 sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null
124 dirs = builtins.filter (v: v.isDir) keys;
125 exclPath = builtins.concatStringsSep " -o " (map (d: " -path $TMP/${d.dest}") dirs);
127 lib.optionalString (builtins.length dirs > 0) " -not \\( ${exclPath} \\) ";
128 in lib.mkIf (builtins.length keys > 0) {
129 system.activationScripts.secrets = {
130 deps = [ "users" "wrappers" ];
132 install -m0750 -o root -g keys -d ${location}
133 TMP=$(${pkgs.coreutils}/bin/mktemp -d)
134 TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d)
135 chmod go-rwx $TMPWORK
136 if [ -n "$TMP" -a -n "$TMPWORK" -a -f ${config.secrets.secretsVars} ]; then
137 install -m0750 -o root -g keys -d $TMP
138 ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt
139 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x
140 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml
141 if [ -f $TMPWORK/vars.yml ]; then
142 find $TMP -name "*.gucci.tpl" -exec \
143 /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}' \;
145 find $TMP -type d ${pathChmodExcl}-exec chown root:keys {} \; -exec chmod o-rx {} \;
146 ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location}
147 rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars}