2 description = "Secrets handling";
5 nixosModule = { config, lib, pkgs, ... }: {
6 # Necessary for situations where flake gets included multiple times
7 key = builtins.hashString "sha256" (builtins.path { path = self.sourceInfo.outPath; name = "source"; });
8 options.secrets = with lib; {
10 type = types.attrsOf (types.submodule {
12 isTemplated = mkOption {
15 description = "If the file is a gucci template that needs to be resolved";
20 description = "If the entry is a directory";
25 description = "Group to associate to the entry";
30 description = "User to associate to the entry";
32 permissions = mkOption {
35 description = "Permissions to associate to the entry";
39 description = "Content of the entry";
41 keyDependencies = mkOption {
43 type = types.listOf (types.either types.path types.package);
45 (public) system dependencies that needs to be
46 uploaded with the key.
48 keyDependencies + ignoredKeyDependencies should
49 contain the exhaustive list of the text context.
51 A warning will be thrown if there are remaning
52 dependencies from the text.
55 ignoredKeyDependencies = mkOption {
57 type = types.listOf (types.either types.path types.package);
59 dependencies that must not be sent along with the key.
61 keyDependencies + ignoredKeyDependencies should
62 contain the exhaustive list of the text context.
64 A warning will be thrown if there are remaning
65 dependencies from the text.
71 description = "Keys attrs to upload to the server";
72 apply = builtins.mapAttrs (dest: v: v // { inherit dest; });
75 type = types.listOf types.path;
77 description = "GPG public keys files to encrypt to";
80 type = types.listOf types.str;
82 description = "AGE keys to encrypt to";
84 decryptKey = mkOption {
86 default = "/etc/ssh/ssh_host_ed25519_key";
87 description = "ed25519 key used to decrypt with AGE";
91 default = "/var/secrets";
92 description = "Location where to put the keys";
94 secretsVars = mkOption {
96 description = "Location where the secrets variables are defined, to be used to fill the templates in secrets";
98 deleteSecretsVars = mkOption {
101 description = "Delete secrets file after deployment";
103 # Read-only variables
104 fullPaths = mkOption {
105 type = types.attrsOf types.path;
106 default = builtins.mapAttrs
107 (n: v: "${config.secrets.location}/${n}") config.secrets.keys;
109 description = "set of full paths to secrets";
114 location = config.secrets.location;
115 keys = builtins.attrValues config.secrets.keys;
116 empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done";
117 fpath = v: "secrets/${v.dest}${lib.optionalString v.isTemplated ".gucci.tpl"}";
121 mkdir -p secrets/${v.dest}
123 ${v.user} ${v.group} ${v.permissions} secrets/${v.dest}
127 mkdir -p secrets/$(dirname ${v.dest})
128 echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v}
130 ${v.user} ${v.group} ${v.permissions} ${fpath v}
133 secrets = pkgs.runCommand "secrets.tar.enc" {
134 buildInputs = [ pkgs.gnupg pkgs.sops ];
137 tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done
138 ${builtins.concatStringsSep "\n" (map dumpKey keys)}
139 cat mods | while read u g p k; do
140 tar --no-recursion --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k"
144 for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do
145 gpg --import $key 2>/dev/null
146 fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1)
149 sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null
153 dirs = builtins.filter (v: v.isDir) keys;
154 exclPath = builtins.concatStringsSep " -o " (map (d: " -path $TMP/${d.dest}") dirs);
156 lib.optionalString (builtins.length dirs > 0) " -not \\( ${exclPath} \\) ";
158 checkKeyDependencies = key:
160 allDeps = builtins.map (n: if builtins.isPath n then "${n}" else n.drvPath) (key.keyDependencies ++ key.ignoredKeyDependencies);
161 context = builtins.attrNames (builtins.getContext key.text);
162 missing = builtins.foldl' (o: n: lib.remove n o) context allDeps;
164 lib.optional (!key.isDir && builtins.length missing > 0)
166 Key ${key.dest} has non declared dependencies in its context: ${builtins.concatStringsSep " " missing}
167 Add them to ignoredKeyDependencies to ignore
169 in lib.mkIf (builtins.length keys > 0) {
170 warnings = lib.concatMap checkKeyDependencies keys;
171 # FIXME: Use lib.concatMap (k: k.keyDependencies) keys in latest nixpkgs
172 system.extraDependencies = lib.concatMap (k: builtins.map (dep:
173 if builtins.isPath dep then pkgs.writeText "extra-dep" "${dep}" else dep
174 ) k.keyDependencies) keys;
175 system.activationScripts.secrets = {
176 deps = [ "users" "wrappers" ];
178 install -m0750 -o root -g keys -d ${location}
179 TMP=$(${pkgs.coreutils}/bin/mktemp -d)
180 TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d)
181 chmod go-rwx $TMPWORK
182 if [ -n "$TMP" -a -n "$TMPWORK" -a -f ${config.secrets.secretsVars} ]; then
183 install -m0750 -o root -g keys -d $TMP
184 ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt
185 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x
186 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml
187 if [ -f $TMPWORK/vars.yml ]; then
188 find $TMP -name "*.gucci.tpl" -exec \
189 /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}' \;
191 find $TMP -type d ${pathChmodExcl}-exec chown root:keys {} \; -exec chmod o-rx {} \;
192 ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location}
193 rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars}