From 75085bb1698adab554722d0c33efc43d7b6d63fe Mon Sep 17 00:00:00 2001 From: Dzuchun Date: Wed, 25 Nov 2026 13:53:14 +0000 Subject: [PATCH] add the `lib` output --- nix/output/default.nix | 1 + nix/output/lib.nix | 33 +++++++++++++++++++++++++++++++++ tests/export-lib.nix | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 nix/output/lib.nix create mode 100644 tests/export-lib.nix diff --git a/nix/output/default.nix b/nix/output/default.nix index a920c64..4524c2c 100644 --- a/nix/output/default.nix +++ b/nix/output/default.nix @@ -1,6 +1,7 @@ args: { definition = import ./def.nix; defaultOutputs = { + lib = import ./lib.nix; apps = import ./apps.nix; checks = import ./checks.nix args; devShells = import ./devShells.nix; diff --git a/nix/output/lib.nix b/nix/output/lib.nix new file mode 100644 index 0000000..58d1b7a --- /dev/null +++ b/nix/output/lib.nix @@ -0,0 +1,33 @@ +{ lib, ... }: +with lib; +{ + config.outputKinds = [ + (_: { + options = { + lib = mkOption { + type = with types; anything; + description = '' + Exported from the flake at `.lib` and `.lib.` respectively. + Arbitrary shape. Intended for use in "library" flakes, exporting the Nix + expressions. + ''; + }; + }; + outputs = + { + config, + hasSystem, + system, + ... + }: + if hasSystem then + { + lib.${system} = config.lib; + } + else + { + lib = config.lib; + }; + }) + ]; +} diff --git a/tests/export-lib.nix b/tests/export-lib.nix new file mode 100644 index 0000000..2e86495 --- /dev/null +++ b/tests/export-lib.nix @@ -0,0 +1,36 @@ +{ + checkSystem, + modularFlake, + ... +}: +let + noSystem = { + ans = 42; + str = "no system, no pkgs"; + }; + yesSystem = { + ans = 43; + str = "we have nixpkgs here"; + }; +in +{ + outputs = with modularFlake.defaultOutputs; [ lib ]; + modules = [ + ( + { + lib, + hasSystem, + ... + }: + { + lib = if hasSystem then yesSystem else noSystem; + } + ) + ]; + fails = false; + expectedOutput = { + lib = noSystem // { + ${checkSystem} = yesSystem; + }; + }; +} -- 2.54.0