git.iko.soy/typeable/nixpkgs
on master help sign in
writeDirectReferencesToFile: init
commitcc60f81e69154789ad3e23f0ec60110ed3f7aece authorRobert Hensing <robert@roberthensing.nl> dateFri 14 May 2021, 17:27 (6 years ago) parent80a3e0d
5 files changed+113
doc/builders/trivial-builders.chapter.md+21 pkgs/build-support/trivial-builders.nix+29 pkgs/build-support/trivial-builders/test.nix+20 pkgs/build-support/trivial-builders/test.sh+41 pkgs/test/default.nix+2
doc/builders/trivial-builders.chapter.md+21
@@ -50,3 +50,24 @@ 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+## `writeDirectReferencesToFile` {#trivial-builder-writeDirectReferencesToFile}
55+
56+Writes the set of references to the output file, that is, their immediate dependencies.
57+
58+This produces the equivalent of `nix-store -q --references`.
59+
60+For example,
61+
62+```nix
63+writeDirectReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
64+```
65+
66+produces an output path `/nix/store/<hash>-runtime-references` containing
67+
68+```nix
69+/nix/store/<hash>-hello-2.10
70+```
71+
72+but none of `hello`'s dependencies, because those are not referenced directly
73+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+41
@@ -0,0 +1,41 @@
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+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}