diff --git a/check-utils.nix b/check-utils.nix new file mode 100644 index 0000000..809d3ed --- /dev/null +++ b/check-utils.nix @@ -0,0 +1,22 @@ +systemOrPkgs: +let + str = it: if it == null then "null" else (toString it); + system = systemOrPkgs.system or systemOrPkgs; + test = name: command: derivation { + inherit name system; + builder = "/bin/sh"; + args = [ "-c" command ]; + }; +in +{ + + isEqual = a: b: + if a == b + then test "SUCCESS__${str a}__IS_EQUAL__${str b}" "echo success > $out" + else test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1"; + + hasKey = attrset: key: + if attrset ? ${str key} + then test "SUCCESS__${str key}__EXISTS_IN_ATTRSET" "echo success > $out" + else test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str(builtins.length (builtins.attrNames attrset))}" "exit 1"; +} diff --git a/default.nix b/default.nix index f81a59c..f61aed2 100644 --- a/default.nix +++ b/default.nix @@ -135,14 +135,18 @@ let # This function tries to capture a common flake pattern. simpleFlake = import ./simpleFlake.nix { inherit lib; }; + # Helper functions for Nix evaluation + check-utils = import ./check-utils.nix; + lib = { inherit allSystems + check-utils defaultSystems eachDefaultSystem eachSystem - flattenTree filterPackages + flattenTree mkApp simpleFlake ; diff --git a/examples/checks-utils/flake.nix b/examples/checks-utils/flake.nix new file mode 100644 index 0000000..4b16e1c --- /dev/null +++ b/examples/checks-utils/flake.nix @@ -0,0 +1,29 @@ +{ + description = "Flake utils demo"; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + inherit (flake-utils.lib.check-utils system) isEqual hasKey; + + testDataset = { key1 = "value1"; key2 = "value2"; key3 = "value3"; }; + in + rec { + checks = { + valid_key1 = isEqual testDataset.key1 "value1"; + contains_key2 = hasKey testDataset "key2"; + + failing_valid_key1 = isEqual testDataset.key1 "failing-data"; + failing_contains_key2 = hasKey testDataset "failing-data"; + + number_formatting_isEqual = isEqual testDataset.key1 123; + number_formatting_hasKey = hasKey testDataset 123; + + null_formatting_key1 = isEqual testDataset.key1 null; + null_formatting_hasKey = hasKey testDataset null; + }; + } + ); +} diff --git a/filterPackages.nix b/filterPackages.nix index 6cd935d..5bd4eba 100644 --- a/filterPackages.nix +++ b/filterPackages.nix @@ -6,11 +6,12 @@ let nameValuePair = name: value: { inherit name value; }; filterAttrs = pred: set: listToAttrs ( - concatMap (name: - let v = set.${name}; in - if pred name v then [(nameValuePair name v)] else [] - ) - (attrNames set) + concatMap + (name: + let v = set.${name}; in + if pred name v then [ (nameValuePair name v) ] else [ ] + ) + (attrNames set) ); # Everything that nix flake check requires for the packages output @@ -22,9 +23,9 @@ let isBroken = meta.broken or false; platforms = meta.hydraPlatforms or meta.platforms or allSystems; in - # check for isDerivation, so this is independently useful of + # check for isDerivation, so this is independently useful of # flattenTree, which also does filter on derviations - isDerivation v && !isBroken && builtins.elem system platforms + isDerivation v && !isBroken && builtins.elem system platforms ; in filterAttrs sieve packages