diff --git a/README.md b/README.md index df9a7610..535c3076 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ source by. | `caisson-facter` | none | the hardware of a machine, a nixos-facter report checked against the report versions caisson knows and extended with a project's own options, with the NixOS configurations that run on it declared inside it; a machine gets the report through nixpkgs' `hardware.facter` | | `facter` | none | a `facter.json` as nixos-facter writes it, with the NixOS configurations that run on it declared inside it; a machine gets it as `hardware.facter.reportPath` | | `treefmt` | `treefmt-nix` | the formatters of a tree, as treefmt-nix modules; the configuration named `default` is published as the `formatter.` that `nix fmt` runs | +| `nvf` | `nvf` | a Neovim written as nvf modules, published as `nvfConfigurations..`, in the shape `neovimConfiguration` returns | | `system-manager` | `system-manager` | the systems of machines running another Linux distribution, published as the `systemConfigs` that `system-manager switch` reads | | `structural` | none | a configuration that only exports and holds configurations beneath it: the top of a repository whose point is what it exports, such as caisson itself, or a layer at any depth | diff --git a/configs/structural/impl/default.nix b/configs/structural/impl/default.nix index 7de75c95..fcea215a 100644 --- a/configs/structural/impl/default.nix +++ b/configs/structural/impl/default.nix @@ -35,6 +35,7 @@ terranix system-manager treefmt + nvf ; }; diff --git a/docs/development/repository-structure.md b/docs/development/repository-structure.md index 1dbe5874..c16f2213 100644 --- a/docs/development/repository-structure.md +++ b/docs/development/repository-structure.md @@ -28,7 +28,7 @@ The layer users reach for: the integrations (`structural`, `flake-parts`, `nixpkgs`, `nixos`, `nixos-minimal`, `home-manager`, `home-manager-minimal`, `darwin`, `hjem`, `caisson-colmena`, `colmena`, `caisson-disko`, `disko`, `caisson-facter`, `facter`, `terranix`, -`system-manager`, `treefmt`), each +`system-manager`, `treefmt`, `nvf`), each a library overlay contributing its `lib.caisson` namespace, registering its module class where it has a class (`nixos-minimal` and `home-manager-minimal` diff --git a/lib-overlays/nvf/default.nix b/lib-overlays/nvf/default.nix new file mode 100644 index 00000000..b9d4816c --- /dev/null +++ b/lib-overlays/nvf/default.nix @@ -0,0 +1,236 @@ +# SPDX-License-Identifier: MIT +# +# The nvf integration: it owns the `nvf` class, the class of nvf's +# modules. An nvf configuration is a Neovim, written as nvf modules +# (`vim.*`), evaluated at every system in force where it is declared, +# on the package set selected for it. +# +# nvf evaluates its modules with `neovimConfiguration`, on nixpkgs' +# library extended with nvf's own, `lib.nvim`. caisson evaluates the +# same modules, nvf's `modules/modules.nix`, with the special arguments +# `neovimConfiguration` passes (`self` and `inputs`, the nvf flake, and +# `modulesPath`), on the composed library extended with `lib.nvim` +# built from the same nvf source. So the integration needs the nvf +# flake, whose inputs nvf's modules read. +# +# The output is `neovim`, the wrapped Neovim, the `neovim` that +# `neovimConfiguration` returns. A configuration is published as +# `nvfConfigurations..`, in the shape `neovimConfiguration` +# returns: `neovim`, `config` and `options`. +{ + mkLibOverlay, + ... +}: +{ + + imports = [ + # The library of nixpkgs, which this overlay calls through the + # composed library, imported by key so it is composed wherever + # this is. + ((mkLibOverlay ../nixpkgs-lib) // { key = "nixpkgs-lib"; }) + # What an integration is written from, imported by key so it is + # composed wherever this integration is. + ((mkLibOverlay ../integrations) // { key = "integrations"; }) + ]; + + overlay = + final: prev: + let + context = "lib.caisson.nvf.mkConfiguration"; + + resolveEcosystemSrc = final.caisson.integrations.resolveEcosystemSrc { + name = "nvf"; + context = "caisson.nvf"; + }; + + # An nvf source: the nvf flake, which holds its library at `lib`, + # its modules at `modules/modules.nix`, and the inputs its modules + # read. + assertNvfSrc = + ecosystemSrc: + if + builtins.isAttrs ecosystemSrc + && ecosystemSrc ? inputs + && builtins.pathExists "${ecosystemSrc}/modules/modules.nix" + && builtins.pathExists "${ecosystemSrc}/lib/default.nix" + then + ecosystemSrc + else + throw "${context} requires `ecosystemSrc` to be the nvf flake (with its inputs, holding modules/modules.nix and lib/default.nix)."; + + evaluate = + args: + { lib, manifest }: + let + selection = lib.caisson.integrations; + src = assertNvfSrc (resolveEcosystemSrc { + explicit = args.ecosystemSrc or null; + manifest = lib.caisson-core.libManifest or { }; + }); + # The inputs nvf's modules read, with the flake as `self`, as a + # flake's `outputs` receives them. + inputs = src.inputs // { + self = src; + }; + + name = manifest.name or null; + inherit (manifest) system; + what = + if name != null then + "the nvf configuration `${name}` at ${system}" + else + "this nvf configuration at ${system}"; + + pkgSets = selection.pkgSetsAt { inherit context what; } manifest system; + pkgs = selection.pkgSetOf { inherit context what; } manifest pkgSets; + + # The composed library extended with the library of nvf, as + # `lib/stdlib-extended.nix` of nvf extends the library of + # nixpkgs. `extend` extends the library of nixpkgs, so the + # composed library is laid over it again, with the module + # system of the extension, as the home-manager integration lays + # it for `lib.hm`. + nvfLib = lib.extend ( + self: super: + lib + // { + inherit (super) modules types evalModules; + nvim = import "${src}/lib" { + inherit inputs; + self = src; + lib = self; + }; + literalExpression = super.literalExpression or super.literalExample; + } + ); + + registry = lib.caisson.modules.nvf or { }; + frameworkModules = selection.frameworkModules "nvf" registry; + moduleImports = selection.moduleImportsOf "nvf" { inherit lib manifest; } args; + + # The configuration's module: the module passed, else the + # configuration registered under the configuration's name + # (`configs/nvf/`), else none. + registered = lib.caisson.configs.nvf or { }; + configModule = + if (args.configModule or null) != null then + args.configModule + else if name != null then + registered.${name} or null + else + null; + + value = nvfLib.evalModules ( + { + class = "nvf"; + specialArgs = { + inherit inputs pkgSets; + self = src; + modulesPath = toString "${src}/modules"; + lib = nvfLib; + } + // (if (args.specialArgs or null) != null then args.specialArgs else { }); + modules = + import "${src}/modules/modules.nix" { + inherit pkgs; + lib = nvfLib; + } + ++ frameworkModules + ++ moduleImports registry + ++ (if configModule == null then [ ] else [ configModule ]); + } + // (if (args.ecosystemArgs or null) != null then args.ecosystemArgs else { }) + ); + in + { + inherit value; + outputs.neovim = value.config.vim.build.finalPackage; + }; + + # An nvf configuration is published as + # `nvfConfigurations..`, in the shape + # `neovimConfiguration` returns. + exportsTo = { + attrset = "nvfConfigurations"; + bySystem = true; + value = manifest: { + inherit (manifest.outputs) neovim; + inherit (manifest.value) config options; + }; + }; + + configuration = + args: + final.caisson.integrations.mkModuleConfiguration { + type = "nvf"; + # An nvf configuration is evaluated at a system: it has an + # evaluation for every system in force where it is declared. + perSystem = true; + defaultPkgs = args.defaultPkgs or null; + inherit exportsTo; + evaluate = evaluate args; + }; + + integration = final.caisson.integrations.mkIntegration { + name = "nvf"; + class = "nvf"; + # An nvf configuration that is a top, as a tool reads it: the + # evaluation, or the evaluations by system where several are in + # force. + top = final.caisson.integrations.topValue; + # What these return is a configuration, a function of + # `{ name, parent }`: a parent that declares it under + # `caisson.nvf.configurations.` finalizes it, and + # `mkTopConfiguration` finalizes it at a top. + mkConfiguration = + { + # The configuration's module. When absent, the configuration + # registered under the configuration's name + # (`lib.caisson.configs.nvf.`), if any. Further modules + # of the class are selected with `moduleImports`, from the + # registry. + configModule ? null, + # The nvf flake; resolved from the composition's + # declarations when absent. + ecosystemSrc ? null, + # The package set the configuration runs on: a function + # that receives the package sets available where it is + # declared, by package config name, each at the system of + # the evaluation, and returns the set to run on. When + # absent, the selection of the nearest configuration above, + # and the set named `default` where none above selects. + defaultPkgs ? null, + # The selection over the nvf class of the registry. It + # replaces the default of the class, which is every entry + # named `default` followed by what the configurations + # above added. + moduleImports ? null, + # A selection added to that selection, whichever it is. + extraModuleImports ? null, + # Extra module arguments, merged over those the framework + # supplies. + specialArgs ? null, + }@args: + configuration args; + # The same arguments and `ecosystemArgs`, the evaluator's + # arguments merged over the composed call last. + mkConfigurationWithEcosystemArgs = + { + configModule ? null, + ecosystemSrc ? null, + defaultPkgs ? null, + moduleImports ? null, + extraModuleImports ? null, + specialArgs ? null, + ecosystemArgs ? null, + }@args: + configuration args; + }; + in + { + caisson = (prev.caisson or { }) // { + nvf = integration.namespace; + }; + }; + +} diff --git a/modules/generic/core/default.nix b/modules/generic/core/default.nix index 0d756b9b..1c32bb82 100644 --- a/modules/generic/core/default.nix +++ b/modules/generic/core/default.nix @@ -40,6 +40,7 @@ "home-manager" "nixos" "nixpkgs" + "nvf" "structural" "system-manager" "terranix" diff --git a/modules/nvf/core b/modules/nvf/core new file mode 120000 index 00000000..3715354a --- /dev/null +++ b/modules/nvf/core @@ -0,0 +1 @@ +../generic/core \ No newline at end of file diff --git a/tests/argument-errors/check.sh b/tests/argument-errors/check.sh index 3f3ffbfe..c2518f1c 100755 --- a/tests/argument-errors/check.sh +++ b/tests/argument-errors/check.sh @@ -41,6 +41,7 @@ namespaces=( terranix system-manager treefmt + nvf ) failures=0 diff --git a/tests/dependencies/flake.lock b/tests/dependencies/flake.lock index c64eaf79..9b1335aa 100644 --- a/tests/dependencies/flake.lock +++ b/tests/dependencies/flake.lock @@ -76,6 +76,22 @@ } }, "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1777699697, + "narHash": "sha256-Eg9b/rq/ECYwNwEXs5i9wHyhxNI0JrYx2srdI2uZMaQ=", + "ref": "refs/heads/main", + "rev": "382052b74656a369c5408822af3f2501e9b1af81", + "revCount": 94, + "type": "git", + "url": "https://git.lix.systems/lix-project/flake-compat.git" + }, + "original": { + "type": "git", + "url": "https://git.lix.systems/lix-project/flake-compat.git" + } + }, + "flake-compat_3": { "flake": false, "locked": { "lastModified": 1767039857, @@ -250,6 +266,21 @@ "type": "github" } }, + "mnw": { + "locked": { + "lastModified": 1784154424, + "narHash": "sha256-HRn4O1X2ShYtbq5Egji72/aemyc2cIQicjRZD9hBqM8=", + "owner": "Gerg-L", + "repo": "mnw", + "rev": "0151d9fa87992cbeb67c86616fb9170dcec830b6", + "type": "github" + }, + "original": { + "owner": "Gerg-L", + "repo": "mnw", + "type": "github" + } + }, "nix-darwin": { "inputs": { "nixpkgs": [ @@ -365,6 +396,28 @@ "type": "github" } }, + "nvf": { + "inputs": { + "flake-compat": "flake-compat_2", + "mnw": "mnw", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1792175215, + "narHash": "sha256-XD64JVLV8iE4lR+W5gkTweEaT+SBwOrSy0+le2WgITI=", + "owner": "NotAShelf", + "repo": "nvf", + "rev": "a736b60b1e2019931168979951c94dc46e342957", + "type": "github" + }, + "original": { + "owner": "NotAShelf", + "repo": "nvf", + "type": "github" + } + }, "pre-commit-hooks-nix": { "inputs": { "flake-compat": [ @@ -405,6 +458,7 @@ "nix-unit": "nix-unit", "nixpkgs": "nixpkgs", "nixpkgs-lib": "nixpkgs-lib", + "nvf": "nvf", "system-manager": "system-manager", "terranix": "terranix", "treefmt-nix": "treefmt-nix_2" @@ -428,7 +482,7 @@ }, "system-manager": { "inputs": { - "flake-compat": "flake-compat_2", + "flake-compat": "flake-compat_3", "nixpkgs": [ "nixpkgs" ], diff --git a/tests/dependencies/flake.nix b/tests/dependencies/flake.nix index b09d16b2..42cb7fa5 100644 --- a/tests/dependencies/flake.nix +++ b/tests/dependencies/flake.nix @@ -21,6 +21,8 @@ inputs.system-manager.inputs.nixpkgs.follows = "nixpkgs"; inputs.disko.url = "github:nix-community/disko"; inputs.disko.inputs.nixpkgs.follows = "nixpkgs"; + inputs.nvf.url = "github:NotAShelf/nvf"; + inputs.nvf.inputs.nixpkgs.follows = "nixpkgs"; inputs.hjem.url = "github:feel-co/hjem"; inputs.hjem.inputs.nixpkgs.follows = "nixpkgs"; inputs.nix-darwin.url = "github:nix-darwin/nix-darwin"; diff --git a/tests/pinned-world/default.nix b/tests/pinned-world/default.nix index d8d46337..c47f700d 100644 --- a/tests/pinned-world/default.nix +++ b/tests/pinned-world/default.nix @@ -72,6 +72,7 @@ let registered."caisson/terranix" registered."caisson/system-manager" registered."caisson/treefmt" + registered."caisson/nvf" registered."caisson/structural" # Last, so its value replaces the pin the overlays carry while # its position stays where the first import placed it. @@ -118,6 +119,7 @@ let "nixos" "nixos-minimal" "nixpkgs" + "nvf" "pins" "pkgOverlays" "pkgOverlaysFor" @@ -483,6 +485,7 @@ let "caisson/terranix" "caisson/system-manager" "caisson/treefmt" + "caisson/nvf" "caisson/structural" ]; @@ -583,6 +586,7 @@ let "terranix" "system-manager" "treefmt" + "nvf" ]; minimalNixosSystemEvaluates = @@ -1703,6 +1707,74 @@ let # The configuration `docs` is evaluated. && builtins.isString children.docs.outputs.wrapper.drvPath; + # An nvf configuration is published as + # `nvfConfigurations..`, and its Neovim is the one + # nvf's own `neovimConfiguration` builds + # from the same module on the same package set. An nvf configuration + # declared inside a NixOS machine or a home is published with the + # others. + 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; + published = top.nvfConfigurations.x86_64-linux.editor; + in + # The configuration is published in the shape `neovimConfiguration` + # returns. + published.neovim.drvPath == upstream.drvPath + && published.config.vim.viAlias + && published.options ? vim + && + builtins.attrNames top.nvfConfigurations.x86_64-linux == [ + "editor" + "home-editor" + "machine-editor" + ]; + # A terranix configuration that is a top is its `config.tf.json` # file, built from the package set of the composition with the # evaluated configuration in its `passthru`, and its modules get the diff --git a/tests/unit/lib-overlays.nix b/tests/unit/lib-overlays.nix index 4087d6e6..9e59e4ea 100644 --- a/tests/unit/lib-overlays.nix +++ b/tests/unit/lib-overlays.nix @@ -127,6 +127,7 @@ let "terranix" "system-manager" "treefmt" + "nvf" ]; in @@ -5391,6 +5392,7 @@ in "caisson-facter" "facter" "treefmt" + "nvf" ]; setsFromTheComposition = builtins.elem name [ "nixos" @@ -5404,6 +5406,7 @@ in "caisson-facter" "facter" "treefmt" + "nvf" ]; in signature ? configModule