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
Description
nixos/lib/make-options-doc/default.nixusesbuiltins.toFileto createoptions.jsonin a way that loses store path context, triggering a warning:Location
https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/make-options-doc/default.nix#L217
Context
The use of
builtins.toFilewithbuiltins.unsafeDiscardStringContextappears to be a deliberate performance optimization:builtins.toFileruns at evaluation time and creates the file directly in the store without a derivation (faster)pkgs.writeTextcreates a derivation that runs at build time (slower, but properly tracks dependencies)Since
builtins.toFilecannot reference store paths,unsafeDiscardStringContextis used to strip the context. This works but loses dependency tracking, which Nix now warns about.Impact
nixos-rebuild,nix build, ornix evalof NixOS configurationsSuggested Fix
Replace
builtins.toFilewithpkgs.writeTextor 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.toplevelMetadata