Skip to content

Fetcher testing#136022

Merged
roberth merged 7 commits intoNixOS:masterfrom
hercules-ci:fetcher-testing
Sep 21, 2021
Merged

Fetcher testing#136022
roberth merged 7 commits intoNixOS:masterfrom
hercules-ci:fetcher-testing

Conversation

@roberth
Copy link
Member

@roberth roberth commented Aug 28, 2021

Motivation for this change

Make maintenance easier on the fetchers and avoid problems.

The packageTests attribute will serve two purposes:

  • for ofborg: allow it to build tests in a __functor attrset like fetchgit
  • for hydra, potentially, build the package tests. Enabling these is not part of this PR.

Closes #133739

Refs #128749 #135881 or really all of topic: fetch

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added the 6.topic: fetch Fetchers (e.g. fetchgit, fetchsvn, ...) label Aug 28, 2021
@roberth roberth added 6.topic: developer experience nixpkgs development workflow 6.topic: testing Tooling for automated testing of packages and modules labels Aug 28, 2021
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. labels Aug 28, 2021
@roberth
Copy link
Member Author

roberth commented Aug 28, 2021

Not picked up automatically by ofborg, as expected. So,

@GrahamcOfBorg build packageTests.fetchgit

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/593

@Artturin
Copy link
Member

probably needs documentation in doc/builders/fetchers.chapter.md?

@Artturin
Copy link
Member

can't get this to work

diff --git a/pkgs/build-support/fetchfirefoxaddon/tests.nix b/pkgs/build-support/fetchfirefoxaddon/tests.nix
new file mode 100644
index 00000000000..05ee43aba29
--- /dev/null
+++ b/pkgs/build-support/fetchfirefoxaddon/tests.nix
@@ -0,0 +1,10 @@
+{ invalidateFetcherByDrvHash, fetchFirefoxAddon, ... }:
+
+{
+  simple = invalidateFetcherByDrvHash fetchFirefoxAddon {
+    name = "image-search-options";
+    # Chosen because its only 147KB
+    url = "https://addons.mozilla.org/firefox/downloads/file/3059971/image_search_options-3.0.12-fx.xpi";
+    sha256 = "1h768ljlh3pi23l27qp961v1hd0nbj2vasgy11bmcrlqp40zgvnr";
+  };
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 161c48a3d48..d14a587eb11 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -516,7 +516,10 @@ with pkgs;
 
   fetchhg = callPackage ../build-support/fetchhg { };
 
-  fetchFirefoxAddon = callPackage ../build-support/fetchfirefoxaddon {};
+  fetchFirefoxAddon = callPackage ../build-support/fetchfirefoxaddon { }
+    // {
+      tests = callPackages ../build-support/fetchfirefoxaddon/tests.nix;
+  };
 
   # `fetchurl' downloads a file from the network.
   fetchurl = if stdenv.buildPlatform != stdenv.hostPlatform
nix-repl> packageTests.fetchgit
{ recurseForDerivations = true; simple = «derivation /nix/store/5rl9by8nifg2k25rzp3yw5fdj303r6qn-nix-source-salted-v839ywjgprrp.drv»; }

nix-repl> packageTests.fetchFirefoxAddon
error: value is a function while a set was expected

@roberth
Copy link
Member Author

roberth commented Sep 19, 2021

Diffception.

-+      tests = callPackages ../build-support/fetchfirefoxaddon/tests.nix;
++      tests = callPackages ../build-support/fetchfirefoxaddon/tests.nix { };

I also fall into this trap every now and then, fwiw.

documentation

yes.

@github-actions github-actions bot added the 8.has: documentation This PR adds or changes documentation label Sep 19, 2021
Copy link
Member Author

@roberth roberth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've cleaned up some minor docs issues while I was at it. I hope those will not hinder this pr.


`fetchpatch` works very similarly to `fetchurl` with the same arguments expected. It expects patch files as a source and performs normalization on them before computing the checksum. For example it will remove comments or other unstable parts that are sometimes added by version control systems and can change over time.

Other fetcher functions allow you to add source code directly from a VCS such as subversion or git. These are mostly straightforward nambes based on the name of the command used with the VCS system. Because they give you a working repository, they act most like `fetchzip`.
Copy link
Member Author

@roberth roberth Sep 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was 90% stating the obvious. I've preserved the essence of the last sentence.

When using Nix, you will frequently need to download source code and other files from the internet. Nixpkgs comes with a few helper functions that allow you to fetch fixed-output derivations in a structured way.
When using Nix, you will frequently need to download source code and other files from the internet. For this purpose, Nix provides the [_fixed output derivation_](https://nixos.org/manual/nix/stable/#fixed-output-drvs) feature and Nixpkgs provides various functions that implement the actual fetching from various protocols and services.

The two fetcher primitives are `fetchurl` and `fetchzip`. Both of these have two required arguments, a URL and a hash. The hash is typically `sha256`, although many more hash algorithms are supported. Nixpkgs contributors are currently recommended to use `sha256`. This hash will be used by Nix to identify your source. A typical usage of fetchurl is provided below.
Copy link
Member Author

@roberth roberth Sep 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two fetcher primitives

There's no such thing.

The rest in the fetchurl section.

@Artturin
Copy link
Member

Artturin commented Sep 19, 2021

will having all the expressions in packageTests.X slow something(eval?) down
would it be possible to only have the ones that have tests be there

is packageTests even needed since
nix build ".#fetchgit.tests.simple" works?

nix-build doesn't work like thatnix-build -A fetchgit.tests.simple

@roberth
Copy link
Member Author

roberth commented Sep 19, 2021

will having all the expressions in packageTests.X slow something(eval?) down

This adds about as much overhead as a package would. In most use cases it would go unnoticed like any other package. In cases where the whole of Nixpkgs is traversed, those traversals look for the recurseForDerivations attribute, which is unset inside packageTests, causing it to be skipped. This works for nix-build (which shouldn't be used on all of Nixpkgs anyway) and it works for Hydra/release.nix. In this case, the attribute names are shallow-copied into memory once more, but this is still insignificant and the same happens for pkgsStatic etc.

@Artturin
Copy link
Member

@ofborg eval

@Artturin
Copy link
Member

How would one run all the packageTests?

@roberth
Copy link
Member Author

roberth commented Sep 21, 2021

@Artturin you generally wouldn't, if you mean literally all.

@GrahamcOfBorg build packageTests.fetchgit

This reverts commit 87b3740.

packageTests wasn't inherently wrong, but I am not fully convinced
that it is necessary. Specifically, it would only be used by:

 - `nix-build`, which is going to be a legacy command in the mid term
 - ofborg, perhaps, not even implemented

whereas the following use cases do not need it:

 - `nix build` (experimental command with a future, unlike `nix-build`)
 - `release.nix` (Hydra), might use similar logic but not reuse this

Further, considering that this is only necessary for tests of
functions, I am hesitant to introduce it.
Meanwhile, it should not hinder the rest of the PR.
@roberth
Copy link
Member Author

roberth commented Sep 21, 2021

I've removed packageTests. To quote the commit message:

packageTests wasn't inherently wrong, but I am not fully convinced
that it is necessary. Specifically, it would only be used by:

  • nix-build, which is going to be a legacy command in the mid term
  • ofborg, perhaps, not even implemented

whereas the following use cases do not need it:

  • nix build (experimental command with a future, unlike nix-build)
  • release.nix (Hydra), might use similar logic but not reuse this

Further, considering that this is only necessary for tests of
functions, I am hesitant to introduce it.
Meanwhile, it should not hinder the rest of the PR.

@roberth
Copy link
Member Author

roberth commented Sep 21, 2021

Not sure if I tested this or just assumed it still uses nix-build and suffers from its functor problem.

@GrahamcOfBorg build fetchgit.tests

@roberth
Copy link
Member Author

roberth commented Sep 21, 2021

Confirmed. Ofborg tries to autocall the functor instead of using the attribute.

Cannot nix-instantiate `fetchgit.tests' because:
error: cannot auto-call a function that has an argument without a default value ('url')

I'll leave ofborg out of scope for now and merge this when ofborg is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: developer experience nixpkgs development workflow 6.topic: fetch Fetchers (e.g. fetchgit, fetchsvn, ...) 6.topic: testing Tooling for automated testing of packages and modules 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repeatable fetcher tests

3 participants