Skip to content

utils: init genSecretsReplacement - #503858

Open
ibizaman wants to merge 2 commits into
NixOS:masterfrom
ibizaman:secret2
Open

ibizaman wants to merge 2 commits into
NixOS:masterfrom
ibizaman:secret2

Conversation

@ibizaman

@ibizaman ibizaman commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

This PR creates a new function genSecretsReplacement. It's genJqSecretsReplacement for any config file format. It achieves that by taking a generator following the pkgs.formats.*. This new function can become the underlying function that all other secret generation function uses. Now, genJqSecretsReplacement is simply defined in terms of genSecretsReplacement.

This made it possible to integrate free form module with secrets in configuration. It doesn't require any specific wiring, just using the functions in the correct location. This is reflected in the manual.

The previous implementation of genJqSecretsReplacement could not be salvaged as it was very specific to JSON. Instead, it uses the python replace-secrets package which had to be updated to add a --quote and --quote-char option.

The previous PR added regression tests which ensure this function does not break any use case.


From https://secretspec.dev/blog/secrets-dont-belong-in-config/

Where the secret value ends up Modules Share
Merged into a config file at runtime 110 25%
Inlined into a config in /nix/store 42 9%

Potentially 152 modules could benefit from this.


Identified downstream modules are - those using genJqSecretsReplacement:

module comment
filebeat no existing test
recyclarr no existing test
artalk Fails because pnpm-9.15.9 is unsafe
immich-kiosk Fails because pnpm-9.15.9 is unsafe
actual test succeeds
discourse test succeeds
engelsystem test succeeds
ente test succeeds
gitlab test succeeds
glance test succeeds
immich test succeeds
lemmy test succeeds
netbird test succeeds
nginx-sso test succeeds
perses test succeeds
scrutiny test succeeds
sing-box test succeeds

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS labels Mar 26, 2026
@ibizaman
ibizaman force-pushed the secret2 branch 3 times, most recently from 992e961 to 44810f7 Compare March 26, 2026 21:53
@nixpkgs-ci nixpkgs-ci Bot added 8.has: changelog This PR adds or changes release notes and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. labels Mar 26, 2026
@nixpkgs-ci nixpkgs-ci Bot added the 8.has: documentation This PR adds or changes documentation label Mar 26, 2026
@ibizaman

ibizaman commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@ofborg test utils scrutiny glance discourse actual artalk immich-kiosk engelsystem lemmy ente immich nginx-sso perses gitlab recyclarr sing-box filebeat netbird

utils holds all the tests for the the functions and the others are those that come up when I do a grep for genJqSecretsReplacement.

@ibizaman

Copy link
Copy Markdown
Contributor Author

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review pr 503858
Commit: c21448a0aa9e087130dd8b56bc9fd61cb284e38e


x86_64-linux

⏩ 2 packages blacklisted:
  • nixos-install-tools
  • tests.nixos-functions.nixos-test
✅ 2 packages built:
  • nixpkgs-manual
  • replace-secret

@ibizaman
ibizaman marked this pull request as ready for review March 27, 2026 05:41
@nixpkgs-ci
nixpkgs-ci Bot requested a review from talyz March 27, 2026 06:03

@ambroisie ambroisie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see tests with strings that need escaping, both in JSON and the key-value generator.

Comment thread nixos/lib/utils.nix Resolved
Comment thread nixos/lib/utils.nix Outdated
setWithSecretsReplaced = mapAttrsRecursiveCond' (as: !(isAttrs as && hasAttr attr as)) (
path: item:
if (isAttrs item && hasAttr attr item) then
sanitizePath "@.${concatMapStringsSep "." (n: ''"${n}"'') path}@"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: should this use some form of lib.escape rather than `''"${n}"''?

@ibizaman ibizaman Mar 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will surely be better indeed. I will test with some strings that include quotes and all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added tests for that and sanitizePath takes care of correctly escaping what is needed.

Comment thread nixos/lib/utils.nix Resolved
Comment thread nixos/lib/utils.nix Resolved
Comment thread nixos/lib/utils.nix Resolved
Comment thread nixos/lib/utils.nix Resolved
Comment thread nixos/lib/utils.nix Resolved
@ibizaman

ibizaman commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@ambroisie

I'd like to see tests with strings that need escaping, both in JSON and the key-value generator.

That's exactly why I introduced the regression tests in a previous PR. The idea was that the regression tests on genJqSecretsReplacement would automatically test genSecretsReplacement since it has a trivial implementation now. But it's true from this PR alone it's not obvious.

Thinking about long term maintenance, would you want instead for me to move all those tests to be applied on genSecretsReplacement?

@ambroisie

Copy link
Copy Markdown
Contributor

Thinking about long term maintenance, would you want instead for me to move all those tests to be applied on genSecretsReplacement?

No I think it's fine to test genJqSecretsReplacement itself as well.

I'm asking to test the escapes because I don't see any in your key-value tests, and haven't seen any in the JSON ones either.

There's no real test stress-testing correct handling of escapes (e.g: value = "some 'string' with \"interesting\" escapes\\ in it and $dangerous looking\n\${values}" should be correctly round-tripped despite the various quotes, special characters, and variable-looking strings)

@ibizaman

ibizaman commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@ambroisie so I managed to get something working. It's pretty much a WIP commit but I would like you to validate this implementation makes sense already.

So now the genSecretsReplacement needs two additional arguments, the generator which is a nix function that is used to generate the config file and the escape_style which is the best I found to be able to sanitize secrets on the target host when they replace placeholders in the config file.

The best would've been to be able to serialize the generator but that does not exist. The closest I found was to do:

fn=lib.toJSON nix eval --impure --expr 'let nixpkgs = import ./. {}; fs = nixpkgs.lib.splitString "." (builtins.getEnv "fn"); f = nixpkgs.lib.foldl (base: name: builtins.getAttr name base) nixpkgs fs; in f "abcd"'
"\"abcd\""

But that is frown upon, rightfully so.

So I resorted to have the replace-secrets.py itself understand how to sanitize the secret. Another option would be to have genSecretsReplacement accept some python code that is evaluated inside replace-secrets but I don't really like that.

@ambroisie

Copy link
Copy Markdown
Contributor

Seems fine to me as an approach.

@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 9, 2026
@nixpkgs-ci nixpkgs-ci Bot removed the 8.has: module (update) This PR changes an existing module in `nixos/` label Jul 4, 2026
@ibizaman

ibizaman commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

@ofborg test utils scrutiny glance discourse actual artalk immich-kiosk engelsystem lemmy ente immich nginx-sso perses gitlab recyclarr sing-box filebeat netbird

@ibizaman

ibizaman commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

@ambroisie I made a small change In the implementation with far reaching implications. The generator is now a function that produces a file in the nix store. This makes the genSecretsReplacement function compatible with the pkgs.formats.* functions and the Freeform modules. This is reflected in the manual.

@ibizaman

ibizaman commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

@ofborg test utils scrutiny glance discourse actual artalk immich-kiosk engelsystem lemmy ente immich nginx-sso perses gitlab recyclarr sing-box filebeat netbird

@prescientmoon

Copy link
Copy Markdown
Contributor

@ibizaman This looks very cool!

So if I understand things correctly, the helper will replace (possibly nested) attrsets containing a _secret attribute with a placeholder string/some-other-format-specific-bit-of-syntax. The helper then generates a script one can call at the beginning of the respective systemd service for replacing the placeholders.

A few dumb questions (I skimmed the code, so I'm sure they'd be answerable is I paid more attention):

  • do the secrets remain substituted in place until the next activation? If yes, I guess the replacement script will just do nothing new on subsequent runs?
  • does the file where the replacement happens need to exist beforehand, or is it simply created by the replacement script?
  • if I remove service foo from my config, will the configuration containing the secrets remain on disk? (assuming I wasn't using an impermanence-based setup) If yes, would it be feasible to store all such "configs with secrets substituted-in" in a separate directory and simply symlink them in the correct place? (or would that run into permission issues?)

@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 14, 2026
@ibizaman

ibizaman commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@prescientmoon Thanks for the kind words.

So if I understand things correctly, the helper will replace (possibly nested) attrsets containing a _secret attribute with a placeholder string/some-other-format-specific-bit-of-syntax. The helper then generates a script one can call at the beginning of the respective systemd service for replacing the placeholders.

Exactly.

do the secrets remain substituted in place until the next activation? If yes, I guess the replacement script will just do nothing new on subsequent runs?

Yes the secrets remain in place forever. The replacement script will delete and re-create the config file on each invocation, even if the secrets did not change.

does the file where the replacement happens need to exist beforehand, or is it simply created by the replacement script?

It is created, as well as parent directory tree.

if I remove service foo from my config, will the configuration containing the secrets remain on disk? (assuming I wasn't using an impermanence-based setup) If yes, would it be feasible to store all such "configs with secrets substituted-in" in a separate directory and simply symlink them in the correct place? (or would that run into permission issues?)

That's a great idea. I'll have to think about how to implement this. But currently the config stays forever.


Note that all the answers I gave on the behavior existed before this PR. That is what the previous genJqSecretsReplacement did already. I just gutted out that function and created a more generic function retaining this behavior and accepting a new argument so it could work on any config.

Not changing the behavior was intentional, to avoid scope creeping the PR and increasing the chances of merging it.

@phanirithvij phanirithvij removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 16, 2026
@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 16, 2026
@KiaraGrouwstra

Copy link
Copy Markdown
Contributor

That's a great idea. I'll have to think about how to implement this. But currently the config stays forever.

maybe an alternative there seems like openbao agent templates. that could be at the same level as (i.e. competing with) rather than complementing the approach here, but if so, i would argue say contracts could allow people to plug in whichever solution they would deem better suited to their use-case.

but yeah, i agree if that was pre-existing then this PR shouldn't have to solve that.

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-already-reviewed/2617/3201

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/modular-services-meeting-6-2026-08-07/79408/1

@ibizaman
ibizaman force-pushed the secret2 branch 2 times, most recently from 82280ea to 200b5d0 Compare August 19, 2026 21:16
@ibizaman

ibizaman commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor Author

I really would like to merge this. How can I help move this forward? There are some work I could upstream from my SelfHostBlocks project but it's blocked by not having this function. (There are always ways around it, I could copy this function in other places, but I don't think there's a good reason for doing this).

@nixpkgs-ci nixpkgs-ci Bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Aug 19, 2026
Comment thread doc/functions/secrets.section.md Resolved
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/conventions-for-service-modules/79706/3

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/secrets-don-t-belong-in-config/79078/45

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/modular-services-meeting-8-2026-09-04/79930/1

@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants