Repository navigation
Description
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
copumpkin commentedon Apr 21, 2015
cc @edolstra @shlevy
copumpkin commentedon Apr 21, 2015
To make things even weirder, hydra could use this for its job specification with nondeterministic calls to
fetchgitandfetchsvn.copumpkin commentedon Apr 30, 2015
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.
vcunat commentedon May 1, 2015
To sum up, these derivations would:
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 UPDATEthings 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).Ericson2314 commentedon Oct 27, 2015
I talked about some similar things in my somewhat-recent
fetchgitLocalPR: 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.Ericson2314 commentedon Nov 8, 2015
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.
copumpkin commentedon Nov 10, 2015
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?
Ericson2314 commentedon Nov 10, 2015
That would be great! Unfortunately, school will keep me away from NixCon, but let me know how it goes.
copumpkin commentedon Apr 4, 2016
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 :(
copumpkin commentedon May 13, 2016
Tagging #904 for posterity.
shlevy commentedon Jan 12, 2017
@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?
copumpkin commentedon Jan 27, 2017
I posted this in another ticket:
Add basic impure derivations
chris-martin commentedon Jan 25, 2018
So Shea told me about
fetchgittoday 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?copumpkin commentedon Jan 25, 2018
Yeah, there's
--pureas of a couple of day ago, I think. It should turn off all sources of impurity.35 remaining items
bryanhonof commentedon Aug 3, 2022
@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)Merge pull request NixOS#520 from zmitchell/refs-and-contributing
physics-enthusiast commentedon Apr 4, 2024
Would these kinds of impure derivations be permitted in flakes pure eval mode?
tomberek commentedon Jul 10, 2024
Yes, this would be safe because the outPath is not deterministic and the eval itself is not impure, only the build-phase.
libfetchers/github: distinguish ref/rev provenance
Merge pull request NixOS#520 from DeterminateSystems/eelcodolstra/nix…
tomberek commentedon Aug 13, 2026
Closing as stale. Re-open if needed.
MagicRB commentedon Aug 13, 2026
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.
tomberek commentedon Aug 13, 2026
I think they still need support for multiple outputs, but id have to check.
roberth commentedon Aug 18, 2026
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^outwould refer to a specific run'soutoutput.