Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ source by.
| `disko` | `disko` | disk layouts written as disko reads them, whose machines import disko's NixOS module, so disko's command, `disko-install` and its image builder work on them |
| `caisson-facter` | none | the hardware of a machine, a nixos-facter report checked against the report versions caisson knows and extended with a project's own options, with the NixOS configurations that run on it declared inside it; a machine gets the report through nixpkgs' `hardware.facter` |
| `facter` | none | a `facter.json` as nixos-facter writes it, with the NixOS configurations that run on it declared inside it; a machine gets it as `hardware.facter.reportPath` |
| `treefmt` | `treefmt-nix` | the formatters of a tree, as treefmt-nix modules; the configuration named `default` is published as the `formatter.<system>` that `nix fmt` runs |
| `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads |
| `structural` | none | a configuration that only exports and holds configurations beneath it: the top of a repository whose point is what it exports, such as caisson itself, or a layer at any depth |

Expand Down
1 change: 1 addition & 0 deletions configs/structural/impl/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
darwin
terranix
system-manager
treefmt
;
};

Expand Down
2 changes: 1 addition & 1 deletion docs/development/repository-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The layer users reach for: the integrations (`structural`,
`flake-parts`, `nixpkgs`, `nixos`, `nixos-minimal`, `home-manager`,
`home-manager-minimal`, `darwin`, `hjem`, `caisson-colmena`, `colmena`,
`caisson-disko`, `disko`, `caisson-facter`, `facter`, `terranix`,
`system-manager`), each
`system-manager`, `treefmt`), each
a library overlay
contributing its `lib.caisson` namespace, registering its module
class where it has a class (`nixos-minimal` and `home-manager-minimal`
Expand Down
79 changes: 68 additions & 11 deletions lib-overlays/integrations/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,18 @@
# declared under: a function of `{ name, manifest }`, the name
# the configuration is passed up under and its manifest,
# returning `value`, the name to publish it under, and
# optionally `description`, a sentence saying what it did. It is
# recorded on the manifest, and applied where the configuration
# is passed up (`entriesOf`). Null for a configuration that is
# not published under a name.
# optionally `description`, a sentence saying what it did. It may
# carry `single`, for an output that holds the one configuration
# named `default`, and `bySystem`, for an output keyed by the
# system of each evaluation first (`packages.<system>.<name>`). It
# may carry `only`, a function of the manifest that says whether
# the configuration is published there at all; a configuration it
# leaves out is still passed up.
# `exportsTo` may also be a list of such exports, for an
# integration whose configurations are published in more than
# one output. It is recorded on the manifest, and applied where
# the configuration is passed up (`entriesOf`). Null for a
# configuration that is not published under a name.
mkModuleConfiguration =
{
type,
Expand Down Expand Up @@ -591,10 +599,25 @@

isManifest = value: builtins.isAttrs value && (value._type or null) == "caisson-manifest";

# How a configuration is published: the `exportsTo` its
# manifest records, or null for a configuration that is not
# published under a name (structural, flake-parts).
exportsToOf = manifest: manifest.exportsTo or null;
# How a configuration is published: the exports its manifest
# records in `exportsTo`, as a list, empty for a configuration
# that is not published under a name (structural, flake-parts).
# An integration may give one export or a list of them; an export
# with `only` applies to the configurations whose manifest it
# holds true for.
exportsOf =
manifest:
let
recorded = manifest.exportsTo or null;
all =
if recorded == null then
[ ]
else if builtins.isList recorded then
recorded
else
[ recorded ];
in
builtins.filter (exportsTo: (exportsTo.only or (_: true)) manifest) all;

showPath =
path:
Expand All @@ -606,9 +629,8 @@
# its path, on which the name its `exportsTo` gives, where it
# gives one, stands in place of that name.
entryOf =
prefix: manifest:
prefix: manifest: exportsTo:
let
exportsTo = exportsToOf manifest;
leaf = last prefix;
named =
if exportsTo ? name then
Expand All @@ -623,6 +645,8 @@
inherit manifest;
inherit (exportsTo) attrset;
single = exportsTo.single or false;
bySystem = exportsTo.bySystem or false;
system = manifest.system or null;
value = exportsTo.value manifest;
path = init prefix ++ [
(
Expand Down Expand Up @@ -696,7 +720,7 @@
in
builtins.concatMap (
{ prefix, manifest }:
(if exportsToOf manifest == null then [ ] else [ (entryOf prefix manifest) ])
builtins.map (entryOf prefix manifest) (exportsOf manifest)
++ builtins.map (entry: entry // { path = prefix ++ entry.path; }) (
manifest.outputs.exports.configurations or [ ]
)
Expand All @@ -716,6 +740,12 @@
# of the entry named `default`, the entry whose `exportsTo` says
# `single`. Any other entry for that output is refused, since the
# output has no names to hold it under.
#
# An output whose entries' `exportsTo` says `bySystem` is keyed by
# the system of each evaluation first, as flakes key `packages`
# and `formatter`: `<attrset>.<system>`, holding the entries
# evaluated at that system, named by the rules above over their
# paths without the systems on them, or the single value there.
publish =
entries:
let
Expand All @@ -724,6 +754,33 @@
attrset:
let
members = builtins.filter (entry: entry.attrset == attrset) entries;
keyed = builtins.filter (entry: entry.bySystem or false) members;
systems = unique (builtins.map (entry: entry.system) keyed);
withoutSystems =
entry: entry // { path = builtins.filter (segment: segment.type != "system") entry.path; };
in
if keyed == [ ] then
publishMembers attrset members
else if builtins.length keyed != builtins.length members || builtins.elem null systems then
throw ''
${attrset}: this output is keyed by system, and these configurations
would be published there without a system:
${builtins.concatStringsSep "\n" (
builtins.map (entry: " ${showPath entry.path}") (
builtins.filter (entry: !(entry.bySystem or false) || entry.system == null) members
)
)}
''
else
genAttrs systems (
system:
publishMembers "${attrset}.${system}" (
builtins.map withoutSystems (builtins.filter (entry: entry.system == system) keyed)
)
);
publishMembers =
attrset: members:
let
names = builtins.map displayName (
final.caisson-core.elide (builtins.map (entry: entry.path) members)
);
Expand Down
243 changes: 243 additions & 0 deletions lib-overlays/treefmt/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# SPDX-License-Identifier: MIT
#
# The treefmt integration: it owns the `treefmt` class, the class of
# treefmt-nix's modules. A treefmt configuration is the formatters of a
# tree: `programs.<name>.enable` and `settings`, as treefmt-nix writes
# them. It is evaluated at every system in force where it is declared,
# on the package set selected for it.
#
# treefmt-nix evaluates its modules with `evalModules` on the library of
# a nixpkgs. caisson evaluates them with its own `evalModules`, on the
# composed library, over the modules treefmt-nix publishes for a
# configuration embedded in another, `submodule-modules`, with `pkgs`
# set to the selected package set. Those modules need treefmt-nix's
# special arguments, which treefmt-nix publishes only inside the type
# its `submoduleWith` returns, so caisson reads them from that type.
#
# The outputs are what treefmt-nix builds: `wrapper`, treefmt with its
# configuration, which `nix fmt` runs; `configFile`, the `treefmt.toml`;
# `programs`, the formatters by name; and `check`, the function that
# takes a source tree and builds a derivation that fails when the tree
# is not formatted. The configuration declared under the name `default`
# is published as `formatter.<system>`, its wrapper, which `nix fmt`
# reads, and as `checks.<system>.treefmt`, its check over the root of
# the tree (`mkLib`'s `root`), which `nix flake check` builds.
{
mkLibOverlay,
...
}:
{

imports = [
# The library of nixpkgs, which this overlay calls through the
# composed library, imported by key so it is composed wherever
# this is.
((mkLibOverlay ../nixpkgs-lib) // { key = "nixpkgs-lib"; })
# What an integration is written from, imported by key so it is
# composed wherever this integration is.
((mkLibOverlay ../integrations) // { key = "integrations"; })
];

overlay =
final: prev:
let
context = "lib.caisson.treefmt.mkConfiguration";

resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc {
name = "treefmt-nix";
context = "caisson.treefmt";
};

# A treefmt-nix source: the treefmt-nix flake or source tree, whose
# `default.nix` is its library.
libraryOf =
ecosystemSrc:
let
path = if builtins.isAttrs ecosystemSrc then ecosystemSrc.outPath or null else ecosystemSrc;
library = if path != null && builtins.pathExists "${path}/default.nix" then import path else { };
in
if library ? submodule-modules && library ? submoduleWith then
library
else
throw "${context} requires `ecosystemSrc` to be a treefmt-nix flake or source tree (whose default.nix has submodule-modules and submoduleWith).";

evaluate =
args:
{ lib, manifest }:
let
selection = lib.caisson.integrations;
treefmt = libraryOf (resolveEcosystemSrc {
explicit = args.ecosystemSrc or null;
manifest = lib.caisson-core.libManifest or { };
});

name = manifest.name or null;
inherit (manifest) system;
what =
if name != null then
"the treefmt configuration `${name}` at ${system}"
else
"this treefmt configuration at ${system}";

pkgSets = selection.pkgSetsAt { inherit context what; } manifest system;
pkgs = selection.pkgSetOf { inherit context what; } manifest pkgSets;

registry = lib.caisson.modules.treefmt or { };
frameworkModules = selection.frameworkModules "treefmt" registry;
moduleImports = selection.moduleImportsOf "treefmt" { inherit lib manifest; } args;

# The configuration's module: the module passed, else the
# configuration registered under the configuration's name
# (`configs/treefmt/<name>`), else none.
registered = lib.caisson.configs.treefmt or { };
configModule =
if (args.configModule or null) != null then
args.configModule
else if name != null then
registered.${name} or null
else
null;

# treefmt-nix's special arguments, from the type its
# `submoduleWith` returns.
upstreamSpecialArgs = (treefmt.submoduleWith lib { }).functor.payload.specialArgs;

value = lib.evalModules (
{
class = "treefmt";
specialArgs =
upstreamSpecialArgs
// {
inherit lib pkgSets;
}
// (if (args.specialArgs or null) != null then args.specialArgs else { });
modules =
treefmt.submodule-modules
++ [
{
_file = "caisson-treefmt:pkgs";
inherit pkgs;
}
]
++ frameworkModules
++ moduleImports registry
++ (if configModule == null then [ ] else [ configModule ]);
}
// (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { })
);
inherit (value.config) build;
in
{
inherit value;
outputs = {
inherit (build)
wrapper
configFile
programs
check
;
};
};

# Only the configuration named `default` is published: as
# `formatter.<system>`, its wrapper, which `nix fmt` reads, and,
# in a tree that knows its root, as `checks.<system>.treefmt`,
# its check over that tree, which fails `nix flake check` when
# the tree is not formatted.
isDefault = manifest: (manifest.name or null) == "default";
exportsTo = [
{
attrset = "formatter";
single = true;
bySystem = true;
only = isDefault;
value = manifest: manifest.outputs.wrapper;
}
{
attrset = "checks";
bySystem = true;
only = manifest: isDefault manifest && (manifest.root or null) != null;
name = _: {
value = "treefmt";
description = "named `treefmt`, the check of the configuration named `default`";
};
value = manifest: manifest.outputs.check manifest.root;
}
];

configuration =
args:
final.caisson.integrations.mkModuleConfiguration {
type = "treefmt";
# A treefmt configuration is evaluated at a system: it has an
# evaluation for every system in force where it is declared.
perSystem = true;
defaultPkgs = args.defaultPkgs or null;
inherit exportsTo;
evaluate = evaluate args;
};

integration = final.caisson.integrations.mkIntegration {
name = "treefmt";
class = "treefmt";
# A treefmt configuration that is a top, as a tool reads it: the
# evaluation, or the evaluations by system where several are in
# force.
top = final.caisson.integrations.topValue;
# What these return is a configuration, a function of
# `{ name, parent }`: a parent that declares it under
# `caisson.treefmt.configurations.<name>` finalizes it, and
# `mkTopConfiguration` finalizes it at a top.
mkConfiguration =
{
# The configuration's module. When absent, the configuration
# registered under the configuration's name
# (`lib.caisson.configs.treefmt.<name>`), if any. Further
# modules of the class are selected with `moduleImports`,
# from the registry.
configModule ? null,
# The treefmt-nix flake or source tree; resolved from the
# composition's declarations under the name `treefmt-nix`
# when absent.
ecosystemSrc ? null,
# The package set the configuration runs on: a function
# that receives the package sets available where it is
# declared, by package config name, each at the system of
# the evaluation, and returns the set to run on. When
# absent, the selection of the nearest configuration above,
# and the set named `default` where none above selects.
defaultPkgs ? null,
# The selection over the treefmt class of the registry. It
# replaces the default of the class, which is every entry
# named `default` followed by what the configurations
# above added.
moduleImports ? null,
# A selection added to that selection, whichever it is.
extraModuleImports ? null,
# Extra module arguments, merged over those the framework
# supplies.
specialArgs ? null,
}@args:
configuration args;
# The same arguments and `ecosystemArgs`, the evaluator's
# arguments merged over the composed call last.
mkConfigurationWithEcosystemArgs =
{
configModule ? null,
ecosystemSrc ? null,
defaultPkgs ? null,
moduleImports ? null,
extraModuleImports ? null,
specialArgs ? null,
ecosystemArgs ? null,
}@args:
configuration args;
};
in
{
caisson = (prev.caisson or { }) // {
treefmt = integration.namespace;
};
};

}
Loading
Loading