From ac4c26755b1bb74f3d6b7ec6fa6070e7e725763e Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Sat, 10 Apr 2021 10:59:10 -0700 Subject: [PATCH] init pkgs-lib, move tests and shell there --- flake.nix | 2 +- lib/default.nix | 2 + lib/mkFlake/default.nix | 20 ++++------ lib/pkgs-lib/default.nix | 20 ++++++++++ {shell => lib/pkgs-lib/shell}/default.nix | 26 ++++++------- {shell => lib/pkgs-lib/shell}/devshell.toml | 0 {shell => lib/pkgs-lib/shell}/flk.nix | 0 {shell => lib/pkgs-lib/shell}/flk.sh | 0 {shell => lib/pkgs-lib/shell}/pre-commit.sh | 0 {tests => lib/pkgs-lib/tests}/default.nix | 39 ++++++++++++------- {tests => lib/pkgs-lib/tests}/lib.nix | 38 ++++++++---------- .../pkgs-lib/tests}/profiles/foo/default.nix | 0 .../pkgs-lib/tests}/profiles/t/default.nix | 0 lib/pkgs-lib/tests/testPathsIn/bar | 0 lib/pkgs-lib/tests/testPathsIn/baz | 0 lib/pkgs-lib/tests/testPathsIn/foo | 0 .../tests}/testPathsToImportedAttrs/bar.nix | 0 .../testPathsToImportedAttrs/dir/default.nix | 0 .../tests}/testPathsToImportedAttrs/f.nix | 0 .../tests}/testPathsToImportedAttrs/foo.nix | 0 .../tests}/testPathsToImportedAttrs/t.nix | 0 tests/README.md | 36 ----------------- 22 files changed, 82 insertions(+), 101 deletions(-) create mode 100644 lib/pkgs-lib/default.nix rename {shell => lib/pkgs-lib/shell}/default.nix (75%) rename {shell => lib/pkgs-lib/shell}/devshell.toml (100%) rename {shell => lib/pkgs-lib/shell}/flk.nix (100%) rename {shell => lib/pkgs-lib/shell}/flk.sh (100%) rename {shell => lib/pkgs-lib/shell}/pre-commit.sh (100%) rename {tests => lib/pkgs-lib/tests}/default.nix (60%) rename {tests => lib/pkgs-lib/tests}/lib.nix (69%) rename {tests => lib/pkgs-lib/tests}/profiles/foo/default.nix (100%) rename {tests => lib/pkgs-lib/tests}/profiles/t/default.nix (100%) create mode 100644 lib/pkgs-lib/tests/testPathsIn/bar create mode 100644 lib/pkgs-lib/tests/testPathsIn/baz create mode 100644 lib/pkgs-lib/tests/testPathsIn/foo rename {tests => lib/pkgs-lib/tests}/testPathsToImportedAttrs/bar.nix (100%) rename {tests => lib/pkgs-lib/tests}/testPathsToImportedAttrs/dir/default.nix (100%) rename {tests => lib/pkgs-lib/tests}/testPathsToImportedAttrs/f.nix (100%) rename {tests => lib/pkgs-lib/tests}/testPathsToImportedAttrs/foo.nix (100%) rename {tests => lib/pkgs-lib/tests}/testPathsToImportedAttrs/t.nix (100%) delete mode 100644 tests/README.md diff --git a/flake.nix b/flake.nix index dbb30bf3..fddbc850 100644 --- a/flake.nix +++ b/flake.nix @@ -30,7 +30,7 @@ outputs = inputs@{ deploy, nixos, nur, self, utils, ... }: let - lib = import ./lib { inherit self nixos inputs; }; + lib = import ./lib { inherit self nixos utils inputs; }; in lib.mkFlake { diff --git a/lib/default.nix b/lib/default.nix index 61fe6dd4..ee06a3c9 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -19,6 +19,8 @@ lib.makeExtensible (final: mkFlake = callLibs ./mkFlake; + pkgs-lib = callLibs ./pkgs-lib; + inherit (attrs) mapFilterAttrs genAttrs' safeReadDir pathsToImportedAttrs concatAttrs filterPackages; inherit (lists) pathsIn; diff --git a/lib/mkFlake/default.nix b/lib/mkFlake/default.nix index b2864a1a..700773f1 100644 --- a/lib/mkFlake/default.nix +++ b/lib/mkFlake/default.nix @@ -34,27 +34,21 @@ let systemOutputs = utils.lib.eachDefaultSystem (system: let pkgs = multiPkgs.${system}; + pkgs-lib = dev.pkgs-lib.${system}; # all packages that are defined in ./pkgs legacyPackages = os.mkPackages { inherit pkgs; }; in { - checks = - let - tests = nixos.lib.optionalAttrs (system == "x86_64-linux") - (import "${self}/tests" { inherit self pkgs; }); - deployHosts = nixos.lib.filterAttrs - (n: _: self.nixosConfigurations.${n}.config.nixpkgs.system == system) - self.deploy.nodes; - deployChecks = deploy.lib.${system}.deployChecks { nodes = deployHosts; }; - in - nixos.lib.recursiveUpdate tests deployChecks; + checks = pkgs-lib.tests.mkChecks { + inherit (self.deploy) nodes; + hosts = self.nixosConfigurations; + homes = self.homeConfigurations; + }; inherit legacyPackages; packages = dev.filterPackages system legacyPackages; - devShell = import "${self}/shell" { - inherit self system; - }; + devShell = pkgs-lib.shell; }); in outputs // systemOutputs diff --git a/lib/pkgs-lib/default.nix b/lib/pkgs-lib/default.nix new file mode 100644 index 00000000..807b44fc --- /dev/null +++ b/lib/pkgs-lib/default.nix @@ -0,0 +1,20 @@ +args@{ lib, dev, utils, nixos, ... }: +lib.genAttrs utils.lib.defaultSystems (system: + lib.makeExtensible (final: + let + pkgs = import nixos { inherit system; }; + callLibs = file: import file + (args // { + inherit pkgs system; + pkgs-lib = final; + }); + in + with final; + { + inherit callLibs; + + tests = callLibs ./tests; + shell = callLibs ./shell; + } + ) +) diff --git a/shell/default.nix b/lib/pkgs-lib/shell/default.nix similarity index 75% rename from shell/default.nix rename to lib/pkgs-lib/shell/default.nix index 0b132f99..ed78187c 100644 --- a/shell/default.nix +++ b/lib/pkgs-lib/shell/default.nix @@ -1,25 +1,23 @@ -{ self ? (import ../compat).defaultNix -, system ? builtins.currentSystem -, extern ? import ../extern { inherit (self) inputs; } -, overrides ? import ../overrides -}: +{ lib, dev, inputs, system, nixos, ... }: let - pkgs = (self.lib.os.mkPkgs { - inherit overrides extern; - }).${system}; + overlays = [ + inputs.devshell.overlay + (final: prev: { + deploy-rs = + inputs.deploy.packages.${prev.system}.deploy-rs; + }) + ]; - inherit (pkgs) lib; + pkgs = dev.os.pkgImport nixos overlays system; + + flk = pkgs.callPackage ./flk.nix { }; installPkgs = (lib.nixosSystem { inherit system; modules = [ ]; }).config.system.build; - - flk = pkgs.callPackage ./flk.nix { }; - in -pkgs.devshell.mkShell -{ +pkgs.devshell.mkShell { imports = [ (pkgs.devshell.importTOML ./devshell.toml) ]; packages = with installPkgs; [ diff --git a/shell/devshell.toml b/lib/pkgs-lib/shell/devshell.toml similarity index 100% rename from shell/devshell.toml rename to lib/pkgs-lib/shell/devshell.toml diff --git a/shell/flk.nix b/lib/pkgs-lib/shell/flk.nix similarity index 100% rename from shell/flk.nix rename to lib/pkgs-lib/shell/flk.nix diff --git a/shell/flk.sh b/lib/pkgs-lib/shell/flk.sh similarity index 100% rename from shell/flk.sh rename to lib/pkgs-lib/shell/flk.sh diff --git a/shell/pre-commit.sh b/lib/pkgs-lib/shell/pre-commit.sh similarity index 100% rename from shell/pre-commit.sh rename to lib/pkgs-lib/shell/pre-commit.sh diff --git a/tests/default.nix b/lib/pkgs-lib/tests/default.nix similarity index 60% rename from tests/default.nix rename to lib/pkgs-lib/tests/default.nix index 3c2539ab..323d86d2 100644 --- a/tests/default.nix +++ b/lib/pkgs-lib/tests/default.nix @@ -1,17 +1,29 @@ -{ self, pkgs }: +{ pkgs-lib, pkgs, system, inputs, nixos, lib, ... }: let - inherit (self.inputs) nixos; - inherit (self.nixosConfigurations.NixOS.config.lib) testModule specialArgs; + mkChecks = { hosts, nodes, homes ? { } }: + let + deployHosts = lib.filterAttrs + (n: _: hosts.${n}.config.nixpkgs.system == system) + nodes; + deployChecks = inputs.deploy.lib.${system}.deployChecks { nodes = deployHosts; }; + tests = + { libTests = libTests; } + // lib.optionalAttrs (deployHosts != { }) { + profilesTest = profilesTest (hosts.${(builtins.head (builtins.attrNames deployHosts))}); + } // lib.mapAttrs (n: v: v.home.activationPackage) homes; - mkTest = + in + lib.recursiveUpdate tests deployChecks; + + mkTest = host: let nixosTesting = (import "${nixos}/nixos/lib/testing-python.nix" { - inherit (pkgs.stdenv.hostPlatform) system; - inherit specialArgs; + inherit system; + inherit (host.config.lib) specialArgs; inherit pkgs; extraConfigurations = [ - testModule + host.config.lib.testModule ]; }); in @@ -27,9 +39,8 @@ let else loadedTest; in nixosTesting.makeTest calledTest; -in -{ - profilesTest = mkTest { + + profilesTest = host: mkTest host { name = "profiles"; machine = { suites, ... }: { @@ -41,14 +52,12 @@ in ''; }; - homeTest = self.homeConfigurations."nixos@NixOS".home.activationPackage; - libTests = pkgs.runCommandNoCC "devos-lib-tests" { buildInputs = [ pkgs.nix ( - let tests = import ./lib.nix { inherit self pkgs; }; + let tests = pkgs-lib.callLibs ./lib.nix; in if tests == [ ] then null @@ -70,5 +79,5 @@ in touch $out ''; -} - +in +{ inherit mkTest libTests profilesTest mkChecks; } diff --git a/tests/lib.nix b/lib/pkgs-lib/tests/lib.nix similarity index 69% rename from tests/lib.nix rename to lib/pkgs-lib/tests/lib.nix index e2151571..68baa9f5 100644 --- a/tests/lib.nix +++ b/lib/pkgs-lib/tests/lib.nix @@ -1,6 +1,5 @@ -{ self, pkgs }: -let inherit (self.inputs.nixos) lib; in -with self.lib; +{ pkgs, lib, dev, ... }: +with dev; lib.runTests { testConcatAttrs = { expr = concatAttrs [{ foo = 1; } { bar = 2; } { baz = 3; }]; @@ -28,29 +27,24 @@ lib.runTests { expected = { foobar = 1; }; }; - testPathsIn = - let testPaths = pkgs.runCommandNoCC "test-paths-in" { } '' - mkdir -p $out/{foo,bar,baz} - ''; - in - { - expr = pathsIn testPaths; + testPathsIn = { + expr = pathsIn (toString ./testPathsIn); - expected = [ - "${testPaths}/bar" - "${testPaths}/baz" - "${testPaths}/foo" - ]; - }; + expected = map toString [ + ./testPathsIn/bar + ./testPathsIn/baz + ./testPathsIn/foo + ]; + }; testPathsToImportedAttrs = { expr = pathsToImportedAttrs [ - "${self}/tests/testPathsToImportedAttrs/dir" - "${self}/tests/testPathsToImportedAttrs/foo.nix" - "${self}/tests/testPathsToImportedAttrs/bar.nix" - "${self}/tests/testPathsToImportedAttrs/t.nix" - "${self}/tests/testPathsToImportedAttrs/f.nix" + (toString ./testPathsToImportedAttrs/dir) + ./testPathsToImportedAttrs/foo.nix + ./testPathsToImportedAttrs/bar.nix + ./testPathsToImportedAttrs/t.nix + ./testPathsToImportedAttrs/f.nix ]; expected = { @@ -70,7 +64,7 @@ lib.runTests { ]; testSafeReadDir = { - expr = safeReadDir "${self}/tests/profiles" // safeReadDir "${self}/nonexistentdir"; + expr = safeReadDir ./profiles // safeReadDir ./nonexistentdir; expected = { foo = "directory"; t = "directory"; diff --git a/tests/profiles/foo/default.nix b/lib/pkgs-lib/tests/profiles/foo/default.nix similarity index 100% rename from tests/profiles/foo/default.nix rename to lib/pkgs-lib/tests/profiles/foo/default.nix diff --git a/tests/profiles/t/default.nix b/lib/pkgs-lib/tests/profiles/t/default.nix similarity index 100% rename from tests/profiles/t/default.nix rename to lib/pkgs-lib/tests/profiles/t/default.nix diff --git a/lib/pkgs-lib/tests/testPathsIn/bar b/lib/pkgs-lib/tests/testPathsIn/bar new file mode 100644 index 00000000..e69de29b diff --git a/lib/pkgs-lib/tests/testPathsIn/baz b/lib/pkgs-lib/tests/testPathsIn/baz new file mode 100644 index 00000000..e69de29b diff --git a/lib/pkgs-lib/tests/testPathsIn/foo b/lib/pkgs-lib/tests/testPathsIn/foo new file mode 100644 index 00000000..e69de29b diff --git a/tests/testPathsToImportedAttrs/bar.nix b/lib/pkgs-lib/tests/testPathsToImportedAttrs/bar.nix similarity index 100% rename from tests/testPathsToImportedAttrs/bar.nix rename to lib/pkgs-lib/tests/testPathsToImportedAttrs/bar.nix diff --git a/tests/testPathsToImportedAttrs/dir/default.nix b/lib/pkgs-lib/tests/testPathsToImportedAttrs/dir/default.nix similarity index 100% rename from tests/testPathsToImportedAttrs/dir/default.nix rename to lib/pkgs-lib/tests/testPathsToImportedAttrs/dir/default.nix diff --git a/tests/testPathsToImportedAttrs/f.nix b/lib/pkgs-lib/tests/testPathsToImportedAttrs/f.nix similarity index 100% rename from tests/testPathsToImportedAttrs/f.nix rename to lib/pkgs-lib/tests/testPathsToImportedAttrs/f.nix diff --git a/tests/testPathsToImportedAttrs/foo.nix b/lib/pkgs-lib/tests/testPathsToImportedAttrs/foo.nix similarity index 100% rename from tests/testPathsToImportedAttrs/foo.nix rename to lib/pkgs-lib/tests/testPathsToImportedAttrs/foo.nix diff --git a/tests/testPathsToImportedAttrs/t.nix b/lib/pkgs-lib/tests/testPathsToImportedAttrs/t.nix similarity index 100% rename from tests/testPathsToImportedAttrs/t.nix rename to lib/pkgs-lib/tests/testPathsToImportedAttrs/t.nix diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 4b2c36e9..00000000 --- a/tests/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# Testing - -Testing is always an important aspect of any software development project, and -NixOS offers some incredibly powerful tools to write tests for your -configuration, and, optionally, run them in -[CI](../doc/integrations/hercules.md). - -## Lib Tests -You can easily write tests for your own library functions in the -___tests/lib.nix___ file and they will be run on every `nix flake check` or -during a CI run. - -## Unit Tests -Unit tests are can be created from regular derivations, and they can do -almost anything you can imagine. By convention, it is best to test your -packages during their [check phase][check]. All packages and their tests will -be built during CI. - -## Integration Tests -You can write integration tests for one or more NixOS VMs that can, -optionally, be networked together, and yes, it's as awesome as it sounds! - -Be sure to use the `mkTest` function, in the [___tests/default.nix___][default] -which wraps the official [testing-python][testing-python] function to ensure -that the system is setup exactly as it is for a bare DevOS system. There are -already great resources for learning how to use these tests effectively, -including the official [docs][test-doc], a fantastic [blog post][test-blog], -and the examples in [nixpkgs][nixos-tests]. - -[test-doc]: https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests -[test-blog]: https://www.haskellforall.com/2020/11/how-to-use-nixos-for-lightweight.html -[default]: https://github.com/divnix/devos/tree/core/tests/default.nix -[run-test]: https://github.com/NixOS/nixpkgs/blob/6571462647d7316aff8b8597ecdf5922547bf365/lib/debug.nix#L154-L166 -[nixos-tests]: https://github.com/NixOS/nixpkgs/tree/master/nixos/tests -[testing-python]: https://github.com/NixOS/nixpkgs/tree/master/nixos/lib/testing-python.nix -[check]: https://nixos.org/manual/nixpkgs/stable/#ssec-check-phase