Skip to content

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

@Bad3r

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions