From 7ffc4e1b2ff051ff5c012f411ac824b89a160dd6 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 2 Jun 2017 12:22:36 -0400 Subject: [PATCH] doc: Add "Specifying Dependencies" section to the stdenv chapter This accounts for all the new dependencies and propagation logic changes I'm about to add. Fixes #1915---with this change I think the distinction is finally clear enough. --- doc/cross-compilation.xml | 2 +- doc/stdenv.xml | 311 ++++++++++++++++++++++++++++++++------ 2 files changed, 264 insertions(+), 49 deletions(-) diff --git a/doc/cross-compilation.xml b/doc/cross-compilation.xml index 4b35b72feae0..f1194720cfd5 100644 --- a/doc/cross-compilation.xml +++ b/doc/cross-compilation.xml @@ -187,7 +187,7 @@ How does this work in practice? Nixpkgs is now structured so that build-time dependencies are taken from buildPackages, whereas run-time dependencies are taken from the top level attribute set. For example, buildPackages.gcc should be used at build time, while gcc should be used at run time. Now, for most of Nixpkgs's history, there was no buildPackages, and most packages have not been refactored to use it explicitly. - Instead, one can use the four attributes used for specifying dependencies as documented in . + Instead, one can use the six (gasp) attributes used for specifying dependencies as documented in . We "splice" together the run-time and build-time package sets with callPackage, and then mkDerivation for each of four attributes pulls the right derivation out. This splicing can be skipped when not cross compiling as the package sets are the same, but is a bit slow for cross compiling. Because of this, a best-of-both-worlds solution is in the works with no splicing or explicit access of buildPackages needed. diff --git a/doc/stdenv.xml b/doc/stdenv.xml index 91c659408c4b..67f249146144 100644 --- a/doc/stdenv.xml +++ b/doc/stdenv.xml @@ -179,6 +179,269 @@ genericBuild +
Specifying dependencies + + + As described in the Nix manual, almost any *.drv store path in a derivation's attribute set will induce a dependency on that derivation. + mkDerivation, however, takes a few attributes intended to, between them, include all the dependencies of a package. + This is done both for structure and consistency, but also so that certain other setup can take place. + For example, certain dependencies need their bin directories added to the PATH. + That is built-in, but other setup is done via a pluggable mechanism that works in conjunction with these dependency attributes. + See for details. + + + Dependencies can be broken down along three axes: their host and target platforms relative to the new derivation's, and whether they are propagated. + The platform distinctions are motivated by cross compilation; see for exactly what each platform means. + + The build platform is ignored because it is a mere implementation detail of the package satisfying the dependency: + As a general programming principle, dependencies are always specified as interfaces, not concrete implementation. + + But even if one is not cross compiling, the platforms imply whether or not the dependency is needed at run-time or build-time, a concept that makes perfect sense outside of cross compilation. + For now, the run-time/build-time distinction is just a hint for mental clarity, but in the future it perhaps could be enforced. + + + The extension of PATH with dependencies, alluded to above, proceeds according to the relative platforms alone. + The process is carried out only for dependencies whose host platform matches the new derivation's build platform–i.e. which run on the platform where the new derivation will be built. + + Currently, that means for native builds all dependencies are put on the PATH. + But in the future that may not be the case for sake of matching cross: + the platforms would be assumed to be unique for native and cross builds alike, so only the depsBuild* and nativeBuildDependencies dependencies would affect the PATH. + + For each dependency dep of those dependencies, dep/bin, if present, is added to the PATH environment variable. + + + The dependency is propagated when it forces some of its other-transitive (non-immediate) downstream dependencies to also take it on as an immediate dependency. + Nix itself already takes a package's transitive dependencies into account, but this propagation ensures nixpkgs-specific infrastructure like setup hooks (mentioned above) also are run as if the propagated dependency. + + + It is important to note dependencies are not necessary propagated as the same sort of dependency that they were before, but rather as the corresponding sort so that the platform rules still line up. + The exact rules for dependency propagation can be given by assigning each sort of dependency two integers based one how it's host and target platforms are offset from the depending derivation's platforms. + Those offsets are given are given below in the descriptions of each dependency list attribute. + Algorithmically, we traverse propagated inputs, accumulating every propagated dep's propagated deps and adjusting them to account for the "shift in perspective" described by the current dep's platform offsets. + This results in sort a transitive closure of the dependency relation, with the offsets being approximately summed when two dependency links are combined. + We also prune transitive deps whose combined offsets go out-of-bounds, which can be viewed as a filter over that transitive closure removing dependencies that are blatantly absurd. + + + We can define the process precisely with Natural Deduction using the inference rules. + This probably seems a bit obtuse, but so is the bash code that actually implements it! + + The findInputs function, currently residing in pkgs/stdenv/generic/setup.sh, implements the propagation logic. + + They're confusing in very different ways so...hopefully if something doesn't make sense in one presentation, it does in the other! + +let mapOffset(h, t, i) = i + (if i <= 0 then h else t - 1) + +propagated-dep(h0, t0, A, B) +propagated-dep(h1, t1, B, C) +h0 + h1 in {-1, 0, 1} +h0 + t1 in {-1, 0, 1} +-------------------------------------- Transitive property +propagated-dep(mapOffset(h0, t0, h1), + mapOffset(h0, t0, t1), + A, C) + +let mapOffset(h, t, i) = i + (if i <= 0 then h else t - 1) + +dep(h0, _, A, B) +propagated-dep(h1, t1, B, C) +h0 + h1 in {-1, 0, 1} +h0 + t1 in {-1, 0, -1} +-------------------------------------- Take immediate deps' propagated deps +propagated-dep(mapOffset(h0, t0, h1), + mapOffset(h0, t0, t1), + A, C) + +propagated-dep(h, t, A, B) +-------------------------------------- Propagated deps count as deps +dep(h, t, A, B) + Some explanation of this monstrosity is in order. + In the common case, the target offset of a dependency is the successor to the target offset: t = h + 1. + That means that: + +let f(h, t, i) = i + (if i <= 0 then h else t - 1) +let f(h, h + 1, i) = i + (if i <= 0 then h else (h + 1) - 1) +let f(h, h + 1, i) = i + (if i <= 0 then h else h) +let f(h, h + 1, i) = i + h + + This is where the "sum-like" comes from above: + We can just sum all the host offset to get the host offset of the transitive dependency. + The target offset is the transitive dep is simply the host offset + 1, just as it was with the dependencies composed to make this transitive one; + it can be ignored as it doesn't add any new information. + + + Because of the bounds checks, the uncommon cases are h = t and h + 2 = t. + In the former case, the motivation for mapOffset is that since its host and target platforms are the same, no transitive dep of it should be able to "discover" an offset greater than its reduced target offsets. + mapOffset effectively "squashes" all its transitive dependencies' offsets so that none will ever be greater than the target offset of the original h = t package. + In the other case, h + 1 is skipped over between the host and target offsets. + Instead of squashing the offsets, we need to "rip" them apart so no transitive dependencies' offset is that one. + + +Overall, the unifying theme here is that propagation shouldn't be introducing transitive dependencies involving platforms the needing package is unaware of. +The offset bounds checking and definition of mapOffset together ensure that this is the case. +Discovering a new offset is discovering a new platform, and since those platforms weren't in the derivation "spec" of the needing package, they cannot be relevant. +From a capability perspective, we can imagine that the host and target platforms of a package are the capabilities a package requires, and the depending package must provide the capability to the dependency. + + + + Variables specifying dependencies + + + depsBuildBuild + + + A list of dependencies whose host and target platforms are the new derivation's build platform. + This means a -1 host and -1 target offset from the new derivation's platforms. + They are programs/libraries used at build time that furthermore produce programs/libraries also used at build time. + If the dependency doesn't care about the target platform (i.e. isn't a compiler or similar tool), put it in nativeBuildInputsinstead. + The most common use for this buildPackages.stdenv.cc, the default C compiler for this role. + That example crops up more than one might think in old commonly used C libraries. + + + Since these packages are able to be run at build time, that are always added to the PATH, as described above. + But since these packages are only guaranteed to be able to run then, they shouldn't persist as run-time dependencies. + This isn't currently enforced, but could be in the future. + + + + + + nativeBuildInputs + + + A list of dependencies whose host platform is the new derivation's build platform, and target platform is the new derivation's host platform. + This means a -1 host offset and 0 target offset from the new derivation's platforms. + They are programs/libraries used at build time that, if they are a compiler or similar tool, produce code to run at run time—i.e. tools used to build the new derivation. + If the dependency doesn't care about the target platform (i.e. isn't a compiler or similar tool), put it here, rather than in depsBuildBuild or depsBuildTarget. + This would be called depsBuildHost but for historical continuity. + + + Since these packages are able to be run at build time, that are added to the PATH, as described above. + But since these packages only are guaranteed to be able to run then, they shouldn't persist as run-time dependencies. + This isn't currently enforced, but could be in the future. + + + + + + depsBuildTarget + + + A list of dependencies whose host platform is the new derivation's build platform, and target platform is the new derivation's target platform. + This means a -1 host offset and 1 target offset from the new derivation's platforms. + They are programs used at build time that produce code to run at run with code produced by the depending package. + Most commonly, these would tools used to build the runtime or standard library the currently-being-built compiler will inject into any code it compiles. + In many cases, the currently-being built compiler is itself employed for that task, but when that compiler won't run (i.e. its build and host platform differ) this is not possible. + Other times, the compiler relies on some other tool, like binutils, that is always built separately so the dependency is unconditional. + + + This is a somewhat confusing dependency to wrap ones head around, and for good reason. + As the only one where the platform offsets are not adjacent integers, it requires thinking of a bootstrapping stage two away from the current one. + It and it's use-case go hand in hand and are both considered poor form: + try not to need this sort dependency, and try not avoid building standard libraries / runtimes in the same derivation as the compiler produces code using them. + Instead strive to build those like a normal library, using the newly-built compiler just as a normal library would. + In short, do not use this attribute unless you are packaging a compiler and are sure it is needed. + + + Since these packages are able to be run at build time, that are added to the PATH, as described above. + But since these packages only are guaranteed to be able to run then, they shouldn't persist as run-time dependencies. + This isn't currently enforced, but could be in the future. + + + + + + depsHostHost + + A list of dependencies whose host and target platforms match the new derivation's host platform. + This means a both 0 host offset and 0 target offset from the new derivation's host platform. + These are packages used at run-time to generate code also used at run-time. + In practice, that would usually be tools used by compilers for metaprogramming/macro systems, or libraries used by the macros/metaprogramming code itself. + It's always preferable to use a depsBuildBuild dependency in the derivation being built than a depsHostHost on the tool doing the building for this purpose. + + + + + buildInputs + + + A list of dependencies whose host platform and target platform match the new derivation's. + This means a 0 host offset and 1 target offset from the new derivation's host platform. + This would be called depsHostTarget but for historical continuity. + If the dependency doesn't care about the target platform (i.e. isn't a compiler or similar tool), put it here, rather than in depsBuildBuild. + + + These often are programs/libraries used by the new derivation at run-time, but that isn't always the case. + For example, the machine code in a statically linked library is only used at run time, but the derivation containing the library is only needed at build time. + Even in the dynamic case, the library may also be needed at build time to appease the linker. + + + + + + depsTargetTarget + + A list of dependencies whose host platform matches the new derivation's target platform. + This means a 1 offset from the new derivation's platforms. + These are packages that run on the target platform, e.g. the standard library or run-time deps of standard library that a compiler insists on knowing about. + It's poor form in almost all cases for a package to depend on another from a future stage [future stage corresponding to positive offset]. + Do not use this attribute unless you are packaging a compiler and are sure it is needed. + + + + + depsBuildBuildPropagated + + The propagated equivalent of depsBuildBuild. + This perhaps never ought to be used, but it is included for consistency [see below for the others]. + + + + + propagatedNativeBuildInputs + + The propagated equivalent of nativeBuildInputs. + This would be called depsBuildHostPropagated but for historical continuity. + For example, if package Y has propagatedNativeBuildInputs = [X], and package Z has buildInputs = [Y], then package Z will be built as if it included package X in its nativeBuildInputs. + If instead, package Z has nativeBuildInputs = [Y], then Z will be built as if it included X in the depsBuildBuild of package Z, because of the sum of the two -1 host offsets. + + + + + depsBuildTargetPropagated + + The propagated equivalent of depsBuildTarget. + This is prefixed for the same reason of alerting potential users. + + + + + depsHostHostPropagated + + The propagated equivalent of depsHostHost. + + + + + propagatedBuildInputs + + The propagated equivalent of buildInputs. + This would be called depsHostTargetPropagated but for historical continuity. + + + + + depsTargetTarget + + The propagated equivalent of depsTargetTarget. + This is prefixed for the same reason of alerting potential users. + + + + + +
+ +
Attributes @@ -198,54 +461,6 @@ genericBuild - - Variables specifying dependencies - - - nativeBuildInputs - - A list of dependencies used by the new derivation at build-time. - I.e. these dependencies should not make it into the package's runtime-closure, though this is currently not checked. - For each dependency dir, the directory dir/bin, if it exists, is added to the PATH environment variable. - Other environment variables are also set up via a pluggable mechanism. - For instance, if buildInputs contains Perl, then the lib/site_perl subdirectory of each input is added to the PERL5LIB environment variable. - See for details. - - - - - buildInputs - - A list of dependencies used by the new derivation at run-time. - Currently, the build-time environment is modified in the exact same way as with nativeBuildInputs. - This is problematic in that when cross-compiling, foreign executables can clobber native ones on the PATH. - Even more confusing is static-linking. - A statically-linked library should be listed here because ultimately that generated machine code will be used at run-time, even though a derivation containing the object files or static archives will only be used at build-time. - A less confusing solution to this would be nice. - - - - - - propagatedNativeBuildInputs - - Like nativeBuildInputs, but these dependencies are propagated: - that is, the dependencies listed here are added to the nativeBuildInputs of any package that uses this package as a dependency. - So if package Y has propagatedNativeBuildInputs = [X], and package Z has nativeBuildInputs = [Y], - then package X will appear in Z’s build environment automatically. - - - - - propagatedBuildInputs - - Like buildInputs, but propagated just like propagatedNativeBuildInputs. - This inherits buildInputs's flaws of clobbering native executables when cross-compiling and being confusing for static linking. - - - - - Variables affecting build properties