Skip to content

Commit

Permalink
refactor flake (#362)
Browse files Browse the repository at this point in the history
* refactor: inner let for function used only once

* refactor: remove trivial helper function

it obscured what's really happening.

also skip an unnecessary import.

* formatting

* remove unnecessary recursive update

those namespaces are disjoint, so there's no need to obscure that they
are simply joined.

* refactor: localise filter for broken packages

the only point where we need it is for the checks.

also make the naming a bit less noisy.

* refactor: uniform directory imports

* refactor: group infrastructure checks
  • Loading branch information
fricklerhandwerk committed Sep 19, 2024
1 parent 4a56747 commit 6f30c8f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 67 deletions.
69 changes: 29 additions & 40 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
recursiveUpdate
nameValuePair
filterAttrs
attrByPath
;

inherit
Expand All @@ -65,15 +64,12 @@
}
// args);

# NGI packages are imported from ./pkgs/by-name/default.nix.
importNgiPackages = pkgs:
overlay = final: prev:
import ./pkgs/by-name {
inherit (pkgs) lib;
inherit dream2nix pkgs;
pkgs = prev;
inherit lib dream2nix;
};

overlay = final: prev: importNgiPackages prev;

# NGI projects are imported from ./projects/default.nix.
# Each project includes packages, and optionally, modules, examples and tests.

Expand Down Expand Up @@ -140,20 +136,12 @@
eachDefaultSystemOutputs = flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
overlay
];
overlays = [overlay];
};

ngiPackages = importNgiPackages pkgs;

# Dream2nix is failing to pass through the meta attribute set.
# As a workaround, consider packages with empty meta as non-broken.
nonBrokenNgiPackages = filterAttrs (_: v: !(attrByPath ["meta" "broken"] false v)) ngiPackages;
ngipkgs = import ./pkgs/by-name {inherit pkgs lib dream2nix;};

ngiProjects = importNgiProjects (pkgs // ngiPackages);

nonBrokenNgiProjects = importNgiProjects (pkgs // nonBrokenNgiPackages);
ngiProjects = importNgiProjects (pkgs // ngipkgs);

optionsDoc = pkgs.nixosOptionsDoc {
options =
Expand All @@ -180,7 +168,7 @@
};

packages =
ngiPackages
ngipkgs
// {
overview = import ./overview {
inherit lib pkgs self;
Expand Down Expand Up @@ -217,28 +205,28 @@
(checksForNixosTests projectName (project.nixos.tests or {}))
// (checksForNixosExamples projectName (project.nixos.examples or {}));

checksForAllProjects =
concatMapAttrs
checksForProject
nonBrokenNgiProjects;

checksForPackageDerivation = packageName: package: {"packages/${packageName}" = package;};

checksForPackagePassthruTests = packageName: tests: (concatMapAttrs (passthruName: test: {"packages/${packageName}/passthru/${passthruName}" = test;}) tests);
checksForPackagePassthruTests = packageName: tests:
concatMapAttrs
(passthruName: test: {"packages/${packageName}/passthru/${passthruName}" = test;})
tests;

checksForPackage = packageName: package:
(checksForPackageDerivation packageName package)
// (checksForPackagePassthruTests packageName (package.passthru.tests or {}));

checksForAllPackages =
concatMapAttrs
checksForPackage
nonBrokenNgiPackages;
in
checksForAllProjects
// checksForAllPackages
// {
pre-commit = pre-commit-hooks.lib.${system}.run {
# everything must evaluate for checks to run
nonBrokenPackages = filterAttrs (_: v: ! v.meta.broken or false) ngipkgs;

checksForAllProjects =
concatMapAttrs checksForProject
(importNgiProjects (pkgs // nonBrokenPackages));

checksForAllPackages = concatMapAttrs checksForPackage nonBrokenPackages;

checksForInfrastructure = {
"infra/pre-commit" = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
actionlint.enable = true;
Expand All @@ -248,9 +236,13 @@
"infra/makemake" = toplevel self.nixosConfigurations.makemake;
"infra/overview" = self.packages.${system}.overview;
};
in
checksForInfrastructure
// checksForAllProjects
// checksForAllPackages;

devShells.default = pkgs.mkShell {
inherit (checks.pre-commit) shellHook;
inherit (checks."infra/pre-commit") shellHook;
buildInputs = checks.pre-commit.enabledPackages;
};

Expand All @@ -259,7 +251,7 @@
text = ''
# shellcheck disable=all
shell-hook () {
${checks.pre-commit.shellHook}
${checks."infra/pre-commit".shellHook}
}
shell-hook
Expand All @@ -268,8 +260,5 @@
};
});
in
foldr recursiveUpdate {} [
eachDefaultSystemOutputs
systemAgnosticOutputs
];
eachDefaultSystemOutputs // systemAgnosticOutputs;
}
27 changes: 16 additions & 11 deletions pkgs/by-name/default.nix
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
pkgs,
lib,
dream2nix,
pkgs,
}: let
baseDirectory = ./.;

inherit
(builtins)
elem
pathExists
readDir
;

inherit
(lib.attrsets)
mapAttrs
concatMapAttrs
mapAttrs
;

names = name: type:
if type != "directory"
then assert name == "README.md" || name == "default.nix"; {}
else {${name} = baseDirectory + "/${name}";};
baseDirectory = ./.;

packageDirectories = concatMapAttrs names (readDir baseDirectory);
packageDirectories = let
names = name: type:
if type == "directory"
then {${name} = baseDirectory + "/${name}";}
# nothing else should be kept in this directory reserved for derivations
else assert elem name allowedFiles; {};
allowedFiles = ["README.md" "default.nix"];
in
concatMapAttrs names (readDir baseDirectory);

callModule = module: let
evaluated = lib.evalModules {
specialArgs = {
dream2nix = import dream2nix;
inherit dream2nix;
packageSets.nixpkgs = pkgs;
};
modules = [
Expand All @@ -48,7 +52,8 @@
);

self =
mapAttrs (
mapAttrs
(
_: directory:
if pathExists (directory + "/package.nix")
then callPackage (directory + "/package.nix") {}
Expand Down
34 changes: 18 additions & 16 deletions projects/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@

inherit
(lib.attrsets)
concatMapAttrs
filterAttrs
mapAttrs
recursiveUpdate
filterAttrs
;

baseDirectory = ./.;

allowedFiles = ["README.md" "default.nix"];

isMarkedBroken = project: project.broken or false;

filter = name: type:
if type != "directory"
then assert elem name allowedFiles; false
else true;
projectDirectories = let
names = name: type:
if type == "directory"
then {${name} = baseDirectory + "/${name}";}
# nothing else should be kept in this directory reserved for projects
else assert elem name allowedFiles; {};
allowedFiles = ["README.md" "default.nix"];
in
concatMapAttrs names (readDir baseDirectory);

nixosTest = test: let
# Amenities for interactive tests
Expand All @@ -49,13 +51,13 @@
project
{nixos.tests = mapAttrs (_: nixosTest) project.nixos.tests or {};};
in
mapAttrs (
name: type: let
project = import (baseDirectory + "/${name}") {
inherit lib pkgs sources;
};
mapAttrs
(
name: directory: let
project = import directory {inherit lib pkgs sources;};
in
if isMarkedBroken project
if project.broken or false
then trace "Skipping project '${name}' which is marked as broken for system '${pkgs.system or "undefined"}'." {}
else hydrate project
) (filterAttrs filter (readDir baseDirectory))
)
projectDirectories

0 comments on commit 6f30c8f

Please sign in to comment.