You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
make-options-doc: builtins.toFile creates options.json without proper store path context #485682
nixos/lib/make-options-doc/default.nix uses builtins.toFile to create options.json in a way that loses store path context, triggering a warning:
warning: Using 'builtins.toFile' to create a file named 'options.json' that references the store path '/nix/store/...-source' without a proper context. The resulting file will not have a correct store reference, so this is unreliable and may stop working in the future.
The use of builtins.toFile with builtins.unsafeDiscardStringContext appears to be a deliberate performance optimization:
builtins.toFile runs at evaluation time and creates the file directly in the store without a derivation (faster)
pkgs.writeText creates a derivation that runs at build time (slower, but properly tracks dependencies)
Since builtins.toFile cannot reference store paths, unsafeDiscardStringContext is used to strip the context. This works but loses dependency tracking, which Nix now warns about.
Impact
Currently a warning, but the message indicates this may break in future Nix versions
Affects any NixOS configuration that builds documentation
Triggered during nixos-rebuild, nix build, or nix eval of NixOS configurations
Suggested Fix
Replace builtins.toFile with pkgs.writeText or similar derivation-based approach that properly tracks store path context. This may have a performance impact on documentation builds.
This warning appears to have been introduced around Nix 2.31.x (late September / early October 2025) based on user reports. The exact commit that added this warning is not documented in the release notes.
Uses unsafeDiscardStringContext to strip store path context so builtins.toFile can accept the content, but this loses dependency tracking - which Nix now warns about.
Cant this just be options = builtins.toJSON (builtins.unsafeDiscardStringContext optionsNix); passAsFile = [ "options" ]; and then you can grab $optionsPath? That would solve the problem, correct? Its also practically guaranteed to be faster?
Also, that python script does very little. Would it not be better to just filter the invisible ones and warn on missing descriptions from within nix directly instead? Then IFD would not be a concern for the json version either. Im not 100% sure why that is in python. But if its to stay in python, we can solve this problem with passAsFile
Also, if it helps, the version it was added is after 2.31.3 as I can't trigger it locally on that version but I can trigger it in my github action (which is Nix (Determinate Nix 3.15.2) 2.33.1 at the moment because it works well on the mac runner)
Im actually not sure I successfully solved the issue.
I improved the derivation. It was still a sound change. There was no harm in merging what I did.
Im 90% sure I solved the issue. But I might have just changed the warning from warning about builtins.toFile over to builtins.derivation. Im getting a warning for that now, but I am also doing some strange things with the docgen on my end and I think my new warning is from that and not this. Im going to need someone to double check that I succeeded or not.
However, if this does not solve it, I would assume that pkgs.writeText would not have either, as that is what pkgs.writeText does.
Regardless we NEED to strip them, as otherwise we will have to build a lot of packages to build the docs.
It is not a small performance optimization to discard the context. It is the only one that really matters. As otherwise you would build a large portion of nixpkgs to build the docs.
Edit: I found my new warning. It was indeed from my personal modifications and not from using passAsFile on these. I am more sure that I successfully solved the issue now. __structuredAttrs also works, which I used in my docgen for my wrapper modules project
Sure why not backport it. It is just a warning, but since people use warnings-as-errors and stuff, it is probably worth it. And the change is something that has worked for many years anyway.
But I also think most of what I am doing in that particular section I linked is possibly overcomplicated and only necessary because I was already using __structuredAttrs
I think this because the following also doesn't warn or anything when you build it
I have no idea how to improve it from here, nothing I am trying is working, although this is a very reliable way to test it it seems.
I am using determinate nix to trigger this issue. I changed my nix version so that I could reproduce the bug, and that was what I did in order to do that. I forgot that this might be specific to that version.
If we want to solve it (we don't have to, it doesn't affect upstream nix), the correct solution is probably to scrub store paths from the JSON string; e.g. using a similar strategy to remove-references-to.
Im not sure why we would want to revert it, as that simply adds an unnecessary builtins.toFile in the middle of it.
But it is up to you I suppose, it doesn't really matter to me if the change is left there or not. I personally still think it is a reasonable change even though it does not solve the issue.
I thought that it would be an effective fix, as the original filer of the issue said that pkgs.writeText would fix it, and so I realized that if pkgs.writeText would fix it, so should this, as that is what pkgs.writeText does, and that this was cleaner regardless.
It turns out pkgs.writeText does not fix it either. I simply got fooled by caching.
But it is still somewhat cleaner of a solution than what was there before and works fine.
Again, it really doesn't matter to me if you revert it or not, if you feel that is better just so that we don't change stuff, that is fine with me. But if we leave it, it is still a fine change.
If we want to solve it (we don't have to, it doesn't affect upstream nix), the correct solution is probably to scrub store paths from the JSON string; e.g. using a similar strategy to remove-references-to.
That sounds expensive, it is a long file, but maybe not? We already render the json like twice anyway. We could add such a scrub right there in the derivation and otherwise leave it as it is now
Activity
Bad3r commentedon Jan 31, 2026
Additional context:
This warning appears to have been introduced around Nix 2.31.x (late September / early October 2025) based on user reports. The exact commit that added this warning is not documented in the release notes.
Related issues:
builtin:write-filewhich would be a proper solutionThe pattern in question:
Uses
unsafeDiscardStringContextto strip store path context sobuiltins.toFilecan accept the content, but this loses dependency tracking - which Nix now warns about.BirdeeHub commentedon Feb 1, 2026
nixpkgs/nixos/lib/make-options-doc/default.nix
Lines 217 to 219 in 799ceb0
Cant this just be
options = builtins.toJSON (builtins.unsafeDiscardStringContext optionsNix); passAsFile = [ "options" ];and then you can grab$optionsPath? That would solve the problem, correct? Its also practically guaranteed to be faster?Also, that python script does very little. Would it not be better to just filter the invisible ones and warn on missing descriptions from within nix directly instead? Then IFD would not be a concern for the json version either. Im not 100% sure why that is in python. But if its to stay in python, we can solve this problem with
passAsFileAlso, if it helps, the version it was added is after
2.31.3as I can't trigger it locally on that version but I can trigger it in my github action (which isNix (Determinate Nix 3.15.2) 2.33.1at the moment because it works well on the mac runner)fix(nix): disable abort-on-warn due to upstream nixpkgs issue #485682
nixosOptionsDoc: fix discarded context warning
BirdeeHub commentedon Feb 1, 2026
Also, separate question
nixpkgs/nixos/lib/make-options-doc/default.nix
Lines 94 to 102 in 799ceb0
These are not used anywhere. Why are they there?
They do get set here
nixpkgs/nixos/doc/manual/default.nix
Lines 92 to 94 in 32defc7
But they do literally nothing.
nixosOptionsDoc: fix discarded context warning
BirdeeHub commentedon Feb 2, 2026
Im actually not sure I successfully solved the issue.
I improved the derivation. It was still a sound change. There was no harm in merging what I did.
Im 90% sure I solved the issue. But I might have just changed the warning from warning about
builtins.toFileover tobuiltins.derivation. Im getting a warning for that now, but I am also doing some strange things with the docgen on my end and I think my new warning is from that and not this. Im going to need someone to double check that I succeeded or not.However, if this does not solve it, I would assume that
pkgs.writeTextwould not have either, as that is whatpkgs.writeTextdoes.Regardless we NEED to strip them, as otherwise we will have to build a lot of packages to build the docs.
It is not a small performance optimization to discard the context. It is the only one that really matters. As otherwise you would build a large portion of nixpkgs to build the docs.
Edit: I found my new warning. It was indeed from my personal modifications and not from using passAsFile on these. I am more sure that I successfully solved the issue now. __structuredAttrs also works, which I used in my docgen for my wrapper modules project
MattSturgeon commentedon Feb 3, 2026
Good to hear! Thanks for verifying. I'll close this then.
Do we need to backport this? I assume some people might end up evaluating 25.11 with a new enough
nixto get the warnings.EDIT: #486456
nixosOptionsDoc: fix discarded context warning
BirdeeHub commentedon Feb 3, 2026
Sure why not backport it. It is just a warning, but since people use warnings-as-errors and stuff, it is probably worth it. And the change is something that has worked for many years anyway.
BirdeeHub commentedon Feb 3, 2026
Ok so, bad news. I don't think I fixed it anymore I think I was getting some weird cache action when I tested it that made me think it was fixed.
Good news. I also did not break it and likely made it faster.
It warns about using builtins.derivation for the same purpose now.
FWIW I have this which doesn't warn but for a different project.
But I also think most of what I am doing in that particular section I linked is possibly overcomplicated and only necessary because I was already using __structuredAttrs
I think this because the following also doesn't warn or anything when you build it
Likewise neither does this
And from testing, doing the above also does not provision it to your system.
So at the very least, what I did was sound but maybe not sufficient.
BirdeeHub commentedon Feb 3, 2026
For the next person to test this easier than the way I was trying to test it originally, a flake
I have no idea how to improve it from here, nothing I am trying is working, although this is a very reliable way to test it it seems.
I am using determinate nix to trigger this issue. I changed my nix version so that I could reproduce the bug, and that was what I did in order to do that. I forgot that this might be specific to that version.
nixosOptionsDoc: fix discarded context warning
MattSturgeon commentedon Feb 4, 2026
I just spent a while trying to find the quoted warning in upstream nix's source code (it doesn't exist).
From the home-manager issue:
So it seems detnix has added this "no context store path check" warning as part of DeterminateSystems/nix-src#56
If we want to solve it (we don't have to, it doesn't affect upstream nix), the correct solution is probably to scrub store paths from the JSON string; e.g. using a similar strategy to
remove-references-to.With this in mind I'm tempted to revert #485913
BirdeeHub commentedon Feb 4, 2026
Im not sure why we would want to revert it, as that simply adds an unnecessary builtins.toFile in the middle of it.
But it is up to you I suppose, it doesn't really matter to me if the change is left there or not. I personally still think it is a reasonable change even though it does not solve the issue.
I thought that it would be an effective fix, as the original filer of the issue said that
pkgs.writeTextwould fix it, and so I realized that ifpkgs.writeTextwould fix it, so should this, as that is whatpkgs.writeTextdoes, and that this was cleaner regardless.It turns out
pkgs.writeTextdoes not fix it either. I simply got fooled by caching.But it is still somewhat cleaner of a solution than what was there before and works fine.
Again, it really doesn't matter to me if you revert it or not, if you feel that is better just so that we don't change stuff, that is fine with me. But if we leave it, it is still a fine change.
That sounds expensive, it is a long file, but maybe not? We already render the json like twice anyway. We could add such a scrub right there in the derivation and otherwise leave it as it is now
Revert the Home Manager warning workaround
Disable NixOS documentation generation on lumquat