From b9630e7d64325b636943157c41b486bc66413766 Mon Sep 17 00:00:00 2001 From: Jacek Galowicz Date: Tue, 10 Dec 2024 06:45:37 +0000 Subject: [PATCH] Add overlay --- doc/getting-started.md | 38 ++++++++++++++++++++++++++++++++++++++ fedora/default.nix | 2 +- flake.lock | 33 +++++++++++++++++++++++++++++++++ flake.nix | 21 ++++++++++++++++----- overlay.nix | 17 +++++++++++++++++ tests/debian.nix | 4 ++-- tests/fedora.nix | 4 ++-- tests/ubuntu.nix | 4 ++-- 8 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 overlay.nix diff --git a/doc/getting-started.md b/doc/getting-started.md index 974d617..6896352 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -270,3 +270,41 @@ $ ./result/bin/test-driver ``` on your CI. + +## Using the Overlay + +If you would like to use this testing library with your own nixpkgs, use our +overlay function. + +The overlay function makes all linux distributions available under the attribute +`pkgs.testers.legacyDistros...`: + +```nix +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + nix-vm-test.url = "github:numtide/nix-vm-test"; + }; + + outputs = { self, nixpkgs, nix-vm-test }: + let + system = "x86_64-linux"; + pkgs = import nixpkgs { + inherit system; + overlays = [ + nix-vm-test.overlays.default + ]; + }; + in + packages.x86_64-linux.mytest = pkgs.testers.legacyDistros.debian."13" { + sharedDirs.debDir = { + source = "${./.}"; + target = "/mnt/debdir"; + }; + testScript = '' + vm.wait_for_unit("multi-user.target") + vm.succeed("apt-get -yq install /mnt/debdir/hello.deb") + ''; + }; +} +``` diff --git a/fedora/default.nix b/fedora/default.nix index dcd56ee..dbebb85 100644 --- a/fedora/default.nix +++ b/fedora/default.nix @@ -5,7 +5,7 @@ let inherit (image) hash; url = "https://download.fedoraproject.org/pub/fedora/linux/releases/${image.name}"; }; - images = lib.mapAttrs (k: v: fetchImage v) imagesJSON.${system}; + images = lib.mapAttrs (k: v: fetchImage v) (imagesJSON.${system} or {}); makeVmTestForImage = image: { testScript, sharedDirs, diskSize ? null, extraPathsToRegister ? [ ]}: generic.makeVmTest { inherit system testScript sharedDirs; image = prepareFedoraImage { diff --git a/flake.lock b/flake.lock index 419cf1d..b223862 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,22 @@ { "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "id": "flake-utils", + "type": "indirect" + } + }, "nixpkgs": { "locked": { "lastModified": 1708475490, @@ -18,8 +35,24 @@ }, "root": { "inputs": { + "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index dc0a29d..6d4235e 100644 --- a/flake.nix +++ b/flake.nix @@ -5,11 +5,22 @@ nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; }; - outputs = { self, nixpkgs, ... }: rec { - lib.x86_64-linux = import ./lib.nix { - inherit nixpkgs; + outputs = { self, nixpkgs, flake-utils }: + let system = "x86_64-linux"; + pkgs = import nixpkgs { + overlays = [ self.overlays.default ]; + localSystem = system; + }; + in + { + lib.${system} = pkgs.testers.legacyDistros; + + checks.${system} = import ./tests { + package = pkgs.testers.legacyDistros; + inherit pkgs system; + }; + + overlays.default = import ./overlay.nix; }; - checks.x86_64-linux = import ./tests { package = lib; pkgs = nixpkgs.legacyPackages.x86_64-linux; system = "x86_64-linux"; }; - }; } diff --git a/overlay.nix b/overlay.nix new file mode 100644 index 0000000..b2c19e8 --- /dev/null +++ b/overlay.nix @@ -0,0 +1,17 @@ +final: prev: + +let + inherit (prev) system; + generic = import ./generic { inherit (prev) lib; pkgs = final; nixpkgs = prev.path; }; + ubuntu = prev.callPackage ./ubuntu { inherit generic system; }; + debian = prev.callPackage ./debian { inherit generic system; }; + fedora = prev.callPackage ./fedora { inherit generic system; }; +in + +{ + testers = prev.testers or { } // { + legacyDistros = { + inherit debian ubuntu fedora; + }; + }; +} diff --git a/tests/debian.nix b/tests/debian.nix index 6b022c2..2e38715 100644 --- a/tests/debian.nix +++ b/tests/debian.nix @@ -1,6 +1,6 @@ { pkgs, package, system }: let - lib = package.${system}; + lib = package; multiUserTest = runner: (runner { sharedDirs = {}; testScript = '' @@ -49,4 +49,4 @@ in { }).sandboxed; } // runTestOnEveryImage multiUserTest // -package.${system}.debian.images +package.debian.images diff --git a/tests/fedora.nix b/tests/fedora.nix index 714880e..945de62 100644 --- a/tests/fedora.nix +++ b/tests/fedora.nix @@ -1,6 +1,6 @@ { pkgs, package, system }: let - lib = package.${system}; + lib = package; multiUserTest = runner: (runner { sharedDirs = {}; testScript = '' @@ -49,4 +49,4 @@ in { }).sandboxed; } // runTestOnEveryImage multiUserTest // -package.${system}.fedora.images +package.fedora.images diff --git a/tests/ubuntu.nix b/tests/ubuntu.nix index 295833c..da88554 100644 --- a/tests/ubuntu.nix +++ b/tests/ubuntu.nix @@ -1,7 +1,7 @@ { pkgs, package, system }: let - lib = package.${system}; + lib = package; multiUserTest = runner: (runner { sharedDirs = {}; testScript = '' @@ -50,5 +50,5 @@ in { }).sandboxed; } -// package.${system}.ubuntu.images +// package.ubuntu.images // runTestOnEveryImage multiUserTest