From 906598c9145f13ed742e45b1440bf1f9232c58ce Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 8 Aug 2024 13:25:52 +0000 Subject: [PATCH 1/5] testers: format inputs per RFC166 --- pkgs/build-support/testers/default.nix | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index d0d88115003f..9176a09d7a1b 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -1,4 +1,16 @@ -{ pkgs, pkgsLinux, buildPackages, diffoscopeMinimal, lib, callPackage, runCommand, stdenv, substituteAll, testers }: +{ + lib, + buildPackages, + callPackage, + pkgs, + pkgsLinux, + + diffoscopeMinimal, + runCommand, + stdenv, + substituteAll, + testers, +}: # Documentation is in doc/build-helpers/testers.chapter.md { # See https://nixos.org/manual/nixpkgs/unstable/#tester-lycheeLinkCheck From d0a96c6eda12b1209029f9f5b510dd0a834f34d5 Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 8 Aug 2024 14:11:25 +0000 Subject: [PATCH 2/5] testers.runCommand: add, document, and test --- doc/build-helpers/testers.chapter.md | 33 +++++++++++++++++++++ pkgs/build-support/testers/default.nix | 27 +++++++++++++++++ pkgs/build-support/testers/test/default.nix | 21 +++++++++++++ 3 files changed, 81 insertions(+) diff --git a/doc/build-helpers/testers.chapter.md b/doc/build-helpers/testers.chapter.md index ec659e75bdb5..71822b82d774 100644 --- a/doc/build-helpers/testers.chapter.md +++ b/doc/build-helpers/testers.chapter.md @@ -339,6 +339,39 @@ once to get a derivation hash, and again to produce the final fixed output deriv ::: +## `runCommand` {#tester-runCommand} + +This is a wrapper around `pkgs.runCommandWith`, which +- produces a fixed-output derivation, enabling the command(s) to access the network ; +- salts the derivation's name based on its inputs, ensuring the command is re-run whenever the inputs changes. + +It accepts the following attributes: +- the derivation's `name` ; +- the `script` to be executed ; +- `stdenv`, the environment to use, defaulting to `stdenvNoCC` ; +- the derivation's output `hash`, defaulting to the empty file's. + The derivation's `outputHashMode` is set by default to recursive, so the `script` can output a directory as well. + +All other attributes are passed through to [`mkDerivation`](#sec-using-stdenv), +including `nativeBuildInputs` to specify dependencies available to the `script`. + +:::{.example #ex-tester-runCommand-nix} + +# Run a command with network access + +```nix +testers.runCommand { + name = "access-the-internet"; + command = '' + curl -o /dev/null https://example.com + touch $out + ''; + nativeBuildInputs = with pkgs; [ cacert curl ]; +} +``` + +::: + ## `runNixOSTest` {#tester-runNixOSTest} A helper function that behaves exactly like the NixOS `runTest`, except it also assigns this Nixpkgs package set as the `pkgs` of the test and makes the `nixpkgs.*` options read-only. diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index 9176a09d7a1b..361517507bc7 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -7,7 +7,9 @@ diffoscopeMinimal, runCommand, + runCommandWith, stdenv, + stdenvNoCC, substituteAll, testers, }: @@ -99,6 +101,31 @@ else salted; in checked; + # See https://nixos.org/manual/nixpkgs/unstable/#tester-runCommand + runCommand = testers.invalidateFetcherByDrvHash ( + { + hash ? "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=", # hash value of empty file + name, + script, + stdenv ? stdenvNoCC, + ... + }@args: + + runCommandWith { + inherit name stdenv; + + derivationArgs = { + outputHash = hash; + outputHashMode = "recursive"; + } // lib.removeAttrs args [ + "hash" + "name" + "script" + "stdenv" + ]; + } script + ); + # See https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest # or doc/build-helpers/testers.chapter.md runNixOSTest = diff --git a/pkgs/build-support/testers/test/default.nix b/pkgs/build-support/testers/test/default.nix index 48855df91627..9ef203b37516 100644 --- a/pkgs/build-support/testers/test/default.nix +++ b/pkgs/build-support/testers/test/default.nix @@ -18,6 +18,27 @@ lib.recurseIntoAttrs { shellcheck = pkgs.callPackage ../shellcheck/tests.nix { }; + runCommand = lib.recurseIntoAttrs { + dns-resolution = testers.runCommand { + name = "runCommand-dns-resolution-test"; + nativeBuildInputs = [ pkgs.ldns ]; + script = '' + drill example.com + touch $out + ''; + }; + + nonDefault-hash = testers.runCommand { + name = "runCommand-nonDefaultHash-test"; + script = '' + mkdir $out + touch $out/empty + echo aaaaaaaaaaicjnrkeflncmrlk > $out/keymash + ''; + hash = "sha256-eMy+6bkG+KS75u7Zt4PM3APhtdVd60NxmBRN5GKJrHs="; + }; + }; + runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: { name = "runNixOSTest-test"; nodes.machine = { pkgs, ... }: { From 244229a8b6f29264cdd2efbcb64f945980b47871 Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 8 Aug 2024 14:28:17 +0000 Subject: [PATCH 3/5] python3Packages.bork: add test using `testers.runCommand` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will serve as a “real-world” example, running tests requiring network. --- pkgs/build-support/testers/test/default.nix | 2 ++ .../python-modules/bork/default.nix | 3 ++ .../development/python-modules/bork/tests.nix | 28 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 pkgs/development/python-modules/bork/tests.nix diff --git a/pkgs/build-support/testers/test/default.nix b/pkgs/build-support/testers/test/default.nix index 9ef203b37516..cc46e3cf4b71 100644 --- a/pkgs/build-support/testers/test/default.nix +++ b/pkgs/build-support/testers/test/default.nix @@ -19,6 +19,8 @@ lib.recurseIntoAttrs { shellcheck = pkgs.callPackage ../shellcheck/tests.nix { }; runCommand = lib.recurseIntoAttrs { + bork = pkgs.python3Packages.bork.tests.pytest-network; + dns-resolution = testers.runCommand { name = "runCommand-dns-resolution-test"; nativeBuildInputs = [ pkgs.ldns ]; diff --git a/pkgs/development/python-modules/bork/default.nix b/pkgs/development/python-modules/bork/default.nix index 44a8564558e9..9da54dc1ec69 100644 --- a/pkgs/development/python-modules/bork/default.nix +++ b/pkgs/development/python-modules/bork/default.nix @@ -1,6 +1,7 @@ { lib, buildPythonPackage, + callPackage, fetchFromGitHub, pytestCheckHook, pythonOlder, @@ -61,6 +62,8 @@ buildPythonPackage rec { "test_repo" ]; + passthru.tests = callPackage ./tests.nix { }; + meta = with lib; { description = "Python build and release management tool"; mainProgram = "bork"; diff --git a/pkgs/development/python-modules/bork/tests.nix b/pkgs/development/python-modules/bork/tests.nix new file mode 100644 index 000000000000..7d5c84f2bc35 --- /dev/null +++ b/pkgs/development/python-modules/bork/tests.nix @@ -0,0 +1,28 @@ +{ + testers, + + bork, + cacert, + git, + pytest, +}: +{ + # a.k.a. `tests.testers.runCommand.bork` + pytest-network = testers.runCommand { + name = "bork-pytest-network"; + nativeBuildInputs = [ + bork + cacert + git + pytest + ]; + script = '' + # Copy the source tree over, and make it writeable + cp -r ${bork.src} bork/ + find -type d -exec chmod 0755 '{}' '+' + + pytest -v -m network bork/ + touch $out + ''; + }; +} From e0fc12cd1260c4196da1dc7189f70a6c56c43115 Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 8 Aug 2024 15:43:47 +0000 Subject: [PATCH 4/5] doc: add type signature of `testers.runCommand` --- doc/build-helpers/testers.chapter.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/build-helpers/testers.chapter.md b/doc/build-helpers/testers.chapter.md index 71822b82d774..5c1b704655cf 100644 --- a/doc/build-helpers/testers.chapter.md +++ b/doc/build-helpers/testers.chapter.md @@ -341,6 +341,8 @@ once to get a derivation hash, and again to produce the final fixed output deriv ## `runCommand` {#tester-runCommand} +`runCommand :: { name, script, stdenv ? stdenvNoCC, hash ? "...", ... } -> Derivation` + This is a wrapper around `pkgs.runCommandWith`, which - produces a fixed-output derivation, enabling the command(s) to access the network ; - salts the derivation's name based on its inputs, ensuring the command is re-run whenever the inputs changes. From 18dd486bb9b5b19379c1c4c3bf78c04c239d032e Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 8 Aug 2024 20:06:21 +0000 Subject: [PATCH 5/5] emptyFile: use SRI hash --- pkgs/build-support/testers/default.nix | 2 +- pkgs/build-support/trivial-builders/default.nix | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index 361517507bc7..705946e0eab7 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -104,7 +104,7 @@ # See https://nixos.org/manual/nixpkgs/unstable/#tester-runCommand runCommand = testers.invalidateFetcherByDrvHash ( { - hash ? "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=", # hash value of empty file + hash ? pkgs.emptyFile.outputHash, name, script, stdenv ? stdenvNoCC, diff --git a/pkgs/build-support/trivial-builders/default.nix b/pkgs/build-support/trivial-builders/default.nix index fc6f07fdd11b..686819725d59 100644 --- a/pkgs/build-support/trivial-builders/default.nix +++ b/pkgs/build-support/trivial-builders/default.nix @@ -887,9 +887,8 @@ rec { /* An immutable file in the store with a length of 0 bytes. */ emptyFile = runCommand "empty-file" { - outputHashAlgo = "sha256"; + outputHash = "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY="; outputHashMode = "recursive"; - outputHash = "0ip26j2h11n1kgkz36rl4akv694yz65hr72q4kv4b3lxcbi65b3p"; preferLocalBuild = true; } "touch $out";