From 91b754edff9120b6dd868e61b2f50348f465a0d1 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 16 Jan 2024 20:40:39 +0100 Subject: tests.nixpkgs-check-by-name: Minor Nix build refactor --- pkgs/test/nixpkgs-check-by-name/default.nix | 32 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'pkgs/test') diff --git a/pkgs/test/nixpkgs-check-by-name/default.nix b/pkgs/test/nixpkgs-check-by-name/default.nix index d2de2d960042..c6991820fff2 100644 --- a/pkgs/test/nixpkgs-check-by-name/default.nix +++ b/pkgs/test/nixpkgs-check-by-name/default.nix @@ -10,6 +10,21 @@ let runtimeExprPath = ./src/eval.nix; nixpkgsLibPath = ../../../lib; + + # Needed to make Nix evaluation work inside nix builds + initNix = '' + export TEST_ROOT=$(pwd)/test-tmp + export NIX_CONF_DIR=$TEST_ROOT/etc + export NIX_LOCALSTATE_DIR=$TEST_ROOT/var + export NIX_LOG_DIR=$TEST_ROOT/var/log/nix + export NIX_STATE_DIR=$TEST_ROOT/var/nix + export NIX_STORE_DIR=$TEST_ROOT/store + + # Ensure that even if tests run in parallel, we don't get an error + # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first + nix-store --init + ''; + package = rustPlatform.buildRustPackage { name = "nixpkgs-check-by-name"; @@ -22,21 +37,8 @@ let makeWrapper ]; env.NIX_CHECK_BY_NAME_EXPR_PATH = "${runtimeExprPath}"; - # Needed to make Nix evaluation work inside the nix build - preCheck = '' - export TEST_ROOT=$(pwd)/test-tmp - export NIX_CONF_DIR=$TEST_ROOT/etc - export NIX_LOCALSTATE_DIR=$TEST_ROOT/var - export NIX_LOG_DIR=$TEST_ROOT/var/log/nix - export NIX_STATE_DIR=$TEST_ROOT/var/nix - export NIX_STORE_DIR=$TEST_ROOT/store - - export NIXPKGS_LIB_PATH=${nixpkgsLibPath} - - # Ensure that even if tests run in parallel, we don't get an error - # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first - nix-store --init - ''; + env.NIXPKGS_LIB_PATH = "${nixpkgsLibPath}"; + preCheck = initNix; postCheck = '' cargo fmt --check cargo clippy -- -D warnings -- cgit 1.4.1 From 3ebd239cd5645f679b45f7f843fc4cc67a281ea4 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 16 Jan 2024 20:43:03 +0100 Subject: tests.nixpkgs-check-by-name: Test on the current Nixpkgs This test actually fails right now --- pkgs/test/nixpkgs-check-by-name/default.nix | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'pkgs/test') diff --git a/pkgs/test/nixpkgs-check-by-name/default.nix b/pkgs/test/nixpkgs-check-by-name/default.nix index c6991820fff2..f2a6473ad202 100644 --- a/pkgs/test/nixpkgs-check-by-name/default.nix +++ b/pkgs/test/nixpkgs-check-by-name/default.nix @@ -6,6 +6,7 @@ clippy, mkShell, makeWrapper, + runCommand, }: let runtimeExprPath = ./src/eval.nix; @@ -52,6 +53,19 @@ let env.NIXPKGS_LIB_PATH = toString nixpkgsLibPath; inputsFrom = [ package ]; }; + + # Tests the tool on the current Nixpkgs tree, this is a good sanity check + passthru.tests.nixpkgs = runCommand "test-nixpkgs-check-by-name" { + nativeBuildInputs = [ + package + nix + ]; + nixpkgsPath = lib.cleanSource ../../..; + } '' + ${initNix} + nixpkgs-check-by-name --base "$nixpkgsPath" "$nixpkgsPath" + touch $out + ''; }; in package -- cgit 1.4.1 From 0f27917d9afb22106362942c30cb4547bd0a1142 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 16 Jan 2024 21:10:22 +0100 Subject: tests.nixpkgs-check-by-name: Don't error for pkgs/by-name aliases This was an oversight in https://github.com/NixOS/nixpkgs/pull/275539 not accounted for, which would've failed in CI --- pkgs/test/nixpkgs-check-by-name/src/eval.rs | 17 +++++++++++++---- pkgs/test/nixpkgs-check-by-name/src/nixpkgs_problem.rs | 8 -------- .../nixpkgs-check-by-name/tests/aliases/aliases.nix | 3 +++ .../tests/aliases/all-packages.nix | 3 +++ .../nixpkgs-check-by-name/tests/aliases/default.nix | 1 + .../tests/aliases/pkgs/by-name/fo/foo/package.nix | 1 + .../tests/internalCallPackage/expected | 1 - pkgs/test/nixpkgs-check-by-name/tests/mock-nixpkgs.nix | 9 +++++++++ 8 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 pkgs/test/nixpkgs-check-by-name/tests/aliases/aliases.nix create mode 100644 pkgs/test/nixpkgs-check-by-name/tests/aliases/all-packages.nix create mode 100644 pkgs/test/nixpkgs-check-by-name/tests/aliases/default.nix create mode 100644 pkgs/test/nixpkgs-check-by-name/tests/aliases/pkgs/by-name/fo/foo/package.nix delete mode 100644 pkgs/test/nixpkgs-check-by-name/tests/internalCallPackage/expected (limited to 'pkgs/test') diff --git a/pkgs/test/nixpkgs-check-by-name/src/eval.rs b/pkgs/test/nixpkgs-check-by-name/src/eval.rs index b411a2a3c601..c4dad8257cf9 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/eval.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/eval.rs @@ -168,14 +168,23 @@ pub fn check_values( // so the UsesByName ratchet is already as tight as it can be NonAttributeSet => Success(Tight), NonCallPackage => Success(Tight), - // This is an odd case when _internalCallByNamePackageFile is used to define a package. + // This is the case when the `pkgs/by-name`-internal _internalCallByNamePackageFile + // is used for a package outside `pkgs/by-name` CallPackage(CallPackageInfo { call_package_variant: Auto, .. - }) => NixpkgsProblem::InternalCallPackageUsed { - attr_name: attribute_name.clone(), + }) => { + // With the current detection mechanism, this also triggers for aliases + // to pkgs/by-name packages, and there's no good method of + // distinguishing alias vs non-alias. + // Using `config.allowAliases = false` at least currently doesn't work + // because there's nothing preventing people from defining aliases that + // are present even with that disabled. + // In the future we could kind of abuse this behavior to have better + // enforcement of conditional aliases, but for now we just need to not + // give an error. + Success(Tight) } - .into(), // Only derivations can be in pkgs/by-name, // so this attribute doesn't qualify CallPackage(CallPackageInfo { diff --git a/pkgs/test/nixpkgs-check-by-name/src/nixpkgs_problem.rs b/pkgs/test/nixpkgs-check-by-name/src/nixpkgs_problem.rs index 127583078074..16ea65deebfc 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/nixpkgs_problem.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/nixpkgs_problem.rs @@ -88,9 +88,6 @@ pub enum NixpkgsProblem { text: String, io_error: io::Error, }, - InternalCallPackageUsed { - attr_name: String, - }, MovedOutOfByName { package_name: String, call_package_path: Option, @@ -227,11 +224,6 @@ impl fmt::Display for NixpkgsProblem { subpath.display(), text, ), - NixpkgsProblem::InternalCallPackageUsed { attr_name } => - write!( - f, - "pkgs.{attr_name}: This attribute is defined using `_internalCallByNamePackageFile`, which is an internal function not intended for manual use.", - ), NixpkgsProblem::MovedOutOfByName { package_name, call_package_path, empty_arg } => { let call_package_arg = if let Some(path) = &call_package_path { diff --git a/pkgs/test/nixpkgs-check-by-name/tests/aliases/aliases.nix b/pkgs/test/nixpkgs-check-by-name/tests/aliases/aliases.nix new file mode 100644 index 000000000000..c70ec23478ea --- /dev/null +++ b/pkgs/test/nixpkgs-check-by-name/tests/aliases/aliases.nix @@ -0,0 +1,3 @@ +self: super: { + baz = self.foo; +} diff --git a/pkgs/test/nixpkgs-check-by-name/tests/aliases/all-packages.nix b/pkgs/test/nixpkgs-check-by-name/tests/aliases/all-packages.nix new file mode 100644 index 000000000000..2cdfcef08fa6 --- /dev/null +++ b/pkgs/test/nixpkgs-check-by-name/tests/aliases/all-packages.nix @@ -0,0 +1,3 @@ +self: super: { + bar = self.foo; +} diff --git a/pkgs/test/nixpkgs-check-by-name/tests/aliases/default.nix b/pkgs/test/nixpkgs-check-by-name/tests/aliases/default.nix new file mode 100644 index 000000000000..861260cdca4b --- /dev/null +++ b/pkgs/test/nixpkgs-check-by-name/tests/aliases/default.nix @@ -0,0 +1 @@ +import { root = ./.; } diff --git a/pkgs/test/nixpkgs-check-by-name/tests/aliases/pkgs/by-name/fo/foo/package.nix b/pkgs/test/nixpkgs-check-by-name/tests/aliases/pkgs/by-name/fo/foo/package.nix new file mode 100644 index 000000000000..a1b92efbbadb --- /dev/null +++ b/pkgs/test/nixpkgs-check-by-name/tests/aliases/pkgs/by-name/fo/foo/package.nix @@ -0,0 +1 @@ +{ someDrv }: someDrv diff --git a/pkgs/test/nixpkgs-check-by-name/tests/internalCallPackage/expected b/pkgs/test/nixpkgs-check-by-name/tests/internalCallPackage/expected deleted file mode 100644 index 404795ee5c79..000000000000 --- a/pkgs/test/nixpkgs-check-by-name/tests/internalCallPackage/expected +++ /dev/null @@ -1 +0,0 @@ -pkgs.foo: This attribute is defined using `_internalCallByNamePackageFile`, which is an internal function not intended for manual use. diff --git a/pkgs/test/nixpkgs-check-by-name/tests/mock-nixpkgs.nix b/pkgs/test/nixpkgs-check-by-name/tests/mock-nixpkgs.nix index 183f8ff2ae8d..81a9c916ac2d 100644 --- a/pkgs/test/nixpkgs-check-by-name/tests/mock-nixpkgs.nix +++ b/pkgs/test/nixpkgs-check-by-name/tests/mock-nixpkgs.nix @@ -77,12 +77,21 @@ let else [ ]; + # A list optionally containing the `aliases.nix` file from the test case as an overlay + # But only if config.allowAliases is not false + optionalAliasesOverlay = + if (config.allowAliases or true) && builtins.pathExists (root + "/aliases.nix") then + [ (import (root + "/aliases.nix")) ] + else + [ ]; + # All the overlays in the right order, including the user-supplied ones allOverlays = [ autoCalledPackages ] ++ optionalAllPackagesOverlay + ++ optionalAliasesOverlay ++ overlays; # Apply all the overlays in order to the base fixed-point function pkgsFun -- cgit 1.4.1