Skip to content

Commit

Permalink
imp: filterPackages (#28)
Browse files Browse the repository at this point in the history
* imp: filterPackages

* Update filterPackages.nix

* Update filterPackages.nix

Co-authored-by: Jonas Chevalier <[email protected]>
  • Loading branch information
David Arnold and zimbatm authored Apr 12, 2021
1 parent b2c27d1 commit c6169a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ let
# }
flattenTree = tree: import ./flattenTree.nix tree;

# Nix check functionality validates packages for various conditions, like if
# they build for any given platform or if they are marked broken.
#
# This function filters a flattend package set for conditinos that
# would *trivially* break `nix flake check`. It does not flatten a tree and it
# does not implement advanced package validation checks.
#
# Eg:
#
# filterPackages "x86_64-linux" {
# hello = pkgs.hello;
# "gitAndTools/git" = pkgs.gitAndTools // {meta.broken = true;};
# };
#
# Returns:
#
# {
# hello = «derivation»;
# }
filterPackages = system: packages: import ./filterPackages.nix system packages;

# Returns the structure used by `nix app`
mkApp =
{ drv
Expand Down
28 changes: 28 additions & 0 deletions filterPackages.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
system: packages:
let
# Adopted from nixpkgs.lib
inherit (builtins) listToAttrs concatMap attrName;
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)
);

# Everything that nix flake check requires for the packages output
sieve = n: v:
with v;
let
inherit (builtins) isAttrs;
isDerivation = x: isAttrs x && x ? type && x.type == "derivation";
platforms = meta.hydraPlatforms or meta.platforms or [ ];
in
# check for isDerivation, so this is independently useful of
# flattenTree, which also does filter on derviations
isDerivation v && !meta.broken && builtins.elem system platforms
;
in
filterAttrs sieve packages

0 comments on commit c6169a2

Please sign in to comment.