From 8ca6a0844f25c21d7746f9303a3fc108a08fdc62 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 02:03:50 -0700 Subject: [PATCH 01/14] feat(nvf): add the nvf integration An nvf configuration is a Neovim written as nvf modules, evaluated at every system in force on the selected package set. caisson evaluates nvf's module list with its own `evalModules`, with the special arguments nvf's `neovimConfiguration` passes, on the composed library extended with `lib.nvim` from the same nvf flake. The output `neovim` is published as `packages..`; the pinned-world suite checks it is the derivation `neovimConfiguration` builds. A NixOS machine, a darwin machine, a home and an hjem configuration install the Neovim of an nvf configuration declared inside them, with the module `nvf-package` of each class. caisson now registers a `default` home-manager module and a `default` hjem module, which import it. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + configs/structural/impl/default.nix | 1 + docs/development/repository-structure.md | 2 +- lib-overlays/nvf/default.nix | 278 ++++++++++++++++++++ modules/darwin/default/default.nix | 1 + modules/darwin/nvf-package/default.nix | 11 + modules/generic/core/default.nix | 1 + modules/hjem/default/default.nix | 12 + modules/hjem/nvf-package/default.nix | 7 + modules/homeManager/default/default.nix | 12 + modules/homeManager/nvf-package/default.nix | 11 + modules/nixos/default/default.nix | 1 + modules/nixos/nvf-package/default.nix | 11 + modules/nvf/core | 1 + tests/argument-errors/check.sh | 1 + tests/dependencies/flake.lock | 56 +++- tests/dependencies/flake.nix | 2 + tests/pinned-world/default.nix | 65 +++++ tests/unit/lib-overlays.nix | 3 + 19 files changed, 475 insertions(+), 2 deletions(-) create mode 100644 lib-overlays/nvf/default.nix create mode 100644 modules/darwin/nvf-package/default.nix create mode 100644 modules/hjem/default/default.nix create mode 100644 modules/hjem/nvf-package/default.nix create mode 100644 modules/homeManager/default/default.nix create mode 100644 modules/homeManager/nvf-package/default.nix create mode 100644 modules/nixos/nvf-package/default.nix create mode 120000 modules/nvf/core diff --git a/README.md b/README.md index df9a7610..a0f9556a 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ source by. | `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.` that `nix fmt` runs | +| `nvf` | `nvf` | a Neovim written as nvf modules, published as `packages..`; a NixOS machine, a darwin machine, a home or an hjem configuration installs the Neovim of an nvf configuration declared inside it | | `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 | diff --git a/configs/structural/impl/default.nix b/configs/structural/impl/default.nix index 7de75c95..fcea215a 100644 --- a/configs/structural/impl/default.nix +++ b/configs/structural/impl/default.nix @@ -35,6 +35,7 @@ terranix system-manager treefmt + nvf ; }; diff --git a/docs/development/repository-structure.md b/docs/development/repository-structure.md index 1dbe5874..c16f2213 100644 --- a/docs/development/repository-structure.md +++ b/docs/development/repository-structure.md @@ -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`, `treefmt`), each +`system-manager`, `treefmt`, `nvf`), each a library overlay contributing its `lib.caisson` namespace, registering its module class where it has a class (`nixos-minimal` and `home-manager-minimal` diff --git a/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix new file mode 100644 index 00000000..1ae28bb6 --- /dev/null +++ b/lib-overlays/nvf/default.nix @@ -0,0 +1,278 @@ +# SPDX-License-Identifier: MIT +# +# The nvf integration: it owns the `nvf` class, the class of nvf's +# modules. An nvf configuration is a Neovim, written as nvf modules +# (`vim.*`), evaluated at every system in force where it is declared, +# on the package set selected for it. +# +# nvf evaluates its modules with `neovimConfiguration`, on nixpkgs' +# library extended with nvf's own, `lib.nvim`. caisson evaluates the +# same modules, nvf's `modules/modules.nix`, with the special arguments +# `neovimConfiguration` passes (`self` and `inputs`, the nvf flake, and +# `modulesPath`), on the composed library extended with `lib.nvim` +# built from the same nvf source. So the integration needs the nvf +# flake, whose inputs nvf's modules read. +# +# The output is `neovim`, the wrapped Neovim, the `neovim` that +# `neovimConfiguration` returns. A configuration is published as +# `packages..`. A home, an hjem configuration or a NixOS +# configuration installs the Neovim of an nvf configuration declared +# inside it, with the modules `nvf-package` of their classes, which +# caisson's default modules of those classes import; each is +# `packagesModule` for the option of its class that takes packages. +{ + 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.nvf.mkConfiguration"; + + resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc { + name = "nvf"; + context = "caisson.nvf"; + }; + + # An nvf source: the nvf flake, which holds its library at `lib`, + # its modules at `modules/modules.nix`, and the inputs its modules + # read. + assertNvfSrc = + ecosystemSrc: + if + builtins.isAttrs ecosystemSrc + && ecosystemSrc ? inputs + && builtins.pathExists "${ecosystemSrc}/modules/modules.nix" + && builtins.pathExists "${ecosystemSrc}/lib/default.nix" + then + ecosystemSrc + else + throw "${context} requires `ecosystemSrc` to be the nvf flake (with its inputs, holding modules/modules.nix and lib/default.nix)."; + + evaluate = + args: + { lib, manifest }: + let + selection = lib.caisson.integrations; + src = assertNvfSrc (resolveEcosystemSrc { + explicit = args.ecosystemSrc or null; + manifest = lib.caisson-core.libManifest or { }; + }); + # The inputs nvf's modules read, with the flake as `self`, as a + # flake's `outputs` receives them. + inputs = src.inputs // { + self = src; + }; + + name = manifest.name or null; + inherit (manifest) system; + what = + if name != null then + "the nvf configuration `${name}` at ${system}" + else + "this nvf configuration at ${system}"; + + pkgSets = selection.pkgSetsAt { inherit context what; } manifest system; + pkgs = selection.pkgSetOf { inherit context what; } manifest pkgSets; + + # The composed library extended with the library of nvf, as + # `lib/stdlib-extended.nix` of nvf extends the library of + # nixpkgs. `extend` extends the library of nixpkgs, so the + # composed library is laid over it again, with the module + # system of the extension, as the home-manager integration lays + # it for `lib.hm`. + nvfLib = lib.extend ( + self: super: + lib + // { + inherit (super) modules types evalModules; + nvim = import "${src}/lib" { + inherit inputs; + self = src; + lib = self; + }; + literalExpression = super.literalExpression or super.literalExample; + } + ); + + registry = lib.caisson.modules.nvf or { }; + frameworkModules = selection.frameworkModules "nvf" registry; + moduleImports = selection.moduleImportsOf "nvf" { inherit lib manifest; } args; + + # The configuration's module: the module passed, else the + # configuration registered under the configuration's name + # (`configs/nvf/`), else none. + registered = lib.caisson.configs.nvf or { }; + configModule = + if (args.configModule or null) != null then + args.configModule + else if name != null then + registered.${name} or null + else + null; + + value = nvfLib.evalModules ( + { + class = "nvf"; + specialArgs = { + inherit inputs pkgSets; + self = src; + modulesPath = toString "${src}/modules"; + lib = nvfLib; + } + // (if (args.specialArgs or null) != null then args.specialArgs else { }); + modules = + import "${src}/modules/modules.nix" { + inherit pkgs; + lib = nvfLib; + } + ++ frameworkModules + ++ moduleImports registry + ++ (if configModule == null then [ ] else [ configModule ]); + } + // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) + ); + in + { + inherit value; + outputs.neovim = value.config.vim.build.finalPackage; + }; + + # The module, for a class that installs packages, that installs the + # Neovim of each nvf configuration declared inside a configuration + # of that class: the nvf configurations it passes up, evaluated at + # its system, with no configuration but structural ones between + # them and it. `option` is the path of the option of the class + # that takes the packages, such as `[ "environment" "systemPackages" ]`. + # + # The module reads the configurations from the full evaluation, so + # in the childless evaluation it defines nothing; and in an + # evaluation that does not declare the option, or whose library is + # not a caisson composition, it defines nothing either. + packagesModule = + option: + { + config, + lib, + options, + ... + }: + let + manifest = lib.caisson-core.evalManifest or null; + installedHere = + entry: + entry.manifest.type == "nvf" + && builtins.all (segment: segment.type == "structural" || segment.type == "system") ( + lib.lists.init entry.path + ) + && entry.manifest.system == (manifest.system or null); + neovims = builtins.map (entry: entry.manifest.outputs.neovim) ( + builtins.filter installedHere (config.caisson.exports.configurations or [ ]) + ); + in + { + config = lib.optionalAttrs (lib.hasAttrByPath option options) ( + lib.mkIf (manifest != null && !manifest.childless && neovims != [ ]) ( + lib.setAttrByPath option neovims + ) + ); + }; + + # An nvf configuration is published as `packages..`, + # its Neovim. + exportsTo = { + attrset = "packages"; + bySystem = true; + value = manifest: manifest.outputs.neovim; + }; + + configuration = + args: + final.caisson.integrations.mkModuleConfiguration { + type = "nvf"; + # An nvf 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 = "nvf"; + class = "nvf"; + # An nvf 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.nvf.configurations.` 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.nvf.`), if any. Further modules + # of the class are selected with `moduleImports`, from the + # registry. + configModule ? null, + # The nvf flake; resolved from the composition's + # declarations 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 nvf 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; + extra = { + inherit packagesModule; + }; + }; + in + { + caisson = (prev.caisson or { }) // { + nvf = integration.namespace; + }; + }; + +} diff --git a/modules/darwin/default/default.nix b/modules/darwin/default/default.nix index 2982630a..7c9af933 100644 --- a/modules/darwin/default/default.nix +++ b/modules/darwin/default/default.nix @@ -28,6 +28,7 @@ (mkModule ./caisson) closure-lib.caisson.modules.darwin.home-manager-activation closure-lib.caisson.modules.darwin.hjem-activation + closure-lib.caisson.modules.darwin.nvf-package ] else [ ]; diff --git a/modules/darwin/nvf-package/default.nix b/modules/darwin/nvf-package/default.nix new file mode 100644 index 00000000..47d5e912 --- /dev/null +++ b/modules/darwin/nvf-package/default.nix @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: MIT +# +# A darwin module that installs the Neovim of each nvf configuration +# declared inside the darwin configuration, in +# `environment.systemPackages`. caisson's `default` darwin module +# imports it. See `lib.caisson.nvf.packagesModule`. +{ closure-lib, ... }: +closure-lib.caisson.nvf.packagesModule [ + "environment" + "systemPackages" +] diff --git a/modules/generic/core/default.nix b/modules/generic/core/default.nix index 0d756b9b..1c32bb82 100644 --- a/modules/generic/core/default.nix +++ b/modules/generic/core/default.nix @@ -40,6 +40,7 @@ "home-manager" "nixos" "nixpkgs" + "nvf" "structural" "system-manager" "terranix" diff --git a/modules/hjem/default/default.nix b/modules/hjem/default/default.nix new file mode 100644 index 00000000..fc93fa80 --- /dev/null +++ b/modules/hjem/default/default.nix @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: MIT +# +# The `default` hjem module caisson registers. An hjem configuration +# imports every registered module named `default` unless it passes +# `moduleImports`, so this is what it gets from caisson when it does not +# select its modules itself: the module `nvf-package`, which installs +# the Neovim of each nvf configuration declared inside it. +{ closure-lib, ... }: +{ ... }: +{ + imports = [ closure-lib.caisson.modules.hjem.nvf-package ]; +} diff --git a/modules/hjem/nvf-package/default.nix b/modules/hjem/nvf-package/default.nix new file mode 100644 index 00000000..89f821ea --- /dev/null +++ b/modules/hjem/nvf-package/default.nix @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: MIT +# +# An hjem module that installs the Neovim of each nvf configuration +# declared inside the hjem configuration, in `packages`. caisson's +# `default` hjem module imports it. See `lib.caisson.nvf.packagesModule`. +{ closure-lib, ... }: +closure-lib.caisson.nvf.packagesModule [ "packages" ] diff --git a/modules/homeManager/default/default.nix b/modules/homeManager/default/default.nix new file mode 100644 index 00000000..ca94dd96 --- /dev/null +++ b/modules/homeManager/default/default.nix @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: MIT +# +# The `default` home-manager module caisson registers. A home imports +# every registered module named `default` unless it passes +# `moduleImports`, so this is what a home gets from caisson when it does +# not select its modules itself: the module `nvf-package`, which installs +# the Neovim of each nvf configuration declared inside the home. +{ closure-lib, ... }: +{ ... }: +{ + imports = [ closure-lib.caisson.modules.homeManager.nvf-package ]; +} diff --git a/modules/homeManager/nvf-package/default.nix b/modules/homeManager/nvf-package/default.nix new file mode 100644 index 00000000..aa4a3168 --- /dev/null +++ b/modules/homeManager/nvf-package/default.nix @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: MIT +# +# A home-manager module that installs the Neovim of each nvf +# configuration declared inside the home, in `home.packages`. caisson's +# `default` home-manager module imports it. See +# `lib.caisson.nvf.packagesModule`. +{ closure-lib, ... }: +closure-lib.caisson.nvf.packagesModule [ + "home" + "packages" +] diff --git a/modules/nixos/default/default.nix b/modules/nixos/default/default.nix index ed0fa4c7..8d24f41b 100644 --- a/modules/nixos/default/default.nix +++ b/modules/nixos/default/default.nix @@ -42,6 +42,7 @@ (mkModule ./caisson) closure-lib.caisson.modules.nixos.home-manager-activation closure-lib.caisson.modules.nixos.hjem-activation + closure-lib.caisson.modules.nixos.nvf-package closure-lib.caisson.modules.nixos.home-manager-source-marker ] else diff --git a/modules/nixos/nvf-package/default.nix b/modules/nixos/nvf-package/default.nix new file mode 100644 index 00000000..c4228cea --- /dev/null +++ b/modules/nixos/nvf-package/default.nix @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: MIT +# +# A NixOS module that installs the Neovim of each nvf configuration +# declared inside the NixOS configuration, in +# `environment.systemPackages`. caisson's `default` NixOS module imports +# it. See `lib.caisson.nvf.packagesModule`. +{ closure-lib, ... }: +closure-lib.caisson.nvf.packagesModule [ + "environment" + "systemPackages" +] diff --git a/modules/nvf/core b/modules/nvf/core new file mode 120000 index 00000000..3715354a --- /dev/null +++ b/modules/nvf/core @@ -0,0 +1 @@ +../generic/core \ No newline at end of file diff --git a/tests/argument-errors/check.sh b/tests/argument-errors/check.sh index 3f3ffbfe..c2518f1c 100755 --- a/tests/argument-errors/check.sh +++ b/tests/argument-errors/check.sh @@ -41,6 +41,7 @@ namespaces=( terranix system-manager treefmt + nvf ) failures=0 diff --git a/tests/dependencies/flake.lock b/tests/dependencies/flake.lock index c64eaf79..9b1335aa 100644 --- a/tests/dependencies/flake.lock +++ b/tests/dependencies/flake.lock @@ -76,6 +76,22 @@ } }, "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1777699697, + "narHash": "sha256-Eg9b/rq/ECYwNwEXs5i9wHyhxNI0JrYx2srdI2uZMaQ=", + "ref": "refs/heads/main", + "rev": "382052b74656a369c5408822af3f2501e9b1af81", + "revCount": 94, + "type": "git", + "url": "https://git.lix.systems/lix-project/flake-compat.git" + }, + "original": { + "type": "git", + "url": "https://git.lix.systems/lix-project/flake-compat.git" + } + }, + "flake-compat_3": { "flake": false, "locked": { "lastModified": 1767039857, @@ -250,6 +266,21 @@ "type": "github" } }, + "mnw": { + "locked": { + "lastModified": 1784154424, + "narHash": "sha256-HRn4O1X2ShYtbq5Egji72/aemyc2cIQicjRZD9hBqM8=", + "owner": "Gerg-L", + "repo": "mnw", + "rev": "0151d9fa87992cbeb67c86616fb9170dcec830b6", + "type": "github" + }, + "original": { + "owner": "Gerg-L", + "repo": "mnw", + "type": "github" + } + }, "nix-darwin": { "inputs": { "nixpkgs": [ @@ -365,6 +396,28 @@ "type": "github" } }, + "nvf": { + "inputs": { + "flake-compat": "flake-compat_2", + "mnw": "mnw", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1792175215, + "narHash": "sha256-XD64JVLV8iE4lR+W5gkTweEaT+SBwOrSy0+le2WgITI=", + "owner": "NotAShelf", + "repo": "nvf", + "rev": "a736b60b1e2019931168979951c94dc46e342957", + "type": "github" + }, + "original": { + "owner": "NotAShelf", + "repo": "nvf", + "type": "github" + } + }, "pre-commit-hooks-nix": { "inputs": { "flake-compat": [ @@ -405,6 +458,7 @@ "nix-unit": "nix-unit", "nixpkgs": "nixpkgs", "nixpkgs-lib": "nixpkgs-lib", + "nvf": "nvf", "system-manager": "system-manager", "terranix": "terranix", "treefmt-nix": "treefmt-nix_2" @@ -428,7 +482,7 @@ }, "system-manager": { "inputs": { - "flake-compat": "flake-compat_2", + "flake-compat": "flake-compat_3", "nixpkgs": [ "nixpkgs" ], diff --git a/tests/dependencies/flake.nix b/tests/dependencies/flake.nix index b09d16b2..42cb7fa5 100644 --- a/tests/dependencies/flake.nix +++ b/tests/dependencies/flake.nix @@ -21,6 +21,8 @@ inputs.system-manager.inputs.nixpkgs.follows = "nixpkgs"; inputs.disko.url = "github:nix-community/disko"; inputs.disko.inputs.nixpkgs.follows = "nixpkgs"; + inputs.nvf.url = "github:NotAShelf/nvf"; + inputs.nvf.inputs.nixpkgs.follows = "nixpkgs"; inputs.hjem.url = "github:feel-co/hjem"; inputs.hjem.inputs.nixpkgs.follows = "nixpkgs"; inputs.nix-darwin.url = "github:nix-darwin/nix-darwin"; diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index ec9d6f77..82b5173a 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -72,6 +72,7 @@ let registered."caisson/terranix" registered."caisson/system-manager" registered."caisson/treefmt" + registered."caisson/nvf" registered."caisson/structural" # Last, so its value replaces the pin the overlays carry while # its position stays where the first import placed it. @@ -118,6 +119,7 @@ let "nixos" "nixos-minimal" "nixpkgs" + "nvf" "pins" "pkgOverlays" "pkgOverlaysFor" @@ -483,6 +485,7 @@ let "caisson/terranix" "caisson/system-manager" "caisson/treefmt" + "caisson/nvf" "caisson/structural" ]; @@ -583,6 +586,7 @@ let "terranix" "system-manager" "treefmt" + "nvf" ]; minimalNixosSystemEvaluates = @@ -1663,6 +1667,67 @@ let && builtins.isString evaluation.outputs.configFile.drvPath && builtins.isString (evaluation.outputs.check ./.).drvPath; + # An nvf configuration is published as `packages..`, + # and its Neovim is the one nvf's own `neovimConfiguration` builds + # from the same module on the same package set. A NixOS machine and + # a home with an nvf configuration declared inside them install that + # Neovim. + nvfIsTheNeovimNvfBuilds = + let + module = { + vim.viAlias = true; + vim.lineNumberMode = "number"; + }; + nvfConfiguration = + lib: + lib.caisson.nvf.mkConfiguration { + ecosystemSrc = inputs.nvf; + configModule = module; + }; + top = hiveLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = + { lib, ... }: + { + caisson.nvf.configurations.editor = nvfConfiguration lib; + caisson.nixos.configurations.workstation = lib.caisson.nixos.mkConfiguration { + configModule = + { lib, ... }: + { + boot.loader.grub.enable = false; + fileSystems."/" = { + device = "none"; + fsType = "tmpfs"; + }; + system.stateVersion = "25.05"; + caisson.nvf.configurations.machine-editor = nvfConfiguration lib; + }; + }; + caisson.home-manager.configurations.alice = lib.caisson.home-manager.mkConfiguration { + ecosystemSrc = inputs.home-manager; + configModule = + { lib, ... }: + { + home.homeDirectory = "/home/alice"; + home.stateVersion = "25.05"; + caisson.nvf.configurations.home-editor = nvfConfiguration lib; + }; + }; + }; + }; + upstream = + (inputs.nvf.lib.neovimConfiguration { + inherit pkgs; + modules = [ module ]; + }).neovim; + drvPaths = packages: builtins.map (package: package.drvPath) packages; + in + top.packages.x86_64-linux.editor.drvPath == upstream.drvPath + && builtins.elem upstream.drvPath ( + drvPaths top.nixosConfigurations.workstation.config.environment.systemPackages + ) + && builtins.elem upstream.drvPath (drvPaths top.homeConfigurations.alice.config.home.packages); + # A terranix configuration that is a top is its `config.tf.json` # file, built from the package set of the composition with the # evaluated configuration in its `passthru`, and its modules get the diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index 4087d6e6..9e59e4ea 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -127,6 +127,7 @@ let "terranix" "system-manager" "treefmt" + "nvf" ]; in @@ -5391,6 +5392,7 @@ in "caisson-facter" "facter" "treefmt" + "nvf" ]; setsFromTheComposition = builtins.elem name [ "nixos" @@ -5404,6 +5406,7 @@ in "caisson-facter" "facter" "treefmt" + "nvf" ]; in signature ? configModule From 8756668dc3fa5fdaabee2efa549cf884dd2b023a Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 02:10:29 -0700 Subject: [PATCH 02/14] feat(nixvim): add the nixvim integration A nixvim configuration is a Neovim written as nixvim modules, evaluated at every system in force on the selected package set. caisson makes the evaluation `evalNixvim` makes, `evalModules` over `modules/top-level` with the flake as `flake`, on the composed library extended with the overlay nixvim publishes and with `nixpkgs.pkgs` set to the selected package set. The output `package` is the Neovim; the configuration is published as `nixvimConfigurations..`, where nixvim's flake publishes its own. The pinned-world suite checks the package is the derivation `evalNixvim` builds. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + configs/structural/impl/default.nix | 1 + docs/development/repository-structure.md | 2 +- lib-overlays/nixvim/default.nix | 224 +++++++++++++++++++++++ modules/generic/core/default.nix | 1 + modules/nixvim/core | 1 + tests/argument-errors/check.sh | 1 + tests/dependencies/flake.lock | 73 +++++++- tests/dependencies/flake.nix | 2 + tests/pinned-world/default.nix | 37 ++++ tests/unit/lib-overlays.nix | 3 + 11 files changed, 339 insertions(+), 7 deletions(-) create mode 100644 lib-overlays/nixvim/default.nix create mode 120000 modules/nixvim/core diff --git a/README.md b/README.md index a0f9556a..fcc415cc 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ source by. | `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.` that `nix fmt` runs | | `nvf` | `nvf` | a Neovim written as nvf modules, published as `packages..`; a NixOS machine, a darwin machine, a home or an hjem configuration installs the Neovim of an nvf configuration declared inside it | +| `nixvim` | `nixvim` | a Neovim written as nixvim modules, published as `nixvimConfigurations..` | | `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 | diff --git a/configs/structural/impl/default.nix b/configs/structural/impl/default.nix index fcea215a..5a685db1 100644 --- a/configs/structural/impl/default.nix +++ b/configs/structural/impl/default.nix @@ -36,6 +36,7 @@ system-manager treefmt nvf + nixvim ; }; diff --git a/docs/development/repository-structure.md b/docs/development/repository-structure.md index c16f2213..5036c220 100644 --- a/docs/development/repository-structure.md +++ b/docs/development/repository-structure.md @@ -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`, `treefmt`, `nvf`), each +`system-manager`, `treefmt`, `nvf`, `nixvim`), each a library overlay contributing its `lib.caisson` namespace, registering its module class where it has a class (`nixos-minimal` and `home-manager-minimal` diff --git a/lib-overlays/nixvim/default.nix b/lib-overlays/nixvim/default.nix new file mode 100644 index 00000000..14bc71a0 --- /dev/null +++ b/lib-overlays/nixvim/default.nix @@ -0,0 +1,224 @@ +# SPDX-License-Identifier: MIT +# +# The nixvim integration: it owns the `nixvim` class, the class of +# nixvim's modules. A nixvim configuration is a Neovim, written as +# nixvim modules (`plugins.*`, `opts`, `keymaps`), evaluated at every +# system in force where it is declared, on the package set selected for +# it. +# +# nixvim evaluates its modules with `evalNixvim`: `evalModules` over +# `modules/top-level`, on a library extended with the overlay nixvim +# publishes, `lib.overlay`, which adds `lib.nixvim`. caisson makes the +# same evaluation on the composed library extended with that overlay, +# with `nixpkgs.pkgs` set to the selected package set and the nixvim +# flake as `flake`, as `evalNixvim` sets it. So the integration needs +# the nixvim flake. +# +# The output is `package`, the Neovim, `config.build.package`. A +# configuration is published as `nixvimConfigurations..`, +# the evaluated configuration, under the name nixvim's flake publishes +# its own configurations under. +{ + 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.nixvim.mkConfiguration"; + + resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc { + name = "nixvim"; + context = "caisson.nixvim"; + }; + + # A nixvim source: the nixvim flake, which publishes its library + # overlay as `lib.overlay` and holds its modules at + # `modules/top-level`. + assertNixvimSrc = + ecosystemSrc: + if + builtins.isAttrs ecosystemSrc + && (ecosystemSrc.lib or { }) ? overlay + && builtins.pathExists "${ecosystemSrc}/modules/top-level" + then + ecosystemSrc + else + throw "${context} requires `ecosystemSrc` to be the nixvim flake (publishing lib.overlay, holding modules/top-level)."; + + evaluate = + args: + { lib, manifest }: + let + selection = lib.caisson.integrations; + src = assertNixvimSrc (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 nixvim configuration `${name}` at ${system}" + else + "this nixvim configuration at ${system}"; + + pkgSets = selection.pkgSetsAt { inherit context what; } manifest system; + pkgs = selection.pkgSetOf { inherit context what; } manifest pkgSets; + + # The composed library extended with the overlay nixvim + # publishes. `extend` extends the library of nixpkgs, so the + # composed library is laid over it again, with the module + # system of the extension, before the overlay. + nixvimLib = lib.extend ( + self: super: + lib + // { + inherit (super) modules types evalModules; + } + // src.lib.overlay self super + ); + + registry = lib.caisson.modules.nixvim or { }; + frameworkModules = selection.frameworkModules "nixvim" registry; + moduleImports = selection.moduleImportsOf "nixvim" { inherit lib manifest; } args; + + # The configuration's module: the module passed, else the + # configuration registered under the configuration's name + # (`configs/nixvim/`), else none. + registered = lib.caisson.configs.nixvim or { }; + configModule = + if (args.configModule or null) != null then + args.configModule + else if name != null then + registered.${name} or null + else + null; + + value = nixvimLib.evalModules ( + { + class = "nixvim"; + specialArgs = { + inherit pkgSets; + lib = nixvimLib; + modulesPath = "${src}/modules"; + } + // (if (args.specialArgs or null) != null then args.specialArgs else { }); + modules = + frameworkModules + ++ moduleImports registry + ++ (if configModule == null then [ ] else [ configModule ]) + ++ [ + "${src}/modules/top-level" + { + _file = "caisson-nixvim:evalNixvim"; + flake = nixvimLib.mkOptionDefault src; + nixpkgs.pkgs = pkgs; + } + ]; + } + // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) + ); + in + { + inherit value; + outputs.package = value.config.build.package; + }; + + # A nixvim configuration is published as + # `nixvimConfigurations..`, the evaluated + # configuration. + exportsTo = { + attrset = "nixvimConfigurations"; + bySystem = true; + value = manifest: manifest.value; + }; + + configuration = + args: + final.caisson.integrations.mkModuleConfiguration { + type = "nixvim"; + # A nixvim 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 = "nixvim"; + class = "nixvim"; + # A nixvim 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.nixvim.configurations.` 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.nixvim.`), if any. Further + # modules of the class are selected with `moduleImports`, + # from the registry. + configModule ? null, + # The nixvim flake; resolved from the composition's + # declarations 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 nixvim 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 { }) // { + nixvim = integration.namespace; + }; + }; + +} diff --git a/modules/generic/core/default.nix b/modules/generic/core/default.nix index 1c32bb82..f7fcb3b5 100644 --- a/modules/generic/core/default.nix +++ b/modules/generic/core/default.nix @@ -40,6 +40,7 @@ "home-manager" "nixos" "nixpkgs" + "nixvim" "nvf" "structural" "system-manager" diff --git a/modules/nixvim/core b/modules/nixvim/core new file mode 120000 index 00000000..3715354a --- /dev/null +++ b/modules/nixvim/core @@ -0,0 +1 @@ +../generic/core \ No newline at end of file diff --git a/tests/argument-errors/check.sh b/tests/argument-errors/check.sh index c2518f1c..b48b4e2e 100755 --- a/tests/argument-errors/check.sh +++ b/tests/argument-errors/check.sh @@ -42,6 +42,7 @@ namespaces=( system-manager treefmt nvf + nixvim ) failures=0 diff --git a/tests/dependencies/flake.lock b/tests/dependencies/flake.lock index 9b1335aa..6d517bb1 100644 --- a/tests/dependencies/flake.lock +++ b/tests/dependencies/flake.lock @@ -128,6 +128,27 @@ } }, "flake-parts_2": { + "inputs": { + "nixpkgs-lib": [ + "nixvim", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1790898470, + "narHash": "sha256-zTwuwezD0w3hpga6a6P8emidzAZFyWqlNeRBlpWOOl8=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "024633cd702b10285db5cb19b40ad48d2399ba60", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_3": { "inputs": { "nixpkgs-lib": [ "system-manager", @@ -149,7 +170,7 @@ "type": "github" } }, - "flake-parts_3": { + "flake-parts_4": { "inputs": { "nixpkgs-lib": [ "terranix", @@ -396,6 +417,28 @@ "type": "github" } }, + "nixvim": { + "inputs": { + "flake-parts": "flake-parts_2", + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems_2" + }, + "locked": { + "lastModified": 1792203519, + "narHash": "sha256-yJ072hxngo/srNdLTp4BlFeo0hedwnKicI3TGlSXLFE=", + "owner": "nix-community", + "repo": "nixvim", + "rev": "453dc9c5037e6d1bec81d2a51ddacd58a2f44e9c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixvim", + "type": "github" + } + }, "nvf": { "inputs": { "flake-compat": "flake-compat_2", @@ -458,6 +501,7 @@ "nix-unit": "nix-unit", "nixpkgs": "nixpkgs", "nixpkgs-lib": "nixpkgs-lib", + "nixvim": "nixvim", "nvf": "nvf", "system-manager": "system-manager", "terranix": "terranix", @@ -518,6 +562,23 @@ } }, "systems_2": { + "flake": false, + "locked": { + "lastModified": 1791244623, + "narHash": "sha256-uuanbkFk8a313Zfrxdcz8Us/yQtaxNnKJtX8v0ZPpd8=", + "owner": "nix-systems", + "repo": "default", + "rev": "d36eeed142f88b5a636f2e3856a53c61654cf29b", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "ref": "future-26.11", + "repo": "default", + "type": "github" + } + }, + "systems_3": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", @@ -532,7 +593,7 @@ "type": "github" } }, - "systems_3": { + "systems_4": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", @@ -549,12 +610,12 @@ }, "terranix": { "inputs": { - "flake-parts": "flake-parts_3", + "flake-parts": "flake-parts_4", "import-tree": "import-tree", "nixpkgs": [ "nixpkgs" ], - "systems": "systems_3" + "systems": "systems_4" }, "locked": { "lastModified": 1784465933, @@ -617,13 +678,13 @@ "system-manager", "flake-compat" ], - "flake-parts": "flake-parts_2", + "flake-parts": "flake-parts_3", "nixpkgs": [ "system-manager", "nixpkgs" ], "pre-commit-hooks-nix": "pre-commit-hooks-nix", - "systems": "systems_2" + "systems": "systems_3" }, "locked": { "lastModified": 1770377964, diff --git a/tests/dependencies/flake.nix b/tests/dependencies/flake.nix index 42cb7fa5..459b35dc 100644 --- a/tests/dependencies/flake.nix +++ b/tests/dependencies/flake.nix @@ -21,6 +21,8 @@ inputs.system-manager.inputs.nixpkgs.follows = "nixpkgs"; inputs.disko.url = "github:nix-community/disko"; inputs.disko.inputs.nixpkgs.follows = "nixpkgs"; + inputs.nixvim.url = "github:nix-community/nixvim"; + inputs.nixvim.inputs.nixpkgs.follows = "nixpkgs"; inputs.nvf.url = "github:NotAShelf/nvf"; inputs.nvf.inputs.nixpkgs.follows = "nixpkgs"; inputs.hjem.url = "github:feel-co/hjem"; diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 82b5173a..0939136f 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -73,6 +73,7 @@ let registered."caisson/system-manager" registered."caisson/treefmt" registered."caisson/nvf" + registered."caisson/nixvim" registered."caisson/structural" # Last, so its value replaces the pin the overlays carry while # its position stays where the first import placed it. @@ -119,6 +120,7 @@ let "nixos" "nixos-minimal" "nixpkgs" + "nixvim" "nvf" "pins" "pkgOverlays" @@ -486,6 +488,7 @@ let "caisson/system-manager" "caisson/treefmt" "caisson/nvf" + "caisson/nixvim" "caisson/structural" ]; @@ -587,6 +590,7 @@ let "system-manager" "treefmt" "nvf" + "nixvim" ]; minimalNixosSystemEvaluates = @@ -1728,6 +1732,39 @@ let ) && builtins.elem upstream.drvPath (drvPaths top.homeConfigurations.alice.config.home.packages); + # A nixvim configuration is published as + # `nixvimConfigurations..`, and its package is the one + # nixvim's own `evalNixvim` builds from the same module on the same + # package set. + nixvimIsTheNeovimNixvimBuilds = + let + module = { + opts.number = true; + globals.mapleader = " "; + }; + top = hiveLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = + { lib, ... }: + { + caisson.nixvim.configurations.editor = lib.caisson.nixvim.mkConfiguration { + ecosystemSrc = inputs.nixvim; + configModule = module; + }; + }; + }; + upstream = inputs.nixvim.lib.evalNixvim { + modules = [ + module + { nixpkgs.pkgs = pkgs; } + ]; + }; + in + builtins.attrNames top.nixvimConfigurations == [ "x86_64-linux" ] + && + top.nixvimConfigurations.x86_64-linux.editor.config.build.package.drvPath + == upstream.config.build.package.drvPath; + # A terranix configuration that is a top is its `config.tf.json` # file, built from the package set of the composition with the # evaluated configuration in its `passthru`, and its modules get the diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index 9e59e4ea..8b618afd 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -128,6 +128,7 @@ let "system-manager" "treefmt" "nvf" + "nixvim" ]; in @@ -5393,6 +5394,7 @@ in "facter" "treefmt" "nvf" + "nixvim" ]; setsFromTheComposition = builtins.elem name [ "nixos" @@ -5407,6 +5409,7 @@ in "facter" "treefmt" "nvf" + "nixvim" ]; in signature ? configModule From 09b045c42090def20bd6390416d806475ca765fd Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 02:20:24 -0700 Subject: [PATCH 03/14] perf(core): declare the module export settings as one option Every evaluation declared `caisson.modules..export.enabled` and `caisson.modules..exported` as options for every registered class. `caisson.modules` is now one option, an attribute set by class of the two settings, so a class no module sets costs nothing to evaluate. The definitions are written as before; a class with no registered module, or a setting other than the two, is refused when the exports are read. `exported` is now a value, so two definitions of it for one class conflict instead of merging. Co-Authored-By: Claude Opus 5.5 --- modules/generic/core/caisson/modules.nix | 107 +++++++++++++---------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/modules/generic/core/caisson/modules.nix b/modules/generic/core/caisson/modules.nix index 790bc974..2930a6bf 100644 --- a/modules/generic/core/caisson/modules.nix +++ b/modules/generic/core/caisson/modules.nix @@ -2,7 +2,6 @@ { config, lib, - options, ... }: let @@ -20,65 +19,77 @@ let # The classes that have a registered module, in the registry of the # library and in the registry the manifest of this evaluation holds. # Both manifests are in the library, so the classes are known before - # this evaluation starts. The export settings of each class are - # declared as options from this list. A setting for a class that is - # not in the list is refused as an option that does not exist. + # this evaluation starts. A setting for a class that is not in the + # list is refused. evalManifest = lib.caisson-core.evalManifest; classes = lib.lists.unique ( builtins.attrNames lib.caisson.modules ++ builtins.attrNames (if evalManifest == null then { } else evalManifest.modules or { }) ); - # The types of the options, built once for every class. The modules - # are exported as they were registered. - types = { - enabled = lib.types.bool; - exported = lib.types.functionTo (lib.types.lazyAttrsOf lib.types.raw); - }; - - # True when a module of the evaluation defines the option: its highest - # priority is above the priority of an option default. - defined = option: option.highestPrio < 1500; + settings = config.caisson.modules; + unknown = builtins.filter (class: !(builtins.elem class classes)) (builtins.attrNames settings); + settingsNames = [ + "export" + "exported" + ]; - # A class whose options no module defines exports its default, the - # modules the composition registers itself, without merging the two - # options, which spares merging them in every evaluation for every - # class. - exportedClassModules = builtins.mapAttrs ( - class: classModules: + # The settings of a class: `export.enabled`, true unless set, and + # `exported`, the function that selects the modules to export, the + # modules the composition registers itself unless set. + settingsOf = + class: let - classOptions = options.caisson.modules.${class}; - classConfig = config.caisson.modules.${class}; + set = settings.${class} or { }; + others = builtins.filter (name: !(builtins.elem name settingsNames)) (builtins.attrNames set); + enabled = (set.export or { }).enabled or true; in - if !(defined classOptions.export.enabled) && !(defined classOptions.exported) then - localModules class classModules - else if classConfig.export.enabled then - classConfig.exported classModules + if others != [ ] then + throw "`caisson.modules.${class}` sets ${lib.concatStringsSep ", " others}; the settings of a class are `export.enabled` and `exported`." + else if !builtins.isBool enabled then + throw "`caisson.modules.${class}.export.enabled` must be a boolean." else - { } - ) registeredModules; + { + inherit enabled; + exported = set.exported or (localModules class); + }; + + exportedClassModules = + if unknown != [ ] then + throw '' + `caisson.modules` sets the export of ${lib.concatMapStringsSep ", " (class: "`${class}`") unknown}, + which no module is registered for. The classes are: ${lib.concatStringsSep ", " classes}. + '' + else + builtins.mapAttrs ( + class: classModules: + let + classSettings = settingsOf class; + in + if classSettings.enabled then classSettings.exported classModules else { } + ) registeredModules; in { - options.caisson.modules = lib.genAttrs classes (class: { - export.enabled = lib.mkOption { - type = types.enabled; - default = true; - description = "Whether to export the modules of the class `${class}`."; - }; - exported = lib.mkOption { - type = types.exported; - default = localModules class; - defaultText = lib.literalMD "the modules of the class this composition registers itself"; - description = '' - Function that selects which registered modules of the class - `${class}` to export. Receives the modules registered for the - class and returns the modules to export. Defaults to the - modules this composition registers itself. A module that came - from an entry of `projects`, registered as `/`, - is exported only when the function names it. - ''; - }; - }); + # The export settings of each class, in one option for every class: + # a class no module sets costs nothing to evaluate, where options per + # class would be merged in every evaluation for every class, and a + # submodule per class would be an evaluation of its own. + options.caisson.modules = lib.mkOption { + type = lib.types.lazyAttrsOf (lib.types.lazyAttrsOf lib.types.raw); + default = { }; + description = '' + The export settings of the registered modules, by class. + `caisson.modules..exported` is the function that selects + which registered modules of the class to export: it receives the + modules registered for the class and returns the modules to + export. It defaults to the modules this composition registers + itself; a module that came from an entry of `projects`, + registered as `/`, is exported only when the + function names it. `caisson.modules..export.enabled`, + true unless set, turns the export of the class off. A class with + a registered module can be set; any other class is refused. + ''; + }; config.caisson.exports.modules = exportedClassModules; } From 412fa4bdda03edad705bd72eb18d9f2956715907 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 02:29:15 -0700 Subject: [PATCH 04/14] feat: add the topology, kubenix and nixidy integrations A topology configuration is the diagram of the NixOS configurations declared inside it. caisson evaluates nix-topology's `topology` modules on the composed library and the selected package set extended with nix-topology's packages, with `nixosConfigurations` set to the machines declared inside it at its system; those machines get nix-topology's NixOS module by default. The configuration named `default` is published as `topology.`, where nix-topology's flake module publishes it. A kubenix configuration is Kubernetes resources written as kubenix modules, evaluated as kubenix's `evalModules` evaluates them, on the composed library extended with kubenix's `upstreamables.nix`. Its `result` is published as `packages..`. A nixidy configuration is an Argo CD environment, evaluated with nixidy's own `mkEnv` over the composed library, with the namespaces of the composed library added to nixidy's through `libOverlay`. It is published as `nixidyEnvs..`, which the nixidy CLI reads. The pinned-world suite checks each against its upstream evaluator: the same topology nodes and diagram, the same kubenix result and the same nixidy environment. Co-Authored-By: Claude Opus 5.5 --- README.md | 3 + configs/structural/impl/default.nix | 3 + docs/development/repository-structure.md | 3 +- lib-overlays/kubenix/default.nix | 239 +++++++++++++++++ lib-overlays/nixidy/default.nix | 202 +++++++++++++++ lib-overlays/topology/default.nix | 238 +++++++++++++++++ modules/generic/core/default.nix | 3 + modules/kubenix/core | 1 + modules/nixidy/core | 1 + modules/topology/core | 1 + tests/argument-errors/check.sh | 3 + tests/dependencies/flake.lock | 310 +++++++++++++++++++++-- tests/dependencies/flake.nix | 6 + tests/pinned-world/default.nix | 136 ++++++++++ tests/unit/lib-overlays.nix | 9 + 15 files changed, 1139 insertions(+), 19 deletions(-) create mode 100644 lib-overlays/kubenix/default.nix create mode 100644 lib-overlays/nixidy/default.nix create mode 100644 lib-overlays/topology/default.nix create mode 120000 modules/kubenix/core create mode 120000 modules/nixidy/core create mode 120000 modules/topology/core diff --git a/README.md b/README.md index fcc415cc..d2d3e3c8 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,9 @@ source by. | `treefmt` | `treefmt-nix` | the formatters of a tree, as treefmt-nix modules; the configuration named `default` is published as the `formatter.` that `nix fmt` runs | | `nvf` | `nvf` | a Neovim written as nvf modules, published as `packages..`; a NixOS machine, a darwin machine, a home or an hjem configuration installs the Neovim of an nvf configuration declared inside it | | `nixvim` | `nixvim` | a Neovim written as nixvim modules, published as `nixvimConfigurations..` | +| `topology` | `nix-topology` | the diagram of the NixOS configurations declared inside it, as nix-topology renders it; the configuration named `default` is published as `topology.` | +| `kubenix` | `kubenix` | Kubernetes resources written as kubenix modules, published as `packages..`, the JSON `kubectl apply -f` reads | +| `nixidy` | `nixidy` | an Argo CD environment written as nixidy modules, evaluated with nixidy's `mkEnv` and published as the `nixidyEnvs..` the nixidy CLI reads | | `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 | diff --git a/configs/structural/impl/default.nix b/configs/structural/impl/default.nix index 5a685db1..a3b0487c 100644 --- a/configs/structural/impl/default.nix +++ b/configs/structural/impl/default.nix @@ -37,6 +37,9 @@ treefmt nvf nixvim + topology + kubenix + nixidy ; }; diff --git a/docs/development/repository-structure.md b/docs/development/repository-structure.md index 5036c220..07e439ee 100644 --- a/docs/development/repository-structure.md +++ b/docs/development/repository-structure.md @@ -28,7 +28,8 @@ 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`, `treefmt`, `nvf`, `nixvim`), each +`system-manager`, `treefmt`, `nvf`, `nixvim`, `topology`, `kubenix`, +`nixidy`), each a library overlay contributing its `lib.caisson` namespace, registering its module class where it has a class (`nixos-minimal` and `home-manager-minimal` diff --git a/lib-overlays/kubenix/default.nix b/lib-overlays/kubenix/default.nix new file mode 100644 index 00000000..530e8368 --- /dev/null +++ b/lib-overlays/kubenix/default.nix @@ -0,0 +1,239 @@ +# SPDX-License-Identifier: MIT +# +# The kubenix integration: it owns the `kubenix` class, the class of +# kubenix's modules. A kubenix configuration is a set of Kubernetes +# resources written as kubenix modules, which import the kubenix modules +# they use from the `kubenix` argument (`kubenix.modules.k8s`). It is +# evaluated at every system in force where it is declared, on the +# package set selected for it. +# +# kubenix evaluates its modules with its `evalModules`: `evalModules` on +# the library of the package set extended with kubenix's +# `upstreamables.nix`, with the special argument `kubenix` holding its +# library, its `evalModules` and its modules. caisson makes that +# evaluation on the composed library extended the same way. +# +# The output is `result`, the resources as a JSON file, which +# `kubectl apply -f` reads; it is the `kubernetes.result` of kubenix's +# `k8s` module, so a configuration that imports that module has it. A +# configuration is published as `packages..`, its result. +{ + 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.kubenix.mkConfiguration"; + + resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc { + name = "kubenix"; + context = "caisson.kubenix"; + }; + + # A kubenix source: the kubenix flake or source tree, which holds + # its library at `lib` and its modules at `modules`. + assertKubenixSrc = + ecosystemSrc: + let + path = if builtins.isAttrs ecosystemSrc then ecosystemSrc.outPath or null else ecosystemSrc; + in + if + path != null + && builtins.pathExists "${path}/lib/upstreamables.nix" + && builtins.pathExists "${path}/lib/eval-modules.nix" + && builtins.pathExists "${path}/modules/default.nix" + then + ecosystemSrc + else + throw "${context} requires `ecosystemSrc` to be a kubenix flake or source tree (holding lib/upstreamables.nix, lib/eval-modules.nix and modules)."; + + evaluate = + args: + { lib, manifest }: + let + selection = lib.caisson.integrations; + src = assertKubenixSrc (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 kubenix configuration `${name}` at ${system}" + else + "this kubenix configuration at ${system}"; + + pkgSets = selection.pkgSetsAt { inherit context what; } manifest system; + pkgs = selection.pkgSetOf { inherit context what; } manifest pkgSets; + + # The composed library extended with kubenix's + # `upstreamables.nix`, as kubenix extends the library of the + # package set. `extend` extends the library of nixpkgs, so the + # composed library is laid over it again, with the module + # system of the extension. + kubenixLib = lib.extend ( + self: super: + lib + // { + inherit (super) modules types evalModules; + } + // import "${src}/lib/upstreamables.nix" { + lib = self; + inherit pkgs; + } + ); + + registry = lib.caisson.modules.kubenix or { }; + frameworkModules = selection.frameworkModules "kubenix" registry; + moduleImports = selection.moduleImportsOf "kubenix" { inherit lib manifest; } args; + + # The configuration's module: the module passed, else the + # configuration registered under the configuration's name + # (`configs/kubenix/`), else none. + registered = lib.caisson.configs.kubenix or { }; + configModule = + if (args.configModule or null) != null then + args.configModule + else if name != null then + registered.${name} or null + else + null; + + value = kubenixLib.evalModules ( + { + class = "kubenix"; + specialArgs = { + inherit pkgs pkgSets; + lib = kubenixLib; + kubenix = { + lib = import "${src}/lib" { + inherit pkgs; + inherit (pkgs) lib; + }; + evalModules = import "${src}/lib/eval-modules.nix" { inherit pkgs; }; + modules = import "${src}/modules"; + }; + } + // (if (args.specialArgs or null) != null then args.specialArgs else { }); + modules = + frameworkModules + ++ moduleImports registry + ++ (if configModule == null then [ ] else [ configModule ]) + ++ [ + { + _file = "caisson-kubenix:args"; + config._module.args = { + inherit pkgs; + name = "default"; + }; + } + ]; + } + // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) + ); + in + { + inherit value; + outputs.result = + value.config.kubernetes.result + or (throw "${what} has no `kubernetes.result`; it imports kubenix's `k8s` module from `kubenix.modules.k8s` to have one."); + }; + + # A kubenix configuration is published as + # `packages..`, its result. + exportsTo = { + attrset = "packages"; + bySystem = true; + value = manifest: manifest.outputs.result; + }; + + configuration = + args: + final.caisson.integrations.mkModuleConfiguration { + type = "kubenix"; + # A kubenix 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 = "kubenix"; + class = "kubenix"; + # A kubenix 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.kubenix.configurations.` 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.kubenix.`), if any. Further + # modules of the class are selected with `moduleImports`, + # from the registry. + configModule ? null, + # The kubenix flake or source tree; resolved from the + # composition's declarations 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 kubenix 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 { }) // { + kubenix = integration.namespace; + }; + }; + +} diff --git a/lib-overlays/nixidy/default.nix b/lib-overlays/nixidy/default.nix new file mode 100644 index 00000000..1f31b393 --- /dev/null +++ b/lib-overlays/nixidy/default.nix @@ -0,0 +1,202 @@ +# SPDX-License-Identifier: MIT +# +# The nixidy integration: it owns the `nixidy` class, the class of +# nixidy's modules. A nixidy configuration is an environment of Argo CD +# applications, written as nixidy modules (`applications.*`, +# `nixidy.target`). It is evaluated at every system in force where it +# is declared, on the package set selected for it. +# +# caisson evaluates it with nixidy's own `mkEnv`, the function the +# nixidy flake publishes as `lib.mkEnv`. `mkEnv` runs the module system +# of the library it is given, here the composed library, and gives the +# modules nixidy's library extended by its `libOverlay`, here the +# namespaces of the composed library that nixidy's library does not +# have, such as `caisson`. nixidy returns the evaluated configuration +# and its packages but not its options, so a nixidy configuration holds +# no configurations beneath it. +# +# The outputs are nixidy's packages: `environmentPackage`, the +# rendered manifests, `activationPackage` and `bootstrapPackage`. A +# configuration is published as `nixidyEnvs..`, which the +# nixidy CLI reads. +{ + 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.nixidy.mkConfiguration"; + + resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc { + name = "nixidy"; + context = "caisson.nixidy"; + }; + + # A nixidy source: the nixidy flake, which publishes `lib.mkEnv`. + assertNixidySrc = + ecosystemSrc: + if builtins.isAttrs ecosystemSrc && (ecosystemSrc.lib or { }) ? mkEnv then + ecosystemSrc + else + throw "${context} requires `ecosystemSrc` to be the nixidy flake (publishing lib.mkEnv)."; + + evaluate = + args: + { lib, manifest }: + let + selection = lib.caisson.integrations; + src = assertNixidySrc (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 nixidy configuration `${name}` at ${system}" + else + "this nixidy configuration at ${system}"; + + pkgSets = selection.pkgSetsAt { inherit context what; } manifest system; + pkgs = selection.pkgSetOf { inherit context what; } manifest pkgSets; + + registry = lib.caisson.modules.nixidy or { }; + frameworkModules = selection.frameworkModules "nixidy" registry; + moduleImports = selection.moduleImportsOf "nixidy" { inherit lib manifest; } args; + + # The configuration's module: the module passed, else the + # configuration registered under the configuration's name + # (`configs/nixidy/`), else none. + registered = lib.caisson.configs.nixidy or { }; + configModule = + if (args.configModule or null) != null then + args.configModule + else if name != null then + registered.${name} or null + else + null; + + value = src.lib.mkEnv ( + { + inherit pkgs lib; + modules = + frameworkModules + ++ moduleImports registry + ++ (if configModule == null then [ ] else [ configModule ]); + extraSpecialArgs = { + inherit pkgSets; + } + // (if (args.specialArgs or null) != null then args.specialArgs else { }); + # The namespaces of the composed library that nixidy's + # library does not have. + libOverlay = _self: super: builtins.removeAttrs lib (builtins.attrNames super); + } + // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) + ); + in + { + inherit value; + outputs = { + inherit (value) environmentPackage activationPackage bootstrapPackage; + }; + inherit (value) config; + options = { }; + }; + + # A nixidy configuration is published as + # `nixidyEnvs..`, which the nixidy CLI reads. + exportsTo = { + attrset = "nixidyEnvs"; + bySystem = true; + value = manifest: manifest.value; + }; + + configuration = + args: + final.caisson.integrations.mkModuleConfiguration { + type = "nixidy"; + # A nixidy 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 = "nixidy"; + class = "nixidy"; + # A nixidy 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.nixidy.configurations.` 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.nixidy.`), if any. Further + # modules of the class are selected with `moduleImports`, + # from the registry. + configModule ? null, + # The nixidy flake; resolved from the composition's + # declarations 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 nixidy 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; nixidy names these `extraSpecialArgs`. + 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 { }) // { + nixidy = integration.namespace; + }; + }; + +} diff --git a/lib-overlays/topology/default.nix b/lib-overlays/topology/default.nix new file mode 100644 index 00000000..f9ab49cd --- /dev/null +++ b/lib-overlays/topology/default.nix @@ -0,0 +1,238 @@ +# SPDX-License-Identifier: MIT +# +# The topology integration: it owns the `topology` class, the class of +# nix-topology's modules. A topology configuration is the diagram of a +# network of machines: the NixOS configurations declared inside it, +# whose `topology` options nix-topology reads, and the modules of the +# class, which add nodes and networks of their own. It is evaluated at +# every system in force where it is declared, on the package set +# selected for it. +# +# nix-topology evaluates its modules with `evalModules` over its +# `topology` directory, given the NixOS configurations to read as the +# option `nixosConfigurations`. caisson makes that evaluation on the +# composed library, on the selected package set extended with the +# package overlay of nix-topology, and sets `nixosConfigurations` to +# the NixOS configurations declared inside the topology configuration, +# evaluated at its system. Those NixOS configurations get the NixOS +# module of nix-topology by default, through `caisson.forChildren`. +# +# The output is `output`, the rendered diagram. The configuration +# declared under the name `default` is published as `topology.`, +# the evaluation, where nix-topology's flake module publishes it, so +# `nix build .#topology..config.output` builds the diagram. +{ + 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.topology.mkConfiguration"; + + resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc { + name = "nix-topology"; + context = "caisson.topology"; + }; + + # A nix-topology source: the nix-topology flake or source tree, + # which holds the topology modules, its NixOS module and its + # package overlay. + assertTopologySrc = + ecosystemSrc: + let + path = if builtins.isAttrs ecosystemSrc then ecosystemSrc.outPath or null else ecosystemSrc; + in + if + path != null + && builtins.pathExists "${path}/topology/default.nix" + && builtins.pathExists "${path}/nixos/module.nix" + && builtins.pathExists "${path}/pkgs/default.nix" + then + ecosystemSrc + else + throw "${context} requires `ecosystemSrc` to be a nix-topology flake or source tree (holding topology, nixos/module.nix and pkgs/default.nix)."; + + evaluate = + args: + { lib, manifest }: + let + selection = lib.caisson.integrations; + src = assertTopologySrc (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 topology configuration `${name}` at ${system}" + else + "this topology configuration at ${system}"; + + pkgSets = selection.pkgSetsAt { inherit context what; } manifest system; + pkgs = (selection.pkgSetOf { inherit context what; } manifest pkgSets).extend ( + import "${src}/pkgs/default.nix" + ); + + registry = lib.caisson.modules.topology or { }; + frameworkModules = selection.frameworkModules "topology" registry; + moduleImports = selection.moduleImportsOf "topology" { inherit lib manifest; } args; + + # The configuration's module: the module passed, else the + # configuration registered under the configuration's name + # (`configs/topology/`), else none. + registered = lib.caisson.configs.topology or { }; + configModule = + if (args.configModule or null) != null then + args.configModule + else if name != null then + registered.${name} or null + else + null; + + # The NixOS configurations declared inside, at this system, as + # the configurations nix-topology reads. They are read in the + # full evaluation; the childless evaluation reads none. + machinesModule = + { config, lib, ... }: + { + _file = "caisson-topology:machines"; + nixosConfigurations = + if lib.caisson-core.evalManifest.childless then + { } + else + builtins.mapAttrs (_: evaluations: evaluations.${system}.value) ( + lib.filterAttrs (_: evaluations: evaluations ? ${system}) config.caisson.nixos.configurations + ); + # The NixOS configurations declared inside get the NixOS + # module of nix-topology, which declares the options + # nix-topology reads. + caisson.forChildren.extraDefaultModuleImports.nixos = _lib: [ "${src}/nixos/module.nix" ]; + }; + + value = lib.evalModules ( + { + class = "topology"; + prefix = [ "topology" ]; + specialArgs = { + inherit lib pkgs pkgSets; + modulesPath = toString "${src}/topology"; + } + // (if (args.specialArgs or null) != null then args.specialArgs else { }); + modules = [ + "${src}/topology" + machinesModule + ] + ++ frameworkModules + ++ moduleImports registry + ++ (if configModule == null then [ ] else [ configModule ]); + } + // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) + ); + in + { + inherit value; + outputs.output = value.config.output; + }; + + # The configuration named `default` is published as + # `topology.`, the evaluation. + exportsTo = { + attrset = "topology"; + single = true; + bySystem = true; + value = manifest: manifest.value; + }; + + configuration = + args: + final.caisson.integrations.mkModuleConfiguration { + type = "topology"; + # A topology 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 = "topology"; + class = "topology"; + # A topology 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.topology.configurations.` finalizes it, and + # `mkTopConfiguration` finalizes it at a top. + mkConfiguration = + { + # The configuration's module: the nodes and networks of the + # diagram, and the NixOS configurations it reads under + # `caisson.nixos.configurations`. When absent, the + # configuration registered under the configuration's name + # (`lib.caisson.configs.topology.`), if any. Further + # modules of the class are selected with `moduleImports`, + # from the registry. + configModule ? null, + # The nix-topology flake or source tree; resolved from the + # composition's declarations under the name `nix-topology` + # 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 topology 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 { }) // { + topology = integration.namespace; + }; + }; + +} diff --git a/modules/generic/core/default.nix b/modules/generic/core/default.nix index f7fcb3b5..cfc89466 100644 --- a/modules/generic/core/default.nix +++ b/modules/generic/core/default.nix @@ -38,6 +38,8 @@ "flake-parts" "hjem" "home-manager" + "kubenix" + "nixidy" "nixos" "nixpkgs" "nixvim" @@ -45,6 +47,7 @@ "structural" "system-manager" "terranix" + "topology" "treefmt" ]) ] diff --git a/modules/kubenix/core b/modules/kubenix/core new file mode 120000 index 00000000..3715354a --- /dev/null +++ b/modules/kubenix/core @@ -0,0 +1 @@ +../generic/core \ No newline at end of file diff --git a/modules/nixidy/core b/modules/nixidy/core new file mode 120000 index 00000000..3715354a --- /dev/null +++ b/modules/nixidy/core @@ -0,0 +1 @@ +../generic/core \ No newline at end of file diff --git a/modules/topology/core b/modules/topology/core new file mode 120000 index 00000000..3715354a --- /dev/null +++ b/modules/topology/core @@ -0,0 +1 @@ +../generic/core \ No newline at end of file diff --git a/tests/argument-errors/check.sh b/tests/argument-errors/check.sh index b48b4e2e..cf81e8ae 100755 --- a/tests/argument-errors/check.sh +++ b/tests/argument-errors/check.sh @@ -43,6 +43,9 @@ namespaces=( treefmt nvf nixvim + topology + kubenix + nixidy ) failures=0 diff --git a/tests/dependencies/flake.lock b/tests/dependencies/flake.lock index 6d517bb1..89d88bfe 100644 --- a/tests/dependencies/flake.lock +++ b/tests/dependencies/flake.lock @@ -39,6 +39,27 @@ "type": "github" } }, + "devshell": { + "inputs": { + "nixpkgs": [ + "kubenix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1768818222, + "narHash": "sha256-460jc0+CZfyaO8+w8JNtlClB2n4ui1RbHfPTLkpwhU8=", + "owner": "numtide", + "repo": "devshell", + "rev": "255a2b1725a20d060f566e4755dbf571bbbb5f76", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, "disko": { "inputs": { "nixpkgs": [ @@ -76,6 +97,22 @@ } }, "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-compat_3": { "flake": false, "locked": { "lastModified": 1777699697, @@ -91,7 +128,7 @@ "url": "https://git.lix.systems/lix-project/flake-compat.git" } }, - "flake-compat_3": { + "flake-compat_4": { "flake": false, "locked": { "lastModified": 1767039857, @@ -128,6 +165,42 @@ } }, "flake-parts_2": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1785627969, + "narHash": "sha256-4dtXQk/NMePegK/nWp5NSeuZKLATItOq61lpEvmXqGw=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "427bf4bd9435fdf21321c8cc628c24efc14c0f7a", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_3": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib_2" + }, + "locked": { + "lastModified": 1772408722, + "narHash": "sha256-rHuJtdcOjK7rAHpHphUb1iCvgkU3GpfvicLMwwnfMT0=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f20dc5d9b8027381c474144ecabc9034d6a839a3", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_4": { "inputs": { "nixpkgs-lib": [ "nixvim", @@ -148,7 +221,7 @@ "type": "github" } }, - "flake-parts_3": { + "flake-parts_5": { "inputs": { "nixpkgs-lib": [ "system-manager", @@ -170,7 +243,7 @@ "type": "github" } }, - "flake-parts_4": { + "flake-parts_6": { "inputs": { "nixpkgs-lib": [ "terranix", @@ -209,6 +282,24 @@ "type": "github" } }, + "flake-utils_2": { + "inputs": { + "systems": "systems_3" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "gitignore": { "inputs": { "nixpkgs": [ @@ -287,6 +378,47 @@ "type": "github" } }, + "import-tree_2": { + "locked": { + "lastModified": 1784254960, + "narHash": "sha256-iI88R3wHz8wTKQb5orvpc51L/Xr64AJyxid/0MKa/b8=", + "owner": "vic", + "repo": "import-tree", + "rev": "4ebb10ae17d5f1ad366e7aef5b92cb8eecf24f69", + "type": "github" + }, + "original": { + "owner": "vic", + "repo": "import-tree", + "type": "github" + } + }, + "kubenix": { + "inputs": { + "devshell": "devshell", + "flake-compat": "flake-compat_2", + "flake-parts": "flake-parts_2", + "import-tree": "import-tree", + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems_2", + "treefmt-nix": "treefmt-nix" + }, + "locked": { + "lastModified": 1787550939, + "narHash": "sha256-BlFIpyBO7uYiw1Ra/uPLS93I4wUK1A6oAPmG2CxCwVE=", + "owner": "hall", + "repo": "kubenix", + "rev": "6a37ce99125b068a7da9eb7bebd29898ed043eb5", + "type": "github" + }, + "original": { + "owner": "hall", + "repo": "kubenix", + "type": "github" + } + }, "mnw": { "locked": { "lastModified": 1784154424, @@ -364,13 +496,49 @@ "type": "github" } }, + "nix-kube-generators": { + "locked": { + "lastModified": 1748274972, + "narHash": "sha256-Z8Bh7Y9Rr8PPspfb5M1nNW+zdwdyt43htN1lDqiH09M=", + "owner": "farcaller", + "repo": "nix-kube-generators", + "rev": "eeeaeacd7b7a2618baf08b8458f3dc4766752df8", + "type": "github" + }, + "original": { + "owner": "farcaller", + "repo": "nix-kube-generators", + "type": "github" + } + }, + "nix-topology": { + "inputs": { + "flake-parts": "flake-parts_3", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1782153600, + "narHash": "sha256-Vbkgl7/x8+Gg0gMe94EStfIfNC4bHQIbaZrIAxpuQ/w=", + "owner": "oddlama", + "repo": "nix-topology", + "rev": "e61876f548e4f301d63640d30ecc1305c05c3986", + "type": "github" + }, + "original": { + "owner": "oddlama", + "repo": "nix-topology", + "type": "github" + } + }, "nix-unit": { "inputs": { "nix-github-actions": "nix-github-actions_2", "nixpkgs": [ "nixpkgs" ], - "treefmt-nix": "treefmt-nix" + "treefmt-nix": "treefmt-nix_2" }, "locked": { "lastModified": 1779338171, @@ -386,6 +554,28 @@ "type": "github" } }, + "nixidy": { + "inputs": { + "flake-utils": "flake-utils_2", + "nix-kube-generators": "nix-kube-generators", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1792062694, + "narHash": "sha256-uEacRc07s+nJl2ZQ1VejSl9fgdpureM+4uZQJF26uZk=", + "owner": "arnarg", + "repo": "nixidy", + "rev": "9a2521f7ae2b56b87070de09a5b78ab14b440220", + "type": "github" + }, + "original": { + "owner": "arnarg", + "repo": "nixidy", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1789785513, @@ -403,6 +593,36 @@ } }, "nixpkgs-lib": { + "locked": { + "lastModified": 1785031560, + "narHash": "sha256-OmshNvn2vupOFpYinLUu+1Dnpu4n7Q5N3ggGVNHpkUI=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "0e79af5e3d4dcfcd676ab5ba3f95d2e3352e078c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "nixpkgs-lib_2": { + "locked": { + "lastModified": 1772328832, + "narHash": "sha256-e+/T/pmEkLP6BHhYjx6GmwP5ivonQQn0bJdH9YrRB+Q=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "c185c7a5e5dd8f9add5b2f8ebeff00888b070742", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "nixpkgs-lib_3": { "locked": { "lastModified": 1789266065, "narHash": "sha256-XtTdVeJX6X/gU+vGIUEJHirotv/lYP5+Q9irMH6HWPY=", @@ -419,11 +639,11 @@ }, "nixvim": { "inputs": { - "flake-parts": "flake-parts_2", + "flake-parts": "flake-parts_4", "nixpkgs": [ "nixpkgs" ], - "systems": "systems_2" + "systems": "systems_4" }, "locked": { "lastModified": 1792203519, @@ -441,7 +661,7 @@ }, "nvf": { "inputs": { - "flake-compat": "flake-compat_2", + "flake-compat": "flake-compat_3", "mnw": "mnw", "nixpkgs": [ "nixpkgs" @@ -497,15 +717,18 @@ "flake-parts": "flake-parts", "hjem": "hjem", "home-manager": "home-manager", + "kubenix": "kubenix", "nix-darwin": "nix-darwin", + "nix-topology": "nix-topology", "nix-unit": "nix-unit", + "nixidy": "nixidy", "nixpkgs": "nixpkgs", - "nixpkgs-lib": "nixpkgs-lib", + "nixpkgs-lib": "nixpkgs-lib_3", "nixvim": "nixvim", "nvf": "nvf", "system-manager": "system-manager", "terranix": "terranix", - "treefmt-nix": "treefmt-nix_2" + "treefmt-nix": "treefmt-nix_3" } }, "stable": { @@ -526,7 +749,7 @@ }, "system-manager": { "inputs": { - "flake-compat": "flake-compat_3", + "flake-compat": "flake-compat_4", "nixpkgs": [ "nixpkgs" ], @@ -562,6 +785,36 @@ } }, "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_3": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_4": { "flake": false, "locked": { "lastModified": 1791244623, @@ -578,7 +831,7 @@ "type": "github" } }, - "systems_3": { + "systems_5": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", @@ -593,7 +846,7 @@ "type": "github" } }, - "systems_4": { + "systems_6": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", @@ -610,12 +863,12 @@ }, "terranix": { "inputs": { - "flake-parts": "flake-parts_4", - "import-tree": "import-tree", + "flake-parts": "flake-parts_6", + "import-tree": "import-tree_2", "nixpkgs": [ "nixpkgs" ], - "systems": "systems_4" + "systems": "systems_6" }, "locked": { "lastModified": 1784465933, @@ -632,6 +885,27 @@ } }, "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "kubenix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1786901030, + "narHash": "sha256-WSFCsDSE5ffgD2MqzkM2CYjeFiKhRF/dJUN8uedb6YE=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "27b3b12a8e6375f28ebe122f07d230ca5459bbfa", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + }, + "treefmt-nix_2": { "inputs": { "nixpkgs": [ "nix-unit", @@ -652,7 +926,7 @@ "type": "github" } }, - "treefmt-nix_2": { + "treefmt-nix_3": { "inputs": { "nixpkgs": [ "nixpkgs" @@ -678,13 +952,13 @@ "system-manager", "flake-compat" ], - "flake-parts": "flake-parts_3", + "flake-parts": "flake-parts_5", "nixpkgs": [ "system-manager", "nixpkgs" ], "pre-commit-hooks-nix": "pre-commit-hooks-nix", - "systems": "systems_3" + "systems": "systems_5" }, "locked": { "lastModified": 1770377964, diff --git a/tests/dependencies/flake.nix b/tests/dependencies/flake.nix index 459b35dc..8ee43b2c 100644 --- a/tests/dependencies/flake.nix +++ b/tests/dependencies/flake.nix @@ -21,6 +21,12 @@ inputs.system-manager.inputs.nixpkgs.follows = "nixpkgs"; inputs.disko.url = "github:nix-community/disko"; inputs.disko.inputs.nixpkgs.follows = "nixpkgs"; + inputs.nix-topology.url = "github:oddlama/nix-topology"; + inputs.nix-topology.inputs.nixpkgs.follows = "nixpkgs"; + inputs.kubenix.url = "github:hall/kubenix"; + inputs.kubenix.inputs.nixpkgs.follows = "nixpkgs"; + inputs.nixidy.url = "github:arnarg/nixidy"; + inputs.nixidy.inputs.nixpkgs.follows = "nixpkgs"; inputs.nixvim.url = "github:nix-community/nixvim"; inputs.nixvim.inputs.nixpkgs.follows = "nixpkgs"; inputs.nvf.url = "github:NotAShelf/nvf"; diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 0939136f..4b97e788 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -74,6 +74,9 @@ let registered."caisson/treefmt" registered."caisson/nvf" registered."caisson/nixvim" + registered."caisson/topology" + registered."caisson/kubenix" + registered."caisson/nixidy" registered."caisson/structural" # Last, so its value replaces the pin the overlays carry while # its position stays where the first import placed it. @@ -105,6 +108,7 @@ let "home-manager" "importApply" "integrations" + "kubenix" "libManifest" "libOverlays" "manifestOf" @@ -117,6 +121,7 @@ let "mkPkgOverlay" "mkPkgOverlays" "modules" + "nixidy" "nixos" "nixos-minimal" "nixpkgs" @@ -130,6 +135,7 @@ let "structural" "system-manager" "terranix" + "topology" "treefmt" ]; @@ -489,6 +495,9 @@ let "caisson/treefmt" "caisson/nvf" "caisson/nixvim" + "caisson/topology" + "caisson/kubenix" + "caisson/nixidy" "caisson/structural" ]; @@ -591,6 +600,9 @@ let "treefmt" "nvf" "nixvim" + "topology" + "kubenix" + "nixidy" ]; minimalNixosSystemEvaluates = @@ -1765,6 +1777,130 @@ let top.nixvimConfigurations.x86_64-linux.editor.config.build.package.drvPath == upstream.config.build.package.drvPath; + # A topology configuration with two machines declared inside it reads + # them as nix-topology reads NixOS configurations evaluated with its + # NixOS module: the same nodes, and the same rendered diagram. It is + # published as `topology.` under the name `default`, and its + # machines as `nixosConfigurations`. + topologyReadsTheMachinesDeclaredInside = + let + machine = { + boot.loader.grub.enable = false; + fileSystems."/" = { + device = "none"; + fsType = "tmpfs"; + }; + system.stateVersion = "25.05"; + }; + top = hiveLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = + { lib, ... }: + { + caisson.topology.configurations.default = lib.caisson.topology.mkConfiguration { + ecosystemSrc = inputs.nix-topology; + configModule = + { lib, ... }: + { + caisson.nixos.configurations.alpha = lib.caisson.nixos.mkConfiguration { + configModule = machine; + }; + caisson.nixos.configurations.beta = lib.caisson.nixos.mkConfiguration { + configModule = machine; + }; + }; + }; + }; + }; + upstreamMachine = + hostName: + import "${inputs.nixpkgs}/nixos/lib/eval-config.nix" { + system = "x86_64-linux"; + modules = [ + machine + "${inputs.nix-topology}/nixos/module.nix" + { networking.hostName = hostName; } + ]; + }; + upstream = import inputs.nix-topology { + pkgs = pkgs.extend (import "${inputs.nix-topology}/pkgs/default.nix"); + modules = [ + { + nixosConfigurations = { + alpha = upstreamMachine "alpha"; + beta = upstreamMachine "beta"; + }; + } + ]; + }; + topology = top.topology.x86_64-linux; + in + builtins.attrNames top.nixosConfigurations == [ + "alpha" + "beta" + ] + && builtins.attrNames topology.config.nodes == builtins.attrNames upstream.config.nodes + && builtins.elem "alpha" (builtins.attrNames topology.config.nodes) + && topology.config.output.drvPath == upstream.config.output.drvPath; + + # A kubenix configuration is published as `packages..`, + # its result, which is the result kubenix's own `evalModules` gives + # the same module on the same package set. + kubenixIsTheResultKubenixBuilds = + let + module = + { kubenix, ... }: + { + imports = [ kubenix.modules.k8s ]; + kubernetes.resources.configMaps.demo.data.greeting = "hello"; + }; + top = hiveLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = + { lib, ... }: + { + caisson.kubenix.configurations.cluster = lib.caisson.kubenix.mkConfiguration { + ecosystemSrc = inputs.kubenix; + configModule = module; + }; + }; + }; + upstream = import "${inputs.kubenix}/lib/eval-modules.nix" { inherit pkgs; } { inherit module; }; + in + top.packages.x86_64-linux.cluster.drvPath == upstream.config.kubernetes.result.drvPath; + + # A nixidy configuration is published as + # `nixidyEnvs..`, and its rendered environment is the + # one nixidy's own `mkEnv` renders from the same module on the same + # package set. + nixidyIsTheEnvironmentNixidyRenders = + let + module = { + nixidy.target.repository = "https://example.com/manifests.git"; + nixidy.target.branch = "main"; + applications.demo = { + namespace = "demo"; + resources.configMaps.demo.data.greeting = "hello"; + }; + }; + top = hiveLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = + { lib, ... }: + { + caisson.nixidy.configurations.prod = lib.caisson.nixidy.mkConfiguration { + ecosystemSrc = inputs.nixidy; + configModule = module; + }; + }; + }; + upstream = inputs.nixidy.lib.mkEnv { + inherit pkgs; + modules = [ module ]; + }; + in + top.nixidyEnvs.x86_64-linux.prod.environmentPackage.drvPath == upstream.environmentPackage.drvPath; + # A terranix configuration that is a top is its `config.tf.json` # file, built from the package set of the composition with the # evaluated configuration in its `passthru`, and its modules get the diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index 8b618afd..e34bb3a5 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -129,6 +129,9 @@ let "treefmt" "nvf" "nixvim" + "topology" + "kubenix" + "nixidy" ]; in @@ -5395,6 +5398,9 @@ in "treefmt" "nvf" "nixvim" + "topology" + "kubenix" + "nixidy" ]; setsFromTheComposition = builtins.elem name [ "nixos" @@ -5410,6 +5416,9 @@ in "treefmt" "nvf" "nixvim" + "topology" + "kubenix" + "nixidy" ]; in signature ? configModule From 764b9db3217d5d257d6814784a3d71ee8c2aa70a Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 10:06:39 -0700 Subject: [PATCH 05/14] maint: say a configuration is evaluated per system A configuration with `perSystem` has an evaluation for each system in force; the comments called it "evaluated at a system", which names one of those evaluations. Co-Authored-By: Claude Opus 5.5 --- lib-overlays/darwin/default.nix | 2 +- lib-overlays/hjem/default.nix | 2 +- lib-overlays/home-manager/default.nix | 2 +- lib-overlays/integrations/default.nix | 6 +++--- lib-overlays/kubenix/default.nix | 2 +- lib-overlays/nixidy/default.nix | 2 +- lib-overlays/nixos/default.nix | 2 +- lib-overlays/nixvim/default.nix | 2 +- lib-overlays/nvf/default.nix | 2 +- lib-overlays/system-manager/default.nix | 2 +- lib-overlays/terranix/default.nix | 2 +- lib-overlays/topology/default.nix | 2 +- lib-overlays/treefmt/default.nix | 2 +- modules/generic/core/caisson/configurations.nix | 2 +- tests/hjem-system/default.nix | 4 ++-- tests/unit/lib-overlays.nix | 2 +- 16 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib-overlays/darwin/default.nix b/lib-overlays/darwin/default.nix index 7723be7b..1b3c605a 100644 --- a/lib-overlays/darwin/default.nix +++ b/lib-overlays/darwin/default.nix @@ -159,7 +159,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "darwin"; - # A darwin configuration is evaluated at a system: it has an + # A darwin configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/hjem/default.nix b/lib-overlays/hjem/default.nix index 5adc3221..aa17d9d6 100644 --- a/lib-overlays/hjem/default.nix +++ b/lib-overlays/hjem/default.nix @@ -273,7 +273,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "hjem"; - # An hjem configuration is evaluated at a system: it has an + # An hjem configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/home-manager/default.nix b/lib-overlays/home-manager/default.nix index ee5f8191..9674b65b 100644 --- a/lib-overlays/home-manager/default.nix +++ b/lib-overlays/home-manager/default.nix @@ -509,7 +509,7 @@ variant: args: selection.mkModuleConfiguration { type = "home-manager"; - # A home is evaluated at a system: it has an evaluation for + # A home is evaluated per system: it has an evaluation for # every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index 90e98dc4..200dacdd 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -226,7 +226,7 @@ # A configuration that is a module evaluation. `type` is the # name of the integration and `perSystem` whether it evaluates a - # configuration at a system, as caisson-core.mkConfiguration + # configuration per system, as caisson-core.mkConfiguration # takes them. `evaluate` is the evaluator step of the # integration: it takes the view being evaluated, `{ lib, # manifest }`, and returns `value`, the evaluation as the @@ -462,7 +462,7 @@ } child; # The integration of what was declared: of the # manifest, or of the evaluations of a configuration - # evaluated at a system, null where it has none. + # evaluated per system, null where it has none. declaredType = if isManifest finalized then finalized.type @@ -803,7 +803,7 @@ genAttrs attrsets publishIn; # What a tool reads from a top that is a configuration evaluated - # at a system, given its evaluations by system. They are named + # per system, given its evaluations by system. They are named # like anything published, by `caisson-core.elide` over their # paths; the top is addressed by the file that returns it, so the # name of the top is left out of each. Where that leaves a single diff --git a/lib-overlays/kubenix/default.nix b/lib-overlays/kubenix/default.nix index 530e8368..238432ea 100644 --- a/lib-overlays/kubenix/default.nix +++ b/lib-overlays/kubenix/default.nix @@ -166,7 +166,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "kubenix"; - # A kubenix configuration is evaluated at a system: it has an + # A kubenix configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/nixidy/default.nix b/lib-overlays/nixidy/default.nix index 1f31b393..82e9692d 100644 --- a/lib-overlays/nixidy/default.nix +++ b/lib-overlays/nixidy/default.nix @@ -129,7 +129,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "nixidy"; - # A nixidy configuration is evaluated at a system: it has an + # A nixidy configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/nixos/default.nix b/lib-overlays/nixos/default.nix index 52c8521a..037b5fcd 100644 --- a/lib-overlays/nixos/default.nix +++ b/lib-overlays/nixos/default.nix @@ -109,7 +109,7 @@ variant: args: final.caisson.integrations.mkModuleConfiguration { type = "nixos"; - # A NixOS configuration is evaluated at a system: it has an + # A NixOS configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/nixvim/default.nix b/lib-overlays/nixvim/default.nix index 14bc71a0..cb955d8a 100644 --- a/lib-overlays/nixvim/default.nix +++ b/lib-overlays/nixvim/default.nix @@ -151,7 +151,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "nixvim"; - # A nixvim configuration is evaluated at a system: it has an + # A nixvim configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix index 1ae28bb6..6792d4d7 100644 --- a/lib-overlays/nvf/default.nix +++ b/lib-overlays/nvf/default.nix @@ -202,7 +202,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "nvf"; - # An nvf configuration is evaluated at a system: it has an + # An nvf configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/system-manager/default.nix b/lib-overlays/system-manager/default.nix index 9a0fe409..1cd74ef3 100644 --- a/lib-overlays/system-manager/default.nix +++ b/lib-overlays/system-manager/default.nix @@ -294,7 +294,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "system-manager"; - # A system-manager configuration is evaluated at a system: it + # A system-manager configuration is evaluated per system: it # has an evaluation for every system in force where it is # declared. perSystem = true; diff --git a/lib-overlays/terranix/default.nix b/lib-overlays/terranix/default.nix index 6c9701aa..7939c1fe 100644 --- a/lib-overlays/terranix/default.nix +++ b/lib-overlays/terranix/default.nix @@ -145,7 +145,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "terranix"; - # A terranix configuration is evaluated at a system: it has an + # A terranix configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/topology/default.nix b/lib-overlays/topology/default.nix index f9ab49cd..ee522004 100644 --- a/lib-overlays/topology/default.nix +++ b/lib-overlays/topology/default.nix @@ -162,7 +162,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "topology"; - # A topology configuration is evaluated at a system: it has an + # A topology configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/lib-overlays/treefmt/default.nix b/lib-overlays/treefmt/default.nix index 2a5db890..46e300b3 100644 --- a/lib-overlays/treefmt/default.nix +++ b/lib-overlays/treefmt/default.nix @@ -151,7 +151,7 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "treefmt"; - # A treefmt configuration is evaluated at a system: it has an + # A treefmt configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; diff --git a/modules/generic/core/caisson/configurations.nix b/modules/generic/core/caisson/configurations.nix index 06b02d47..ade157f6 100644 --- a/modules/generic/core/caisson/configurations.nix +++ b/modules/generic/core/caisson/configurations.nix @@ -37,7 +37,7 @@ let integration: config.caisson.${integration}.exported config.caisson.${integration}.configurations ); - # The manifests passed up: a configuration evaluated at a system + # The manifests passed up: a configuration evaluated per system # stands for each of its evaluations. exported = builtins.concatMap ( integration: diff --git a/tests/hjem-system/default.nix b/tests/hjem-system/default.nix index 1194fa41..925c7eba 100644 --- a/tests/hjem-system/default.nix +++ b/tests/hjem-system/default.nix @@ -113,8 +113,8 @@ in user ; # The machine and the hjem configuration `probe` inside it, as their - # manifests. A configuration evaluated at a system sits under that - # system in the manifest of its parent. + # manifests. Each evaluation of a configuration evaluated per system + # sits under its system in the manifest of its parent. machineManifest = top.caisson.manifest.children.system.${system}.children.nixos.probe-machine; inner = top.caisson.manifest.children.system.${system}.children.nixos.probe-machine.children.system.${system}.children.hjem.probe; diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index e34bb3a5..bb595812 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -5367,7 +5367,7 @@ in # The entry points whose constructor returns a configuration take # `configModule` as optional: each finds the configuration # registered under the name of the configuration. The entry points - # of the configurations evaluated at a system take no `pkgSets`: + # of the configurations evaluated per system take no `pkgSets`: # those configurations take their package sets from the # composition. The structural, colmena, disko and flake-parts # entry points take `pkgSets` and hand it to their modules. From 88ef9c9f6637b17cb1c0c4d18a6faadb8dddcb2b Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 10:50:08 -0700 Subject: [PATCH 06/14] feat(system-manager): activate the homes, hjem and nvf configurations declared inside a machine A system-manager configuration now acts as the parent of the configurations declared inside it, as a NixOS or darwin machine does. Its new registered `default` module imports: - `home-manager-activation` and `hjem-activation`, the NixOS modules registered a second time for the `systemManager` class. They import the core module of the class they are evaluated in, and the hjem module keeps extra dependencies only where the class declares `system.extraDependencies`. - `nvf-package`, which installs the Neovim of each nvf configuration in `environment.systemPackages`. - `caisson.forChildren` settings that give homes and hjem configurations the `system-manager-parent` module, the `nixos-parent` module under a second name, which reads the nearest NixOS or system-manager machine. system-manager counts as a machine for naming homes (`user@machine`), for the `osConfig` and `utils` an hjem configuration receives, and for the check that leaves out what an inner machine activates. A pinned-world test declares a home, an hjem configuration and an nvf configuration inside a system-manager machine and checks the units, the packages and the published home name. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +- lib-overlays/hjem/default.nix | 28 ++++++--- lib-overlays/home-manager/default.nix | 4 +- modules/darwin/hjem-activation/default.nix | 11 +++- .../home-manager-activation/default.nix | 9 ++- modules/hjem/nixos-parent/default.nix | 37 ++++++----- modules/hjem/system-manager-parent | 1 + modules/homeManager/nixos-parent/default.nix | 24 ++++--- modules/homeManager/system-manager-parent | 1 + modules/nixos/hjem-activation/default.nix | 51 +++++++++++---- .../nixos/home-manager-activation/default.nix | 49 +++++++++++---- .../systemManager/default/caisson/default.nix | 13 ++++ .../default/caisson/forChildren.nix | 33 ++++++++++ modules/systemManager/default/default.nix | 39 ++++++++++++ modules/systemManager/hjem-activation | 1 + modules/systemManager/home-manager-activation | 1 + modules/systemManager/nvf-package/default.nix | 11 ++++ tests/pinned-world/default.nix | 63 +++++++++++++++++++ 18 files changed, 319 insertions(+), 61 deletions(-) create mode 120000 modules/hjem/system-manager-parent create mode 120000 modules/homeManager/system-manager-parent create mode 100644 modules/systemManager/default/caisson/default.nix create mode 100644 modules/systemManager/default/caisson/forChildren.nix create mode 100644 modules/systemManager/default/default.nix create mode 120000 modules/systemManager/hjem-activation create mode 120000 modules/systemManager/home-manager-activation create mode 100644 modules/systemManager/nvf-package/default.nix diff --git a/README.md b/README.md index d2d3e3c8..fc6997c2 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ source by. | `nixos-minimal` | `nixpkgs` | the `nixos` class under the minimal evaluator (no NixOS base modules); a second integration over the class `nixos` owns | | `home-manager` | `home-manager` | Home Manager configurations | | `home-manager-minimal` | `home-manager` | the `homeManager` class with Home Manager's necessary modules alone, the configuration importing what it uses from `modulesPath`; a second integration over the class `home-manager` owns | -| `hjem` | `hjem` | the files and packages of a user, as hjem manifests; activated by the NixOS configuration they are declared inside, or applied with `hjem standalone` | +| `hjem` | `hjem` | the files and packages of a user, as hjem manifests; activated by the NixOS, darwin or system-manager configuration they are declared inside, or applied with `hjem standalone` | | `terranix` | `terranix` | Terraform configurations written as terranix modules, published as `terranixConfigurations` of the `config.tf.json` files Terraform reads | | `caisson-colmena` | `colmena` | colmena configurations whose machines are NixOS configurations declared inside them, published as the hive colmena's command reads; the colmena integration we recommend for new configurations | | `colmena` | `colmena` | a hive written for colmena, taken as it is, with each node a NixOS configuration; for porting an existing hive and the modules written for it | @@ -138,7 +138,7 @@ source by. | `topology` | `nix-topology` | the diagram of the NixOS configurations declared inside it, as nix-topology renders it; the configuration named `default` is published as `topology.` | | `kubenix` | `kubenix` | Kubernetes resources written as kubenix modules, published as `packages..`, the JSON `kubectl apply -f` reads | | `nixidy` | `nixidy` | an Argo CD environment written as nixidy modules, evaluated with nixidy's `mkEnv` and published as the `nixidyEnvs..` the nixidy CLI reads | -| `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads | +| `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads; they activate the homes and hjem configurations declared inside them, and install the Neovim of the nvf configurations declared inside them | | `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 | Each integration finds its ecosystem in this order: the `ecosystemSrc` diff --git a/lib-overlays/hjem/default.nix b/lib-overlays/hjem/default.nix index aa17d9d6..2484e971 100644 --- a/lib-overlays/hjem/default.nix +++ b/lib-overlays/hjem/default.nix @@ -130,12 +130,15 @@ null; # The machine the configuration is declared inside, when there - # is one: the childless evaluation of the nearest NixOS or - # darwin configuration above it, which hjem's modules receive - # as `osConfig`, null when there is none. + # is one: the childless evaluation of the nearest NixOS, + # darwin or system-manager configuration above it, which hjem's + # modules receive as `osConfig`, null when there is none. A + # system-manager evaluation is its system, with the + # configuration and the options in its `passthru`. machine = selection.nearestOf [ "nixos" "darwin" + "system-manager" ] manifest; osConfig = if machine != null then machine.value.config else null; @@ -143,10 +146,21 @@ # `modules/nixos/systemd.nix`, which declares the user's # systemd units and links them into the user's files, with the # `utils` and the options of the machine. A configuration inside - # a NixOS machine that has `utils` gets the same; the minimal - # NixOS evaluator gives none. - machineArgs = if machine != null then (machine.value._module or { }).args or { } else { }; - nixosUser = machine != null && machine.type == "nixos" && machineArgs ? utils; + # a NixOS or system-manager machine that has `utils` gets the + # same; the minimal NixOS evaluator gives none. + machineArgs = + if machine == null then + { } + else + (machine.value._module or { }).args + or (((machine.value.options or { })._module or { }).args.value or { }); + nixosUser = + machine != null + && builtins.elem machine.type [ + "nixos" + "system-manager" + ] + && machineArgs ? utils; # What hjem's OS modules set on a user that caisson sets too: # the user's name from the name the configuration is declared diff --git a/lib-overlays/home-manager/default.nix b/lib-overlays/home-manager/default.nix index 9674b65b..ffaf3a97 100644 --- a/lib-overlays/home-manager/default.nix +++ b/lib-overlays/home-manager/default.nix @@ -235,7 +235,8 @@ }; # The machine the home is declared inside, when there is one: - # the nearest NixOS or darwin configuration above the home, so + # the nearest NixOS, darwin or system-manager configuration + # above the home, so # a structural configuration between the two changes nothing. # The manifest holds the childless evaluation of that machine, # which is the evaluation that leaves out the configurations @@ -476,6 +477,7 @@ machineTypes = [ "nixos" "darwin" + "system-manager" ]; # A home is published as `homeConfigurations.`, the diff --git a/modules/darwin/hjem-activation/default.nix b/modules/darwin/hjem-activation/default.nix index b9594e8c..5dacbc5b 100644 --- a/modules/darwin/hjem-activation/default.nix +++ b/modules/darwin/hjem-activation/default.nix @@ -48,9 +48,14 @@ let isActivatedHere = entry: entry.manifest.type == "hjem" - && !(builtins.any (segment: segment.type == "nixos" || segment.type == "darwin") ( - lib.lists.init entry.path - )) + && !(builtins.any ( + segment: + builtins.elem segment.type [ + "nixos" + "darwin" + "system-manager" + ] + ) (lib.lists.init entry.path)) && entry.manifest.system == evalManifest.system; users = diff --git a/modules/darwin/home-manager-activation/default.nix b/modules/darwin/home-manager-activation/default.nix index ef0f7fc0..dda143e1 100644 --- a/modules/darwin/home-manager-activation/default.nix +++ b/modules/darwin/home-manager-activation/default.nix @@ -51,7 +51,14 @@ let above = lib.lists.init entry.path; in entry.manifest.type == "home-manager" - && !(builtins.any (segment: segment.type == "nixos" || segment.type == "darwin") above) + && !(builtins.any ( + segment: + builtins.elem segment.type [ + "nixos" + "darwin" + "system-manager" + ] + ) above) && entry.manifest.system == evalManifest.system; homes = builtins.map (entry: { diff --git a/modules/hjem/nixos-parent/default.nix b/modules/hjem/nixos-parent/default.nix index 18235e8a..15184bf4 100644 --- a/modules/hjem/nixos-parent/default.nix +++ b/modules/hjem/nixos-parent/default.nix @@ -1,29 +1,36 @@ # SPDX-License-Identifier: MIT # -# An hjem module for an hjem configuration declared inside a NixOS -# configuration. It sets the home directory of the user from the -# account the machine declares, as hjem's NixOS module does for the -# users it embeds, and refuses a user the machine has no account for. +# An hjem module for an hjem configuration declared inside a NixOS or +# system-manager configuration. It sets the home directory of the user +# from the account the machine declares, as hjem's NixOS module does for +# the users it embeds, and refuses a user the machine has no account +# for. # -# caisson does not use hjem's NixOS module. A NixOS configuration adds -# this module to the modules that the hjem configurations declared -# inside it import by default; caisson's `default` NixOS module does -# that, through `caisson.forChildren`. An hjem configuration that -# passes `moduleImports` selects its modules itself, and gets this +# caisson does not use hjem's NixOS module. A NixOS or system-manager +# configuration adds this module to the modules that the hjem +# configurations declared inside it import by default; caisson's +# `default` module of either class does that, through +# `caisson.forChildren`. The module is registered as `nixos-parent` and, +# for the same file, as `system-manager-parent`. An hjem configuration +# that passes `moduleImports` selects its modules itself, and gets this # module only if it lists it. # # The module reads the machine through the manifest of the hjem # configuration, so it can only be imported by an hjem configuration -# that has a NixOS configuration above it. +# that has a NixOS or system-manager configuration above it. { ... }: # `lib` is the library of the hjem configuration, and `config` is its # configuration. { lib, config, ... }: let - # The configuration of the NixOS configuration the hjem configuration - # is declared inside, in its childless evaluation, which leaves out - # the configurations declared inside it. - machine = lib.caisson.evalManifest.nearest.nixos.value.config; + # The configuration of the machine the hjem configuration is declared + # inside, in its childless evaluation, which leaves out the + # configurations declared inside it. + machine = + (lib.caisson.integrations.nearestOf [ + "nixos" + "system-manager" + ] lib.caisson.evalManifest).value.config; # The account of the user, when the machine declares one. account = (machine.users.users or { }).${config.user} or null; @@ -34,7 +41,7 @@ in assertions = [ { assertion = config.enable -> account != null && (account.enable or true); - message = "The hjem user `${config.user}` has no enabled account in `users.users` of the NixOS configuration it is declared inside."; + message = "The hjem user `${config.user}` has no enabled account in `users.users` of the machine it is declared inside."; } ]; } diff --git a/modules/hjem/system-manager-parent b/modules/hjem/system-manager-parent new file mode 120000 index 00000000..dc907f13 --- /dev/null +++ b/modules/hjem/system-manager-parent @@ -0,0 +1 @@ +nixos-parent \ No newline at end of file diff --git a/modules/homeManager/nixos-parent/default.nix b/modules/homeManager/nixos-parent/default.nix index 7d08e402..ba550e91 100644 --- a/modules/homeManager/nixos-parent/default.nix +++ b/modules/homeManager/nixos-parent/default.nix @@ -16,22 +16,28 @@ # homes declared inside it import by default. caisson's `default` # NixOS module does that, through `caisson.forChildren`. A home that # passes `moduleImports` selects its modules itself, and gets this -# module only if it lists it. +# module only if it lists it. A system-manager configuration does the +# same with this file, registered also as `system-manager-parent`. # # The module reads the machine through the manifest of the home, so it -# can only be imported by a home that has a NixOS configuration above -# it. +# can only be imported by a home that has a NixOS or system-manager +# configuration above it. { ... }: # `lib` is the library of the home, and `config` is the configuration # of the home. { lib, config, ... }: let - # The configuration of the NixOS configuration the home is declared - # inside. The home reads the childless evaluation of the NixOS - # configuration, which is the evaluation that leaves out the - # configurations declared inside it. The home is one of those - # configurations, so it cannot read an evaluation that includes it. - machine = lib.caisson.evalManifest.nearest.nixos.value.config; + # The configuration of the machine the home is declared inside, the + # nearest NixOS or system-manager configuration above it. The home + # reads the childless evaluation of the machine, which is the + # evaluation that leaves out the configurations declared inside it. + # The home is one of those configurations, so it cannot read an + # evaluation that includes it. + machine = + (lib.caisson.integrations.nearestOf [ + "nixos" + "system-manager" + ] lib.caisson.evalManifest).value.config; # The account of the user of the home, when the NixOS configuration # declares one in `users.users`. It is null when the NixOS diff --git a/modules/homeManager/system-manager-parent b/modules/homeManager/system-manager-parent new file mode 120000 index 00000000..dc907f13 --- /dev/null +++ b/modules/homeManager/system-manager-parent @@ -0,0 +1 @@ +nixos-parent \ No newline at end of file diff --git a/modules/nixos/hjem-activation/default.nix b/modules/nixos/hjem-activation/default.nix index 0eaea2f1..70fc6261 100644 --- a/modules/nixos/hjem-activation/default.nix +++ b/modules/nixos/hjem-activation/default.nix @@ -39,6 +39,15 @@ # The module reads the hjem configurations from the full evaluation of # the NixOS configuration; in the childless evaluation, and in an # evaluation without systemd options, it defines nothing. +# +# The same module is registered for the `systemManager` class, under +# the same name, and a system-manager machine activates the hjem +# configurations declared inside it the same way. system-manager has no +# `system.extraDependencies`, so there the module keeps no extra +# dependencies in the system. The module reads +# `config.caisson.exports.configurations`, which the core module +# declares; it imports the core module of the class of the evaluation, +# read from its manifest. { closure-lib, ... }: { config, @@ -52,17 +61,33 @@ let # True when this evaluation declares the options the module defines. hasUnits = - options ? systemd - && options.systemd ? services - && options.systemd ? targets - && options ? users - && options ? system - && options.system ? extraDependencies; + options ? systemd && options.systemd ? services && options.systemd ? targets && options ? users; + + # True when the evaluation keeps extra dependencies in the system. + hasExtraDependencies = options ? system && options.system ? extraDependencies; + + # The core module of the class of this evaluation: of the + # `systemManager` class in a system-manager evaluation, else of the + # `nixos` class. The registered core modules of the two classes are + # one file under two names, so a module imports the one its + # evaluation already holds. + coreModule = + if (lib.caisson-core.evalManifest.type or null) == "system-manager" then + closure-lib.caisson.modules.systemManager.core + else + closure-lib.caisson.modules.nixos.core; + + # The integrations whose configurations are machines. + machineTypes = [ + "nixos" + "darwin" + "system-manager" + ]; isActivatedHere = entry: entry.manifest.type == "hjem" - && !(builtins.any (segment: segment.type == "nixos") (lib.lists.init entry.path)) + && !(builtins.any (segment: builtins.elem segment.type machineTypes) (lib.lists.init entry.path)) && entry.manifest.system == evalManifest.system; # What the units need of each enabled hjem configuration. @@ -119,9 +144,9 @@ let ); in { - # The module reads `config.caisson.exports.configurations`. The core - # module declares that option. - imports = [ closure-lib.caisson.modules.nixos.core ]; + # The core module of the class of this evaluation, which declares + # `config.caisson.exports.configurations`. + imports = [ coreModule ]; # `optionalAttrs` and not `mkIf` decides whether the options are # defined at all: NixOS refuses a definition, even under `mkIf false`, @@ -131,7 +156,7 @@ in assertions = [ { assertion = repeated == [ ]; - message = "caisson: this NixOS configuration activates more than one hjem configuration for ${lib.concatStringsSep ", " repeated}."; + message = "caisson: this machine activates more than one hjem configuration for ${lib.concatStringsSep ", " repeated}."; } ]; @@ -142,7 +167,9 @@ in }) users ); - system.extraDependencies = builtins.concatMap (user: user.extraDependencies) users; + system = lib.optionalAttrs hasExtraDependencies { + extraDependencies = builtins.concatMap (user: user.extraDependencies) users; + }; systemd.targets.hjem = { description = "hjem file management"; diff --git a/modules/nixos/home-manager-activation/default.nix b/modules/nixos/home-manager-activation/default.nix index 016b4bc6..020c4ebc 100644 --- a/modules/nixos/home-manager-activation/default.nix +++ b/modules/nixos/home-manager-activation/default.nix @@ -23,8 +23,8 @@ # NixOS configuration passes up: the homes declared inside it, also # through a structural configuration, that its # `caisson.home-manager.exported` selector selects. It leaves out two -# kinds. A home declared inside a NixOS configuration that is itself -# inside this one belongs to that inner machine. A home evaluated for +# kinds. A home declared inside a machine that is itself inside this +# one belongs to that inner machine. A home evaluated for # a system other than the system of this machine cannot run here. # # Which unit @@ -69,6 +69,15 @@ # that has no systemd options and no assertions. A NixOS configuration # of that kind cannot run units, so there the module also defines # nothing. +# +# The same module is registered for the `systemManager` class, under +# the same name, and a system-manager machine activates the homes +# declared inside it the same way: system-manager declares +# `users.users` and runs the units, and starts those wanted by +# `multi-user.target` with `system-manager.target`. The module reads +# `config.caisson.exports.configurations`, which the core module +# declares; it imports the core module of the class of the evaluation, +# read from its manifest. { closure-lib, ... }: { config, @@ -89,6 +98,24 @@ let && options.systemd ? user && options ? users; + # The core module of the class of this evaluation: of the + # `systemManager` class in a system-manager evaluation, else of the + # `nixos` class. The registered core modules of the two classes are + # one file under two names, so a module imports the one its + # evaluation already holds. + coreModule = + if (lib.caisson-core.evalManifest.type or null) == "system-manager" then + closure-lib.caisson.modules.systemManager.core + else + closure-lib.caisson.modules.nixos.core; + + # The integrations whose configurations are machines. + machineTypes = [ + "nixos" + "darwin" + "system-manager" + ]; + # True for an entry this machine activates. `entry.path` is the path # from this NixOS configuration to the configuration of the entry, a # list of `{ type, name }` segments. @@ -98,9 +125,9 @@ let above = lib.lists.init entry.path; in entry.manifest.type == "home-manager" - # A NixOS configuration on the path is a machine inside this - # machine, and the home belongs to it. - && !(builtins.any (segment: segment.type == "nixos") above) + # A machine on the path is a machine inside this machine, and the + # home belongs to it. + && !(builtins.any (segment: builtins.elem segment.type machineTypes) above) && entry.manifest.system == evalManifest.system; # What the units need of each home. @@ -225,9 +252,9 @@ let ); in { - # The module reads `config.caisson.exports.configurations`. The core - # module declares that option. - imports = [ closure-lib.caisson.modules.nixos.core ]; + # The core module of the class of this evaluation, which declares + # `config.caisson.exports.configurations`. + imports = [ coreModule ]; options.caisson.home-manager.activation.usersDeclaredElsewhere = lib.mkOption { type = lib.types.listOf lib.types.str; @@ -265,11 +292,11 @@ in { assertion = withoutAccount == [ ]; message = '' - caisson: this NixOS configuration has a home whose user has no account in `users.users`. + caisson: this machine has a home whose user has no account in `users.users`. ${lib.concatMapStringsSep "\n" ( home: " the home `${home.name}` is for the user `${home.username}`" ) withoutAccount} - Check `home.username` of the home, and that the home is declared inside the right NixOS configuration. + Check `home.username` of the home, and that the home is declared inside the right machine. If something other than `users.users` provides the account, as systemd-homed does, add the user to `caisson.home-manager.activation.usersDeclaredElsewhere`. ''; } @@ -283,7 +310,7 @@ in { assertion = repeated == [ ]; message = '' - caisson: this NixOS configuration activates more than one home for the same user. + caisson: this machine activates more than one home for the same user. ${lib.concatMapStringsSep "\n" ( username: " ${username}: ${ diff --git a/modules/systemManager/default/caisson/default.nix b/modules/systemManager/default/caisson/default.nix new file mode 100644 index 00000000..48313128 --- /dev/null +++ b/modules/systemManager/default/caisson/default.nix @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: MIT +# +# The `caisson.*` part of the default system-manager module. +{ closure-lib, ... }: +{ ... }: +{ + imports = [ + # `forChildren.nix` sets options under `caisson.forChildren`. The + # core module declares those options. + closure-lib.caisson.modules.systemManager.core + (import ./forChildren.nix { inherit closure-lib; }) + ]; +} diff --git a/modules/systemManager/default/caisson/forChildren.nix b/modules/systemManager/default/caisson/forChildren.nix new file mode 100644 index 00000000..3366dcdf --- /dev/null +++ b/modules/systemManager/default/caisson/forChildren.nix @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: MIT +# +# What a system-manager configuration gives the homes and the hjem +# configurations declared inside it. +# +# A configuration gives things to the configurations declared inside +# it through the options under `caisson.forChildren`. This file adds the +# home-manager module `system-manager-parent` to the modules that a home +# declared inside this system-manager configuration imports by default, +# and the hjem module `system-manager-parent` to those of an hjem +# configuration declared inside it. Each sets the options that come from +# the machine: the home directory from the account of the user that the +# machine declares in `users.users`, and for a home the user ID and the +# Nix of the machine. +# +# A system-manager configuration that passes `moduleImports` selects +# its modules itself, and then nothing inside it gets these modules from +# here. A home or hjem configuration that passes `moduleImports` +# selects its modules itself, and gets the module only if it lists it. +# +# `closure-lib` is the library of caisson, where the modules are +# registered. +{ closure-lib }: +{ ... }: +{ + caisson.forChildren.extraDefaultModuleImports.homeManager = _lib: [ + closure-lib.caisson.modules.homeManager.system-manager-parent + ]; + + caisson.forChildren.extraDefaultModuleImports.hjem = _lib: [ + closure-lib.caisson.modules.hjem.system-manager-parent + ]; +} diff --git a/modules/systemManager/default/default.nix b/modules/systemManager/default/default.nix new file mode 100644 index 00000000..4e067cc6 --- /dev/null +++ b/modules/systemManager/default/default.nix @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: MIT +# +# The `default` system-manager module caisson registers. A +# system-manager configuration imports every registered module named +# `default` unless it passes `moduleImports`, so this is what it gets +# from caisson when it does not select its modules itself. +# +# It holds what a machine running another Linux distribution does for +# the configurations declared inside it, as caisson's `default` NixOS +# module does for a NixOS machine: +# +# - `./caisson/forChildren.nix` gives each home the home-manager module +# `system-manager-parent` and each hjem configuration the hjem module +# `system-manager-parent`, which set the options that come from the +# account of the user on the machine. +# - The registered modules `home-manager-activation` and +# `hjem-activation`, the same modules a NixOS machine uses, write the +# systemd units that activate the homes and the hjem configurations +# when the machine is activated. +# - The registered module `nvf-package` installs the Neovim of each nvf +# configuration declared inside the machine. +# +# The parts read options of caisson's core module. The core module +# leaves its options out when the library of the evaluation is not a +# caisson composition, so the parts are left out in the same case. +{ closure-lib, mkModule, ... }: +{ lib, ... }: +{ + imports = + if lib ? caisson && lib ? caisson-core then + [ + (mkModule ./caisson) + closure-lib.caisson.modules.systemManager.home-manager-activation + closure-lib.caisson.modules.systemManager.hjem-activation + closure-lib.caisson.modules.systemManager.nvf-package + ] + else + [ ]; +} diff --git a/modules/systemManager/hjem-activation b/modules/systemManager/hjem-activation new file mode 120000 index 00000000..f6ababfd --- /dev/null +++ b/modules/systemManager/hjem-activation @@ -0,0 +1 @@ +../nixos/hjem-activation \ No newline at end of file diff --git a/modules/systemManager/home-manager-activation b/modules/systemManager/home-manager-activation new file mode 120000 index 00000000..27511a77 --- /dev/null +++ b/modules/systemManager/home-manager-activation @@ -0,0 +1 @@ +../nixos/home-manager-activation \ No newline at end of file diff --git a/modules/systemManager/nvf-package/default.nix b/modules/systemManager/nvf-package/default.nix new file mode 100644 index 00000000..0704d662 --- /dev/null +++ b/modules/systemManager/nvf-package/default.nix @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: MIT +# +# A system-manager module that installs the Neovim of each nvf +# configuration declared inside the system-manager configuration, in +# `environment.systemPackages`. caisson's `default` system-manager +# module imports it. See `lib.caisson.nvf.packagesModule`. +{ closure-lib, ... }: +closure-lib.caisson.nvf.packagesModule [ + "environment" + "systemPackages" +] diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 4b97e788..f2046a8e 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -1901,6 +1901,69 @@ let in top.nixidyEnvs.x86_64-linux.prod.environmentPackage.drvPath == upstream.environmentPackage.drvPath; + # A system-manager machine activates the home, the hjem configuration + # and the nvf configuration declared inside it, as a NixOS machine + # does: the home and the hjem configuration take their directory from + # the account the machine declares, the machine writes the units that + # activate them, installs the packages of the hjem user and the + # Neovim, and the home is published as `probe@box`. + systemManagerActivatesWhatIsDeclaredInside = + let + top = hiveLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = + { lib, ... }: + { + caisson.system-manager.configurations.box = lib.caisson.system-manager.mkConfiguration { + ecosystemSrc = inputs.system-manager; + configModule = + { lib, ... }: + { + system-manager.allowAnyDistro = true; + users.groups.probe.gid = 5000; + users.users.probe = { + isNormalUser = true; + uid = 5000; + group = "probe"; + home = "/home/probe"; + }; + caisson.home-manager.configurations.probe = lib.caisson.home-manager.mkConfiguration { + ecosystemSrc = inputs.home-manager; + configModule.home.stateVersion = "25.05"; + }; + caisson.hjem.configurations.probe = lib.caisson.hjem.mkConfiguration { + ecosystemSrc = inputs.hjem; + configModule = + { pkgs, ... }: + { + files.".config/probe/greeting".text = "hello"; + packages = [ pkgs.hello ]; + }; + }; + caisson.nvf.configurations.editor = lib.caisson.nvf.mkConfiguration { + ecosystemSrc = inputs.nvf; + configModule.vim.viAlias = true; + }; + }; + }; + }; + }; + box = top.systemConfigs.box.config; + children = + top.caisson.manifest.children.system.x86_64-linux.children.system-manager.box.children.system.x86_64-linux.children; + home = children.home-manager.probe.value.config; + hjem = children.hjem.probe.value.config; + neovim = children.nvf.editor.outputs.neovim; + names = packages: builtins.map (package: package.name or "") packages; + in + home.home.homeDirectory == "/home/probe" + && hjem.directory == "/home/probe" + && box.systemd.services ? home-manager-probe + && box.systemd.services ? "hjem-activate@" + && builtins.elem neovim.name (names box.environment.systemPackages) + && builtins.elem pkgs.hello.name (names box.users.users.probe.packages) + && builtins.attrNames top.homeConfigurations == [ "probe@box" ]; + # A terranix configuration that is a top is its `config.tf.json` # file, built from the package set of the composition with the # evaluated configuration in its `passthru`, and its modules get the From cd19c111599a5221e0f16c99df7c57a353903261 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 11:33:47 -0700 Subject: [PATCH 07/14] feat(integrations): check drift between every activated configuration and the one above it Drift detection was written for one pair: a home inside a NixOS machine compared `/etc/caisson-home-manager/source.json` with the machine it was built for. It is now a property of the framework. An integration whose configurations are activated passes `activation` to `mkModuleConfiguration`: `identity`, the store path of the childless evaluation, and `marker`, the file its activation writes that identity into. NixOS, darwin and system-manager record the system and `/etc/caisson/marker.json`; a home records its activation package and a file in the state directory of its user; an hjem configuration records its manifest file and a file in its directory. A machine `standsAlone`: it is applied on its own host, so it checks nothing above it. `lib.caisson.integrations` gains `activatingAncestorOf`, `markerOf`, `checksMarkerOf` and `driftCheckOf`. Each activating class writes its marker from its `default` module (`activation-marker` for nixos, darwin, systemManager, homeManager and hjem) when a configuration declared inside it checks it. A home runs `driftCheckOf` against the nearest activating configuration above it from the home-manager module `activation-drift`, now part of the home-manager `default` module, so a home inside a darwin or system-manager machine is checked as one inside NixOS is. The NixOS module `home-manager-source-marker` and the home-manager module `nixos-source-marker` are removed. The option `caisson.home-manager.machineDrift.warn` becomes `caisson.drift.warn`, and the marker field `baseSystemOutPath` becomes `identity`. Co-Authored-By: Claude Opus 5.5 --- lib-overlays/darwin/default.nix | 4 + lib-overlays/hjem/default.nix | 16 ++ lib-overlays/home-manager/default.nix | 11 ++ lib-overlays/integrations/default.nix | 157 +++++++++++++++++- lib-overlays/nixos/default.nix | 4 + lib-overlays/system-manager/default.nix | 4 + modules/darwin/activation-marker | 1 + modules/darwin/default/default.nix | 1 + modules/hjem/activation-marker/default.nix | 36 ++++ modules/hjem/default/default.nix | 13 +- .../homeManager/activation-drift/default.nix | 60 +++++++ .../homeManager/activation-marker/default.nix | 42 +++++ modules/homeManager/default/default.nix | 18 +- .../nixos-source-marker/default.nix | 135 --------------- modules/nixos/activation-marker/default.nix | 77 +++++++++ modules/nixos/default/caisson/forChildren.nix | 15 +- modules/nixos/default/default.nix | 22 +-- .../home-manager-source-marker/default.nix | 110 ------------ modules/systemManager/activation-marker | 1 + modules/systemManager/default/default.nix | 1 + tests/pinned-world/default.nix | 29 ++-- tests/unit/lib-overlays.nix | 15 +- 22 files changed, 479 insertions(+), 293 deletions(-) create mode 120000 modules/darwin/activation-marker create mode 100644 modules/hjem/activation-marker/default.nix create mode 100644 modules/homeManager/activation-drift/default.nix create mode 100644 modules/homeManager/activation-marker/default.nix delete mode 100644 modules/homeManager/nixos-source-marker/default.nix create mode 100644 modules/nixos/activation-marker/default.nix delete mode 100644 modules/nixos/home-manager-source-marker/default.nix create mode 120000 modules/systemManager/activation-marker diff --git a/lib-overlays/darwin/default.nix b/lib-overlays/darwin/default.nix index 1b3c605a..dd633f6a 100644 --- a/lib-overlays/darwin/default.nix +++ b/lib-overlays/darwin/default.nix @@ -164,6 +164,10 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # A darwin configuration is activated on a machine; the + # configurations with an activation declared inside it check + # its marker. + activation = final.caisson.integrations.machineActivation; evaluate = evaluate args; }; diff --git a/lib-overlays/hjem/default.nix b/lib-overlays/hjem/default.nix index 2484e971..43f6c7bf 100644 --- a/lib-overlays/hjem/default.nix +++ b/lib-overlays/hjem/default.nix @@ -292,9 +292,25 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # An hjem configuration is activated for its user. Its + # identity is its manifest file; its marker is a file in the + # directory of the user, which the hjem module + # `activation-marker` adds to the files of the configuration. + # The configurations with an activation declared inside it + # check the marker. + activation = { + identity = manifest: manifest.outputs.manifestFile.outPath; + marker = + manifest: + "${manifest.value.config.directory}/${markerDirectory}/${final.caisson.integrations.markerFileName manifest}"; + }; evaluate = evaluate args; }; + # The directory, relative to the directory of the user, that holds + # the marker of an hjem configuration. + markerDirectory = ".local/state/caisson/markers"; + integration = final.caisson.integrations.mkIntegration { name = "hjem"; class = "hjem"; diff --git a/lib-overlays/home-manager/default.nix b/lib-overlays/home-manager/default.nix index ffaf3a97..28fcddd5 100644 --- a/lib-overlays/home-manager/default.nix +++ b/lib-overlays/home-manager/default.nix @@ -516,6 +516,17 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # A home is activated for its user. Its identity is its + # activation package; its marker is a file in the state + # directory of the user, which the home-manager module + # `activation-marker` writes. The configurations with an + # activation declared inside the home check it. + activation = { + identity = manifest: manifest.outputs.activationPackage.outPath; + marker = + manifest: + "\${XDG_STATE_HOME:-$HOME/.local/state}/caisson/markers/${selection.markerFileName manifest}"; + }; evaluate = evaluation variant args; }; diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index 200dacdd..a5e63862 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -260,19 +260,37 @@ # recorded on the manifest, and applied where the configuration # is passed up (`entriesOf`). Null for a configuration that is # not published under a name. + # + # `activation` is given by an integration whose configurations + # are activated on a machine or for a user: NixOS, darwin and + # system-manager machines, homes, hjem configurations. It holds + # two functions of a manifest of the configuration. `identity` + # returns the store path that names what the activation applies, + # computed from the childless evaluation, or null where there is + # none. `standsAlone`, when true, says the configuration is + # applied on its own, as a machine is on its own host, so that it + # checks no configuration above it. `marker` returns the path of the file the activation + # writes that identity into, as shell text, which may name + # variables such as `$HOME`. It is recorded on the manifest. A + # configuration with an activation declared beneath one checks + # the marker of the nearest such configuration above it when it + # is activated (`driftCheckOf`). Null for a configuration that is + # not activated. mkModuleConfiguration = { type, perSystem ? false, defaultPkgs ? null, exportsTo ? null, + activation ? null, evaluate, }: final.caisson-core.mkConfiguration { inherit type perSystem; record = (if defaultPkgs == null then { } else { inherit defaultPkgs; }) - // (if exportsTo == null then { } else { inherit exportsTo; }); + // (if exportsTo == null then { } else { inherit exportsTo; }) + // (if activation == null then { } else { inherit activation; }); evaluate = view: let @@ -592,6 +610,137 @@ found ) null types; + # The nearest configuration above `manifest` that has an + # activation (`mkModuleConfiguration`'s `activation`), as its + # childless manifest, or null when there is none. A home inside a + # NixOS machine finds the machine; an nvf configuration inside + # that home finds the home. + activatingAncestorOf = + manifest: + let + found = builtins.filter (ancestor: (ancestor.activation or null) != null) ( + manifest.ancestors or [ ] + ); + in + if found == [ ] then null else last found; + + # The `activation` of a machine: a NixOS, darwin or system-manager + # configuration. Its identity is its system, `toplevel`, null for + # a configuration that builds none, as the nixos-minimal + # integration can produce. Its marker is a file under `/etc`, + # which the module `activation-marker` of its class writes. + # + # A machine is activated on its own host. A machine declared + # inside another, such as an image, is applied on a different host + # than the machine above, and the marker under `/etc` that it + # finds there is its own, so it checks nothing above it: + # `standsAlone`. + machineActivation = { + standsAlone = true; + identity = + manifest: if (manifest.outputs or { }) ? toplevel then manifest.outputs.toplevel.outPath else null; + marker = _manifest: "/etc/caisson/marker.json"; + }; + + # The file name of the marker of a configuration activated for a + # user: the integration and the name the configuration is declared + # under, with the characters the shell would read inside double + # quotes, and the path separator, replaced. + markerFileName = + manifest: + "${manifest.type}-${ + builtins.replaceStrings [ "/" "\"" "$" "`" "\\" ] [ "_" "_" "_" "_" "_" ] ( + manifest.name or "default" + ) + }.json"; + + # What the activation of the configuration of `manifest` writes + # into its marker: the identity of its childless evaluation, as a + # plain string, so that the marker does not depend on that + # evaluation. `path` is the shell text of the path of the marker. + # Null when the configuration has no activation or no identity. + markerOf = + manifest: + let + activation = manifest.activation or null; + childless = manifest.childlessManifest or manifest; + identity = if activation == null then null else activation.identity childless; + in + if identity == null then + null + else + { + path = activation.marker childless; + identity = builtins.unsafeDiscardStringContext identity; + text = builtins.toJSON { identity = builtins.unsafeDiscardStringContext identity; }; + }; + + # True when `entry`, an entry of `caisson.exports.configurations` + # of the configuration of `manifest`, checks the marker of that + # configuration when it is activated: the entry has an activation, + # and the configuration is the nearest one above it that has an + # activation. + checksMarkerOf = + manifest: entry: + let + ancestor = activatingAncestorOf entry.manifest; + in + (entry.manifest.activation or null) != null + && !(entry.manifest.activation.standsAlone or false) + && ancestor != null + && builtins.length (ancestor.ancestors or [ ]) == builtins.length (manifest.ancestors or [ ]); + + # The shell text with which the activation of the configuration of + # `manifest` checks for drift: it compares the marker of the + # nearest configuration above with an activation against the + # identity of that configuration as this configuration was built + # for it, and prints a warning to standard error when the marker is + # missing or names another identity. It does not stop the + # activation. When that configuration activates this one, its + # marker already belongs to the generation being activated, and + # the check prints nothing. Empty when there is no such + # configuration, or it has no identity. `jq` is the path of a jq + # executable, and `optionText` names the option that turns the + # warning off, for the message. + driftCheckOf = + { jq, optionText }: + manifest: + let + ancestor = + if (manifest.activation.standsAlone or false) then null else activatingAncestorOf manifest; + expected = if ancestor == null then null else ancestor.activation.identity ancestor; + marker = ancestor.activation.marker ancestor; + above = "the ${ancestor.type} configuration `${ancestor.name or "(unnamed)"}`"; + self = "the ${manifest.type} configuration `${manifest.name or "(unnamed)"}`"; + # The identity, quoted for the shell. A store path holds no + # single quote, so quoting it whole is enough. + escaped = "'${builtins.unsafeDiscardStringContext expected}'"; + in + if expected == null then + "" + else + '' + _caissonMarker="${marker}" + if [ ! -f "$_caissonMarker" ]; then + echo "WARNING: $_caissonMarker does not exist." >&2 + echo " ${self} is declared inside ${above}," >&2 + echo " and the activation of ${above} has not recorded what it applied." >&2 + echo " Activate ${above} as this configuration was built with it," >&2 + echo " or set ${optionText} to false in ${self}." >&2 + else + _caissonApplied="$(${jq} -r '.identity // empty' < "$_caissonMarker")" + if [ "$_caissonApplied" != ${escaped} ]; then + echo "WARNING: ${self} was built for a version of ${above} that differs from the version that is applied." >&2 + echo " applied (configurations declared inside it left out):" >&2 + echo " ''${_caissonApplied:-unknown}" >&2 + echo " built for (configurations declared inside it left out):" >&2 + echo " "${escaped} >&2 + echo " Activate ${above} as this configuration was built with it," >&2 + echo " or set ${optionText} to false in ${self}." >&2 + fi + fi + ''; + isManifest = value: builtins.isAttrs value && (value._type or null) == "caisson-manifest"; # How a configuration is published: the `exportsTo` its @@ -875,6 +1024,12 @@ integrationsIn integrationsDeclaringIn nearestOf + activatingAncestorOf + machineActivation + markerFileName + markerOf + checksMarkerOf + driftCheckOf mkEvaluation publish topValue diff --git a/lib-overlays/nixos/default.nix b/lib-overlays/nixos/default.nix index 037b5fcd..765aab0c 100644 --- a/lib-overlays/nixos/default.nix +++ b/lib-overlays/nixos/default.nix @@ -114,6 +114,10 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # A NixOS configuration is activated on a machine; the + # configurations with an activation declared inside it check + # its marker. + activation = final.caisson.integrations.machineActivation; evaluate = evaluate variant args; }; diff --git a/lib-overlays/system-manager/default.nix b/lib-overlays/system-manager/default.nix index 1cd74ef3..1c4aaa8b 100644 --- a/lib-overlays/system-manager/default.nix +++ b/lib-overlays/system-manager/default.nix @@ -300,6 +300,10 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # A system-manager configuration is activated on a machine; + # the configurations with an activation declared inside it + # check its marker. + activation = final.caisson.integrations.machineActivation; evaluate = evaluate args; }; diff --git a/modules/darwin/activation-marker b/modules/darwin/activation-marker new file mode 120000 index 00000000..d4a211a0 --- /dev/null +++ b/modules/darwin/activation-marker @@ -0,0 +1 @@ +../nixos/activation-marker \ No newline at end of file diff --git a/modules/darwin/default/default.nix b/modules/darwin/default/default.nix index 7c9af933..944d7439 100644 --- a/modules/darwin/default/default.nix +++ b/modules/darwin/default/default.nix @@ -29,6 +29,7 @@ closure-lib.caisson.modules.darwin.home-manager-activation closure-lib.caisson.modules.darwin.hjem-activation closure-lib.caisson.modules.darwin.nvf-package + closure-lib.caisson.modules.darwin.activation-marker ] else [ ]; diff --git a/modules/hjem/activation-marker/default.nix b/modules/hjem/activation-marker/default.nix new file mode 100644 index 00000000..81a6d445 --- /dev/null +++ b/modules/hjem/activation-marker/default.nix @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: MIT +# +# An hjem module with which an hjem configuration records what it +# applied, so that the configurations declared inside it can detect +# drift. It is the counterpart, for an hjem configuration, of the +# machine module `activation-marker`: see there for why. +# +# The module adds a file to the files of the configuration, +# `.local/state/caisson/markers/hjem-.json` in the directory of +# the user, holding the identity of the childless evaluation of the +# configuration: its manifest file without the configurations declared +# inside it. hjem links the file into place when it applies the +# configuration. +# +# caisson's `default` hjem module imports this module. The file is +# added only when the configuration has a configuration declared inside +# it that checks it, and not in the childless evaluation. +{ ... }: +{ + config, + lib, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + checked = builtins.any (selection.checksMarkerOf evalManifest) config.caisson.exports.configurations; + + marker = selection.markerOf evalManifest; +in +{ + config = lib.mkIf (!evalManifest.childless && checked && marker != null) { + files.".local/state/caisson/markers/${selection.markerFileName evalManifest}".text = marker.text; + }; +} diff --git a/modules/hjem/default/default.nix b/modules/hjem/default/default.nix index fc93fa80..69f82a57 100644 --- a/modules/hjem/default/default.nix +++ b/modules/hjem/default/default.nix @@ -3,10 +3,17 @@ # The `default` hjem module caisson registers. An hjem configuration # imports every registered module named `default` unless it passes # `moduleImports`, so this is what it gets from caisson when it does not -# select its modules itself: the module `nvf-package`, which installs -# the Neovim of each nvf configuration declared inside it. +# select its modules itself: +# +# - `nvf-package`, which installs the Neovim of each nvf configuration +# declared inside the hjem configuration; +# - `activation-marker`, which records what the hjem configuration +# applied, for the configurations declared inside it to check. { closure-lib, ... }: { ... }: { - imports = [ closure-lib.caisson.modules.hjem.nvf-package ]; + imports = [ + closure-lib.caisson.modules.hjem.nvf-package + closure-lib.caisson.modules.hjem.activation-marker + ]; } diff --git a/modules/homeManager/activation-drift/default.nix b/modules/homeManager/activation-drift/default.nix new file mode 100644 index 00000000..dc0ee1a4 --- /dev/null +++ b/modules/homeManager/activation-drift/default.nix @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: MIT +# +# A home-manager module with which a home declared inside another +# configuration that has an activation, such as a NixOS, darwin or +# system-manager machine, detects drift when it is activated. +# +# The user can activate such a home by hand, with `home-manager switch`, +# built from the source the user has checked out. That source can +# describe a different version of the configuration above than the one +# that is applied. The configuration above records what it applied in a +# marker (the module `activation-marker` of its class). A step of the +# activation of the home compares that marker with the identity of the +# configuration above as the home was built for it, and prints a +# warning when they differ or when the marker is missing. The step does +# not stop the activation. When the configuration above activates the +# home, its marker belongs to the generation being activated, and the +# step prints nothing. +# +# The check is `lib.caisson.integrations.driftCheckOf`, the same check +# every configuration with an activation runs against the nearest +# configuration above it that has one. A home with no such +# configuration above it has nothing to check, and the module then +# defines no step. +# +# caisson's `default` home-manager module imports this module. +{ ... }: +{ + config, + lib, + pkgs, + ... +}: +let + check = lib.caisson.integrations.driftCheckOf { + jq = lib.getExe' pkgs.jq "jq"; + optionText = "`caisson.drift.warn`"; + } lib.caisson.evalManifest; +in +{ + options.caisson.drift.warn = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + Whether the activation of this home prints a warning when the home + was built for a version of the configuration it is declared inside + that differs from the version that is applied. + + The configuration above, such as the machine, records what it + applied in a marker file. The activation of this home compares the + marker with the configuration above as it is in the source this + home was built from. The comparison leaves out the configurations + declared inside the configuration above, so a change to a home + does not cause a warning. + ''; + }; + + config = lib.mkIf (config.caisson.drift.warn && check != "") { + home.activation.caissonDrift = lib.hm.dag.entryBefore [ "writeBoundary" ] check; + }; +} diff --git a/modules/homeManager/activation-marker/default.nix b/modules/homeManager/activation-marker/default.nix new file mode 100644 index 00000000..8654916e --- /dev/null +++ b/modules/homeManager/activation-marker/default.nix @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: MIT +# +# A home-manager module with which a home records what it applied, so +# that the configurations declared inside it can detect drift. It is the +# counterpart, for a home, of the machine module `activation-marker`: +# see there for why. +# +# A step of the activation of the home writes the identity of the +# childless evaluation of the home, its activation package without the +# configurations declared inside it, into a file in the state directory +# of the user, `$XDG_STATE_HOME/caisson/markers/home-manager-.json`. +# A configuration declared inside the home, such as an nvf +# configuration, compares that file with the identity it was built for +# when it is activated by hand. The step runs after the files of the +# home are linked, so a configuration the home activates after it reads +# the identity of the generation being activated. +# +# caisson's `default` home-manager module imports this module. The file +# is written only when the home has a configuration declared inside it +# that checks it, and not in the childless evaluation. +{ ... }: +{ + config, + lib, + pkgs, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + checked = builtins.any (selection.checksMarkerOf evalManifest) config.caisson.exports.configurations; + + marker = selection.markerOf evalManifest; +in +{ + config = lib.mkIf (!evalManifest.childless && checked && marker != null) { + home.activation.caissonActivationMarker = lib.hm.dag.entryAfter [ "linkGeneration" ] '' + run ${lib.getExe' pkgs.coreutils "install"} -D -m 0644 ${pkgs.writeText "caisson-marker.json" marker.text} "${marker.path}" + ''; + }; +} diff --git a/modules/homeManager/default/default.nix b/modules/homeManager/default/default.nix index ca94dd96..cb30242d 100644 --- a/modules/homeManager/default/default.nix +++ b/modules/homeManager/default/default.nix @@ -3,10 +3,22 @@ # The `default` home-manager module caisson registers. A home imports # every registered module named `default` unless it passes # `moduleImports`, so this is what a home gets from caisson when it does -# not select its modules itself: the module `nvf-package`, which installs -# the Neovim of each nvf configuration declared inside the home. +# not select its modules itself: +# +# - `nvf-package`, which installs the Neovim of each nvf configuration +# declared inside the home; +# - `activation-drift`, with which the activation of the home warns when +# the home was built for a different version of the configuration it +# is declared inside, such as its machine; +# - `activation-marker`, with which the activation of the home records +# what it applied, for the configurations declared inside the home to +# check in turn. { closure-lib, ... }: { ... }: { - imports = [ closure-lib.caisson.modules.homeManager.nvf-package ]; + imports = [ + closure-lib.caisson.modules.homeManager.nvf-package + closure-lib.caisson.modules.homeManager.activation-drift + closure-lib.caisson.modules.homeManager.activation-marker + ]; } diff --git a/modules/homeManager/nixos-source-marker/default.nix b/modules/homeManager/nixos-source-marker/default.nix deleted file mode 100644 index ffcd3370..00000000 --- a/modules/homeManager/nixos-source-marker/default.nix +++ /dev/null @@ -1,135 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# A home-manager module for a home that is declared inside a NixOS -# configuration. The module adds a step to the activation of the home. -# The step prints a warning if the home was built for a NixOS -# configuration that differs from the machine that is running. -# -# Why -# -# The machine activates the homes declared inside its NixOS -# configuration. The user can also activate such a home by hand, with -# `home-manager switch`. `home-manager switch` builds the home from -# the source the user has checked out. -# -# That source can describe a different machine than the machine that -# is running. This happens when the user changed the NixOS -# configuration and has not switched the machine yet. It also happens -# when the checkout is older than the machine. -# -# A home reads settings of its machine. A home built from such a -# source has read the settings of a machine that is not the machine -# the home is activated on. -# -# How -# -# The machine has a file, `/etc/caisson-home-manager/source.json`, -# that names the NixOS configuration the machine is running. The NixOS -# module `home-manager-source-marker` writes the file. The field -# `baseSystemOutPath` of the file is a store path. The store path is -# the system that the childless evaluation of the NixOS configuration -# builds. The childless evaluation is the evaluation that leaves out -# the homes. -# -# This module computes the same store path from the source the home is -# built from. The module reads the childless evaluation of the NixOS -# configuration through the manifest of the home. -# -# The step in the activation compares the store path this module -# computed with the store path in the file. The step prints a warning -# when the two store paths differ. The step also prints a warning when -# the file does not exist. The step does not stop the activation. -# -# When the machine activates the home, the file on the machine already -# belongs to the generation that is being activated. The two store -# paths are then equal, and the step prints nothing. -# -# Cost -# -# Building the activation package of the home evaluates the system of -# the childless evaluation of the NixOS configuration. -# -# Which homes import this module -# -# caisson's `default` NixOS module gives this module to the homes -# declared inside the NixOS configuration, through -# `caisson.forChildren`. Such a home imports this module by default. -# A home that passes `moduleImports` selects its modules itself. That -# home imports this module only if the home lists it. -# -# The module reads the NixOS configuration through the manifest of the -# home. A home with no NixOS configuration above it cannot import this -# module. -{ ... }: -# `lib` is the library of the home, and `config` is the configuration -# of the home. -{ - lib, - config, - pkgs, - ... -}: -let - # The manifest of the childless evaluation of the NixOS configuration - # the home is declared inside. - machine = lib.caisson.evalManifest.nearest.nixos; - - # The store path of the system that the childless evaluation builds. - # - # The store path is kept as a plain string. A string that refers to - # the system would make the home depend on the system. Building the - # home would then build the system. - # - # The value is null for a NixOS configuration that builds no system. - # The nixos-minimal integration can produce such a NixOS - # configuration. - expected = - if machine.outputs ? toplevel then - builtins.unsafeDiscardStringContext machine.outputs.toplevel.outPath - else - null; - - marker = "/etc/caisson-home-manager/source.json"; - jq = lib.getExe' pkgs.jq "jq"; -in -{ - options.caisson.home-manager.machineDrift.warn = lib.mkOption { - type = lib.types.bool; - default = true; - description = '' - Whether the activation of this home prints a warning when the - home was built for a NixOS configuration that differs from the - machine that is running. - - The file `/etc/caisson-home-manager/source.json` on the machine - names the NixOS configuration the machine is running. The - activation of this home compares the file with the NixOS - configuration in the source this home was built from. - - The comparison leaves the homes out. A change to a home does not - cause a warning. - ''; - }; - - config = lib.mkIf (config.caisson.home-manager.machineDrift.warn && expected != null) { - home.activation.caissonMachineDrift = lib.hm.dag.entryBefore [ "writeBoundary" ] '' - if [ ! -f ${marker} ]; then - echo "WARNING: ${marker} does not exist." >&2 - echo " This home is declared inside the NixOS configuration \`${machine.name}\`," >&2 - echo " and the machine that is running does not record which NixOS configuration it runs." >&2 - echo " Switch the machine to the NixOS configuration this home was built with." >&2 - else - _caissonRunning="$(${jq} -r '.baseSystemOutPath // empty' < ${marker})" - if [ "$_caissonRunning" != ${lib.escapeShellArg expected} ]; then - echo "WARNING: this home was built for a NixOS configuration that differs from the machine that is running." >&2 - echo " the machine runs (homes left out):" >&2 - echo " ''${_caissonRunning:-unknown}" >&2 - echo " this home was built for (homes left out):" >&2 - echo " ${expected}" >&2 - echo " Switch the machine to the NixOS configuration this home was built with," >&2 - echo " or set \`caisson.home-manager.machineDrift.warn = false\` in the home." >&2 - fi - fi - ''; - }; -} diff --git a/modules/nixos/activation-marker/default.nix b/modules/nixos/activation-marker/default.nix new file mode 100644 index 00000000..559813e2 --- /dev/null +++ b/modules/nixos/activation-marker/default.nix @@ -0,0 +1,77 @@ +# SPDX-License-Identifier: MIT +# +# A module with which a machine records what it applied, so that the +# configurations declared inside it can detect drift. +# +# Why +# +# A machine activates the configurations declared inside it: homes, +# hjem configurations, and the others that have an activation. A user +# can also activate such a configuration by hand, built from the source +# the user has checked out, for example with `home-manager switch`. +# That source can describe a different machine than the one that is +# applied: the user changed the machine and has not switched it yet, or +# the checkout is older than the machine. A configuration declared +# inside a machine reads settings of the machine, so it was then built +# for a machine that is not the one it is activated on. +# +# What the file holds +# +# The module writes `/etc/caisson/marker.json`, with one field, +# `identity`: the system that the childless evaluation of this machine +# builds, which is the evaluation without the configurations declared +# inside it. Changing one of those configurations leaves the identity +# the same; changing anything else about the machine changes it. +# +# A configuration declared inside the machine reads the same childless +# evaluation through its own manifest, so it computes the same identity +# from the same source. Its activation compares the two +# (`lib.caisson.integrations.driftCheckOf`), and warns when they differ. +# When the machine activates it, the file already belongs to the +# generation being activated, and the two are equal. +# +# The module is registered for the `nixos`, `darwin` and +# `systemManager` classes, under the same name, and the `default` +# module of each imports it. The file is written only when the machine +# has a configuration declared inside it that checks it, because +# computing the identity evaluates the machine a second time. It is not +# written in the childless evaluation, and not in an evaluation without +# `environment.etc`, which the nixos-minimal integration can produce. +{ closure-lib, ... }: +{ + config, + lib, + options, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + hasEtc = options ? environment && options.environment ? etc; + + # The core module of the class of this evaluation, which declares + # `caisson.exports`. + coreModule = + { + darwin = closure-lib.caisson.modules.darwin.core; + system-manager = closure-lib.caisson.modules.systemManager.core; + } + .${evalManifest.type or "nixos"} or closure-lib.caisson.modules.nixos.core; + + checked = builtins.any (selection.checksMarkerOf evalManifest) config.caisson.exports.configurations; + + marker = selection.markerOf evalManifest; +in +{ + imports = [ coreModule ]; + + # `optionalAttrs` and not `mkIf` decides whether the option is defined + # at all: a definition under `mkIf false` is still a definition, and + # NixOS refuses a definition of an option that no module declares. + config = lib.optionalAttrs hasEtc ( + lib.mkIf (!evalManifest.childless && checked && marker != null) { + environment.etc."caisson/marker.json".text = marker.text; + } + ); +} diff --git a/modules/nixos/default/caisson/forChildren.nix b/modules/nixos/default/caisson/forChildren.nix index 3e50c996..c24d4c14 100644 --- a/modules/nixos/default/caisson/forChildren.nix +++ b/modules/nixos/default/caisson/forChildren.nix @@ -4,16 +4,10 @@ # # A configuration gives things to the configurations declared inside # it through the options under `caisson.forChildren`. This file adds -# two home-manager modules, `nixos-parent` and `nixos-source-marker`, -# to the modules that a home declared inside this NixOS configuration -# imports by default, and the hjem module `nixos-parent` to those of an -# hjem configuration declared inside it. -# -# `nixos-source-marker` is the module caisson registers as -# `modules/homeManager/nixos-source-marker`. It adds a step to the -# activation of the home. The step prints a warning if the home was -# built for a NixOS configuration that differs from the machine that -# is running. +# the home-manager module `nixos-parent` to the modules that a home +# declared inside this NixOS configuration imports by default, and the +# hjem module `nixos-parent` to those of an hjem configuration declared +# inside it. # # `nixos-parent` is the module caisson registers as # `modules/homeManager/nixos-parent`. It sets the options of a home @@ -35,7 +29,6 @@ { caisson.forChildren.extraDefaultModuleImports.homeManager = _lib: [ closure-lib.caisson.modules.homeManager.nixos-parent - closure-lib.caisson.modules.homeManager.nixos-source-marker ]; # The hjem module `nixos-parent` sets the home directory of an hjem diff --git a/modules/nixos/default/default.nix b/modules/nixos/default/default.nix index 8d24f41b..3b8d085a 100644 --- a/modules/nixos/default/default.nix +++ b/modules/nixos/default/default.nix @@ -15,19 +15,19 @@ # machine's account. # # It also holds what a NixOS configuration does for the homes declared -# inside it, in three parts: +# inside it, in two parts: # -# - `./caisson/forChildren.nix` gives each of those homes two -# home-manager modules. The first module sets the options of the -# home that come from the machine. The second module prints a -# warning when the home is activated on a machine that differs from -# the machine the home was built for. +# - `./caisson/forChildren.nix` gives each of those homes the +# home-manager module `nixos-parent`, which sets the options of the +# home that come from the machine. # - The registered module `home-manager-activation` writes the # systemd units that activate those homes on the machine. -# - The registered module `home-manager-source-marker` writes a file -# on the machine. The file names the NixOS configuration the -# machine is running. The warning of the second module compares -# against that file. +# +# The registered module `activation-marker` writes a file on the +# machine that names what the machine applied. Every configuration with +# an activation declared inside the machine, a home among them, compares +# against that file when it is activated by hand, and warns when it was +# built for a different machine. # # The parts read options of caisson's core module. The core module # leaves its options out when the library of the evaluation is not a @@ -43,7 +43,7 @@ closure-lib.caisson.modules.nixos.home-manager-activation closure-lib.caisson.modules.nixos.hjem-activation closure-lib.caisson.modules.nixos.nvf-package - closure-lib.caisson.modules.nixos.home-manager-source-marker + closure-lib.caisson.modules.nixos.activation-marker ] else [ ]; diff --git a/modules/nixos/home-manager-source-marker/default.nix b/modules/nixos/home-manager-source-marker/default.nix deleted file mode 100644 index a6f01252..00000000 --- a/modules/nixos/home-manager-source-marker/default.nix +++ /dev/null @@ -1,110 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# A NixOS module for a NixOS configuration that has homes declared -# inside it. The module writes a file on the machine. The file names -# the NixOS configuration that the machine is running. A home reads -# the file when the home is activated. The home prints a warning if -# the home was built for a different NixOS configuration. -# -# Why a home needs this -# -# The machine activates the homes declared inside its NixOS -# configuration. The user can also activate such a home by hand, with -# `home-manager switch`. `home-manager switch` builds the home from -# the source the user has checked out. -# -# That source can describe a different machine than the machine that -# is running. This happens when the user changed the NixOS -# configuration and has not switched the machine yet. It also happens -# when the checkout is older than the machine. -# -# A home reads settings of its machine. A home built from such a -# source has read the settings of a machine that is not the machine -# the home is activated on. -# -# What the file holds -# -# The file is `/etc/caisson-home-manager/source.json`. It has one -# field, `baseSystemOutPath`, which is a store path. The store path is -# the system that the childless evaluation of this NixOS configuration -# builds. -# -# The childless evaluation is the evaluation of the NixOS -# configuration that leaves out the configurations declared inside the -# NixOS configuration. The homes are among those configurations. So -# the store path does not depend on the homes. Changing a home leaves -# the store path the same. Changing anything else about the machine -# changes the store path. -# -# A home declared inside the NixOS configuration can read the same -# childless evaluation through the manifest of the home. The home -# therefore computes the same store path from the same source. The -# home-manager module `nixos-source-marker` compares the store path -# the home computed with the store path in the file. -# -# Cost -# -# To write the file, the module evaluates the system of the childless -# evaluation. That is a second evaluation of the whole NixOS -# configuration. The module writes the file only when at least one -# home is declared inside the NixOS configuration. A machine with no -# homes does not pay for the second evaluation. -# -# Where the module defines nothing -# -# The module defines nothing in the childless evaluation, because the -# childless evaluation has no homes. -# -# The module also defines nothing in a NixOS evaluation that has no -# `environment.etc` option. The nixos-minimal integration can produce -# such an evaluation, and such an evaluation has nowhere to put the -# file. -{ closure-lib, ... }: -{ - config, - lib, - options, - ... -}: -let - inherit (lib.caisson) evalManifest; - - hasEtc = options ? environment && options.environment ? etc; - - # True for a home that belongs to this machine. - # - # `entry.path` is a list of `{ type, name }` segments. The list is - # the path from this NixOS configuration to the configuration of the - # entry. A NixOS configuration on that path is a machine inside this - # machine. A home below such a machine belongs to that machine and - # not to this machine. - isHomeOfThisMachine = - entry: - entry.manifest.type == "home-manager" - && !(builtins.any (segment: segment.type == "nixos") (lib.lists.init entry.path)); - - hasHomes = builtins.any isHomeOfThisMachine config.caisson.exports.configurations; - - # The store path is written as a plain string. A string that refers - # to the system would make the file depend on the system. The - # machine would then have to build the system of the childless - # evaluation, only to write the name of that system into the file. - baseSystemOutPath = builtins.unsafeDiscardStringContext evalManifest.childlessManifest.outputs.toplevel.outPath; -in -{ - # The module reads `config.caisson.exports.configurations`. The core - # module declares that option. - imports = [ closure-lib.caisson.modules.nixos.core ]; - - # `optionalAttrs` and not `mkIf` decides whether the option is - # defined at all. A definition under `mkIf false` is still a - # definition, and NixOS refuses a definition of an option that no - # module declares. - config = lib.optionalAttrs hasEtc ( - lib.mkIf (!evalManifest.childless && hasHomes) { - environment.etc."caisson-home-manager/source.json".text = builtins.toJSON { - inherit baseSystemOutPath; - }; - } - ); -} diff --git a/modules/systemManager/activation-marker b/modules/systemManager/activation-marker new file mode 120000 index 00000000..d4a211a0 --- /dev/null +++ b/modules/systemManager/activation-marker @@ -0,0 +1 @@ +../nixos/activation-marker \ No newline at end of file diff --git a/modules/systemManager/default/default.nix b/modules/systemManager/default/default.nix index 4e067cc6..b46f60d4 100644 --- a/modules/systemManager/default/default.nix +++ b/modules/systemManager/default/default.nix @@ -33,6 +33,7 @@ closure-lib.caisson.modules.systemManager.home-manager-activation closure-lib.caisson.modules.systemManager.hjem-activation closure-lib.caisson.modules.systemManager.nvf-package + closure-lib.caisson.modules.systemManager.activation-marker ] else [ ]; diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index f2046a8e..5eaa52a9 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -464,7 +464,7 @@ let builtins.all evaluates [ "default" "home-manager-activation" - "home-manager-source-marker" + "activation-marker" ]; composesTheCaissonLibrary = @@ -737,10 +737,9 @@ let && !(machine.systemd.user.services ? home-manager-declared) && builtins.isString machine.system.build.toplevel.drvPath; - # The same machine writes the file - # `/etc/caisson-home-manager/source.json`. The file holds a store - # path. The store path is the system of the machine with the homes - # left out. + # The same machine writes the file `/etc/caisson/marker.json`. The + # file holds a store path. The store path is the system of the + # machine with the configurations declared inside it left out. # # The system that the machine builds has the units of the homes in # it. So the store path in the file must differ from the system @@ -748,7 +747,8 @@ let # # Each home computes the same store path. The activation of each # home has a step that compares that store path with the file. The - # script of the step must therefore contain the store path. + # script of the step must therefore contain the store path and the + # path of the file. aMachineRecordsWhatItsHomesCompareAgainst = let machine = machineWithHomes.nixosConfigurations.probe.config; @@ -758,16 +758,17 @@ let ]; # fromJSON refuses a string that refers to a store path. record = builtins.fromJSON ( - builtins.unsafeDiscardStringContext machine.environment.etc."caisson-home-manager/source.json".text + builtins.unsafeDiscardStringContext machine.environment.etc."caisson/marker.json".text ); - base = record.baseSystemOutPath; + base = record.identity; comparesAgainstIt = home: - pkgs.lib.hasInfix base ( - builtins.unsafeDiscardStringContext home.config.home.activation.caissonMachineDrift.data - ); + let + step = builtins.unsafeDiscardStringContext home.config.home.activation.caissonDrift.data; + in + pkgs.lib.hasInfix base step && pkgs.lib.hasInfix "/etc/caisson/marker.json" step; in - builtins.attrNames record == [ "baseSystemOutPath" ] + builtins.attrNames record == [ "identity" ] && pkgs.lib.hasPrefix builtins.storeDir base && base != builtins.unsafeDiscardStringContext machine.system.build.toplevel.outPath && builtins.all comparesAgainstIt homes; @@ -1957,6 +1958,10 @@ let names = packages: builtins.map (package: package.name or "") packages; in home.home.homeDirectory == "/home/probe" + # The machine records what it applied, and the home compares + # against it when activated by hand, as inside a NixOS machine. + && box.environment.etc ? "caisson/marker.json" + && pkgs.lib.hasInfix "/etc/caisson/marker.json" home.home.activation.caissonDrift.data && hjem.directory == "/home/probe" && box.systemd.services ? home-manager-probe && box.systemd.services ? "hjem-activate@" diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index bb595812..f013fedb 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -4008,9 +4008,9 @@ in }; # A NixOS configuration with a home declared inside it writes the - # file `caisson-home-manager/source.json` under `/etc`. The file - # holds a store path. The store path is the system of the - # machine with the homes left out. + # file `caisson/marker.json` under `/etc`. The file holds a store + # path. The store path is the system of the machine with the + # configurations declared inside it left out. # # The stub of NixOS puts the number of system units into the # store path of a system. The machine `laptop` has one home, and @@ -4022,7 +4022,8 @@ in # # The machine `outer` has a machine `image` declared inside it, # and `image` has a home. That home belongs to `image`. So - # `image` writes the file, and `outer` does not. + # `image` writes the file, and `outer` does not: `image` is + # applied on its own host, so it checks no marker of `outer`. "test: a NixOS configuration with homes records the system it runs without them" = { expr = let @@ -4056,7 +4057,7 @@ in }; }; }; - file = "caisson-home-manager/source.json"; + file = "caisson/marker.json"; recordOf = name: let @@ -4073,13 +4074,13 @@ in }; expected = { laptop = { - baseSystemOutPath = "/nix/store/stub-system-with-0-units"; + identity = "/nix/store/stub-system-with-0-units"; }; laptopBuilds = "/nix/store/stub-system-with-1-units"; bare = null; outer = null; image = { - baseSystemOutPath = "/nix/store/stub-system-with-0-units"; + identity = "/nix/store/stub-system-with-0-units"; }; }; }; From e8d3ccb8380fa59d1f70d37c4c44cc22561be748 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 11:54:40 -0700 Subject: [PATCH 08/14] refactor(integrations): record a digest of the identity in a drift marker A drift marker held the store path of the childless evaluation with its string context dropped, which made it a string that looks like a store path but is not tracked as one. The marker and the check now hold the SHA-256 digest of that store path. Computing it evaluates the childless evaluation and builds nothing, and the digest names no store path, so nothing depends on it and nothing can read it as a path. Co-Authored-By: Claude Opus 5.5 --- lib-overlays/integrations/default.nix | 29 +++++++++++------- modules/nixos/activation-marker/default.nix | 11 ++++--- tests/pinned-world/default.nix | 33 ++++++++++----------- tests/unit/lib-overlays.nix | 12 ++++---- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index a5e63862..fc336040 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -654,11 +654,18 @@ ) }.json"; + # The digest a marker holds for an identity: the SHA-256 of its + # store path. Computing the store path evaluates the childless + # evaluation and builds nothing, and the digest refers to no store + # path, so a marker that holds it depends on nothing. The digest + # is compared, never read as a path. + digestOf = identity: builtins.hashString "sha256" identity; + # What the activation of the configuration of `manifest` writes - # into its marker: the identity of its childless evaluation, as a - # plain string, so that the marker does not depend on that - # evaluation. `path` is the shell text of the path of the marker. - # Null when the configuration has no activation or no identity. + # into its marker: the digest of the identity of its childless + # evaluation (`digestOf`). `path` is the shell text of the path of + # the marker. Null when the configuration has no activation or no + # identity. markerOf = manifest: let @@ -671,8 +678,8 @@ else { path = activation.marker childless; - identity = builtins.unsafeDiscardStringContext identity; - text = builtins.toJSON { identity = builtins.unsafeDiscardStringContext identity; }; + identity = digestOf identity; + text = builtins.toJSON { identity = digestOf identity; }; }; # True when `entry`, an entry of `caisson.exports.configurations` @@ -712,9 +719,9 @@ marker = ancestor.activation.marker ancestor; above = "the ${ancestor.type} configuration `${ancestor.name or "(unnamed)"}`"; self = "the ${manifest.type} configuration `${manifest.name or "(unnamed)"}`"; - # The identity, quoted for the shell. A store path holds no - # single quote, so quoting it whole is enough. - escaped = "'${builtins.unsafeDiscardStringContext expected}'"; + # The digest of the identity, quoted for the shell. A digest is + # hexadecimal, so quoting it whole is enough. + escaped = "'${digestOf expected}'"; in if expected == null then "" @@ -731,9 +738,9 @@ _caissonApplied="$(${jq} -r '.identity // empty' < "$_caissonMarker")" if [ "$_caissonApplied" != ${escaped} ]; then echo "WARNING: ${self} was built for a version of ${above} that differs from the version that is applied." >&2 - echo " applied (configurations declared inside it left out):" >&2 + echo " digest of the version applied (configurations declared inside it left out):" >&2 echo " ''${_caissonApplied:-unknown}" >&2 - echo " built for (configurations declared inside it left out):" >&2 + echo " digest of the version built for (configurations declared inside it left out):" >&2 echo " "${escaped} >&2 echo " Activate ${above} as this configuration was built with it," >&2 echo " or set ${optionText} to false in ${self}." >&2 diff --git a/modules/nixos/activation-marker/default.nix b/modules/nixos/activation-marker/default.nix index 559813e2..133962a8 100644 --- a/modules/nixos/activation-marker/default.nix +++ b/modules/nixos/activation-marker/default.nix @@ -18,10 +18,13 @@ # What the file holds # # The module writes `/etc/caisson/marker.json`, with one field, -# `identity`: the system that the childless evaluation of this machine -# builds, which is the evaluation without the configurations declared -# inside it. Changing one of those configurations leaves the identity -# the same; changing anything else about the machine changes it. +# `identity`: the SHA-256 digest of the store path of the system that +# the childless evaluation of this machine builds, which is the +# evaluation without the configurations declared inside it. Computing +# the store path evaluates that system and builds nothing, and the +# digest names no store path, so the file depends on nothing. Changing +# one of the configurations declared inside leaves the identity the +# same; changing anything else about the machine changes it. # # A configuration declared inside the machine reads the same childless # evaluation through its own manifest, so it computes the same identity diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 5eaa52a9..9526930b 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -738,17 +738,17 @@ let && builtins.isString machine.system.build.toplevel.drvPath; # The same machine writes the file `/etc/caisson/marker.json`. The - # file holds a store path. The store path is the system of the - # machine with the configurations declared inside it left out. + # file holds the digest of the system of the machine with the + # configurations declared inside it left out. # # The system that the machine builds has the units of the homes in - # it. So the store path in the file must differ from the system - # that the machine builds. + # it. So the digest in the file is the digest of the childless + # system, and not of the system that the machine builds. # - # Each home computes the same store path. The activation of each - # home has a step that compares that store path with the file. The - # script of the step must therefore contain the store path and the - # path of the file. + # Each home computes the same digest. The activation of each home + # has a step that compares that digest with the file. The script of + # the step must therefore contain the digest and the path of the + # file. aMachineRecordsWhatItsHomesCompareAgainst = let machine = machineWithHomes.nixosConfigurations.probe.config; @@ -756,21 +756,20 @@ let machineWithHomes.homeConfigurations."declared@probe" machineWithHomes.homeConfigurations."undeclared@probe" ]; - # fromJSON refuses a string that refers to a store path. - record = builtins.fromJSON ( - builtins.unsafeDiscardStringContext machine.environment.etc."caisson/marker.json".text - ); - base = record.identity; + record = builtins.fromJSON machine.environment.etc."caisson/marker.json".text; + digest = record.identity; + childless = + machineWithHomes.caisson.manifest.children.system.x86_64-linux.children.nixos.probe.childlessManifest.outputs.toplevel; comparesAgainstIt = home: let - step = builtins.unsafeDiscardStringContext home.config.home.activation.caissonDrift.data; + step = home.config.home.activation.caissonDrift.data; in - pkgs.lib.hasInfix base step && pkgs.lib.hasInfix "/etc/caisson/marker.json" step; + pkgs.lib.hasInfix digest step && pkgs.lib.hasInfix "/etc/caisson/marker.json" step; in builtins.attrNames record == [ "identity" ] - && pkgs.lib.hasPrefix builtins.storeDir base - && base != builtins.unsafeDiscardStringContext machine.system.build.toplevel.outPath + && digest == builtins.hashString "sha256" childless.outPath + && digest != builtins.hashString "sha256" machine.system.build.toplevel.outPath && builtins.all comparesAgainstIt homes; # A NixOS configuration can be evaluated alone, with diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index f013fedb..6aa2bd48 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -4008,15 +4008,15 @@ in }; # A NixOS configuration with a home declared inside it writes the - # file `caisson/marker.json` under `/etc`. The file holds a store - # path. The store path is the system of the machine with the + # file `caisson/marker.json` under `/etc`. The file holds the digest + # of the store path of the system of the machine with the # configurations declared inside it left out. # # The stub of NixOS puts the number of system units into the # store path of a system. The machine `laptop` has one home, and # `laptop` declares the account of that home. The system of - # `laptop` therefore has one unit. The store path in the file - # must be the system with no units. + # `laptop` therefore has one unit. The digest in the file must be + # that of the system with no units. # # The machine `bare` has no home, and writes no file. # @@ -4074,13 +4074,13 @@ in }; expected = { laptop = { - identity = "/nix/store/stub-system-with-0-units"; + identity = builtins.hashString "sha256" "/nix/store/stub-system-with-0-units"; }; laptopBuilds = "/nix/store/stub-system-with-1-units"; bare = null; outer = null; image = { - identity = "/nix/store/stub-system-with-0-units"; + identity = builtins.hashString "sha256" "/nix/store/stub-system-with-0-units"; }; }; }; From 8a36e437a7ede903314f20395a3500d7360b5db2 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 12:18:57 -0700 Subject: [PATCH 09/14] test: check that a home unit runs its activation through string context The test of the units that activate the homes dropped the string context of the activation package and of the unit script to search one in the other. It now reads the context of the script, which records the store paths the script refers to, and checks that it holds the activation package of the home and not that of the other home. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit b6f1ff3f821a30d1c423abef7aafb5cfe97c2a98) --- tests/pinned-world/default.nix | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 9526930b..717eda86 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -719,20 +719,25 @@ let undeclared = machineWithHomes.homeConfigurations."undeclared@probe"; systemUnit = machine.systemd.services.home-manager-declared; userUnit = machine.systemd.user.services.home-manager-undeclared; - # The comparison is made on plain strings: a string that - # refers to a store path cannot be searched for. - plain = builtins.unsafeDiscardStringContext; + # The script of the unit runs `activate` of the activation + # package of the home: the string context of the script records + # that it refers to the activation package, and the script runs + # an `activate`. runs = unit: home: - pkgs.lib.hasInfix (plain "${home.activationPackage}/activate") ( - plain unit.serviceConfig.ExecStart.text - ); + let + script = unit.serviceConfig.ExecStart.text; + in + builtins.elem home.activationPackage.drvPath (builtins.attrNames (builtins.getContext script)) + && pkgs.lib.hasInfix "/activate" script; in systemUnit.serviceConfig.User == "declared" && systemUnit.unitConfig.RequiresMountsFor == "/home/declared" && runs systemUnit declared + && !(runs systemUnit undeclared) && userUnit.unitConfig.ConditionUser == "undeclared" && runs userUnit undeclared + && !(runs userUnit declared) && !(machine.systemd.services ? home-manager-undeclared) && !(machine.systemd.user.services ? home-manager-declared) && builtins.isString machine.system.build.toplevel.drvPath; From 293bcdd713b90bb841022cc70cab3a6825c005cc Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 11:50:47 -0700 Subject: [PATCH 10/14] feat(nvf,nixvim): keep each Neovim in a profile that its parent activates An nvf or nixvim configuration declared inside a machine, a home or an hjem configuration used to be installed into the packages of the configuration above. It now relates to that configuration as a home relates to its machine: it has an activation of its own, which the configuration above runs each time it is activated itself. `lib.caisson.integrations.profileOf` gives such a configuration the outputs `activationPackage` and `profile`. `activate` checks for drift against the nearest configuration above with an activation, and points a Nix profile at the Neovim with `nix-env --profile`, which also makes the profile a garbage-collector root; it adds no generation when the profile already points there. Inside a machine the profile is a profile of the system, `/nix/var/nix/profiles/caisson//`; inside a home or an hjem configuration, or at a top, it is a profile of the user, `$HOME/.local/state/caisson/profiles//`. The home marker moves under `$HOME/.local/state` as well, since a unit of the machine and a shell may see `$XDG_STATE_HOME` differently. Each class has a `profile-activation` module in its `default` module: - nixos and systemManager: a unit per profile wanted by `multi-user.target`, run as root for a profile of the system, and as the user after `hjem-activate@` for a profile inside an hjem configuration, whose own activation has no step to add; - darwin: steps of `postActivation`, the same way; - homeManager: an activation step after the files are linked and the marker is written, so `home-manager switch`, a switch of the machine and a boot all put the profile back; - hjem: the programs and the variable below. Each also installs `profileShimsOf`: `nvim` (and `vi`, `vim` where the Neovim has them) runs the profile `$CAISSON_NVIM_PROFILE` names, read each time it runs, and the default of the configuration when it is unset. A home or an hjem configuration sets the variable to its own profile, and a shell can `export` another. Where several profiles share the variable, `caisson.profiles.selected` names the default. The modules `nvf-package` of every class and `lib.caisson.nvf.packagesModule` are removed. nvf and nixvim get a `default` module declaring `caisson.drift.warn`. Co-Authored-By: Claude Opus 5.5 --- lib-overlays/home-manager/default.nix | 10 +- lib-overlays/integrations/default.nix | 183 ++++++++++++++++++ lib-overlays/nixvim/default.nix | 31 ++- lib-overlays/nvf/default.nix | 83 ++++---- modules/darwin/default/default.nix | 2 +- modules/darwin/nvf-package/default.nix | 11 -- modules/darwin/profile-activation/default.nix | 91 +++++++++ modules/hjem/default/default.nix | 7 +- modules/hjem/nvf-package/default.nix | 7 - modules/hjem/profile-activation/default.nix | 73 +++++++ .../homeManager/activation-marker/default.nix | 4 +- modules/homeManager/default/default.nix | 7 +- modules/homeManager/nvf-package/default.nix | 11 -- .../profile-activation/default.nix | 79 ++++++++ modules/nixos/default/default.nix | 6 +- modules/nixos/nvf-package/default.nix | 11 -- modules/nixos/profile-activation/default.nix | 170 ++++++++++++++++ modules/nixvim/default | 1 + modules/nvf/default/default.nix | 26 +++ modules/systemManager/default/default.nix | 11 +- modules/systemManager/nvf-package/default.nix | 11 -- modules/systemManager/profile-activation | 1 + tests/pinned-world/default.nix | 53 ++++- 23 files changed, 760 insertions(+), 129 deletions(-) delete mode 100644 modules/darwin/nvf-package/default.nix create mode 100644 modules/darwin/profile-activation/default.nix delete mode 100644 modules/hjem/nvf-package/default.nix create mode 100644 modules/hjem/profile-activation/default.nix delete mode 100644 modules/homeManager/nvf-package/default.nix create mode 100644 modules/homeManager/profile-activation/default.nix delete mode 100644 modules/nixos/nvf-package/default.nix create mode 100644 modules/nixos/profile-activation/default.nix create mode 120000 modules/nixvim/default create mode 100644 modules/nvf/default/default.nix delete mode 100644 modules/systemManager/nvf-package/default.nix create mode 120000 modules/systemManager/profile-activation diff --git a/lib-overlays/home-manager/default.nix b/lib-overlays/home-manager/default.nix index 28fcddd5..0753ab5f 100644 --- a/lib-overlays/home-manager/default.nix +++ b/lib-overlays/home-manager/default.nix @@ -517,15 +517,15 @@ defaultPkgs = args.defaultPkgs or null; inherit exportsTo; # A home is activated for its user. Its identity is its - # activation package; its marker is a file in the state - # directory of the user, which the home-manager module + # activation package; its marker is a file under + # `$HOME/.local/state`, named by `$HOME` and not by + # `$XDG_STATE_HOME`, which a unit of the machine and a shell + # may see differently. The home-manager module # `activation-marker` writes. The configurations with an # activation declared inside the home check it. activation = { identity = manifest: manifest.outputs.activationPackage.outPath; - marker = - manifest: - "\${XDG_STATE_HOME:-$HOME/.local/state}/caisson/markers/${selection.markerFileName manifest}"; + marker = manifest: "$HOME/.local/state/caisson/markers/${selection.markerFileName manifest}"; }; evaluate = evaluation variant args; }; diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index fc336040..cca660fa 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -748,6 +748,184 @@ fi ''; + # A name a configuration is declared under, with the characters + # the shell would read inside double quotes, and the path + # separator, replaced, for use in a path. + safeName = + name: + builtins.replaceStrings [ "/" "\"" "$" "`" "\\" ] [ "_" "_" "_" "_" "_" ] ( + if name == null then "default" else name + ); + + # A configuration that is a program a person runs, such as a + # Neovim, kept in a Nix profile of its own, the way a home keeps + # its generations in the home-manager profile. + # + # `activationPackage` holds `activate`, which checks for drift + # against the nearest configuration above with an activation + # (`driftCheckOf`, when `warn`), and points the profile at + # `package` with `nix-env --profile`, which also makes the + # profile a garbage-collector root. When the profile already + # points at `package`, it changes nothing, so activating the same + # generation again adds no generation to the profile. + # + # Where the profile is depends on the configuration above. Inside + # a machine it is a profile of the system, + # `/nix/var/nix/profiles/caisson//`, which the machine + # activates as root. Inside a home or an hjem configuration, or as + # a top, it is a profile of the user, + # `$HOME/.local/state/caisson/profiles//`. The path + # names `$HOME` and not `$XDG_STATE_HOME`: the units of a machine + # run without `$XDG_STATE_HOME`, and a shell may set it, and the + # profile must be the same file for both. The + # configuration above activates it each time it is activated + # itself, so activating the configuration above puts the profile + # back to what that configuration declares. + # + # `profile` is what the configuration above reads from the + # manifest: the shell text of the path of the profile, whether it + # is a profile of the system or of the user, the `package` the + # profile points at, the `programs` the + # package has in `bin`, and `variable`, the name of the variable a + # shell sets to choose which profile of this sort it runs + # (`profileShimsOf`). + profileOf = + { + pkgs, + manifest, + kind, + package, + programs, + variable, + warn ? true, + }: + let + ancestor = activatingAncestorOf manifest; + ofSystem = ancestor != null && (ancestor.activation.standsAlone or false); + name = safeName (manifest.name or null); + path = + if ofSystem then + "/nix/var/nix/profiles/caisson/${kind}/${name}" + else + "$HOME/.local/state/caisson/profiles/${kind}/${name}"; + check = + if warn then + driftCheckOf { + jq = "${pkgs.jq}/bin/jq"; + optionText = "`caisson.drift.warn`"; + } manifest + else + ""; + in + { + activationPackage = pkgs.writeTextFile { + name = "${kind}-${name}-activation"; + destination = "/activate"; + executable = true; + text = '' + #!${pkgs.runtimeShell} + set -eu + ${check} + profile="${path}" + ${pkgs.coreutils}/bin/mkdir -p "$(${pkgs.coreutils}/bin/dirname "$profile")" + if [ "$(${pkgs.coreutils}/bin/readlink -f "$profile" 2>/dev/null || true)" != '${package}' ]; then + ${pkgs.nix}/bin/nix-env --profile "$profile" --set '${package}' + fi + ''; + }; + profile = { + inherit + path + programs + variable + kind + name + package + ; + scope = if ofSystem then "system" else "user"; + }; + }; + + # The entries of `caisson.exports.configurations` of the + # configuration of `manifest` that it activates as profiles + # (`profileOf`): those with a `profile`, evaluated for the system + # of the configuration, whose nearest configuration above with an + # activation is this one. A profile inside a home inside this + # machine is the home's to activate, and is left out. + profilesActivatedBy = + manifest: configurations: + builtins.filter ( + entry: + (entry.manifest.outputs or { }) ? profile + && (entry.manifest.system or null) == (manifest.system or null) + && checksMarkerOf manifest entry + ) configurations; + + # For each variable the profiles in `entries` are chosen with, the + # profile a configuration runs when a shell sets no other, and the + # programs of all of them. The profile is the one `selected` names + # for the variable, as `/`, or the only one. `what` + # names the configuration, for the message when several profiles + # share a variable and `selected` names none of them. + profileChoicesOf = + { selected, what }: + entries: + let + variables = unique (builtins.map (entry: entry.manifest.outputs.profile.variable) entries); + choiceOf = + variable: + let + sharing = builtins.filter (entry: entry.manifest.outputs.profile.variable == variable) entries; + nameOf = entry: "${entry.manifest.outputs.profile.kind}/${entry.manifest.outputs.profile.name}"; + named = builtins.filter (entry: nameOf entry == selected.${variable}) sharing; + chosen = + if selected ? ${variable} then + if named != [ ] then + builtins.head named + else + throw "caisson: ${what} selects `${selected.${variable}}` with `caisson.profiles.selected.${variable}`, and its profiles for that variable are ${builtins.concatStringsSep ", " (builtins.map nameOf sharing)}." + else if builtins.length sharing == 1 then + builtins.head sharing + else + throw "caisson: ${what} activates several profiles chosen with `${variable}` (${builtins.concatStringsSep ", " (builtins.map nameOf sharing)}). Set `caisson.profiles.selected.${variable}` to the one a shell runs when it sets no other."; + in + { + inherit chosen; + programs = unique (builtins.concatMap (entry: entry.manifest.outputs.profile.programs) sharing); + }; + in + genAttrs variables choiceOf; + + # The programs with which a configuration that activates profiles + # (`profileOf`) lets a shell choose among them: for each program, + # an executable of that name that runs the program of the profile + # the variable names, read each time it runs, and of `fallback` + # when the variable is unset. A configuration sets the variable to + # its default, a configuration declared inside it sets it to its + # own, and a shell can `export` another. `fallback` is shell text, + # which may name variables such as `$HOME`. + profileShimsOf = + { + pkgs, + variable, + programs, + fallback, + }: + pkgs.runCommand "caisson-profile-shims-${variable}" { } ( + '' + mkdir -p "$out/bin" + '' + + builtins.concatStringsSep "" ( + builtins.map (program: '' + cat > "$out/bin/${program}" <<'EOF' + #!${pkgs.runtimeShell} + exec "''${${variable}:-${fallback}}/bin/${program}" "$@" + EOF + chmod +x "$out/bin/${program}" + '') programs + ) + ); + isManifest = value: builtins.isAttrs value && (value._type or null) == "caisson-manifest"; # How a configuration is published: the `exportsTo` its @@ -1037,6 +1215,11 @@ markerOf checksMarkerOf driftCheckOf + safeName + profileOf + profileShimsOf + profilesActivatedBy + profileChoicesOf mkEvaluation publish topValue diff --git a/lib-overlays/nixvim/default.nix b/lib-overlays/nixvim/default.nix index cb955d8a..46bec048 100644 --- a/lib-overlays/nixvim/default.nix +++ b/lib-overlays/nixvim/default.nix @@ -18,6 +18,12 @@ # configuration is published as `nixvimConfigurations..`, # the evaluated configuration, under the name nixvim's flake publishes # its own configurations under. +# +# A nixvim configuration is kept in a Nix profile of its own, as an nvf +# configuration is (`lib.caisson.integrations.profileOf`): its output +# `activationPackage` holds `activate`, and the configuration it is +# declared inside activates it with the module `profile-activation` of +# its class. { mkLibOverlay, ... @@ -132,10 +138,27 @@ } // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) ); + package = value.config.build.package; + kept = selection.profileOf { + inherit pkgs manifest package; + kind = "nixvim"; + # The programs the Neovim has in `bin`: `nvim`, and `vi` + # and `vim` when nixvim adds them. + programs = [ + "nvim" + ] + ++ (if value.config.viAlias or false then [ "vi" ] else [ ]) + ++ (if value.config.vimAlias or false then [ "vim" ] else [ ]); + variable = "CAISSON_NVIM_PROFILE"; + warn = value.config.caisson.drift.warn or true; + }; in { inherit value; - outputs.package = value.config.build.package; + outputs = { + inherit package; + inherit (kept) activationPackage profile; + }; }; # A nixvim configuration is published as @@ -156,6 +179,12 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # A nixvim configuration is activated into its profile. Its + # identity is its Neovim. + activation = { + identity = manifest: manifest.outputs.package.outPath; + marker = manifest: "${manifest.outputs.profile.path}.marker.json"; + }; evaluate = evaluate args; }; diff --git a/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix index 6792d4d7..d72dbe00 100644 --- a/lib-overlays/nvf/default.nix +++ b/lib-overlays/nvf/default.nix @@ -15,11 +15,15 @@ # # The output is `neovim`, the wrapped Neovim, the `neovim` that # `neovimConfiguration` returns. A configuration is published as -# `packages..`. A home, an hjem configuration or a NixOS -# configuration installs the Neovim of an nvf configuration declared -# inside it, with the modules `nvf-package` of their classes, which -# caisson's default modules of those classes import; each is -# `packagesModule` for the option of its class that takes packages. +# `packages..`. +# +# An nvf configuration is kept in a Nix profile of its own, the way a +# home keeps its generations (`lib.caisson.integrations.profileOf`): +# its output `activationPackage` holds `activate`, which points the +# profile at the Neovim, and `profile` says where the profile is. A +# machine, a home or an hjem configuration activates the nvf +# configurations declared inside it each time it is activated itself, +# with the module `profile-activation` of its class. { mkLibOverlay, ... @@ -144,50 +148,28 @@ } // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) ); + neovim = value.config.vim.build.finalPackage; + kept = selection.profileOf { + inherit pkgs manifest; + kind = "nvf"; + package = neovim; + # The programs the Neovim has in `bin`: `nvim`, and `vi` + # and `vim` when nvf adds them. + programs = [ + "nvim" + ] + ++ (if value.config.vim.viAlias then [ "vi" ] else [ ]) + ++ (if value.config.vim.vimAlias then [ "vim" ] else [ ]); + variable = "CAISSON_NVIM_PROFILE"; + warn = value.config.caisson.drift.warn or true; + }; in { inherit value; - outputs.neovim = value.config.vim.build.finalPackage; - }; - - # The module, for a class that installs packages, that installs the - # Neovim of each nvf configuration declared inside a configuration - # of that class: the nvf configurations it passes up, evaluated at - # its system, with no configuration but structural ones between - # them and it. `option` is the path of the option of the class - # that takes the packages, such as `[ "environment" "systemPackages" ]`. - # - # The module reads the configurations from the full evaluation, so - # in the childless evaluation it defines nothing; and in an - # evaluation that does not declare the option, or whose library is - # not a caisson composition, it defines nothing either. - packagesModule = - option: - { - config, - lib, - options, - ... - }: - let - manifest = lib.caisson-core.evalManifest or null; - installedHere = - entry: - entry.manifest.type == "nvf" - && builtins.all (segment: segment.type == "structural" || segment.type == "system") ( - lib.lists.init entry.path - ) - && entry.manifest.system == (manifest.system or null); - neovims = builtins.map (entry: entry.manifest.outputs.neovim) ( - builtins.filter installedHere (config.caisson.exports.configurations or [ ]) - ); - in - { - config = lib.optionalAttrs (lib.hasAttrByPath option options) ( - lib.mkIf (manifest != null && !manifest.childless && neovims != [ ]) ( - lib.setAttrByPath option neovims - ) - ); + outputs = { + inherit neovim; + inherit (kept) activationPackage profile; + }; }; # An nvf configuration is published as `packages..`, @@ -207,6 +189,12 @@ perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; + # An nvf configuration is activated into its profile. Its + # identity is its Neovim. + activation = { + identity = manifest: manifest.outputs.neovim.outPath; + marker = manifest: "${manifest.outputs.profile.path}.marker.json"; + }; evaluate = evaluate args; }; @@ -264,9 +252,6 @@ ecosystemArgs ? null, }@args: configuration args; - extra = { - inherit packagesModule; - }; }; in { diff --git a/modules/darwin/default/default.nix b/modules/darwin/default/default.nix index 944d7439..ec3e6fc2 100644 --- a/modules/darwin/default/default.nix +++ b/modules/darwin/default/default.nix @@ -28,7 +28,7 @@ (mkModule ./caisson) closure-lib.caisson.modules.darwin.home-manager-activation closure-lib.caisson.modules.darwin.hjem-activation - closure-lib.caisson.modules.darwin.nvf-package + closure-lib.caisson.modules.darwin.profile-activation closure-lib.caisson.modules.darwin.activation-marker ] else diff --git a/modules/darwin/nvf-package/default.nix b/modules/darwin/nvf-package/default.nix deleted file mode 100644 index 47d5e912..00000000 --- a/modules/darwin/nvf-package/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# A darwin module that installs the Neovim of each nvf configuration -# declared inside the darwin configuration, in -# `environment.systemPackages`. caisson's `default` darwin module -# imports it. See `lib.caisson.nvf.packagesModule`. -{ closure-lib, ... }: -closure-lib.caisson.nvf.packagesModule [ - "environment" - "systemPackages" -] diff --git a/modules/darwin/profile-activation/default.nix b/modules/darwin/profile-activation/default.nix new file mode 100644 index 00000000..6fc2231e --- /dev/null +++ b/modules/darwin/profile-activation/default.nix @@ -0,0 +1,91 @@ +# SPDX-License-Identifier: MIT +# +# A darwin module with which a macOS machine activates the profiles +# declared inside it, as the NixOS module `profile-activation` does for a +# NixOS machine: see there. +# +# A step of the machine's `postActivation` script runs `activate` of +# each profile of the system as root, and of each profile declared +# inside an hjem configuration inside the machine as its user, through +# `launchctl asuser` and `sudo`, after the hjem agents are restarted. A +# profile inside a home is activated by the home. The machine installs +# the programs that choose among the profiles of the system in +# `environment.systemPackages`. +# +# caisson's `default` darwin module imports this module. It defines +# nothing in the childless evaluation. +{ closure-lib, ... }: +{ + config, + lib, + pkgs, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + exported = config.caisson.exports.configurations; + + ofSystem = selection.profilesActivatedBy evalManifest exported; + + hjemUsers = + builtins.map + (entry: { + inherit (entry.manifest.value.config) user; + profiles = selection.profilesActivatedBy entry.manifest exported; + }) + ( + builtins.filter ( + entry: entry.manifest.type == "hjem" && selection.checksMarkerOf evalManifest entry + ) exported + ); + + asRoot = entry: '' + ${entry.manifest.outputs.activationPackage}/activate + ''; + + asUser = user: entry: '' + launchctl asuser "$(id -u ${lib.escapeShellArg user})" sudo -u ${lib.escapeShellArg user} --set-home ${entry.manifest.outputs.activationPackage}/activate + ''; + + choices = selection.profileChoicesOf { + selected = config.caisson.profiles.selected; + what = "this machine"; + } ofSystem; + + shims = lib.mapAttrsToList ( + variable: choice: + selection.profileShimsOf { + inherit pkgs variable; + inherit (choice) programs; + fallback = choice.chosen.manifest.outputs.profile.path; + } + ) choices; + + steps = + builtins.map asRoot ofSystem + ++ builtins.concatMap (hjem: builtins.map (asUser hjem.user) hjem.profiles) hjemUsers; +in +{ + imports = [ closure-lib.caisson.modules.darwin.core ]; + + options.caisson.profiles.selected = lib.mkOption { + type = lib.types.attrsOf lib.types.str; + default = { }; + example = { + CAISSON_NVIM_PROFILE = "nvf/editor"; + }; + description = '' + By the variable that chooses among them, the profile a shell runs + when it sets no other, as `/`: `nvf/editor` is the nvf + configuration declared as `editor`. Needed only where several + profiles declared inside this configuration share a variable. + ''; + }; + + config = lib.mkIf (!evalManifest.childless && steps != [ ]) { + system.activationScripts.postActivation.text = lib.mkAfter (lib.concatStrings steps); + environment.systemPackages = shims; + }; +} diff --git a/modules/hjem/default/default.nix b/modules/hjem/default/default.nix index 69f82a57..9283efa8 100644 --- a/modules/hjem/default/default.nix +++ b/modules/hjem/default/default.nix @@ -5,15 +5,16 @@ # `moduleImports`, so this is what it gets from caisson when it does not # select its modules itself: # -# - `nvf-package`, which installs the Neovim of each nvf configuration -# declared inside the hjem configuration; +# - `profile-activation`, which lets a shell choose among the profiles +# of the nvf or nixvim configurations declared inside the hjem +# configuration, which the machine activates; # - `activation-marker`, which records what the hjem configuration # applied, for the configurations declared inside it to check. { closure-lib, ... }: { ... }: { imports = [ - closure-lib.caisson.modules.hjem.nvf-package + closure-lib.caisson.modules.hjem.profile-activation closure-lib.caisson.modules.hjem.activation-marker ]; } diff --git a/modules/hjem/nvf-package/default.nix b/modules/hjem/nvf-package/default.nix deleted file mode 100644 index 89f821ea..00000000 --- a/modules/hjem/nvf-package/default.nix +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# An hjem module that installs the Neovim of each nvf configuration -# declared inside the hjem configuration, in `packages`. caisson's -# `default` hjem module imports it. See `lib.caisson.nvf.packagesModule`. -{ closure-lib, ... }: -closure-lib.caisson.nvf.packagesModule [ "packages" ] diff --git a/modules/hjem/profile-activation/default.nix b/modules/hjem/profile-activation/default.nix new file mode 100644 index 00000000..9dfa2753 --- /dev/null +++ b/modules/hjem/profile-activation/default.nix @@ -0,0 +1,73 @@ +# SPDX-License-Identifier: MIT +# +# An hjem module for the profiles declared inside an hjem configuration: +# the configurations that are programs a person runs, such as an nvf or +# nixvim Neovim, each kept in a profile of the user, +# `$HOME/.local/state/caisson/profiles//` +# (`lib.caisson.integrations.profileOf`). +# +# hjem's activation has no step a configuration can add, so the machine +# the hjem configuration is declared inside activates the profiles, as +# the user, after the hjem configuration is applied (the module +# `profile-activation` of the class of the machine). This module adds +# to the packages of the user the programs with which a shell chooses +# among profiles (`lib.caisson.integrations.profileShimsOf`), and sets +# the variable that chooses, `CAISSON_NVIM_PROFILE` for a Neovim, to the +# profile of the user, so that the programs the machine installs choose +# the same. +# +# caisson's `default` hjem module imports this module. It defines +# nothing in the childless evaluation. +{ ... }: +{ + config, + lib, + pkgs, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + profiles = selection.profilesActivatedBy evalManifest config.caisson.exports.configurations; + + choices = selection.profileChoicesOf { + selected = config.caisson.profiles.selected; + what = "this hjem configuration"; + } profiles; + + # The path of a profile, with `$HOME` written out as the directory of + # the user, for a variable hjem writes as given. + pathOf = + choice: + builtins.replaceStrings [ "$HOME" ] [ config.directory ] + choice.chosen.manifest.outputs.profile.path; +in +{ + options.caisson.profiles.selected = lib.mkOption { + type = lib.types.attrsOf lib.types.str; + default = { }; + example = { + CAISSON_NVIM_PROFILE = "nvf/editor"; + }; + description = '' + By the variable that chooses among them, the profile a shell runs + when it sets no other, as `/`: `nvf/editor` is the nvf + configuration declared as `editor`. Needed only where several + profiles declared inside this hjem configuration share a variable. + ''; + }; + + config = lib.mkIf (!evalManifest.childless && profiles != [ ]) { + packages = lib.mapAttrsToList ( + variable: choice: + selection.profileShimsOf { + inherit pkgs variable; + inherit (choice) programs; + fallback = pathOf choice; + } + ) choices; + + environment.sessionVariables = builtins.mapAttrs (_variable: pathOf) choices; + }; +} diff --git a/modules/homeManager/activation-marker/default.nix b/modules/homeManager/activation-marker/default.nix index 8654916e..80df72ee 100644 --- a/modules/homeManager/activation-marker/default.nix +++ b/modules/homeManager/activation-marker/default.nix @@ -7,8 +7,8 @@ # # A step of the activation of the home writes the identity of the # childless evaluation of the home, its activation package without the -# configurations declared inside it, into a file in the state directory -# of the user, `$XDG_STATE_HOME/caisson/markers/home-manager-.json`. +# configurations declared inside it, into a file in the home directory +# of the user, `$HOME/.local/state/caisson/markers/home-manager-.json`. # A configuration declared inside the home, such as an nvf # configuration, compares that file with the identity it was built for # when it is activated by hand. The step runs after the files of the diff --git a/modules/homeManager/default/default.nix b/modules/homeManager/default/default.nix index cb30242d..35cd0c99 100644 --- a/modules/homeManager/default/default.nix +++ b/modules/homeManager/default/default.nix @@ -5,8 +5,9 @@ # `moduleImports`, so this is what a home gets from caisson when it does # not select its modules itself: # -# - `nvf-package`, which installs the Neovim of each nvf configuration -# declared inside the home; +# - `profile-activation`, which activates the profile of each nvf or +# nixvim configuration declared inside the home, and lets a shell +# choose among them; # - `activation-drift`, with which the activation of the home warns when # the home was built for a different version of the configuration it # is declared inside, such as its machine; @@ -17,7 +18,7 @@ { ... }: { imports = [ - closure-lib.caisson.modules.homeManager.nvf-package + closure-lib.caisson.modules.homeManager.profile-activation closure-lib.caisson.modules.homeManager.activation-drift closure-lib.caisson.modules.homeManager.activation-marker ]; diff --git a/modules/homeManager/nvf-package/default.nix b/modules/homeManager/nvf-package/default.nix deleted file mode 100644 index aa4a3168..00000000 --- a/modules/homeManager/nvf-package/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# A home-manager module that installs the Neovim of each nvf -# configuration declared inside the home, in `home.packages`. caisson's -# `default` home-manager module imports it. See -# `lib.caisson.nvf.packagesModule`. -{ closure-lib, ... }: -closure-lib.caisson.nvf.packagesModule [ - "home" - "packages" -] diff --git a/modules/homeManager/profile-activation/default.nix b/modules/homeManager/profile-activation/default.nix new file mode 100644 index 00000000..78b4c440 --- /dev/null +++ b/modules/homeManager/profile-activation/default.nix @@ -0,0 +1,79 @@ +# SPDX-License-Identifier: MIT +# +# A home-manager module with which a home activates the profiles +# declared inside it: the configurations that are programs a person +# runs, such as an nvf or nixvim Neovim, each kept in a Nix profile of +# its own, here a profile of the user, +# `$HOME/.local/state/caisson/profiles//` +# (`lib.caisson.integrations.profileOf`). +# +# A step of the activation of the home runs `activate` of each, after +# the files of the home are linked and the home has recorded what it +# applied. So `home-manager switch` puts each profile back to what the +# home declares, and so does the machine when it activates the home, at +# a switch of the machine and at boot. +# +# The home also installs the programs with which a shell chooses among +# profiles (`lib.caisson.integrations.profileShimsOf`), and sets the +# variable that chooses, `CAISSON_NVIM_PROFILE` for a Neovim, to the +# profile of the home, so that the programs the machine installs choose +# the same. A shell can `export` another value. +# +# caisson's `default` home-manager module imports this module. It +# defines nothing in the childless evaluation. +{ ... }: +{ + config, + lib, + pkgs, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + profiles = selection.profilesActivatedBy evalManifest config.caisson.exports.configurations; + + choices = selection.profileChoicesOf { + selected = config.caisson.profiles.selected; + what = "this home"; + } profiles; +in +{ + options.caisson.profiles.selected = lib.mkOption { + type = lib.types.attrsOf lib.types.str; + default = { }; + example = { + CAISSON_NVIM_PROFILE = "nvf/editor"; + }; + description = '' + By the variable that chooses among them, the profile a shell runs + when it sets no other, as `/`: `nvf/editor` is the nvf + configuration declared as `editor`. Needed only where several + profiles declared inside this home share a variable. + ''; + }; + + config = lib.mkIf (!evalManifest.childless && profiles != [ ]) { + home.activation.caissonProfiles = + lib.hm.dag.entryAfter [ "linkGeneration" "caissonActivationMarker" ] + ( + lib.concatMapStrings (entry: '' + run ${entry.manifest.outputs.activationPackage}/activate + '') profiles + ); + + home.packages = lib.mapAttrsToList ( + variable: choice: + selection.profileShimsOf { + inherit pkgs variable; + inherit (choice) programs; + fallback = choice.chosen.manifest.outputs.profile.path; + } + ) choices; + + home.sessionVariables = builtins.mapAttrs ( + _variable: choice: choice.chosen.manifest.outputs.profile.path + ) choices; + }; +} diff --git a/modules/nixos/default/default.nix b/modules/nixos/default/default.nix index 3b8d085a..b1e9167a 100644 --- a/modules/nixos/default/default.nix +++ b/modules/nixos/default/default.nix @@ -23,6 +23,10 @@ # - The registered module `home-manager-activation` writes the # systemd units that activate those homes on the machine. # +# The registered module `profile-activation` activates the profile of +# each nvf or nixvim configuration declared inside the machine, and +# installs the programs with which a shell chooses among them. +# # The registered module `activation-marker` writes a file on the # machine that names what the machine applied. Every configuration with # an activation declared inside the machine, a home among them, compares @@ -42,7 +46,7 @@ (mkModule ./caisson) closure-lib.caisson.modules.nixos.home-manager-activation closure-lib.caisson.modules.nixos.hjem-activation - closure-lib.caisson.modules.nixos.nvf-package + closure-lib.caisson.modules.nixos.profile-activation closure-lib.caisson.modules.nixos.activation-marker ] else diff --git a/modules/nixos/nvf-package/default.nix b/modules/nixos/nvf-package/default.nix deleted file mode 100644 index c4228cea..00000000 --- a/modules/nixos/nvf-package/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# A NixOS module that installs the Neovim of each nvf configuration -# declared inside the NixOS configuration, in -# `environment.systemPackages`. caisson's `default` NixOS module imports -# it. See `lib.caisson.nvf.packagesModule`. -{ closure-lib, ... }: -closure-lib.caisson.nvf.packagesModule [ - "environment" - "systemPackages" -] diff --git a/modules/nixos/profile-activation/default.nix b/modules/nixos/profile-activation/default.nix new file mode 100644 index 00000000..efe81529 --- /dev/null +++ b/modules/nixos/profile-activation/default.nix @@ -0,0 +1,170 @@ +# SPDX-License-Identifier: MIT +# +# A module with which a machine activates the profiles declared inside +# it: the configurations that are programs a person runs, such as an +# nvf or nixvim Neovim, each kept in a Nix profile of its own +# (`lib.caisson.integrations.profileOf`). +# +# A profile declared directly inside the machine is a profile of the +# system, `/nix/var/nix/profiles/caisson//`. The module +# writes a system unit for each, `caisson-profile--`, which +# runs its `activate` as root. The unit is wanted by +# `multi-user.target`, so it runs at boot and again when a switch +# changes it, and the profile is put back to what the machine declares +# each time. +# +# A profile declared inside an hjem configuration inside the machine is +# a profile of the hjem user. hjem's activation has no step a +# configuration can add, so the machine activates it, with a unit +# `caisson-profile---` that runs as the user after the +# hjem configuration is applied. A profile inside a home is activated by +# the home. +# +# The machine also installs, in `environment.systemPackages`, the +# programs with which a shell chooses among the profiles of the system +# (`lib.caisson.integrations.profileShimsOf`): `nvim` runs the Neovim of +# the profile `$CAISSON_NVIM_PROFILE` names, and of the default profile +# of the machine when it is unset. A home or an hjem configuration sets +# the variable to its own profile. +# +# The module is registered for the `nixos` and `systemManager` classes, +# under the same name, and the `default` module of each imports it. +# system-manager starts the units wanted by `multi-user.target` with +# `system-manager.target`. It defines nothing in the childless +# evaluation, or in an evaluation without systemd options. +{ closure-lib, ... }: +{ + config, + lib, + options, + pkgs, + ... +}: +let + inherit (lib.caisson) evalManifest; + selection = lib.caisson.integrations; + + hasUnits = options ? systemd && options.systemd ? services; + hasPackages = options ? environment && options.environment ? systemPackages; + + coreModule = + if (evalManifest.type or null) == "system-manager" then + closure-lib.caisson.modules.systemManager.core + else + closure-lib.caisson.modules.nixos.core; + + exported = config.caisson.exports.configurations; + + ofSystem = selection.profilesActivatedBy evalManifest exported; + + # The hjem configurations this machine activates, each with the + # profiles declared inside it, which the machine passes up as well. + hjemUsers = + builtins.map + (entry: { + inherit (entry.manifest.value.config) user; + profiles = selection.profilesActivatedBy entry.manifest exported; + }) + ( + builtins.filter ( + entry: entry.manifest.type == "hjem" && selection.checksMarkerOf evalManifest entry + ) exported + ); + + # The parts of a unit name: the name of a profile has no `/` + # (`lib.caisson.integrations.safeName`), and a user name is used as + # the module `hjem-activation` uses it for `hjem-activate@`. + escape = part: part; + + unitOf = + { + entry, + user ? null, + }: + let + inherit (entry.manifest.outputs) profile activationPackage; + in + { + name = + if user == null then + "caisson-profile-${escape profile.kind}-${escape profile.name}" + else + "caisson-profile-${escape user}-${escape profile.kind}-${escape profile.name}"; + value = { + description = "Activate the ${profile.kind} profile ${profile.name}${ + if user == null then "" else " of ${user}" + }"; + wantedBy = [ "multi-user.target" ]; + wants = [ "nix-daemon.socket" ]; + after = [ + "nix-daemon.socket" + ] + ++ (if user == null then [ ] else [ "hjem-activate@${escape user}.service" ]); + # The unit is not stopped when its definition changes; the new + # generation starts it again, which activates the new profile. + stopIfChanged = false; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + ExecStart = "${activationPackage}/activate"; + } + // (if user == null then { } else { User = user; }); + }; + }; + + choices = selection.profileChoicesOf { + selected = config.caisson.profiles.selected; + what = "this machine"; + } ofSystem; + + shims = lib.mapAttrsToList ( + variable: choice: + selection.profileShimsOf { + inherit pkgs variable; + inherit (choice) programs; + fallback = choice.chosen.manifest.outputs.profile.path; + } + ) choices; +in +{ + imports = [ coreModule ]; + + options.caisson.profiles.selected = lib.mkOption { + type = lib.types.attrsOf lib.types.str; + default = { }; + example = { + CAISSON_NVIM_PROFILE = "nvf/editor"; + }; + description = '' + By the variable that chooses among them, the profile a shell runs + when it sets no other, as `/`: `nvf/editor` is the nvf + configuration declared as `editor`. Needed only where several + profiles declared inside this configuration share a variable. + ''; + }; + + # `optionalAttrs` and not `mkIf` decides whether each option is + # defined at all: NixOS refuses a definition, even under `mkIf false`, + # of an option that no module declares, and the nixos-minimal + # integration can produce an evaluation without these options. + config = lib.mkIf (!evalManifest.childless) ( + lib.optionalAttrs hasUnits { + systemd.services = builtins.listToAttrs ( + builtins.map (entry: unitOf { inherit entry; }) ofSystem + ++ builtins.concatMap ( + hjem: + builtins.map ( + entry: + unitOf { + inherit entry; + inherit (hjem) user; + } + ) hjem.profiles + ) hjemUsers + ); + } + // lib.optionalAttrs hasPackages { + environment.systemPackages = shims; + } + ); +} diff --git a/modules/nixvim/default b/modules/nixvim/default new file mode 120000 index 00000000..a5ee35aa --- /dev/null +++ b/modules/nixvim/default @@ -0,0 +1 @@ +../nvf/default \ No newline at end of file diff --git a/modules/nvf/default/default.nix b/modules/nvf/default/default.nix new file mode 100644 index 00000000..338a1964 --- /dev/null +++ b/modules/nvf/default/default.nix @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: MIT +# +# The `default` module caisson registers for the `nvf` class, and, for +# the same file, for the `nixvim` class. A configuration of either +# class imports every registered module named `default` unless it +# passes `moduleImports`. +# +# It declares `caisson.drift.warn`. The configuration is kept in a Nix +# profile of its own, and its `activate` checks for drift against the +# configuration it is declared inside, as a home does +# (`lib.caisson.integrations.profileOf`). The option turns the warning +# off. +{ ... }: +{ lib, ... }: +{ + options.caisson.drift.warn = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + Whether `activate` of this configuration prints a warning when the + configuration was built for a version of the configuration it is + declared inside, such as a home or a machine, that differs from + the version that is applied. + ''; + }; +} diff --git a/modules/systemManager/default/default.nix b/modules/systemManager/default/default.nix index b46f60d4..d2def52e 100644 --- a/modules/systemManager/default/default.nix +++ b/modules/systemManager/default/default.nix @@ -17,8 +17,13 @@ # `hjem-activation`, the same modules a NixOS machine uses, write the # systemd units that activate the homes and the hjem configurations # when the machine is activated. -# - The registered module `nvf-package` installs the Neovim of each nvf -# configuration declared inside the machine. +# - The registered module `profile-activation`, the same module a NixOS +# machine uses, activates the profile of each nvf or nixvim +# configuration declared inside the machine, and lets a shell choose +# among them. +# - The registered module `activation-marker` records what the machine +# applied, for the configurations declared inside it to check for +# drift. # # The parts read options of caisson's core module. The core module # leaves its options out when the library of the evaluation is not a @@ -32,7 +37,7 @@ (mkModule ./caisson) closure-lib.caisson.modules.systemManager.home-manager-activation closure-lib.caisson.modules.systemManager.hjem-activation - closure-lib.caisson.modules.systemManager.nvf-package + closure-lib.caisson.modules.systemManager.profile-activation closure-lib.caisson.modules.systemManager.activation-marker ] else diff --git a/modules/systemManager/nvf-package/default.nix b/modules/systemManager/nvf-package/default.nix deleted file mode 100644 index 0704d662..00000000 --- a/modules/systemManager/nvf-package/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# A system-manager module that installs the Neovim of each nvf -# configuration declared inside the system-manager configuration, in -# `environment.systemPackages`. caisson's `default` system-manager -# module imports it. See `lib.caisson.nvf.packagesModule`. -{ closure-lib, ... }: -closure-lib.caisson.nvf.packagesModule [ - "environment" - "systemPackages" -] diff --git a/modules/systemManager/profile-activation b/modules/systemManager/profile-activation new file mode 120000 index 00000000..dd679a0a --- /dev/null +++ b/modules/systemManager/profile-activation @@ -0,0 +1 @@ +../nixos/profile-activation \ No newline at end of file diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 717eda86..240794e9 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -1741,13 +1741,31 @@ let inherit pkgs; modules = [ module ]; }).neovim; - drvPaths = packages: builtins.map (package: package.drvPath) packages; + machine = top.nixosConfigurations.workstation.config; + home = top.homeConfigurations.alice.config; + unit = machine.systemd.services.caisson-profile-nvf-machine-editor; + names = packages: builtins.map (package: package.name or "") packages; + topProfile = top.caisson.manifest.children.system.x86_64-linux.children.nvf.editor.outputs.profile; in top.packages.x86_64-linux.editor.drvPath == upstream.drvPath - && builtins.elem upstream.drvPath ( - drvPaths top.nixosConfigurations.workstation.config.environment.systemPackages + # The machine activates the profile of the system as root, at boot + # and at a switch, and lets a shell choose it with `nvim`. + && unit.wantedBy == [ "multi-user.target" ] + && !(unit.serviceConfig ? User) + && builtins.elem "caisson-profile-shims-CAISSON_NVIM_PROFILE" ( + names machine.environment.systemPackages ) - && builtins.elem upstream.drvPath (drvPaths top.homeConfigurations.alice.config.home.packages); + # The home activates its own profile, after its files are linked, + # and chooses it for its shells. + && pkgs.lib.hasInfix "/activate" home.home.activation.caissonProfiles.data + && + home.home.sessionVariables.CAISSON_NVIM_PROFILE + == "$HOME/.local/state/caisson/profiles/nvf/home-editor" + # The top's own profile is a profile of the user, and it points at + # the Neovim nvf builds. + && topProfile.scope == "user" + && topProfile.path == "$HOME/.local/state/caisson/profiles/nvf/editor" + && topProfile.package.drvPath == upstream.drvPath; # A nixvim configuration is published as # `nixvimConfigurations..`, and its package is the one @@ -1907,11 +1925,11 @@ let top.nixidyEnvs.x86_64-linux.prod.environmentPackage.drvPath == upstream.environmentPackage.drvPath; # A system-manager machine activates the home, the hjem configuration - # and the nvf configuration declared inside it, as a NixOS machine + # and the nvf configurations declared inside it, as a NixOS machine # does: the home and the hjem configuration take their directory from # the account the machine declares, the machine writes the units that - # activate them, installs the packages of the hjem user and the - # Neovim, and the home is published as `probe@box`. + # activate them and the profiles, installs the packages of the hjem + # user, and the home is published as `probe@box`. systemManagerActivatesWhatIsDeclaredInside = let top = hiveLib.caisson.structural.mkTopConfiguration { @@ -1939,10 +1957,14 @@ let caisson.hjem.configurations.probe = lib.caisson.hjem.mkConfiguration { ecosystemSrc = inputs.hjem; configModule = - { pkgs, ... }: + { lib, pkgs, ... }: { files.".config/probe/greeting".text = "hello"; packages = [ pkgs.hello ]; + caisson.nvf.configurations.hjem-editor = lib.caisson.nvf.mkConfiguration { + ecosystemSrc = inputs.nvf; + configModule.vim.vimAlias = true; + }; }; }; caisson.nvf.configurations.editor = lib.caisson.nvf.mkConfiguration { @@ -1958,8 +1980,9 @@ let top.caisson.manifest.children.system.x86_64-linux.children.system-manager.box.children.system.x86_64-linux.children; home = children.home-manager.probe.value.config; hjem = children.hjem.probe.value.config; - neovim = children.nvf.editor.outputs.neovim; names = packages: builtins.map (package: package.name or "") packages; + systemUnit = box.systemd.services.caisson-profile-nvf-editor; + hjemUnit = box.systemd.services.caisson-profile-probe-nvf-hjem-editor; in home.home.homeDirectory == "/home/probe" # The machine records what it applied, and the home compares @@ -1969,7 +1992,17 @@ let && hjem.directory == "/home/probe" && box.systemd.services ? home-manager-probe && box.systemd.services ? "hjem-activate@" - && builtins.elem neovim.name (names box.environment.systemPackages) + # The machine activates its profile of the system as root, and the + # profile inside the hjem configuration as its user, after hjem + # applies the configuration; hjem chooses the profile of the user. + && systemUnit.wantedBy == [ "multi-user.target" ] + && !(systemUnit.serviceConfig ? User) + && hjemUnit.serviceConfig.User == "probe" + && builtins.elem "hjem-activate@probe.service" hjemUnit.after + && builtins.elem "caisson-profile-shims-CAISSON_NVIM_PROFILE" (names box.environment.systemPackages) + && + hjem.environment.sessionVariables.CAISSON_NVIM_PROFILE + == "/home/probe/.local/state/caisson/profiles/nvf/hjem-editor" && builtins.elem pkgs.hello.name (names box.users.users.probe.packages) && builtins.attrNames top.homeConfigurations == [ "probe@box" ]; From d70112bccd4efcacd31327c26302d2ee4a4dc132 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 12:04:51 -0700 Subject: [PATCH 11/14] docs(readme): say nvf and nixvim configurations are kept in profiles Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fc6997c2..03db58d3 100644 --- a/README.md +++ b/README.md @@ -133,12 +133,12 @@ source by. | `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.` that `nix fmt` runs | -| `nvf` | `nvf` | a Neovim written as nvf modules, published as `packages..`; a NixOS machine, a darwin machine, a home or an hjem configuration installs the Neovim of an nvf configuration declared inside it | -| `nixvim` | `nixvim` | a Neovim written as nixvim modules, published as `nixvimConfigurations..` | +| `nvf` | `nvf` | a Neovim written as nvf modules, published as `packages..`, kept in a Nix profile of its own that the machine, home or hjem configuration it is declared inside activates | +| `nixvim` | `nixvim` | a Neovim written as nixvim modules, published as `nixvimConfigurations..`, kept in a profile as an nvf configuration is | | `topology` | `nix-topology` | the diagram of the NixOS configurations declared inside it, as nix-topology renders it; the configuration named `default` is published as `topology.` | | `kubenix` | `kubenix` | Kubernetes resources written as kubenix modules, published as `packages..`, the JSON `kubectl apply -f` reads | | `nixidy` | `nixidy` | an Argo CD environment written as nixidy modules, evaluated with nixidy's `mkEnv` and published as the `nixidyEnvs..` the nixidy CLI reads | -| `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads; they activate the homes and hjem configurations declared inside them, and install the Neovim of the nvf configurations declared inside them | +| `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads; they activate the homes and hjem configurations declared inside them, and the profiles of the nvf and nixvim configurations declared inside them | | `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 | Each integration finds its ecosystem in this order: the `ecosystemSrc` From c94c06c8e202dd3f98bb83f1bb001572cb34ab1a Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 12:58:31 -0700 Subject: [PATCH 12/14] docs: write "a hjem", not "an hjem" hjem is pronounced with a leading "y" sound, so it takes "a". Co-Authored-By: Claude Opus 5.5 --- configs/flake/caisson/partitions/checks.nix | 4 ++-- docs/development/testing-architecture.md | 4 ++-- docs/development/testing.md | 2 +- lib-overlays/hjem/default.nix | 16 ++++++++-------- lib-overlays/integrations/default.nix | 2 +- lib-overlays/nvf/default.nix | 2 +- modules/darwin/default/caisson/forChildren.nix | 2 +- modules/darwin/hjem-activation/default.nix | 2 +- modules/darwin/profile-activation/default.nix | 2 +- modules/hjem/activation-marker/default.nix | 4 ++-- modules/hjem/darwin-parent/default.nix | 6 +++--- modules/hjem/default/default.nix | 2 +- modules/hjem/nixos-parent/default.nix | 6 +++--- modules/hjem/profile-activation/default.nix | 2 +- modules/nixos/default/caisson/forChildren.nix | 4 ++-- modules/nixos/hjem-activation/default.nix | 4 ++-- modules/nixos/profile-activation/default.nix | 4 ++-- .../default/caisson/forChildren.nix | 2 +- tests/darwin-system/default.nix | 4 ++-- tests/hjem-system/default.nix | 6 +++--- tests/pinned-world/default.nix | 6 +++--- 21 files changed, 43 insertions(+), 43 deletions(-) diff --git a/configs/flake/caisson/partitions/checks.nix b/configs/flake/caisson/partitions/checks.nix index 0b6ac347..fd9acec4 100644 --- a/configs/flake/caisson/partitions/checks.nix +++ b/configs/flake/caisson/partitions/checks.nix @@ -178,7 +178,7 @@ }; in builtins.seq suite.summary (pkgs.runCommand "pinned-world" { } "touch $out"); - # The manifest file of an hjem configuration inside a NixOS + # The manifest file of a hjem configuration inside a NixOS # machine and the units that activate it, built. Building # the manifest file checks it against hjem's own schema # with `cue vet`, so the file the machine activates is one @@ -297,7 +297,7 @@ }; }; in - # The bare machine, and the machine with a home and an hjem + # The bare machine, and the machine with a home and a hjem # configuration inside it, whose system holds the activation # of the home and the launchd agents of hjem. probe.pkgs.linkFarm "darwin-system" [ diff --git a/docs/development/testing-architecture.md b/docs/development/testing-architecture.md index 4f4def03..16368171 100644 --- a/docs/development/testing-architecture.md +++ b/docs/development/testing-architecture.md @@ -312,7 +312,7 @@ The checks partition merges outputs from all test and example flakes into the | `literate-flake-greeting` | `examples/literate-flake/` | Example greeting package builds | | `debug-disabled` | `checks.nix` (inline) | `self.debug` is not exposed in production | | `pinned-world` | `checks.nix` + `tests/pinned-world/` | caisson composed with the pinned upstream world (nixpkgs, home-manager, colmena, disko, nix-darwin, hjem, terranix, system-manager) evaluates end to end; the `drift` workflow runs the same check against advanced pins | -| `hjem-system` | `checks.nix` + `tests/hjem-system/` | The manifest file of an hjem configuration inside a NixOS machine builds, which checks it against hjem's schema with `cue vet`, and so do the units that activate it, which runs shellcheck on their scripts | -| `darwin-system` (aarch64-darwin) | `checks.nix` + `tests/darwin-system/` | The two macOS machines of the pinned-world suite, declared through the darwin integration, build: the bare machine, and the machine with a home and an hjem configuration inside it; the `darwin-build` job of the check workflow builds them on a macOS runner | +| `hjem-system` | `checks.nix` + `tests/hjem-system/` | The manifest file of a hjem configuration inside a NixOS machine builds, which checks it against hjem's schema with `cue vet`, and so do the units that activate it, which runs shellcheck on their scripts | +| `darwin-system` (aarch64-darwin) | `checks.nix` + `tests/darwin-system/` | The two macOS machines of the pinned-world suite, declared through the darwin integration, build: the bare machine, and the machine with a home and a hjem configuration inside it; the `darwin-build` job of the check workflow builds them on a macOS runner | | `argument-errors` | `checks.nix` + `tests/argument-errors/` | The argument error of every entry point comes from Nix itself, raised at the call site with no frame of caisson | | `eval-weight` | `checks.nix` + `tests/eval-weight/` | Framework evaluation cost held to committed ceilings ([guide](https://nix-caisson.github.io/docs/eval-weight.html)) | diff --git a/docs/development/testing.md b/docs/development/testing.md index 6b3b3a13..5cd74043 100644 --- a/docs/development/testing.md +++ b/docs/development/testing.md @@ -28,7 +28,7 @@ plumbing works. configuration, a NixOS system with homes declared inside it, a colmena hive, disk layouts with the machines installed onto them, a darwin machine evaluated at aarch64-darwin with and without a home - and an hjem configuration inside it, hjem configurations on + and a hjem configuration inside it, hjem configurations on their own and inside a NixOS machine, a terranix and a system-manager configuration, and the composition guarantees of the real exported overlays. The `hjem-system` check builds the manifest file of the hjem diff --git a/lib-overlays/hjem/default.nix b/lib-overlays/hjem/default.nix index 43f6c7bf..99bc5da3 100644 --- a/lib-overlays/hjem/default.nix +++ b/lib-overlays/hjem/default.nix @@ -1,7 +1,7 @@ # SPDX-License-Identifier: MIT # # The hjem integration: it owns the `hjem` class, hjem's class for the -# files and packages of a user. An hjem configuration is the home of a +# files and packages of a user. A hjem configuration is the home of a # user, evaluated at every system in force where it is declared. # # hjem itself evaluates a user only inside its NixOS and nix-darwin @@ -52,7 +52,7 @@ context = "caisson.hjem"; }; - # An hjem source: the hjem flake or source tree, which holds the + # A hjem source: the hjem flake or source tree, which holds the # user module and hjem's library. assertHjemSrc = ecosystemSrc: @@ -66,7 +66,7 @@ then ecosystemSrc else - throw "${context} requires `ecosystemSrc` to be an hjem flake or source tree (holding modules/common/user.nix and lib.nix)."; + throw "${context} requires `ecosystemSrc` to be a hjem flake or source tree (holding modules/common/user.nix and lib.nix)."; # The manifest of an evaluated user, as hjem's OS modules write # it: version 3, with an entry for every enabled file of the @@ -246,7 +246,7 @@ }; }; - # An hjem configuration is published as + # A hjem configuration is published as # `hjemConfigurations.`, with `manifest` and `packages`, # which is what `hjem standalone switch --flake` reads. exportsTo = { @@ -256,7 +256,7 @@ }; }; - # What a reader of a file gets from an hjem configuration that is + # What a reader of a file gets from a hjem configuration that is # a top: `manifest` and `packages`, which `hjem standalone switch # --config` reads, and the manifest of the evaluation under # `caisson.manifest`. hjem's CLI serializes the whole value with @@ -287,12 +287,12 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "hjem"; - # An hjem configuration is evaluated per system: it has an + # A hjem configuration is evaluated per system: it has an # evaluation for every system in force where it is declared. perSystem = true; defaultPkgs = args.defaultPkgs or null; inherit exportsTo; - # An hjem configuration is activated for its user. Its + # A hjem configuration is activated for its user. Its # identity is its manifest file; its marker is a file in the # directory of the user, which the hjem module # `activation-marker` adds to the files of the configuration. @@ -308,7 +308,7 @@ }; # The directory, relative to the directory of the user, that holds - # the marker of an hjem configuration. + # the marker of a hjem configuration. markerDirectory = ".local/state/caisson/markers"; integration = final.caisson.integrations.mkIntegration { diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index cca660fa..05213ac8 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -772,7 +772,7 @@ # Where the profile is depends on the configuration above. Inside # a machine it is a profile of the system, # `/nix/var/nix/profiles/caisson//`, which the machine - # activates as root. Inside a home or an hjem configuration, or as + # activates as root. Inside a home or a hjem configuration, or as # a top, it is a profile of the user, # `$HOME/.local/state/caisson/profiles//`. The path # names `$HOME` and not `$XDG_STATE_HOME`: the units of a machine diff --git a/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix index d72dbe00..b8aafc5b 100644 --- a/lib-overlays/nvf/default.nix +++ b/lib-overlays/nvf/default.nix @@ -21,7 +21,7 @@ # home keeps its generations (`lib.caisson.integrations.profileOf`): # its output `activationPackage` holds `activate`, which points the # profile at the Neovim, and `profile` says where the profile is. A -# machine, a home or an hjem configuration activates the nvf +# machine, a home or a hjem configuration activates the nvf # configurations declared inside it each time it is activated itself, # with the module `profile-activation` of its class. { diff --git a/modules/darwin/default/caisson/forChildren.nix b/modules/darwin/default/caisson/forChildren.nix index f70efda5..50601e4d 100644 --- a/modules/darwin/default/caisson/forChildren.nix +++ b/modules/darwin/default/caisson/forChildren.nix @@ -7,7 +7,7 @@ # it through the options under `caisson.forChildren`. This file adds # the home-manager module `darwin-parent` to the modules that a home # declared inside this darwin configuration imports by default, and -# the hjem module `darwin-parent` to those of an hjem configuration +# the hjem module `darwin-parent` to those of a hjem configuration # declared inside it. Each sets the options that come from the machine: # the home directory from the account of the user, and for a home the # user ID and the Nix of the machine. diff --git a/modules/darwin/hjem-activation/default.nix b/modules/darwin/hjem-activation/default.nix index 5dacbc5b..40dd5b60 100644 --- a/modules/darwin/hjem-activation/default.nix +++ b/modules/darwin/hjem-activation/default.nix @@ -3,7 +3,7 @@ # A darwin module with which a macOS machine activates the hjem # configurations declared inside it. # -# Activating an hjem configuration means applying its manifest with +# Activating a hjem configuration means applying its manifest with # hjem's CLI. hjem has a nix-darwin module that does this for the users # it embeds, with a launchd agent, `org.hjem.activate`, that applies the # manifest of the user it runs for and keeps the state in diff --git a/modules/darwin/profile-activation/default.nix b/modules/darwin/profile-activation/default.nix index 6fc2231e..1c22ce08 100644 --- a/modules/darwin/profile-activation/default.nix +++ b/modules/darwin/profile-activation/default.nix @@ -6,7 +6,7 @@ # # A step of the machine's `postActivation` script runs `activate` of # each profile of the system as root, and of each profile declared -# inside an hjem configuration inside the machine as its user, through +# inside a hjem configuration inside the machine as its user, through # `launchctl asuser` and `sudo`, after the hjem agents are restarted. A # profile inside a home is activated by the home. The machine installs # the programs that choose among the profiles of the system in diff --git a/modules/hjem/activation-marker/default.nix b/modules/hjem/activation-marker/default.nix index 81a6d445..e263cfaf 100644 --- a/modules/hjem/activation-marker/default.nix +++ b/modules/hjem/activation-marker/default.nix @@ -1,8 +1,8 @@ # SPDX-License-Identifier: MIT # -# An hjem module with which an hjem configuration records what it +# A hjem module with which a hjem configuration records what it # applied, so that the configurations declared inside it can detect -# drift. It is the counterpart, for an hjem configuration, of the +# drift. It is the counterpart, for a hjem configuration, of the # machine module `activation-marker`: see there for why. # # The module adds a file to the files of the configuration, diff --git a/modules/hjem/darwin-parent/default.nix b/modules/hjem/darwin-parent/default.nix index f4306004..caad88fc 100644 --- a/modules/hjem/darwin-parent/default.nix +++ b/modules/hjem/darwin-parent/default.nix @@ -1,6 +1,6 @@ # SPDX-License-Identifier: MIT # -# An hjem module for an hjem configuration declared inside a darwin +# A hjem module for a hjem configuration declared inside a darwin # configuration. It sets the home directory of the user from the # account the machine declares, as hjem's nix-darwin module does for # the users it embeds, and refuses a user the machine has no account @@ -9,12 +9,12 @@ # caisson does not use hjem's nix-darwin module. A darwin configuration # adds this module to the modules that the hjem configurations declared # inside it import by default; caisson's `default` darwin module does -# that, through `caisson.forChildren`. An hjem configuration that passes +# that, through `caisson.forChildren`. A hjem configuration that passes # `moduleImports` selects its modules itself, and gets this module only # if it lists it. # # The module reads the machine through the manifest of the hjem -# configuration, so it can only be imported by an hjem configuration +# configuration, so it can only be imported by a hjem configuration # that has a darwin configuration above it. { ... }: # `lib` is the library of the hjem configuration, and `config` is its diff --git a/modules/hjem/default/default.nix b/modules/hjem/default/default.nix index 9283efa8..bd923e99 100644 --- a/modules/hjem/default/default.nix +++ b/modules/hjem/default/default.nix @@ -1,6 +1,6 @@ # SPDX-License-Identifier: MIT # -# The `default` hjem module caisson registers. An hjem configuration +# The `default` hjem module caisson registers. A hjem configuration # imports every registered module named `default` unless it passes # `moduleImports`, so this is what it gets from caisson when it does not # select its modules itself: diff --git a/modules/hjem/nixos-parent/default.nix b/modules/hjem/nixos-parent/default.nix index 15184bf4..3fcd734e 100644 --- a/modules/hjem/nixos-parent/default.nix +++ b/modules/hjem/nixos-parent/default.nix @@ -1,6 +1,6 @@ # SPDX-License-Identifier: MIT # -# An hjem module for an hjem configuration declared inside a NixOS or +# A hjem module for a hjem configuration declared inside a NixOS or # system-manager configuration. It sets the home directory of the user # from the account the machine declares, as hjem's NixOS module does for # the users it embeds, and refuses a user the machine has no account @@ -11,12 +11,12 @@ # configurations declared inside it import by default; caisson's # `default` module of either class does that, through # `caisson.forChildren`. The module is registered as `nixos-parent` and, -# for the same file, as `system-manager-parent`. An hjem configuration +# for the same file, as `system-manager-parent`. A hjem configuration # that passes `moduleImports` selects its modules itself, and gets this # module only if it lists it. # # The module reads the machine through the manifest of the hjem -# configuration, so it can only be imported by an hjem configuration +# configuration, so it can only be imported by a hjem configuration # that has a NixOS or system-manager configuration above it. { ... }: # `lib` is the library of the hjem configuration, and `config` is its diff --git a/modules/hjem/profile-activation/default.nix b/modules/hjem/profile-activation/default.nix index 9dfa2753..b8d71837 100644 --- a/modules/hjem/profile-activation/default.nix +++ b/modules/hjem/profile-activation/default.nix @@ -1,6 +1,6 @@ # SPDX-License-Identifier: MIT # -# An hjem module for the profiles declared inside an hjem configuration: +# A hjem module for the profiles declared inside a hjem configuration: # the configurations that are programs a person runs, such as an nvf or # nixvim Neovim, each kept in a profile of the user, # `$HOME/.local/state/caisson/profiles//` diff --git a/modules/nixos/default/caisson/forChildren.nix b/modules/nixos/default/caisson/forChildren.nix index c24d4c14..8d745d1a 100644 --- a/modules/nixos/default/caisson/forChildren.nix +++ b/modules/nixos/default/caisson/forChildren.nix @@ -6,7 +6,7 @@ # it through the options under `caisson.forChildren`. This file adds # the home-manager module `nixos-parent` to the modules that a home # declared inside this NixOS configuration imports by default, and the -# hjem module `nixos-parent` to those of an hjem configuration declared +# hjem module `nixos-parent` to those of a hjem configuration declared # inside it. # # `nixos-parent` is the module caisson registers as @@ -31,7 +31,7 @@ closure-lib.caisson.modules.homeManager.nixos-parent ]; - # The hjem module `nixos-parent` sets the home directory of an hjem + # The hjem module `nixos-parent` sets the home directory of a hjem # configuration declared inside this machine from the account of its # user, and refuses a user the machine has no account for. caisson.forChildren.extraDefaultModuleImports.hjem = _lib: [ diff --git a/modules/nixos/hjem-activation/default.nix b/modules/nixos/hjem-activation/default.nix index 70fc6261..20e11bde 100644 --- a/modules/nixos/hjem-activation/default.nix +++ b/modules/nixos/hjem-activation/default.nix @@ -3,12 +3,12 @@ # A NixOS module with which a machine activates the hjem configurations # declared inside it. # -# Activating an hjem configuration means applying its manifest with +# Activating a hjem configuration means applying its manifest with # hjem's CLI: `hjem internal activate` links the user's files and # replaces those of the previous generation, whose manifest is kept in # `/var/lib/hjem/manifest-.json`. hjem has a NixOS module that # does this for the users it embeds. caisson does not use that NixOS -# module. An hjem configuration declared inside a NixOS configuration +# module. A hjem configuration declared inside a NixOS configuration # is a caisson configuration, and this module writes the units hjem's # NixOS module writes, under the same names. Each template unit has an # instance for each user: diff --git a/modules/nixos/profile-activation/default.nix b/modules/nixos/profile-activation/default.nix index efe81529..414e7f17 100644 --- a/modules/nixos/profile-activation/default.nix +++ b/modules/nixos/profile-activation/default.nix @@ -13,7 +13,7 @@ # changes it, and the profile is put back to what the machine declares # each time. # -# A profile declared inside an hjem configuration inside the machine is +# A profile declared inside a hjem configuration inside the machine is # a profile of the hjem user. hjem's activation has no step a # configuration can add, so the machine activates it, with a unit # `caisson-profile---` that runs as the user after the @@ -24,7 +24,7 @@ # programs with which a shell chooses among the profiles of the system # (`lib.caisson.integrations.profileShimsOf`): `nvim` runs the Neovim of # the profile `$CAISSON_NVIM_PROFILE` names, and of the default profile -# of the machine when it is unset. A home or an hjem configuration sets +# of the machine when it is unset. A home or a hjem configuration sets # the variable to its own profile. # # The module is registered for the `nixos` and `systemManager` classes, diff --git a/modules/systemManager/default/caisson/forChildren.nix b/modules/systemManager/default/caisson/forChildren.nix index 3366dcdf..283ec3ca 100644 --- a/modules/systemManager/default/caisson/forChildren.nix +++ b/modules/systemManager/default/caisson/forChildren.nix @@ -7,7 +7,7 @@ # it through the options under `caisson.forChildren`. This file adds the # home-manager module `system-manager-parent` to the modules that a home # declared inside this system-manager configuration imports by default, -# and the hjem module `system-manager-parent` to those of an hjem +# and the hjem module `system-manager-parent` to those of a hjem # configuration declared inside it. Each sets the options that come from # the machine: the home directory from the account of the user that the # machine declares in `users.users`, and for a home the user ID and the diff --git a/tests/darwin-system/default.nix b/tests/darwin-system/default.nix index 56372d7a..616a44b8 100644 --- a/tests/darwin-system/default.nix +++ b/tests/darwin-system/default.nix @@ -9,7 +9,7 @@ # there is nixpkgs at aarch64-darwin, and with nixpkgs, nix-darwin, # home-manager and hjem declared as its ecosystems. A structural top declares the machine # `probe` from `machine`, and the machine `family`, which is `machine` -# with a home and an hjem configuration for the user `probe` declared +# with a home and a hjem configuration for the user `probe` declared # inside it. Evaluating them needs no Mac: the package set # and the derivations are evaluated for aarch64-darwin wherever the # evaluation runs. Building the system needs a darwin builder. @@ -69,7 +69,7 @@ let }; }; - # A home and an hjem configuration for the user `probe`. + # A home and a hjem configuration for the user `probe`. home = { home.stateVersion = "25.05"; home.file.".probe".text = "home"; diff --git a/tests/hjem-system/default.nix b/tests/hjem-system/default.nix index 925c7eba..7eabd6cb 100644 --- a/tests/hjem-system/default.nix +++ b/tests/hjem-system/default.nix @@ -7,11 +7,11 @@ # # The composition is at x86_64-linux, with a package config whose set # there is nixpkgs, and with nixpkgs and hjem declared as its -# ecosystems. `user` is an hjem module with a file and a package. -# `standalone` is an hjem configuration that is a top, for the user +# ecosystems. `user` is a hjem module with a file and a package. +# `standalone` is a hjem configuration that is a top, for the user # `probe` with the home directory it sets itself. `top` is a structural # top holding a NixOS machine `probe-machine`, which declares the -# account of `probe` and an hjem configuration `probe` from +# account of `probe` and a hjem configuration `probe` from # `machineUser`, which is `user` with a systemd user unit. { inputs }: let diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 240794e9..34ea8d41 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -1448,7 +1448,7 @@ let && probe.configuration.config.nixpkgs.hostPlatform.system == "aarch64-darwin" && probe.configuration.pkgs.hello.drvPath == probe.pkgs.hello.drvPath; - # A home and an hjem configuration inside a darwin machine take the + # A home and a hjem configuration inside a darwin machine take the # home directory and the user ID from the account the machine # declares, and the machine activates both. Its `postActivation` # step runs the activation of the home as the user, as @@ -1494,7 +1494,7 @@ let && builtins.attrNames probe.top.homeConfigurations == [ "probe@family" ] && builtins.attrNames probe.top.hjemConfigurations == [ "probe" ]; - # An hjem configuration that is a top gives what hjem's standalone + # A hjem configuration that is a top gives what hjem's standalone # CLI reads from a file: the version 3 manifest of the user's files, # with targets in the user's home directory, and the packages. The # manifest of the evaluation sits under `caisson.manifest`, behind an @@ -1516,7 +1516,7 @@ let && builtins.toJSON top.caisson == builtins.toJSON "caisson manifest: read caisson.manifest" && top.caisson.manifest.type == "hjem"; - # An hjem configuration inside a NixOS machine takes the home + # A hjem configuration inside a NixOS machine takes the home # directory of the account the machine declares, and its manifest is # the one hjem's own NixOS module makes from the same user module, # the systemd user unit of the user included. The machine activates From 2479e394923e4994c9314d0d97b4e603e01defdf Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 17 Oct 2026 22:57:43 -0700 Subject: [PATCH 13/14] maint: say a configuration is evaluated per system in every integration The comments of the integrations said a configuration is evaluated at every system in force, and one in the integrations library said a configuration is evaluated at a system. They say it is evaluated per system, once for each system in force. Co-Authored-By: Claude Opus 5.5 --- lib-overlays/darwin/default.nix | 4 ++-- lib-overlays/hjem/default.nix | 2 +- lib-overlays/home-manager/default.nix | 4 ++-- lib-overlays/integrations/default.nix | 5 +++-- lib-overlays/kubenix/default.nix | 2 +- lib-overlays/nixidy/default.nix | 2 +- lib-overlays/nixos/default.nix | 2 +- lib-overlays/nixvim/default.nix | 4 ++-- lib-overlays/nvf/default.nix | 2 +- lib-overlays/terranix/default.nix | 2 +- lib-overlays/treefmt/default.nix | 2 +- 11 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib-overlays/darwin/default.nix b/lib-overlays/darwin/default.nix index dd633f6a..173985be 100644 --- a/lib-overlays/darwin/default.nix +++ b/lib-overlays/darwin/default.nix @@ -2,8 +2,8 @@ # # The darwin integration: it owns the `darwin` class and evaluates it # with nix-darwin's `eval-config.nix`, over the composed library. A -# darwin configuration is a macOS machine, evaluated at every system -# in force where it is declared, as a NixOS configuration is, and +# darwin configuration is a macOS machine, evaluated per system, +# once for each system in force where it is declared, as a NixOS configuration is, and # published as `darwinConfigurations.`, the value `darwin-rebuild` # reads: the evaluated configuration with its `system` beside it. # diff --git a/lib-overlays/hjem/default.nix b/lib-overlays/hjem/default.nix index 99bc5da3..d094644f 100644 --- a/lib-overlays/hjem/default.nix +++ b/lib-overlays/hjem/default.nix @@ -2,7 +2,7 @@ # # The hjem integration: it owns the `hjem` class, hjem's class for the # files and packages of a user. A hjem configuration is the home of a -# user, evaluated at every system in force where it is declared. +# user, evaluated per system, once for each system in force where it is declared. # # hjem itself evaluates a user only inside its NixOS and nix-darwin # modules, as a submodule of `hjem.users`; its standalone CLI evaluates diff --git a/lib-overlays/home-manager/default.nix b/lib-overlays/home-manager/default.nix index 0753ab5f..5d1dfffb 100644 --- a/lib-overlays/home-manager/default.nix +++ b/lib-overlays/home-manager/default.nix @@ -4,7 +4,7 @@ # contributes the declared home-manager's library to the composition # as `lib.hm`, and evaluates the class over the composed library with # home-manager's module list. A home is a configuration: it is -# evaluated at every system in force where it is declared, on the +# evaluated per system, once for each system in force where it is declared, on the # package set in force there, and a top publishes it under # `homeConfigurations`. # @@ -544,7 +544,7 @@ # `caisson.home-manager.configurations.` finalizes it, # and `mkTopConfiguration` finalizes it at a top. # `lib.caisson.home-manager-minimal` takes the same arguments. - # The home is evaluated at every system in force where it is + # The home is evaluated per system, once for each system in force where it is # declared, and its package sets come from the composition, # through the manifest; `defaultPkgs` selects the set it runs # on. diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index 05213ac8..765783bc 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -991,8 +991,9 @@ # declared beneath it, nested ones included: for each, its # manifest, the output attribute set it is published under, the # value published, and its path from here, a list of - # `{ type, name }` segments. A configuration evaluated at a - # system has that system above its name on the path. `selected` + # `{ type, name }` segments. An evaluation of a configuration + # evaluated per system has its system above its name on the + # path. `selected` # is what each integration's `exported` selected, by integration # and then name, each value a manifest or the evaluations of a # configuration by system. A configuration whose manifest diff --git a/lib-overlays/kubenix/default.nix b/lib-overlays/kubenix/default.nix index 238432ea..16f9cc0d 100644 --- a/lib-overlays/kubenix/default.nix +++ b/lib-overlays/kubenix/default.nix @@ -4,7 +4,7 @@ # kubenix's modules. A kubenix configuration is a set of Kubernetes # resources written as kubenix modules, which import the kubenix modules # they use from the `kubenix` argument (`kubenix.modules.k8s`). It is -# evaluated at every system in force where it is declared, on the +# evaluated per system, once for each system in force where it is declared, on the # package set selected for it. # # kubenix evaluates its modules with its `evalModules`: `evalModules` on diff --git a/lib-overlays/nixidy/default.nix b/lib-overlays/nixidy/default.nix index 82e9692d..2d837bfe 100644 --- a/lib-overlays/nixidy/default.nix +++ b/lib-overlays/nixidy/default.nix @@ -3,7 +3,7 @@ # The nixidy integration: it owns the `nixidy` class, the class of # nixidy's modules. A nixidy configuration is an environment of Argo CD # applications, written as nixidy modules (`applications.*`, -# `nixidy.target`). It is evaluated at every system in force where it +# `nixidy.target`). It is evaluated per system, once for each system in force where it # is declared, on the package set selected for it. # # caisson evaluates it with nixidy's own `mkEnv`, the function the diff --git a/lib-overlays/nixos/default.nix b/lib-overlays/nixos/default.nix index 765aab0c..591fc813 100644 --- a/lib-overlays/nixos/default.nix +++ b/lib-overlays/nixos/default.nix @@ -139,7 +139,7 @@ # `mkTopConfiguration` finalizes it at a top. The colmena node # constructors take the same arguments, and # `lib.caisson.nixos-minimal` these plus `prefix`. The - # configuration is evaluated at every system in force where it + # configuration is evaluated per system, once for each system in force where it # is declared, and its package sets come from the composition, # through the manifest; `defaultPkgs` selects the set it runs # on. diff --git a/lib-overlays/nixvim/default.nix b/lib-overlays/nixvim/default.nix index 46bec048..5a737b82 100644 --- a/lib-overlays/nixvim/default.nix +++ b/lib-overlays/nixvim/default.nix @@ -2,8 +2,8 @@ # # The nixvim integration: it owns the `nixvim` class, the class of # nixvim's modules. A nixvim configuration is a Neovim, written as -# nixvim modules (`plugins.*`, `opts`, `keymaps`), evaluated at every -# system in force where it is declared, on the package set selected for +# nixvim modules (`plugins.*`, `opts`, `keymaps`), evaluated per +# system, once for each system in force where it is declared, on the package set selected for # it. # # nixvim evaluates its modules with `evalNixvim`: `evalModules` over diff --git a/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix index b8aafc5b..6b13f35c 100644 --- a/lib-overlays/nvf/default.nix +++ b/lib-overlays/nvf/default.nix @@ -2,7 +2,7 @@ # # The nvf integration: it owns the `nvf` class, the class of nvf's # modules. An nvf configuration is a Neovim, written as nvf modules -# (`vim.*`), evaluated at every system in force where it is declared, +# (`vim.*`), evaluated per system, once for each system in force where it is declared, # on the package set selected for it. # # nvf evaluates its modules with `neovimConfiguration`, on nixpkgs' diff --git a/lib-overlays/terranix/default.nix b/lib-overlays/terranix/default.nix index 7939c1fe..30217ea6 100644 --- a/lib-overlays/terranix/default.nix +++ b/lib-overlays/terranix/default.nix @@ -3,7 +3,7 @@ # The terranix integration: it owns the `terranix` class and # evaluates it with `terranixConfiguration` from the terranix flake. A # terranix configuration is a Terraform configuration written as -# modules, evaluated at every system in force where it is declared, +# modules, evaluated per system, once for each system in force where it is declared, # on the package set selected for it. # # Its output is `json`, the `config.tf.json` file Terraform reads, diff --git a/lib-overlays/treefmt/default.nix b/lib-overlays/treefmt/default.nix index 46e300b3..1c72d5c7 100644 --- a/lib-overlays/treefmt/default.nix +++ b/lib-overlays/treefmt/default.nix @@ -3,7 +3,7 @@ # 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..enable` and `settings`, as treefmt-nix writes -# them. It is evaluated at every system in force where it is declared, +# them. It is evaluated per system, once for each 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 From 087f9bd2a16a7cc9629371502648b88f6f028377 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sun, 18 Oct 2026 00:23:32 -0700 Subject: [PATCH 14/14] feat(treefmt): publish the check of the default configuration, and only that configuration A tree with a treefmt configuration named `default` now also publishes `checks..treefmt`, the configuration's `check` over the root of the tree (`root` on `mkLib`), so `nix flake check` fails when the tree is not formatted. A tree without a root, such as a library composed inside a test, publishes no check. Every treefmt configuration used to be an entry for `formatter`, which holds a single value, so a second configuration, under any other name, was refused when the top published. Now only the configuration named `default` is published; one under another name is evaluated, passed up, and found through the manifest. `exportsTo` may be a list of exports, for an integration published in more than one output, and an export may carry `only`, a function of the manifest saying whether the configuration is published there. `exportsOf` replaces `exportsToOf` in the integrations library. The pinned-world test declares a second configuration, `docs`, which the previous integration refused, and checks the check is published over the root and absent without one. Co-Authored-By: Claude Opus 5.5 --- lib-overlays/integrations/default.nix | 41 +++++++++++++----- lib-overlays/treefmt/default.nix | 39 +++++++++++++---- tests/pinned-world/default.nix | 62 ++++++++++++++++++++++----- 3 files changed, 112 insertions(+), 30 deletions(-) diff --git a/lib-overlays/integrations/default.nix b/lib-overlays/integrations/default.nix index 765783bc..9916fa88 100644 --- a/lib-overlays/integrations/default.nix +++ b/lib-overlays/integrations/default.nix @@ -256,10 +256,15 @@ # 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..`). 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. + # system of each evaluation first (`packages..`). 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, and found through the manifest. + # `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. # # `activation` is given by an integration whose configurations # are activated on a machine or for a user: NixOS, darwin and @@ -928,10 +933,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: @@ -943,9 +963,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 @@ -1036,7 +1055,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 [ ] ) diff --git a/lib-overlays/treefmt/default.nix b/lib-overlays/treefmt/default.nix index 1c72d5c7..af785eda 100644 --- a/lib-overlays/treefmt/default.nix +++ b/lib-overlays/treefmt/default.nix @@ -20,7 +20,10 @@ # 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.`, its wrapper, which `nix fmt` -# reads. +# reads, and as `checks..treefmt`, its check over the root of +# the tree (`mkLib`'s `root`), which `nix flake check` builds. A +# configuration under another name is published nowhere, and is found +# through the manifest. { mkLibOverlay, ... @@ -138,14 +141,32 @@ }; }; - # The configuration named `default` is published as - # `formatter.`, which `nix fmt` reads. - exportsTo = { - attrset = "formatter"; - single = true; - bySystem = true; - value = manifest: manifest.outputs.wrapper; - }; + # Only the configuration named `default` is published: as + # `formatter.`, its wrapper, which `nix fmt` reads, and, + # in a tree that knows its root, as `checks..treefmt`, + # its check over that tree, which fails `nix flake check` when + # the tree is not formatted. A configuration under another name + # is evaluated and passed up, and found through the manifest. + 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: diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index 34ea8d41..3755c06a 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -1661,32 +1661,74 @@ let # published as `formatter.`, and its wrapper is the # derivation treefmt-nix's own `mkWrapper` builds from the same # module on the same package set. Its outputs carry the - # configuration file and the check over a source tree. + # configuration file and the check over a source tree. In a tree + # that knows its root, its check over the root is published as + # `checks..treefmt`; a tree without a root publishes no + # check. A configuration under another name is published nowhere, + # and is found through the manifest. treefmtIsTheFormatterTreefmtNixBuilds = let module = { projectRootFile = "flake.nix"; programs.nixfmt.enable = true; }; + declares = + { lib, ... }: + { + caisson.treefmt.configurations.default = lib.caisson.treefmt.mkConfiguration { + ecosystemSrc = inputs.treefmt-nix; + configModule = module; + }; + caisson.treefmt.configurations.docs = lib.caisson.treefmt.mkConfiguration { + ecosystemSrc = inputs.treefmt-nix; + configModule = { + projectRootFile = "book.toml"; + programs.nixfmt.enable = true; + }; + }; + }; top = hiveLib.caisson.structural.mkTopConfiguration { moduleImports = _modules: [ ]; - configModule = - { lib, ... }: - { - caisson.treefmt.configurations.default = lib.caisson.treefmt.mkConfiguration { - ecosystemSrc = inputs.treefmt-nix; - configModule = module; - }; + configModule = declares; + }; + root = { + outPath = ./.; + }; + rootedLib = core.mkLib ( + declaresPkgs + // { + sources = { }; + inherit root; + projects.caisson = inputs.caisson; + defaultEcosystemSrc = { + inherit (inputs) nixpkgs; }; + } + ); + rooted = rootedLib.caisson.structural.mkTopConfiguration { + moduleImports = _modules: [ ]; + configModule = declares; }; - evaluation = top.caisson.manifest.children.system.x86_64-linux.children.treefmt.default; + children = top.caisson.manifest.children.system.x86_64-linux.children.treefmt; + evaluation = children.default; + rootedEvaluation = rooted.caisson.manifest.children.system.x86_64-linux.children.treefmt.default; upstream = inputs.treefmt-nix.lib.mkWrapper pkgs module; in builtins.attrNames top.formatter == [ "x86_64-linux" ] && top.formatter.x86_64-linux.drvPath == upstream.drvPath && evaluation.outputs.programs ? nixfmt && builtins.isString evaluation.outputs.configFile.drvPath - && builtins.isString (evaluation.outputs.check ./.).drvPath; + && builtins.isString (evaluation.outputs.check ./.).drvPath + # The tree without a root publishes no check; the tree with one + # publishes the check of `default` over it, and nothing else. + && !(top ? checks) + && builtins.attrNames rooted.checks == [ "x86_64-linux" ] + && builtins.attrNames rooted.checks.x86_64-linux == [ "treefmt" ] + && rooted.checks.x86_64-linux.treefmt.drvPath == (rootedEvaluation.outputs.check root).drvPath + && rooted.formatter.x86_64-linux.drvPath == upstream.drvPath + # The configuration `docs` is evaluated, and only the manifest + # holds it. + && builtins.isString children.docs.outputs.wrapper.drvPath; # An nvf configuration is published as `packages..`, # and its Neovim is the one nvf's own `neovimConfiguration` builds