From 62083df5390e4a71236eaa3fb466802da8779762 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Wed, 13 Sep 2023 21:00:14 -0700 Subject: [PATCH] Add functor option for flake's __functor attribute This allows for conveniently making flakes callable. Setting this is expected to be uncommon in general, but having the option is useful as flakelight module flakes can use this to reduce the boilerplate in using them. --- api_guide.md | 27 +++++++++++++++++++++++++++ builtinModules/functor.nix | 19 +++++++++++++++++++ builtinModules/nixDir.nix | 2 ++ default.nix | 13 +++++-------- flake.nix | 9 +++++---- 5 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 builtinModules/functor.nix diff --git a/api_guide.md b/api_guide.md index 7c3cd30..e7834e7 100644 --- a/api_guide.md +++ b/api_guide.md @@ -684,6 +684,32 @@ These can be paths, which is preferred as it results in better debug output: } ``` +### functor + +The `functor` option allows you to make your flake callable. + +If it is set to a function, that function will be set as the `__functor` +attribute of your flake outputs. + +Flakelight uses it so that calling your `flakelight` input calls +`flakelight.lib.mkFlake`. + +As an example: + +```nix +{ + inputs.flakelight.url = "github:accelbread/flakelight"; + outputs = { flakelight, ... }: + flakelight ./. { + outputs.testvalue = 5; + functor = self: x: x + self.testvalue; + } +} +``` + +With the above flake, another flake that has imports it with the name `addFive` +would be able to call `addFive 4` to get 9. + ### meta The following options are available for configuring the meta attributes of the @@ -746,6 +772,7 @@ The following options can be autoloaded (no module args): - nixosModule - homeModule - flakelightModule +- functor ### flakelight diff --git a/builtinModules/functor.nix b/builtinModules/functor.nix new file mode 100644 index 0000000..79a4024 --- /dev/null +++ b/builtinModules/functor.nix @@ -0,0 +1,19 @@ +# flakelight -- Framework for simplifying flake setup +# Copyright (C) 2023 Archit Gupta +# SPDX-License-Identifier: MIT + +{ config, lib, ... }: +let + inherit (lib) mkOption mkIf; + inherit (lib.types) functionTo nullOr raw uniq; +in +{ + options.functor = mkOption { + type = nullOr (uniq (functionTo (functionTo raw))); + default = null; + }; + + config.outputs = mkIf (config.functor != null) { + __functor = config.functor; + }; +} diff --git a/builtinModules/nixDir.nix b/builtinModules/nixDir.nix index 215360a..9de71b9 100644 --- a/builtinModules/nixDir.nix +++ b/builtinModules/nixDir.nix @@ -43,6 +43,7 @@ in homeConfigurations = autoImportArgs' [ "homeConfigurations" "home" ]; flakelightModule = autoImport' "flakelightModule"; flakelightModules = autoImportArgs' "flakelightModules"; + functor = autoImport' "functor"; in mkMerge [ { _module.args = { inherit autoloadArgs; }; } @@ -78,5 +79,6 @@ in (mkIf (homeConfigurations != null) { inherit homeConfigurations; }) (mkIf (flakelightModule != null) { inherit flakelightModule; }) (mkIf (flakelightModules != null) { inherit flakelightModules; }) + (mkIf (functor != null) { inherit functor; }) ]; } diff --git a/default.nix b/default.nix index 98728d4..0623ab6 100644 --- a/default.nix +++ b/default.nix @@ -2,14 +2,14 @@ # Copyright (C) 2023 Archit Gupta # SPDX-License-Identifier: MIT -localInputs: +nixpkgs: let inherit (builtins) isAttrs isPath readDir; - inherit (localInputs.nixpkgs.lib) attrNames composeManyExtensions + inherit (nixpkgs.lib) attrNames composeManyExtensions filter findFirst genAttrs getValues hasSuffix isFunction isList mapAttrsToList pathExists pipe removePrefix removeSuffix evalModules mkDefault mkOptionType singleton; - inherit (localInputs.nixpkgs.lib.types) coercedTo functionTo listOf; + inherit (nixpkgs.lib.types) coercedTo functionTo listOf; builtinModules = mapAttrsToList (k: _: ./builtinModules + ("/" + k)) (readDir ./builtinModules); @@ -17,7 +17,7 @@ let mkFlake = src: root: (evalModules { specialArgs.modulesPath = ./builtinModules; modules = builtinModules ++ [ - { inputs.nixpkgs = mkDefault localInputs.nixpkgs; } + { inputs.nixpkgs = mkDefault nixpkgs; } { _module.args = { inherit src flakelight; }; } root ]; @@ -94,7 +94,4 @@ let let v = autoImport dir name; in if isFunction v then v args else v; in -{ - lib = flakelight; - __functor = _: mkFlake; -} +flakelight diff --git a/flake.nix b/flake.nix index 512840e..958b644 100644 --- a/flake.nix +++ b/flake.nix @@ -6,10 +6,11 @@ description = "A modular Nix flake framework for simplifying flake definitions"; inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; - outputs = inputs: - let flakelight = import ./. inputs; in - flakelight ./. { - outputs = flakelight; + outputs = { nixpkgs, ... }: + let lib = import ./. nixpkgs; in + lib.mkFlake ./. { + outputs = { inherit lib; }; + functor = _: lib.mkFlake; templates = import ./templates; checks.statix = pkgs: "${pkgs.statix}/bin/statix check"; };