Skip to content

builtin:write-file #14011

Open
Open

Description

@roberth

Is your feature request related to a problem?

pkgs.writeFile is trivial yet needs to be built with an expensive sandbox etc

builtins.toFile is only usable for strings without derivation dependencies.

Proposed solution

A built-in derivation type that writes a string to a file really simply and quickly

derivation {
  system = "builtin";
  builder = "builtin:write-file";
  name = "foo.toml";
  content = ''
  ...
  '';
  executable = true;
}

Alternative solutions

  • Make the sandbox setup faster

    • Does not make it derivation-system-independent and inherently locally-buildable
  • Make it builtin:write-tree, capable of writing directories and symlinks as well

    • Symlink trees are typically output-dependent, and we don't want IFD
    • For prototyping and performance measurement, write-file is good enough
    • Only if we can share code with NAR deserialization
  • system = "was[im]-no-io-64"; (or something)

    • @Ericson2314 wasi is pretty capability-based; you need to pass it the stuff you want to allow, so wasi should be fine

Additional context

Brought up by @Mic92

Checklist


Add 👍 to issues you find important.

Activity

  1. added
    feature
    derivation-build
    on Sep 17, 2025
  2. Mic92 commented on Sep 17, 2025

    @Mic92
    Member
  3. lassulus commented on Sep 17, 2025

    @lassulus
    Member

    I also thought about write-tree. But I think doing write-file first is a quick win and write-tree can come later, since I think there is more discussion required.

    Another feature that write-file would be good to have, is adding an executable flag. So we can easier declare scripts (for example in flake outputs) without nixpkgs.

    Writing executable files could also be done by another builtin. something like builtin:write-script

  4. xokdvium commented on Sep 17, 2025

    @xokdvium
    Contributor

    There's the concern that this feature will be very hard to polyfill, since there's no good way of detecting if the store layer supports the right builtin derivation builder. It's unclear what the path to adoption could look like even if this gets implemented. The evaluator doesn't really know what sort of store layer implementation could try building the derivation.

  5. nixos-discourse commented on Sep 17, 2025

    @nixos-discourse
  6. ranomier commented on Sep 22, 2025

    @ranomier

    Potential use case:

    Yesterday i wrote a flake check and i wanted it to be quick. So i avoided the standard environment and used the low-level derivation.

    Since derivation requires an output (which i also don't fully understand why that is. Just require dependencies not to be empty) I had to use busybox as a builder only so i can write the output of my check program to the output.

    I would have loved to just put one pkg, in this case single binary and the data the check runs on, into the derivation and write some output file without needing a shell^^

  7. roberth commented on Sep 24, 2025

    @roberth
    MemberAuthor

    @ranomier I think we're trying to benefit from not having to run any user code for this type of derivation.
    What you're describing seems to be a pretty normal derivation in terms of execution; just a slightly modified output behavior.
    Alternative solution ideas for your use case:

    • No-output derivations, but that would make the "executed builder" unrepresentable as an output, which would put pressure on the system design to be more complex, having to work with both outputs and derivation executions in many places, like the CLI. Doesn't feel like the right direction to me.
    • A derivation flag ("advanced attribute") that captures stdout as a file output
    • Maybe Assertion quasi-derivations #11955?
  8. picnoir commented on Sep 26, 2025

    @picnoir
    Member

    Extra bonus points if we manage to keep this arch-agnostic from a builder perspective.

    When deploying an aarch64 machine from an x86 one, you currently end up building the configuration files using the emulation compatibility layer, which tend to be reaaaaly slow.

    It'd be great to be able to always build these text files using the build platform instead of the host one.

    I'm not sure if this could backfire in some situations, though. From the top of my head, I don't see such a situation.

  9. Mic92 commented on Oct 1, 2025

    @Mic92
    Member

    Remote building NixOS i.e. if architectures are not matching is also one of my motivations for this.

  10. roberth commented on Oct 14, 2025

    @roberth
    MemberAuthor

    This would also be great for unit testing.
    Even more so if we can sneak in build-time dependencies somehow so that we can instantly create a store with varied store path relations.

    Another interesting aspect is support for disallowedReferences and the other related checks.

    I'm not sure that's the best way to sneak in built-time deps, because we've talked about automatically dropping context for those disallow rules (to avoid pulling in unused deps when they're not also referenced by the productive parts of the derivation).

  11. Ericson2314 commented on Oct 15, 2025

    @Ericson2314
    Member

    For polyfill, content-addressing helps a little bit, because it means the choice of a "real" derivation or built-in derivation doesn't affect output paths.

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

    derivation-buildfeature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions