summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2021-05-14 17:27:25 +0200
committerRobert Hensing <robert@roberthensing.nl>2021-05-15 17:04:25 +0200
commitcc60f81e69154789ad3e23f0ec60110ed3f7aece (patch)
tree7c70d66ed73617ef073aeebd8f4ffb49620f81a5
parent80a3e0dc0ef11ecd7c6171318d2e3f2f5390b6ac (diff)
downloadnixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.tar
nixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.tar.gz
nixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.tar.bz2
nixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.tar.lz
nixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.tar.xz
nixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.tar.zst
nixpkgs-cc60f81e69154789ad3e23f0ec60110ed3f7aece.zip
writeDirectReferencesToFile: init
-rw-r--r--doc/builders/trivial-builders.chapter.md21
-rw-r--r--pkgs/build-support/trivial-builders.nix29
-rw-r--r--pkgs/build-support/trivial-builders/test.nix20
-rwxr-xr-xpkgs/build-support/trivial-builders/test.sh41
-rw-r--r--pkgs/test/default.nix2
5 files changed, 113 insertions, 0 deletions
diff --git a/doc/builders/trivial-builders.chapter.md b/doc/builders/trivial-builders.chapter.md
index 32944567c05..432183eaf0f 100644
--- a/doc/builders/trivial-builders.chapter.md
+++ b/doc/builders/trivial-builders.chapter.md
@@ -50,3 +50,24 @@ Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `
 ## `symlinkJoin` {#trivial-builder-symlinkJoin}
 
 This 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.
+
+## `writeDirectReferencesToFile` {#trivial-builder-writeDirectReferencesToFile}
+
+Writes the set of references to the output file, that is, their immediate dependencies.
+
+This produces the equivalent of `nix-store -q --references`.
+
+For example,
+
+```nix
+writeDirectReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
+```
+
+produces an output path `/nix/store/<hash>-runtime-references` containing
+
+```nix
+/nix/store/<hash>-hello-2.10
+```
+
+but none of `hello`'s dependencies, because those are not referenced directly
+by `hi`'s output.
diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix
index 142a04f9a10..0bf67d21b3b 100644
--- a/pkgs/build-support/trivial-builders.nix
+++ b/pkgs/build-support/trivial-builders.nix
@@ -438,6 +438,35 @@ rec {
       done < graph
     '';
 
+  /*
+    Write the set of references to a file, that is, their immediate dependencies.
+
+    This produces the equivalent of `nix-store -q --references`.
+   */
+  writeDirectReferencesToFile = path: runCommand "runtime-references"
+    {
+      exportReferencesGraph = ["graph" path];
+      inherit path;
+    }
+    ''
+      touch ./references
+      while read p; do
+        read dummy
+        read nrRefs
+        if [[ $p == $path ]]; then
+          for ((i = 0; i < nrRefs; i++)); do
+            read ref;
+            echo $ref >>./references
+          done
+        else
+          for ((i = 0; i < nrRefs; i++)); do
+            read ref;
+          done
+        fi
+      done < graph
+      sort ./references >$out
+    '';
+
 
   /* Print an error message if the file with the specified name and
    * hash doesn't exist in the Nix store. This function should only
diff --git a/pkgs/build-support/trivial-builders/test.nix b/pkgs/build-support/trivial-builders/test.nix
new file mode 100644
index 00000000000..0902a537222
--- /dev/null
+++ b/pkgs/build-support/trivial-builders/test.nix
@@ -0,0 +1,20 @@
+{ lib, nixosTest, path, writeText, hello, figlet, stdenvNoCC }:
+
+nixosTest {
+  name = "nixpkgs-trivial-builders";
+  nodes.machine = { ... }: {
+    virtualisation.writableStore = true;
+
+    # Test runs without network, so we don't substitute and prepare our deps
+    nix.binaryCaches = lib.mkForce [];
+    environment.etc."pre-built-paths".source = writeText "pre-built-paths" (
+      builtins.toJSON [hello figlet stdenvNoCC]
+    );
+  };
+  testScript = ''
+    machine.succeed("""
+      cd ${lib.cleanSource path}
+      ./pkgs/build-support/trivial-builders/test.sh 2>/dev/console
+    """)
+  '';
+}
diff --git a/pkgs/build-support/trivial-builders/test.sh b/pkgs/build-support/trivial-builders/test.sh
new file mode 100755
index 00000000000..eec501ae601
--- /dev/null
+++ b/pkgs/build-support/trivial-builders/test.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+
+# -------------------------------------------------------------------------- #
+#
+#                         trivial-builders test
+#
+# -------------------------------------------------------------------------- #
+#
+#  This file can be run independently (quick):
+#
+#      $ pkgs/build-support/trivial-builders/test.sh
+#
+#  or in the build sandbox with a ~20s VM overhead
+#
+#      $ nix-build -A tests.trivial-builders
+#
+# -------------------------------------------------------------------------- #
+
+# strict bash
+set -euo pipefail
+
+# debug
+# set -x
+# PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
+
+cd "$(dirname ${BASH_SOURCE[0]})"  # nixpkgs root
+
+testDirectReferences() {
+  expr="$1"
+  diff -U3 \
+    <(sort <$(nix-build --no-out-link --expr "with import ../../.. {}; writeDirectReferencesToFile ($expr)")) \
+    <(nix-store -q --references $(nix-build --no-out-link --expr "with import ../../.. {}; ($expr)") | sort)
+}
+
+testDirectReferences 'hello'
+testDirectReferences 'figlet'
+testDirectReferences 'writeText "hi" "hello"'
+testDirectReferences 'writeText "hi" "hello ${hello}"'
+testDirectReferences 'writeText "hi" "hello ${hello} ${figlet}"'
+
+echo 'OK!'
diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix
index 89656dde292..b9f05bdff8d 100644
--- a/pkgs/test/default.nix
+++ b/pkgs/test/default.nix
@@ -50,5 +50,7 @@ with pkgs;
 
   cuda = callPackage ./cuda { };
 
+  trivial = callPackage ../build-support/trivial-builders/test.nix {};
+
   writers = callPackage ../build-support/writers/test.nix {};
 }