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 5 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
121 changes: 121 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ in {
# Useful for pre-filtered package-set.
collectChecks' = collectChecks (_: true);

# Flatten the result of collectChecks or collectChecks' for use in flake `checks`
flattenChecks = 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));

# Replacement for lib.cleanSourceWith that has a subDir argument.
inherit (import ./clean-source-with.nix { inherit lib; }) cleanSourceWith;

Expand Down Expand Up @@ -393,4 +403,115 @@ in {
# one in the list will be used.
combineFlakes = sep: prefixAndFlakes: builtins.foldl' addFlakes {}
(lib.mapAttrsToList (prefix: flake: prefixFlake prefix sep flake) prefixAndFlakes);

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));

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));

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));

mkFlakeCiJobs = project: {
checks
, coverage
, devShells
, checkedProject
}: {
# Run all the tests and code coverage
# Also build and cache any tools in the `devShells`
inherit checks coverage 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
, coverageProjectModule ? {}
, checks ? flattenChecks (collectChecks' haskellPackages)
, coverage ? projectCoverageCiJobs project selectPackages coverageProjectModule
, devShell ? project.shell
, devShells ? { default = devShell; }
, checkedProject ? project.appendModule { checkMaterialization = true; }
, ciJobs ? mkFlakeCiJobs project { inherit checks coverage 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`
};
}
2 changes: 1 addition & 1 deletion modules/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
```
'';
};
coverage = lib.mkOption {
coverageProjectModule = lib.mkOption {
type = lib.types.unspecified;
default = {};
description = ''
Expand Down
92 changes: 7 additions & 85 deletions overlays/haskell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -737,89 +737,6 @@ 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.
Expand All @@ -828,10 +745,15 @@ final: prev: {
flake' =
let
combinePrefix = a: b: if a == "default" then b else "${a}:${b}";
flakeArgs = {
selectPackages = project.args.flake.packages;
inherit (project.args.flake) coverageProjectModule;
};
forAllCrossCompilers = prefix: project: (
[{ ${prefix} = project.rawFlake; }]
[{ ${prefix} = haskellLib.mkFlake project flakeArgs; }]
++ (map (project: {
${combinePrefix prefix project.pkgs.stdenv.hostPlatform.config} = project.rawFlake;
${combinePrefix prefix project.pkgs.stdenv.hostPlatform.config} =
haskellLib.mkFlake project flakeArgs;
})
(project.args.flake.crossPlatforms project.projectCross)
));
Expand Down