zaphyra's git: nixfiles

zaphyra's nixfiles

commit 75b973030ebd05edb2a59c277b70b554b98c86db
parent 2235b769c7e33035bf6ad66e6752a3745f47ef77
Author: Katja Ramona Sophie Kwast (zaphyra) <git@zaphyra.eu>
Date: Wed, 22 Jul 2026 21:27:16 +0200

lib/replaceSecrets: init
1 file changed, 35 insertions(+), 0 deletions(-)
A
lib/replaceSecrets.nix
|
35
+++++++++++++++++++++++++++++++++++
diff --git a/lib/replaceSecrets.nix b/lib/replaceSecrets.nix
@@ -0,0 +1,35 @@
+{
+  lib,
+  ...
+}:
+
+let
+  attr = "_secret";
+  isSecret = v: lib.isAttrs v && v ? _secret && (lib.isString v._secret || builtins.isPath v._secret);
+
+in
+{
+  inherit isSecret;
+
+  replaceSecrets =
+    unitName: set:
+    let
+      # Sanitize path to create a valid credential tag
+      sanitizePath =
+        path: lib.stringAsChars (c: if builtins.match "[a-zA-Z0-9_.#=!-]" c != null then c else "_") path;
+
+      credentialTag = path: sanitizePath (lib.concatStringsSep "_" path);
+      credentialPath = path: "/run/credentials/${unitName}/${credentialTag path}";
+    in
+    {
+      output = lib.mapAttrsRecursiveCond (as: !(isSecret as)) (
+        path: value: if (builtins.isAttrs value) then (credentialPath path) else value
+      ) set;
+      credentials = lib.filter (elem: elem != null) (
+        lib.mapAttrsToListRecursiveCond (path: as: !(isSecret as)) (
+          path: value: if (builtins.isAttrs value) then "${credentialTag path}:${value."${attr}"}" else null
+        ) set
+      );
+    };
+
+}