Skip to content

Improve flake code #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ in {
}) (__attrNames flake.devShells));
} // lib.optionalAttrs (flake ? hydraJobs) {
hydraJobs.${lib.removeSuffix ":" prefix} = flake.hydraJobs;
} // lib.optionalAttrs (flake ? ciJobs) {
ciJobs.${lib.removeSuffix ":" prefix} = flake.ciJobs;
};

# Used by `combineFlakes` to combine flakes together
Expand All @@ -393,4 +395,149 @@ in {
# one in the list will be used.
combineFlakes = sep: prefixAndFlakes: builtins.foldl' addFlakes {}
(lib.mapAttrsToList (prefix: flake: prefixFlake prefix sep flake) prefixAndFlakes);

# Make the CI jobs for running code coverage.
# `project` is the base project without code coverage enabled.
# `packages` is a selector function that indicates what packages
# we should run code coverage on (pass haskellLib.selectProjectPackages
# to run it on the packages).
# `coverageProjectModule` is applied to `project` and is useful for
# modifying the project settings when running code coverage (just
# pass `{}` if you do not need to modify anything).
# By default the `doCoverage` flag will be set for the packages
# selected by `packages`.
projectCoverageCiJobs = project: packages: coverageProjectModule:
let
packageNames = project: builtins.attrNames (packages project.hsPkgs);
coverageProject = project.appendModule [
coverageProjectModule
{
modules = [{
packages = lib.genAttrs (packageNames project)
(_: { doCoverage = lib.mkDefault true; });
}];
}
];
in
builtins.listToAttrs (lib.concatMap (packageName: [{
name = packageName;
value = coverageProject.hsPkgs.${packageName}.coverageReport;
}]) (packageNames coverageProject));

# Flake package names that are flat and match the cabal component names.
mkFlakePackages = haskellPackages: builtins.listToAttrs (
lib.concatLists (lib.mapAttrsToList (packageName: package:
lib.optional (package.components ? library)
{ name = "${packageName}:lib:${packageName}"; value = package.components.library; }
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:lib:${n}"; value = v; })
(package.components.sublibs)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:exe:${n}"; value = v; })
(package.components.exes)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = v; })
(package.components.tests)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:bench:${n}"; value = v; })
(package.components.benchmarks)
) haskellPackages));

# Flake package names that are flat and match the cabal component names.
mkFlakeApps = haskellPackages: builtins.listToAttrs (
lib.concatLists (lib.mapAttrsToList (packageName: package:
lib.mapAttrsToList (n: v:
{ name = "${packageName}:exe:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.exes)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.tests)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:benchmark:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.benchmarks)
) haskellPackages));

# Flatten the result of collectChecks or collectChecks' for use in flake `checks`
mkFlakeChecks = allChecks: builtins.listToAttrs (
lib.concatLists (lib.mapAttrsToList (packageName: checks:
# Avoid `recurseForDerivations` issues
lib.optionals (lib.isAttrs checks) (
lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = v; })
(lib.filterAttrs (_: v: lib.isDerivation v) checks))
) allChecks));

removeRecurseForDerivations = x:
let clean = builtins.removeAttrs x ["recurseForDerivations"];
in
if x.recurseForDerivations or false
then builtins.mapAttrs (_: removeRecurseForDerivations) clean
else clean;

mkFlakeCiJobs = project: {
packages
, checks
, coverage
, devShells
, checkedProject
}: {
# Run all the tests and code coverage
checks = removeRecurseForDerivations checks;
inherit
coverage
# Make sure all the packages build
packages
# Build and cache any tools in the `devShells`
devShells;
# Build tools and cache tools needed for the project
inherit (project) roots;
}
# Build the plan-nix and check it if materialized
// lib.optionalAttrs (checkedProject ? plan-nix) {
plan-nix = checkedProject.plan-nix;
}
# Build the stack-nix and check it if materialized
// lib.optionalAttrs (checkedProject ? stack-nix) {
stack-nix = checkedProject.stack-nix;
};

mkFlake = project: {
selectPackages ? haskellLib.selectProjectPackages
, haskellPackages ? selectPackages project.hsPkgs
, packages ? mkFlakePackages haskellPackages
, apps ? mkFlakeApps haskellPackages
, checks ? mkFlakeChecks (collectChecks' haskellPackages)
, coverage ? {}
, devShell ? project.shell
, devShells ? { default = devShell; }
, checkedProject ? project.appendModule { checkMaterialization = true; }
, ciJobs ? mkFlakeCiJobs project { inherit checks coverage packages devShells checkedProject; }
, hydraJobs ? ciJobs
}: {
inherit
# Used by:
# `nix build .#pkg-name:lib:pkg-name`
# `nix build .#pkg-name:lib:sublib-name`
# `nix build .#pkg-name:exe:exe-name`
# `nix build .#pkg-name:test:test-name`
packages
# Used by:
# `nix flake check`
checks
# `nix run .#pkg-name:exe:exe-name`
# `nix run .#pkg-name:test:test-name`
apps
# Used by hydra.
hydraJobs
# Like `hydraJobs` but with `${system}` first so that it the IFDs will not have
# to run for systems we are not testing (placement of `${system}` is done
# by `flake-utils.eachSystem` and it treats `hydraJobs` differently from
# the other flake attributes).
# See https://github.com/numtide/flake-utils/blob/04c1b180862888302ddfb2e3ad9eaa63afc60cf8/default.nix#L131-L134
ciJobs
# Used by:
# `nix develop`
devShells
devShell; # TODO remove devShell once everyone has nix that supports `devShells.default`
};
}
13 changes: 11 additions & 2 deletions modules/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@
```
'';
};
coverage = lib.mkOption {
doCoverage = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Specifies if the flake `ciJobs` and `hydraJobs` should include code
coverage reports.
'';
};
coverageProjectModule = lib.mkOption {
type = lib.types.unspecified;
default = {};
description = ''
Project module for use when generating coverage reports.
The project packages will have `doCoverage` by default.
The project packages will have `packages.X.doCoverage`
turned on by default.
'';
};
};
Expand Down
96 changes: 10 additions & 86 deletions overlays/haskell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -737,101 +737,25 @@ final: prev: {
"Invalid package component name ${componentName}. Expected package:ctype:component (where ctype is one of lib, flib, exe, test, or bench)";
(getPackage (builtins.elemAt m 0)).getComponent "${builtins.elemAt m 1}:${builtins.elemAt m 2}";

rawFlake =
let
packageNames = project: builtins.attrNames (project.args.flake.packages project.hsPkgs);
checkedProject = project.appendModule { checkMaterialization = true; };
in {
# Used by:
# `nix build .#pkg-name:lib:pkg-name`
# `nix build .#pkg-name:lib:sublib-name`
# `nix build .#pkg-name:exe:exe-name`
# `nix build .#pkg-name:test:test-name`
packages = builtins.listToAttrs (
final.lib.concatMap (packageName:
let package = project.hsPkgs.${packageName};
in final.lib.optional (package.components ? library)
{ name = "${packageName}:lib:${packageName}"; value = package.components.library; }
++ final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:lib:${n}"; value = v; })
(package.components.sublibs)
++ final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:exe:${n}"; value = v; })
(package.components.exes)
++ final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = v; })
(package.components.tests)
++ final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:bench:${n}"; value = v; })
(package.components.benchmarks)
) (packageNames project));
# Used by:
# `nix flake check`
checks = builtins.listToAttrs (
final.lib.concatMap (packageName:
let package = project.hsPkgs.${packageName};
in final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = v; })
(final.lib.filterAttrs (_: v: final.lib.isDerivation v) (package.checks))
) (packageNames project));
# `nix run .#pkg-name:exe:exe-name`
# `nix run .#pkg-name:test:test-name`
apps = builtins.listToAttrs (
final.lib.concatMap (packageName:
let package = project.hsPkgs.${packageName};
in final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:exe:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.exes)
++ final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.tests)
++ final.lib.mapAttrsToList (n: v:
{ name = "${packageName}:benchmark:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.benchmarks)
) (packageNames project));
# Used by hydra:
hydraJobs = {
checks = rawFlake.checks;
} // final.lib.optionalAttrs (checkedProject ? plan-nix) {
# Build the plan-nix and check it if materialized
plan-nix = checkedProject.plan-nix;
} // final.lib.optionalAttrs (checkedProject ? stack-nix) {
# Build the stack-nix and check it if materialized
stack-nix = checkedProject.stack-nix;
} // {
# Build tools and cache tools needed for the project
roots = project.roots;
coverage =
let
coverageProject = project.appendModule [
project.args.flake.coverage
{
modules = [{
packages = final.lib.genAttrs (packageNames project)
(_: { doCoverage = final.lib.mkDefault true; });
}];
}
];
in builtins.listToAttrs (final.lib.concatMap (packageName: [{
name = packageName;
value = coverageProject.hsPkgs.${packageName}.coverageReport;
}]) (packageNames coverageProject));
};
devShells.default = project.shell;
devShell = project.shell;
};
# Helper function that can be used to make a Nix Flake out of a project
# by including a flake.nix. See docs/tutorials/getting-started-flakes.md
# for an example flake.nix file.
# This flake function maps the build outputs to the flake `packages`,
# `checks` and `apps` output attributes.
flake' =
let
combinePrefix = a: b: if a == "default" then b else "${a}:${b}";
combinePrefix = a: b: if a == "default" then b else "${a}-${b}";
mkFlake = project: haskellLib.mkFlake project rec {
selectPackages = project.args.flake.packages;
coverage = final.lib.optionalAttrs project.args.flake.doCoverage
(haskellLib.projectCoverageCiJobs
project selectPackages project.args.flake.coverageProjectModule);
};
forAllCrossCompilers = prefix: project: (
[{ ${prefix} = project.rawFlake; }]
[{ ${prefix} = mkFlake project; }]
++ (map (project: {
${combinePrefix prefix project.pkgs.stdenv.hostPlatform.config} = project.rawFlake;
${combinePrefix prefix project.pkgs.stdenv.hostPlatform.config} =
mkFlake project;
})
(project.args.flake.crossPlatforms project.projectCross)
));
Expand Down