Skip to content

make-options-doc: builtins.toFile creates options.json without proper store path context #485682

Open

Description

@Bad3r

Description

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.

Location

https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/make-options-doc/default.nix#L217

options = builtins.toFile "options.json" (
  builtins.unsafeDiscardStringContext (builtins.toJSON optionsNix)
);

Context

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.

Reproduction

Build any NixOS configuration:

nix build .#nixosConfigurations.<hostname>.config.system.build.toplevel

Metadata

  • NixOS version: unstable (nixpkgs master)
  • Nix version: 2.33.1

Activity

  1. Bad3r commented on Jan 31, 2026

    @Bad3r
    ContributorAuthor

    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:

    The pattern in question:

    options = builtins.toFile "options.json" (
      builtins.unsafeDiscardStringContext (builtins.toJSON optionsNix)
    );

    Uses unsafeDiscardStringContext to strip store path context so builtins.toFile can accept the content, but this loses dependency tracking - which Nix now warns about.

  2. BirdeeHub commented on Feb 1, 2026

    @BirdeeHub
    Contributor

    options = builtins.toFile "options.json" (
    builtins.unsafeDiscardStringContext (builtins.toJSON optionsNix)
    );

    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)

  3. BirdeeHub commented on Feb 1, 2026

    @BirdeeHub
    Contributor

    Also, separate question

    documentType ? "appendix",
    # TODO deprecate "appendix" in favor of "none"
    # and/or rename function to moduleOptionDoc for clean slate
    # If you include more than one option list into a document, you need to
    # provide different ids.
    variablelistId ? "configuration-variable-list",
    # String to prefix to the option XML/HTML id attributes.
    optionIdPrefix ? "opt-",

    These are not used anywhere. Why are they there?

    They do get set here

    documentType = "none";
    variablelistId = "test-options-list";
    optionIdPrefix = "test-opt-";

    But they do literally nothing.

  4. BirdeeHub commented on Feb 2, 2026

    @BirdeeHub
    Contributor

    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

  5. MattSturgeon commented on Feb 3, 2026

    @MattSturgeon
    Contributor

    Edit: I found my new warning. It was indeed from my modifications. I am more sure that I successfully solved the issue now.

    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 nix to get the warnings.
    EDIT: #486456

  6. BirdeeHub commented on Feb 3, 2026

    @BirdeeHub
    Contributor

    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.

  7. BirdeeHub commented on Feb 3, 2026

    @BirdeeHub
    Contributor

    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

    pkgs.stdenv.mkDerivation {
      passAsFile = [ "test" ];
      test = builtins.unsafeDiscardStringContext "${pkgs.hello} --greeting ${pkgs.coreutils}";
      buildPhase = "cat \"$testPath\" > $out";
      name = "testdrv";
      src = "";
      dontUnpack = true;
    }

    Likewise neither does this

    pkgs.runCommand "test" {
      passAsFile = [ "test" ];
      test = builtins.unsafeDiscardStringContext "${pkgs.hello} --greeting ${pkgs.coreutils}";
    }
    "cat \"$testPath\" > $out"

    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.

  8. BirdeeHub commented on Feb 3, 2026

    @BirdeeHub
    Contributor

    For the next person to test this easier than the way I was trying to test it originally, a flake

    {
      inputs.nixpkgs.url = "git+file:/home/<YOUR_USERNAME>/YOUR/LOCAL/nixpkgs";
      outputs =
        { nixpkgs, ... }:
        {
          packages = nixpkgs.lib.genAttrs nixpkgs.lib.platforms.all (
            system:
            let
              pkgs = import nixpkgs { inherit system; };
            in
            {
              default =
                (pkgs.nixosOptionsDoc {
                  options =
                    (nixpkgs.lib.nixosSystem {
                      inherit (pkgs) system;
                      modules = [ ];
                    }).options;
                }).optionsJSON;
            }
          );
        };
    }

    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.

  9. MattSturgeon commented on Feb 4, 2026

    @MattSturgeon
    Contributor

    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:

    for some reason it appears only with determinate nix, with usual nix there is no such problem
    --- nix-community/home-manager#7935 (comment)

    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

  10. BirdeeHub commented on Feb 4, 2026

    @BirdeeHub
    Contributor

    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

  11. added
    2.status: stale
    on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.status: stale

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions