git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #123111 from hercules-ci/writeDirectReferencesToFile

writeDirectReferencesToFile and maintenance

commitea0c94f50d2f1b901ef1434eac2dc31c241ffa9a authorRobert Hensing <roberth@users.noreply.github.com> dateMon 17 May 2021, 15:44 (5 years ago) parentd26bd80ed45231
5 files changed+155
doc/builders/trivial-builders.chapter.md+47 pkgs/build-support/trivial-builders.nix+29 pkgs/build-support/trivial-builders/test.nix+20 pkgs/build-support/trivial-builders/test.sh+57 pkgs/test/default.nix+2
doc/builders/trivial-builders.chapter.md+47
@@ -50,3 +50,50 @@ Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `
5050## `symlinkJoin` {#trivial-builder-symlinkJoin}
5151
5252This can be used to put many derivations into the same directory structure. It works by creating a new derivation and adding symlinks to each of the paths listed. It expects two arguments, `name`, and `paths`. `name` is the name used in the Nix store path for the created derivation. `paths` is a list of paths that will be symlinked. These paths can be to Nix store derivations or any other subdirectory contained within.
53+
54+## `writeReferencesToFile` {#trivial-builder-writeReferencesToFile}
55+
56+Writes the closure of transitive dependencies to a file.
57+
58+This produces the equivalent of `nix-store -q --requisites`.
59+
60+For example,
61+
62+```nix
63+writeReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
64+```
65+
66+produces an output path `/nix/store/<hash>-runtime-deps` containing
67+
68+```nix
69+/nix/store/<hash>-hello-2.10
70+/nix/store/<hash>-hi
71+/nix/store/<hash>-libidn2-2.3.0
72+/nix/store/<hash>-libunistring-0.9.10
73+/nix/store/<hash>-glibc-2.32-40
74+```
75+
76+You can see that this includes `hi`, the original input path,
77+`hello`, which is a direct reference, but also
78+the other paths that are indirectly required to run `hello`.
79+
80+## `writeDirectReferencesToFile` {#trivial-builder-writeDirectReferencesToFile}
81+
82+Writes the set of references to the output file, that is, their immediate dependencies.
83+
84+This produces the equivalent of `nix-store -q --references`.
85+
86+For example,
87+
88+```nix
89+writeDirectReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
90+```
91+
92+produces an output path `/nix/store/<hash>-runtime-references` containing
93+
94+```nix
95+/nix/store/<hash>-hello-2.10
96+```
97+
98+but none of `hello`'s dependencies, because those are not referenced directly
99+by `hi`'s output.
pkgs/build-support/trivial-builders.nix+29
@@ -438,6 +438,35 @@ rec {
438438 done < graph
439439 '';
440440
441+ /*
442+ Write the set of references to a file, that is, their immediate dependencies.
443+
444+ This produces the equivalent of `nix-store -q --references`.
445+ */
446+ writeDirectReferencesToFile = path: runCommand "runtime-references"
447+ {
448+ exportReferencesGraph = ["graph" path];
449+ inherit path;
450+ }
451+ ''
452+ touch ./references
453+ while read p; do
454+ read dummy
455+ read nrRefs
456+ if [[ $p == $path ]]; then
457+ for ((i = 0; i < nrRefs; i++)); do
458+ read ref;
459+ echo $ref >>./references
460+ done
461+ else
462+ for ((i = 0; i < nrRefs; i++)); do
463+ read ref;
464+ done
465+ fi
466+ done < graph
467+ sort ./references >$out
468+ '';
469+
441470
442471 /* Print an error message if the file with the specified name and
443472 * hash doesn't exist in the Nix store. This function should only
pkgs/build-support/trivial-builders/test.nixnew file+20
@@ -0,0 +1,20 @@
1+{ lib, nixosTest, path, writeText, hello, figlet, stdenvNoCC }:
2+
3+nixosTest {
4+ name = "nixpkgs-trivial-builders";
5+ nodes.machine = { ... }: {
6+ virtualisation.writableStore = true;
7+
8+ # Test runs without network, so we don't substitute and prepare our deps
9+ nix.binaryCaches = lib.mkForce [];
10+ environment.etc."pre-built-paths".source = writeText "pre-built-paths" (
11+ builtins.toJSON [hello figlet stdenvNoCC]
12+ );
13+ };
14+ testScript = ''
15+ machine.succeed("""
16+ cd ${lib.cleanSource path}
17+ ./pkgs/build-support/trivial-builders/test.sh 2>/dev/console
18+ """)
19+ '';
20+}
pkgs/build-support/trivial-builders/test.shnew file+57
@@ -0,0 +1,57 @@
1+#!/usr/bin/env bash
2+
3+# -------------------------------------------------------------------------- #
4+#
5+# trivial-builders test
6+#
7+# -------------------------------------------------------------------------- #
8+#
9+# This file can be run independently (quick):
10+#
11+# $ pkgs/build-support/trivial-builders/test.sh
12+#
13+# or in the build sandbox with a ~20s VM overhead
14+#
15+# $ nix-build -A tests.trivial-builders
16+#
17+# -------------------------------------------------------------------------- #
18+
19+# strict bash
20+set -euo pipefail
21+
22+# debug
23+# set -x
24+# PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
25+
26+cd "$(dirname ${BASH_SOURCE[0]})" # nixpkgs root
27+
28+testDirectReferences() {
29+ expr="$1"
30+ diff -U3 \
31+ <(sort <$(nix-build --no-out-link --expr "with import ../../.. {}; writeDirectReferencesToFile ($expr)")) \
32+ <(nix-store -q --references $(nix-build --no-out-link --expr "with import ../../.. {}; ($expr)") | sort)
33+}
34+
35+testDirectReferences 'hello'
36+testDirectReferences 'figlet'
37+testDirectReferences 'writeText "hi" "hello"'
38+testDirectReferences 'writeText "hi" "hello ${hello}"'
39+testDirectReferences 'writeText "hi" "hello ${hello} ${figlet}"'
40+
41+
42+
43+testClosure() {
44+ expr="$1"
45+ diff -U3 \
46+ <(sort <$(nix-build --no-out-link --expr "with import ../../.. {}; writeReferencesToFile ($expr)")) \
47+ <(nix-store -q --requisites $(nix-build --no-out-link --expr "with import ../../.. {}; ($expr)") | sort)
48+}
49+
50+testClosure 'hello'
51+testClosure 'figlet'
52+testClosure 'writeText "hi" "hello"'
53+testClosure 'writeText "hi" "hello ${hello}"'
54+testClosure 'writeText "hi" "hello ${hello} ${figlet}"'
55+
56+
57+echo 'OK!'
pkgs/test/default.nix+2
@@ -50,5 +50,7 @@ with pkgs;
5050
5151 cuda = callPackage ./cuda { };
5252
53+ trivial = callPackage ../build-support/trivial-builders/test.nix {};
54+
5355 writers = callPackage ../build-support/writers/test.nix {};
5456}