flake-utils/default.nix

190 lines
4.9 KiB
Nix
Raw Normal View History

2020-04-22 17:12:09 +02:00
let
2020-04-11 16:45:25 +02:00
# The list of systems supported by nixpkgs and hydra
2020-04-22 17:12:09 +02:00
defaultSystems = [
2020-04-11 16:45:25 +02:00
"aarch64-linux"
"aarch64-darwin"
2020-04-11 16:45:25 +02:00
"i686-linux"
"x86_64-darwin"
"x86_64-linux"
];
# List of all systems defined in nixpkgs
# Keep in sync with nixpkgs wit the following command:
# $ nix-instantiate --json --eval --expr "with import <nixpkgs> {}; lib.platforms.all" | jq
allSystems = [
"aarch64-linux"
"armv5tel-linux"
"armv6l-linux"
"armv7a-linux"
"armv7l-linux"
"mipsel-linux"
"i686-cygwin"
"i686-freebsd"
"i686-linux"
"i686-netbsd"
"i686-openbsd"
"x86_64-cygwin"
"x86_64-freebsd"
"x86_64-linux"
"x86_64-netbsd"
"x86_64-openbsd"
"x86_64-solaris"
"x86_64-darwin"
"i686-darwin"
"aarch64-darwin"
"armv7a-darwin"
"x86_64-windows"
"i686-windows"
"wasm64-wasi"
"wasm32-wasi"
"x86_64-redox"
"powerpc64le-linux"
"riscv32-linux"
"riscv64-linux"
"arm-none"
"armv6l-none"
"aarch64-none"
"avr-none"
"i686-none"
"x86_64-none"
"powerpc-none"
"msp430-none"
"riscv64-none"
"riscv32-none"
"vc4-none"
"js-ghcjs"
"aarch64-genode"
"x86_64-genode"
];
2020-04-22 17:12:09 +02:00
# eachSystem using defaultSystems
eachDefaultSystem = eachSystem defaultSystems;
# Builds a map from <attr>=value to <attr>.<system>=value for each system,
# except for the `hydraJobs` attribute, where it maps the inner attributes,
# from hydraJobs.<attr>=value to hydraJobs.<attr>.<system>=value.
2020-04-11 16:45:25 +02:00
#
2020-04-22 17:12:09 +02:00
eachSystem = systems: f:
let
# Taken from <nixpkgs/lib/attrsets.nix>
isDerivation = x: builtins.isAttrs x && x ? type && x.type == "derivation";
# Used to match Hydra's convention of how to define jobs. Basically transforms
#
# hydraJobs = {
# hello = <derivation>;
# haskellPackages.aeson = <derivation>;
# }
#
# to
#
# hydraJobs = {
# hello.x86_64-linux = <derivation>;
# haskellPackages.aeson.x86_64-linux = <derivation>;
# }
#
# if the given flake does `eachSystem [ "x86_64-linux" ] { ... }`.
pushDownSystem = system: merged:
builtins.mapAttrs
(name: value:
if ! (builtins.isAttrs value) then value
else if isDerivation value then (merged.${name} or {}) // { ${system} = value; }
else pushDownSystem system (merged.${name} or {}) value);
# Merge together the outputs for all systems.
2020-04-22 17:12:09 +02:00
op = attrs: system:
let
ret = f system;
2020-04-22 17:17:40 +02:00
op = attrs: key:
let
appendSystem = key: system: ret:
if key == "hydraJobs"
then (pushDownSystem system (attrs.hydraJobs or {}) ret.hydraJobs)
else { ${system} = ret.${key}; };
in attrs //
{
${key} = (attrs.${key} or { })
// (appendSystem key system ret);
}
2020-05-02 22:51:26 +02:00
;
2020-04-22 17:12:09 +02:00
in
builtins.foldl' op attrs (builtins.attrNames ret);
in
2020-05-02 22:51:26 +02:00
builtins.foldl' op { } systems
;
2020-04-11 19:29:17 +02:00
2020-07-22 10:38:25 +02:00
# Nix flakes insists on having a flat attribute set of derivations in
# various places like the `packages` and `checks` attributes.
#
# This function traverses a tree of attributes (by respecting
# recurseIntoAttrs) and only returns their derivations, with a flattened
# key-space.
#
# Eg:
#
# flattenTree { hello = pkgs.hello; gitAndTools = pkgs.gitAndTools };
#
# Returns:
#
# {
# hello = «derivation»;
# "gitAndTools/git" = «derivation»;
# "gitAndTools/hub" = «derivation»;
2020-07-22 10:38:25 +02:00
# # ...
# }
flattenTree = tree: import ./flattenTree.nix tree;
2020-07-22 10:42:18 +02:00
# 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 = import ./filterPackages.nix { inherit allSystems; };
2020-07-22 10:42:18 +02:00
# Returns the structure used by `nix app`
mkApp =
{ drv
, name ? drv.pname or drv.name
, exePath ? drv.passthru.exePath or "/bin/${name}"
}:
{
type = "app";
program = "${drv}${exePath}";
};
2020-07-22 10:38:25 +02:00
2020-08-23 15:28:05 +02:00
# 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;
2020-08-23 15:28:05 +02:00
lib = {
inherit
allSystems
check-utils
2020-08-23 15:28:05 +02:00
defaultSystems
eachDefaultSystem
eachSystem
2021-04-25 12:08:52 +02:00
filterPackages
flattenTree
2020-08-23 15:28:05 +02:00
mkApp
simpleFlake
;
};
in
lib