From fca03f175902fe899a87872228bf69c1b43a8543 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Tue, 19 May 2026 14:42:10 +0100 Subject: [PATCH] lib: allow instantiating without a flake instance Separate `overlay.nix` and `overlay.internal.nix`, to encapsulate the `flake` argument. This allows non-flake users to import `./overlay.nix` without providing a dummy `flake = null` attr. The read-only `config.flake` option is only defined by `evalNixvim` when `lib` is extended using the overlay in Nixvim's flake outputs. --- docs/lib/index.md | 3 ++- flake/lib.nix | 2 +- lib/modules.nix | 6 +++--- lib/overlay.internal.nix | 42 ++++++++++++++++++++++++++++++++++++++++ lib/overlay.nix | 39 +++---------------------------------- lib/top-level.nix | 2 +- 6 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 lib/overlay.internal.nix diff --git a/docs/lib/index.md b/docs/lib/index.md index 8931435f..7627beb0 100644 --- a/docs/lib/index.md +++ b/docs/lib/index.md @@ -84,7 +84,8 @@ When Nixvim is built in standalone mode, it expects `lib` to have Nixvim's exten If you'd like to use a `lib` with your own extensions, you must supply it via `specialArgs`, however you must ensure Nixvim's extensions are also present. -This can be achieved using the lib overlay, available via the `.lib.overlay` flake output. +This can be achieved using the lib overlay, available via the `.lib.overlay` flake output +or importing the `lib/overlay.nix` file. ```nix # Example flake diff --git a/flake/lib.nix b/flake/lib.nix index bee5147a..66921d4c 100644 --- a/flake/lib.nix +++ b/flake/lib.nix @@ -14,7 +14,7 @@ # on pinning the flake-parts nixpkgs-lib to the nixpkgs pin inherit (inputs.nixpkgs) lib; }; - overlay = import ../lib/overlay.nix { + overlay = import ../lib/overlay.internal.nix { flake = self; }; # Top-top-level aliases diff --git a/lib/modules.nix b/lib/modules.nix index 95cf5f82..c0739ec8 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -1,7 +1,7 @@ { lib, self, - flake, + flake ? null, }: let removed = { @@ -34,10 +34,10 @@ in lib.evalModules { modules = modules ++ [ ../modules/top-level - { + (lib.optionalAttrs (lib.isAttrs flake) { _file = ""; flake = lib.mkOptionDefault flake; - } + }) (lib.optionalAttrs (system != null) { _file = "evalNixvim"; nixpkgs.hostPlatform = lib.mkOptionDefault { inherit system; }; diff --git a/lib/overlay.internal.nix b/lib/overlay.internal.nix new file mode 100644 index 00000000..2f20c9f7 --- /dev/null +++ b/lib/overlay.internal.nix @@ -0,0 +1,42 @@ +/** + Internal function returning Nixvim's Nixpkgs `lib` overlay. + + Publicly accessible at `./overlay.nix` and the `nixvim.lib.overlay` flake output. +*/ +{ flake }: + +/** + Overlay function extending the Nixpkgs `lib` with Nixvim's additions. + + This function is intended to be passed to `lib.extend`. + + # Examples + + Importing the file directly: + ``` + lib.extend (import (nixvim + "/lib/overlay.nix")) + ``` + + Using the overlay from flake outputs: + ``` + lib.extend nixvim.lib.overlay + ``` + + # Inputs + + `lib` + : The final lib instance; the fixpoint of all extensions, including this one. + + `prevLib` + : The lib instance prior to applying this overlay. +*/ +lib: prevLib: { + # Add Nixvim's section to the lib + nixvim = import ./top-level.nix { inherit flake lib; }; + + # Extend the maintainers set with Nixvim-specific maintainers + maintainers = prevLib.maintainers // import ./maintainers.nix; + + # Extend lib.types with Nixvim's custom types + types = prevLib.types // import ./types.nix { inherit lib; }; +} diff --git a/lib/overlay.nix b/lib/overlay.nix index ac76f151..e9ed5ce5 100644 --- a/lib/overlay.nix +++ b/lib/overlay.nix @@ -1,37 +1,4 @@ -{ flake }: - -/** - Overlay function extending the Nixpkgs `lib` with Nixvim's additions. - - This function is intended to be passed to `lib.extend`. - - # Examples - - Importing the file directly: - ``` - lib.extend (import ./overlay.nix { flake = nixvim; }) - ``` - - Using the overlay from flake outputs: - ``` - lib.extend nixvim.lib.overlay - ``` - - # Inputs - - `lib` - : The final lib instance; the fixpoint of all extensions, including this one. - - `prevLib` - : The lib instance prior to applying this overlay. -*/ -lib: prevLib: { - # Add Nixvim's section to the lib - nixvim = import ./top-level.nix { inherit flake lib; }; - - # Extend the maintainers set with Nixvim-specific maintainers - maintainers = prevLib.maintainers // import ./maintainers.nix; - - # Extend lib.types with Nixvim's custom types - types = prevLib.types // import ./types.nix { inherit lib; }; +# Public non-flake entrypoint for Nixvim's Nixpkgs-lib overlay +import ./overlay.internal.nix { + flake = null; } diff --git a/lib/top-level.nix b/lib/top-level.nix index a27bb0ec..e603d3e5 100644 --- a/lib/top-level.nix +++ b/lib/top-level.nix @@ -16,7 +16,7 @@ */ { lib, - flake, + flake ? null, }: lib.makeExtensible ( self: