diff --git a/README.md b/README.md index df9a7610..03db58d3 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 | @@ -133,7 +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 | -| `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads | +| `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 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` 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/configs/structural/impl/default.nix b/configs/structural/impl/default.nix index 7de75c95..a3b0487c 100644 --- a/configs/structural/impl/default.nix +++ b/configs/structural/impl/default.nix @@ -35,6 +35,11 @@ terranix system-manager treefmt + nvf + nixvim + topology + kubenix + nixidy ; }; diff --git a/docs/development/repository-structure.md b/docs/development/repository-structure.md index 1dbe5874..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`), 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/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/darwin/default.nix b/lib-overlays/darwin/default.nix index 7723be7b..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. # @@ -159,11 +159,15 @@ 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; 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 5adc3221..d094644f 100644 --- a/lib-overlays/hjem/default.nix +++ b/lib-overlays/hjem/default.nix @@ -1,8 +1,8 @@ # 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 -# user, evaluated at every system in force where it is declared. +# files and packages of a user. A hjem configuration is the home of a +# 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 @@ -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 @@ -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 @@ -232,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 = { @@ -242,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 @@ -273,14 +287,30 @@ args: final.caisson.integrations.mkModuleConfiguration { type = "hjem"; - # An hjem configuration is evaluated at a 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; + # 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. + # 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 a 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 ee5f8191..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`. # @@ -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 @@ -509,11 +511,22 @@ 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; inherit exportsTo; + # A home is activated for its user. Its identity is its + # 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: "$HOME/.local/state/caisson/markers/${selection.markerFileName manifest}"; + }; evaluate = evaluation variant args; }; @@ -531,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 90e98dc4..9916fa88 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 @@ -256,23 +256,46 @@ # 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 + # 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 @@ -462,7 +485,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 @@ -592,12 +615,343 @@ 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"; + + # 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 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 + 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 = digestOf identity; + text = builtins.toJSON { identity = digestOf 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 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 + "" + 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 " digest of the version applied (configurations declared inside it left out):" >&2 + echo " ''${_caissonApplied:-unknown}" >&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 + fi + 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 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 + # 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 - # 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: @@ -609,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 @@ -657,8 +1010,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 @@ -701,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 [ ] ) @@ -803,7 +1157,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 @@ -875,6 +1229,17 @@ integrationsIn integrationsDeclaringIn nearestOf + activatingAncestorOf + machineActivation + markerFileName + markerOf + checksMarkerOf + driftCheckOf + safeName + profileOf + profileShimsOf + profilesActivatedBy + profileChoicesOf mkEvaluation publish topValue diff --git a/lib-overlays/kubenix/default.nix b/lib-overlays/kubenix/default.nix new file mode 100644 index 00000000..16f9cc0d --- /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 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 +# 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 per 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..2d837bfe --- /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 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 +# 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 per 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/nixos/default.nix b/lib-overlays/nixos/default.nix index 52c8521a..591fc813 100644 --- a/lib-overlays/nixos/default.nix +++ b/lib-overlays/nixos/default.nix @@ -109,11 +109,15 @@ 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; 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; }; @@ -135,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 new file mode 100644 index 00000000..5a737b82 --- /dev/null +++ b/lib-overlays/nixvim/default.nix @@ -0,0 +1,253 @@ +# 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 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 +# `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. +# +# 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, + ... +}: +{ + + 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 { }) + ); + 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 = { + inherit package; + inherit (kept) activationPackage profile; + }; + }; + + # 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 per system: it has an + # evaluation for every system in force where it is declared. + 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; + }; + + 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/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix new file mode 100644 index 00000000..6b13f35c --- /dev/null +++ b/lib-overlays/nvf/default.nix @@ -0,0 +1,263 @@ +# 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 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' +# 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..`. +# +# 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 a hjem configuration activates the nvf +# configurations declared inside it each time it is activated itself, +# with the module `profile-activation` of its class. +{ + 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 { }) + ); + 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 = { + inherit neovim; + inherit (kept) activationPackage profile; + }; + }; + + # 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 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 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; + }; + + 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; + }; + in + { + caisson = (prev.caisson or { }) // { + nvf = integration.namespace; + }; + }; + +} diff --git a/lib-overlays/system-manager/default.nix b/lib-overlays/system-manager/default.nix index 9a0fe409..1c4aaa8b 100644 --- a/lib-overlays/system-manager/default.nix +++ b/lib-overlays/system-manager/default.nix @@ -294,12 +294,16 @@ 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; 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/lib-overlays/terranix/default.nix b/lib-overlays/terranix/default.nix index 6c9701aa..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, @@ -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 new file mode 100644 index 00000000..ee522004 --- /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 per 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/lib-overlays/treefmt/default.nix b/lib-overlays/treefmt/default.nix index 2a5db890..af785eda 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 @@ -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,20 +141,38 @@ }; }; - # 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: 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/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/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/default/default.nix b/modules/darwin/default/default.nix index 2982630a..ec3e6fc2 100644 --- a/modules/darwin/default/default.nix +++ b/modules/darwin/default/default.nix @@ -28,6 +28,8 @@ (mkModule ./caisson) closure-lib.caisson.modules.darwin.home-manager-activation closure-lib.caisson.modules.darwin.hjem-activation + closure-lib.caisson.modules.darwin.profile-activation + closure-lib.caisson.modules.darwin.activation-marker ] else [ ]; diff --git a/modules/darwin/hjem-activation/default.nix b/modules/darwin/hjem-activation/default.nix index b9594e8c..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 @@ -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/darwin/profile-activation/default.nix b/modules/darwin/profile-activation/default.nix new file mode 100644 index 00000000..1c22ce08 --- /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 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 +# `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/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/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; } diff --git a/modules/generic/core/default.nix b/modules/generic/core/default.nix index 0d756b9b..cfc89466 100644 --- a/modules/generic/core/default.nix +++ b/modules/generic/core/default.nix @@ -38,11 +38,16 @@ "flake-parts" "hjem" "home-manager" + "kubenix" + "nixidy" "nixos" "nixpkgs" + "nixvim" + "nvf" "structural" "system-manager" "terranix" + "topology" "treefmt" ]) ] diff --git a/modules/hjem/activation-marker/default.nix b/modules/hjem/activation-marker/default.nix new file mode 100644 index 00000000..e263cfaf --- /dev/null +++ b/modules/hjem/activation-marker/default.nix @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: MIT +# +# 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 a 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/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 new file mode 100644 index 00000000..bd923e99 --- /dev/null +++ b/modules/hjem/default/default.nix @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: MIT +# +# 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: +# +# - `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.profile-activation + closure-lib.caisson.modules.hjem.activation-marker + ]; +} diff --git a/modules/hjem/nixos-parent/default.nix b/modules/hjem/nixos-parent/default.nix index 18235e8a..3fcd734e 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. +# 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 +# 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`. 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 -# that has a NixOS configuration above it. +# 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 # 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/profile-activation/default.nix b/modules/hjem/profile-activation/default.nix new file mode 100644 index 00000000..b8d71837 --- /dev/null +++ b/modules/hjem/profile-activation/default.nix @@ -0,0 +1,73 @@ +# SPDX-License-Identifier: MIT +# +# 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//` +# (`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/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/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..80df72ee --- /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 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 +# 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 new file mode 100644 index 00000000..35cd0c99 --- /dev/null +++ b/modules/homeManager/default/default.nix @@ -0,0 +1,25 @@ +# 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: +# +# - `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; +# - `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.profile-activation + closure-lib.caisson.modules.homeManager.activation-drift + closure-lib.caisson.modules.homeManager.activation-marker + ]; +} 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/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/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/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/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/nixos/activation-marker/default.nix b/modules/nixos/activation-marker/default.nix new file mode 100644 index 00000000..133962a8 --- /dev/null +++ b/modules/nixos/activation-marker/default.nix @@ -0,0 +1,80 @@ +# 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 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 +# 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..8d745d1a 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 a 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,10 +29,9 @@ { 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 + # 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/default/default.nix b/modules/nixos/default/default.nix index ed0fa4c7..b1e9167a 100644 --- a/modules/nixos/default/default.nix +++ b/modules/nixos/default/default.nix @@ -15,19 +15,23 @@ # 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 `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 +# 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 @@ -42,7 +46,8 @@ (mkModule ./caisson) closure-lib.caisson.modules.nixos.home-manager-activation closure-lib.caisson.modules.nixos.hjem-activation - closure-lib.caisson.modules.nixos.home-manager-source-marker + closure-lib.caisson.modules.nixos.profile-activation + closure-lib.caisson.modules.nixos.activation-marker ] else [ ]; diff --git a/modules/nixos/hjem-activation/default.nix b/modules/nixos/hjem-activation/default.nix index 0eaea2f1..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: @@ -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/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/nixos/profile-activation/default.nix b/modules/nixos/profile-activation/default.nix new file mode 100644 index 00000000..414e7f17 --- /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 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 +# 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 a 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/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/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/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/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/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/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..283ec3ca --- /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 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 +# 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..d2def52e --- /dev/null +++ b/modules/systemManager/default/default.nix @@ -0,0 +1,45 @@ +# 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 `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 +# 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.profile-activation + closure-lib.caisson.modules.systemManager.activation-marker + ] + 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/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/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 3f3ffbfe..cf81e8ae 100755 --- a/tests/argument-errors/check.sh +++ b/tests/argument-errors/check.sh @@ -41,6 +41,11 @@ namespaces=( terranix system-manager treefmt + nvf + nixvim + topology + kubenix + nixidy ) failures=0 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/dependencies/flake.lock b/tests/dependencies/flake.lock index c64eaf79..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": [ @@ -91,6 +112,38 @@ "type": "github" } }, + "flake-compat_3": { + "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_4": { + "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-parts": { "inputs": { "nixpkgs-lib": [ @@ -112,6 +165,63 @@ } }, "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", + "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_5": { "inputs": { "nixpkgs-lib": [ "system-manager", @@ -133,7 +243,7 @@ "type": "github" } }, - "flake-parts_3": { + "flake-parts_6": { "inputs": { "nixpkgs-lib": [ "terranix", @@ -172,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": [ @@ -250,6 +378,62 @@ "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, + "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": [ @@ -312,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, @@ -334,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, @@ -351,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=", @@ -365,6 +637,50 @@ "type": "github" } }, + "nixvim": { + "inputs": { + "flake-parts": "flake-parts_4", + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems_4" + }, + "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_3", + "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": [ @@ -401,13 +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": { @@ -428,7 +749,7 @@ }, "system-manager": { "inputs": { - "flake-compat": "flake-compat_2", + "flake-compat": "flake-compat_4", "nixpkgs": [ "nixpkgs" ], @@ -493,14 +814,61 @@ "type": "github" } }, + "systems_4": { + "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_5": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_6": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, "terranix": { "inputs": { - "flake-parts": "flake-parts_3", - "import-tree": "import-tree", + "flake-parts": "flake-parts_6", + "import-tree": "import-tree_2", "nixpkgs": [ "nixpkgs" ], - "systems": "systems_3" + "systems": "systems_6" }, "locked": { "lastModified": 1784465933, @@ -517,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", @@ -537,7 +926,7 @@ "type": "github" } }, - "treefmt-nix_2": { + "treefmt-nix_3": { "inputs": { "nixpkgs": [ "nixpkgs" @@ -563,13 +952,13 @@ "system-manager", "flake-compat" ], - "flake-parts": "flake-parts_2", + "flake-parts": "flake-parts_5", "nixpkgs": [ "system-manager", "nixpkgs" ], "pre-commit-hooks-nix": "pre-commit-hooks-nix", - "systems": "systems_2" + "systems": "systems_5" }, "locked": { "lastModified": 1770377964, diff --git a/tests/dependencies/flake.nix b/tests/dependencies/flake.nix index b09d16b2..8ee43b2c 100644 --- a/tests/dependencies/flake.nix +++ b/tests/dependencies/flake.nix @@ -21,6 +21,16 @@ 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"; + 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/hjem-system/default.nix b/tests/hjem-system/default.nix index 1194fa41..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 @@ -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/pinned-world/default.nix b/tests/pinned-world/default.nix index ec9d6f77..3755c06a 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -72,6 +72,11 @@ let registered."caisson/terranix" registered."caisson/system-manager" 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. @@ -103,6 +108,7 @@ let "home-manager" "importApply" "integrations" + "kubenix" "libManifest" "libOverlays" "manifestOf" @@ -115,9 +121,12 @@ let "mkPkgOverlay" "mkPkgOverlays" "modules" + "nixidy" "nixos" "nixos-minimal" "nixpkgs" + "nixvim" + "nvf" "pins" "pkgOverlays" "pkgOverlaysFor" @@ -126,6 +135,7 @@ let "structural" "system-manager" "terranix" + "topology" "treefmt" ]; @@ -454,7 +464,7 @@ let builtins.all evaluates [ "default" "home-manager-activation" - "home-manager-source-marker" + "activation-marker" ]; composesTheCaissonLibrary = @@ -483,6 +493,11 @@ let "caisson/terranix" "caisson/system-manager" "caisson/treefmt" + "caisson/nvf" + "caisson/nixvim" + "caisson/topology" + "caisson/kubenix" + "caisson/nixidy" "caisson/structural" ]; @@ -583,6 +598,11 @@ let "terranix" "system-manager" "treefmt" + "nvf" + "nixvim" + "topology" + "kubenix" + "nixidy" ]; minimalNixosSystemEvaluates = @@ -699,36 +719,41 @@ 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; - # 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 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. + # 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; @@ -736,20 +761,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-home-manager/source.json".text - ); - base = record.baseSystemOutPath; + 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: - pkgs.lib.hasInfix base ( - builtins.unsafeDiscardStringContext home.config.home.activation.caissonMachineDrift.data - ); + let + step = home.config.home.activation.caissonDrift.data; + in + pkgs.lib.hasInfix digest step && pkgs.lib.hasInfix "/etc/caisson/marker.json" step; in - builtins.attrNames record == [ "baseSystemOutPath" ] - && pkgs.lib.hasPrefix builtins.storeDir base - && base != builtins.unsafeDiscardStringContext machine.system.build.toplevel.outPath + builtins.attrNames record == [ "identity" ] + && 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 @@ -1423,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 @@ -1469,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 @@ -1491,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 @@ -1636,32 +1661,392 @@ 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 + # 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; + 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 + # 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 + ) + # 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 + # 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 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 system-manager machine activates the home, the hjem configuration + # 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 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 { + 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 = + { 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 { + 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; + 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 + # 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@" + # 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" ]; # A terranix configuration that is a top is its `config.tf.json` # file, built from the package set of the composition with the diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index 4087d6e6..6aa2bd48 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -127,6 +127,11 @@ let "terranix" "system-manager" "treefmt" + "nvf" + "nixvim" + "topology" + "kubenix" + "nixidy" ]; in @@ -4003,21 +4008,22 @@ 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 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. # # 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 @@ -4051,7 +4057,7 @@ in }; }; }; - file = "caisson-home-manager/source.json"; + file = "caisson/marker.json"; recordOf = name: let @@ -4068,13 +4074,13 @@ in }; expected = { laptop = { - baseSystemOutPath = "/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 = { - baseSystemOutPath = "/nix/store/stub-system-with-0-units"; + identity = builtins.hashString "sha256" "/nix/store/stub-system-with-0-units"; }; }; }; @@ -5362,7 +5368,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. @@ -5391,6 +5397,11 @@ in "caisson-facter" "facter" "treefmt" + "nvf" + "nixvim" + "topology" + "kubenix" + "nixidy" ]; setsFromTheComposition = builtins.elem name [ "nixos" @@ -5404,6 +5415,11 @@ in "caisson-facter" "facter" "treefmt" + "nvf" + "nixvim" + "topology" + "kubenix" + "nixidy" ]; in signature ? configModule