mirror of
https://github.com/nix-community/flakelight.git
synced 2025-02-02 10:39:21 +01:00
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.
This commit is contained in:
parent
46ade23b8c
commit
62083df539
27
api_guide.md
27
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
|
||||
|
||||
|
19
builtinModules/functor.nix
Normal file
19
builtinModules/functor.nix
Normal file
@ -0,0 +1,19 @@
|
||||
# flakelight -- Framework for simplifying flake setup
|
||||
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
||||
# 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;
|
||||
};
|
||||
}
|
@ -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; })
|
||||
];
|
||||
}
|
||||
|
13
default.nix
13
default.nix
@ -2,14 +2,14 @@
|
||||
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
||||
# 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
|
||||
|
@ -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";
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user