Skip to content

Impure derivations #520

Open
Open

Description

@copumpkin

This might reveal a deep misunderstanding on my part, but as far as I can tell, nix fundamentally divides its derivations into "fixed-output" and "deterministic build", based on the presence/absence of outputHash. I'm wondering if there could be a third type of fundamental building block which could allow limited but trackable nondeterministic behavior. The main example I can think of right now is the new fetchTarball builtin, which has its own magic caching strategy, but you could imagine wanting to pull the latest git revision of something using fetchgit and the like. If you use fetchgit as a fixed-output derivation, you can't always get the latest version. If you have it "lie" and pretend not to be a fixed-output derivation, nix will only ever do the work once and not bother refreshing itself.

If nix supported this third type of derivation, I could imagine something like:

{
  fetchTarball = url: builtins.nondetDerivation {
    builder = ./fetchtarball.sh; # contains the actual download logic
    inherit url;
    cachingStrategy = "hourly"; # Perhaps it could take frequency specifiers like this, which would tell nix to incorporate evaluation time into the store hash, or possibly a more flexible mechanism that I haven't yet thought of
  };
}

Of course, it should be possible for you to take an expression and figure out all sources of nondeterminism in it (much like how this source downloader works) so as to better trust the evaluation.

Another possible feature of interest could be the notion of a nondetDerivation optionally (it's not possible with all sources of nondeterminism, but is obviously desirable) emitting some sort of an "anchor" allowing one to tie the nondeterministic evaluation down to something deterministic. Think how ruby's Gemfile ties itself down to Gemfile.lock (but we'd obviously provide hashes), and how when you fetch a git ref you can "lock it down" by resolving that ref to a hash. Another example is how the NixOS channel mechanism resolves the top-level redirect to a precise channel revision. Such an anchor file could then be maintained as a way to lock down nondeterminism to get reproducible system states, but you could also selectively (or in bulk) update the locked things (much like nix-channel --update) to get newer versions.

A last example is just how magic path references in nix copy things into the store for you. We could retain the built-in syntax, but translate the syntax into implicit invocations of the same nondetDerivation primitive.

Is this too weird? I'm just trying to think of a principled way to track my nondeterminism, and possibly to unify the channel world into pure nix.

TBC: I'm not proposing adding more nondeterminism to the system. Just want to be able to track/unify the existing stuff better.

Activity

  1. copumpkin commented on Apr 21, 2015

    @copumpkin
    MemberAuthor
  2. copumpkin commented on Apr 21, 2015

    @copumpkin
    MemberAuthor

    To make things even weirder, hydra could use this for its job specification with nondeterministic calls to fetchgit and fetchsvn.

  3. copumpkin commented on Apr 30, 2015

    @copumpkin
    MemberAuthor

    Nobody have any comments? I can flesh out the idea more if it would help. I think it could be a pretty cool way to manage the (limited but often necessary) pieces of mutable state in a Nix-based system.

  4. vcunat commented on May 1, 2015

    @vcunat
    Member

    To sum up, these derivations would:

    • need to have both impure access (mainly internet) and time-changing output, i.e. like fixed-output derivation without fixing the output hash;
    • be useful primarily to fetch latest version of foo from repository;
    • re-run their instantiation when they time-out (perhaps selectable as an attribute) or forced by some command-line flag.

    Do I get this right?

    Current status of code generators?

    I'm certain there are already general tools that prefetch latest source and update hashes in *.nix files – currently I don't see a distinct advantage in having this built in. For example, @MarcWeber has these REGION AUTO UPDATE things IIRC, and there may be others. Putting the nondeterministic part into a separate tool seems easier to update exactly those things you want and let others locked down (shell-scripting your most common use cases).

  5. Ericson2314 commented on Oct 27, 2015

    @Ericson2314
    Member

    I talked about some similar things in my somewhat-recent fetchgitLocal PR: NixOS/nixpkgs#10176 (comment). I think the interplay between the two derivations (a trick that predates my PR to be fair) is like the "anchoring" you mention.

  6. Ericson2314 commented on Nov 8, 2015

    @Ericson2314
    Member

    From these issues with my new fetchgitlocal, NixOS/nixpkgs#10873 I am starting to think we need non-deterministic packages which run under the current user to generalize things putting like private directories in the store.

  7. copumpkin commented on Nov 10, 2015

    @copumpkin
    MemberAuthor

    I'll probably see if I can drum up some interest about this (and flesh out my proposal) at NixCon in Berlin. @Ericson2314, will you be there?

  8. Ericson2314 commented on Nov 10, 2015

    @Ericson2314
    Member

    That would be great! Unfortunately, school will keep me away from NixCon, but let me know how it goes.

  9. copumpkin commented on Apr 4, 2016

    @copumpkin
    MemberAuthor

    I've been tinkering with this recently, and might be able to put up a PR for a hypothetical implementation (subject to lots of implementation and design feedback) in the next week or so, if I get some time.

    Edit: turned out to be more complicated than expected :(

  10. copumpkin commented on May 13, 2016

    @copumpkin
    MemberAuthor

    Tagging #904 for posterity.

  11. shlevy commented on Jan 12, 2017

    @shlevy
    Member

    @edolstra I'm considering working on this. Is there any chance I can get some assurance of a timely review and/or permission to merge myself before I put a large amount of work in?

  12. copumpkin commented on Jan 27, 2017

    @copumpkin
    MemberAuthor

    I posted this in another ticket:

    part of the reason I'm so interested in #520 is that I think that could be a cool model for channels as well as packages. The main properties I want out of a nondeterministic derivation are the ability to (somehow, programmatically) define how often I want it to update, and (most of the time) give myself a way of pinning to a particular version. Think of Ruby's Gemfile and Gemfile.lock distinction: Gemfile (on some level) defines an update policy (via bounds on package versions), and Gemfile.lock is an instantiation of that policy to exact versions that will be reproducible.

    Think of what we want from channels:

    I want to point to e.g., github.com/nixos/nixpkgs-channels/tree/nixpkgs-unstable (basically an update policy; I want to update at most as often as the branch updates)
    The branch can be resolved to an exact hash for later reproducibility
    I want to know explicitly that somewhere in my (otherwise highly deterministic) Nix evaluation, a possibly nondeterministic "moving target" is involved, and be given the opportunity to lock it down to something that point 2 produces
    I don't know of a great UI for this, but here's one not-so-great one that might inspire other ideas:

    When you write a nondeterministic derivation, you generate a UUID and paste it into the expression source
    Any evaluation of that nondeterministic derivation will get added to a top-level list of sources of nondeterminism in your expression, indexed by the associated UUID, and it's very clear when you evaluate an expression that your nondeterminism is included (so like when the top-level list of things to build and things to download from cache is printed, it could include a third category for these)
    Any build of a nondeterministic derivation gets a sandbox that allows network access
    The interface could (at first at least) basically be one that gives you a little "shim" to decide what to feed into a fixed-output derivation. That is, nondeterministic derivation = deterministic FO derivation + "decide (and record) which version to download". That would accommodate many common cases of git hashes and the like.
    Nix maintains a central registry on your machine of current resolved UUIDs, and lets you request that a particular UUID be updated (this is the equivalent of nix-channel --update)
    Then this mechanism can be used for channels, Hydra sources (don't have to make VCS into a first-class notion in Hydra anymore), packages that have sensible update semantics, and so on.

    I realize this is still pretty sketchy and probably doesn't belong in this ticket, but I do think something in this direction would be a killer feature, allowing us to unify the deterministic Nix world with changing surroundings in a relatively painless manner.

  13. added a commit that references this issue on Feb 24, 2017
  14. chris-martin commented on Jan 25, 2018

    @chris-martin
    Contributor

    So Shea told me about fetchgit today and it seems rather upsetting. It seems convenient sometimes, but is there going to be a config option or CLI flag or something to turn determinism back on? When I run a build, how will I be able to tell whether it's a deterministic one or one with unpinned fetches?

  15. copumpkin commented on Jan 25, 2018

    @copumpkin
    MemberAuthor

    Yeah, there's --pure as of a couple of day ago, I think. It should turn off all sources of impurity.

  16. 35 remaining items

  17. bryanhonof commented on Aug 3, 2022

    @bryanhonof
    Member

    @melvyn2 I also get that error when running nix-build. But if I use the new cli, nix build --impure ..., it does seem to work?

    /tmp/tmp.TMbuOx5fGy 
    ❯ cat default.nix 
    { pkgs ? import <nixpkgs> { } }:
    pkgs.stdenv.mkDerivation {
      name = "impure";
      __impure = true;
      buildCommand = "date > $out";
    }
    
    /tmp/tmp.TMbuOx5fGy 
    ❯ nix build --impure --file default.nix
    
    /tmp/tmp.TMbuOx5fGy 
    ❯ cat result 
    Wed Aug  3 10:03:03 UTC 2022
    
    /tmp/tmp.TMbuOx5fGy 
    ❯ nix-build 
    this derivation will be built:
      /nix/store/2ylp1hynhl3902kjzii9ynvby9ljizwp-impure.drv
    resolved derivation: '/nix/store/2ylp1hynhl3902kjzii9ynvby9ljizwp-impure.drv' -> '/nix/store/sm5kqqpsr9v7hk7hdxmhl4kxnd2mc3a6-impure.drv'...
    building '/nix/store/sm5kqqpsr9v7hk7hdxmhl4kxnd2mc3a6-impure.drv'...
    nix-build: src/nix-build/nix-build.cc:594: void main_nix_build(int, char**): Assertion `maybeOutputPath' failed.
    Aborted (core dumped)
  18. added
    UX
    feature
    on Sep 12, 2022
  19. added a commit that references this issue on Jan 1, 2024
  20. physics-enthusiast commented on Apr 4, 2024

    @physics-enthusiast

    Would these kinds of impure derivations be permitted in flakes pure eval mode?

  21. tomberek commented on Jul 10, 2024

    @tomberek
    Contributor

    Would these kinds of impure derivations be permitted in flakes pure eval mode?

    Yes, this would be safe because the outPath is not deterministic and the eval itself is not impure, only the build-phase.

  22. added a commit that references this issue on Mar 8, 2026
    ac6c9a9
  23. added a commit that references this issue on Jun 24, 2026
  24. tomberek commented on Aug 13, 2026

    @tomberek
    Contributor

    Closing as stale. Re-open if needed.

  25. MagicRB commented on Aug 13, 2026

    @MagicRB
    Contributor

    Whats the status of impure derivations? Id theyre finished we should close as done, if theyre not done we shouldnt close, unless were strictly against them for some reason.

  26. tomberek commented on Aug 13, 2026

    @tomberek
    Contributor

    I think they still need support for multiple outputs, but id have to check.

  27. roberth commented on Aug 18, 2026

    @roberth
    Member

    I think they're a bit simplistic. They don't have a way to identify a "run". (Term up for debate)
    A run could be defined as a "second instantiation", where Nix takes an already "evaluation-instantiated" derivation graph, generates a unique "run id", which gets spliced into the impure derivations, giving it a unique derivation graph.

    This derivation graph makes impure derivations a lot more useful, allowing the impure builds to be dispatched more flexibly, including resumption of an interrupted build graph.

    This is a little bit more scary. By giving this control to users, the could use it in a wrong way, e.g. send the re-instantiated derivations of to separate remote builders, who will then (probably) disagree on outputs.
    I think users are more than capable of handling that. We can't do all the thinking for them, and that is already true today.

    In terms of data model this means we'd have two kinds of impure derivations: the "pure" abstract impure derivations of today, and the re-instantiated ones that are responsible for runs and logging.

    It also solves the problem of logs getting overwritten, as they now belong to the re-instantiated derivation instead of the abstract one.

    I'm not sure if the "re-instantiation" framing is necessary. Alternatively, the run id could live in the output selection syntax (and a few other places). E.g. 0nk33c-foo.drv@d08f880d-e983-4d34-8bf6-d577f178b34e^out would refer to a specific run's out output.

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

    UXfeature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions