2023-11-06 00:14:26 +01:00
|
|
|
# flakelight -- Framework for simplifying flake setup
|
|
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2024-01-10 10:16:24 +01:00
|
|
|
{ config, lib, flakelight, genSystems, ... }:
|
2023-11-06 00:14:26 +01:00
|
|
|
let
|
Allow bundlers to be functions that take pkgs
In addition to a regular bundler of the form `x: x`, this allows setting
bundler options to a function of the form `pkgs: x: x` which is passed
the package set for the system (for example: `{ hello, ... }: x: hello`
to always return hello). This allows, in particular, an autoloaded
bundler in its own file to access the package set.
This is non-trivial as we must tell `x: x` and `pkgs: x: x` apart.
Fortunately, given some derivation `drv`, `pkgs // drv` is a valid
derivation and a set with attr names matching pkgs. When applying this
to the function, if it returns a derivation, it was of the `x: x` form,
and if it returns a function, it was of the `pkgs: x: x` form.
In order to prevent IFD when evaluating the flake if the bundler is of
form `x: x` and uses IFD, we determine the form and apply pkgs when
applying the bundler instead of during flake evaluation. This is done by
wrapping the bundler.
Of note, we cannot rely on `builtins.functionArgs`, since
`pkgs: { hello, ... }: (x: hello) pkgs` is the same as the inner
function but with args hidden.
2023-11-07 08:13:47 +01:00
|
|
|
inherit (lib) isFunction mapAttrs mkMerge mkOption mkIf;
|
|
|
|
inherit (lib.types) lazyAttrsOf nullOr;
|
|
|
|
inherit (flakelight.types) function optFunctionTo;
|
2023-11-06 00:14:26 +01:00
|
|
|
|
Allow bundlers to be functions that take pkgs
In addition to a regular bundler of the form `x: x`, this allows setting
bundler options to a function of the form `pkgs: x: x` which is passed
the package set for the system (for example: `{ hello, ... }: x: hello`
to always return hello). This allows, in particular, an autoloaded
bundler in its own file to access the package set.
This is non-trivial as we must tell `x: x` and `pkgs: x: x` apart.
Fortunately, given some derivation `drv`, `pkgs // drv` is a valid
derivation and a set with attr names matching pkgs. When applying this
to the function, if it returns a derivation, it was of the `x: x` form,
and if it returns a function, it was of the `pkgs: x: x` form.
In order to prevent IFD when evaluating the flake if the bundler is of
form `x: x` and uses IFD, we determine the form and apply pkgs when
applying the bundler instead of during flake evaluation. This is done by
wrapping the bundler.
Of note, we cannot rely on `builtins.functionArgs`, since
`pkgs: { hello, ... }: (x: hello) pkgs` is the same as the inner
function but with args hidden.
2023-11-07 08:13:47 +01:00
|
|
|
wrapBundler = pkgs: bundler: drv:
|
|
|
|
if isFunction (bundler (pkgs // drv)) then bundler pkgs drv else bundler drv;
|
2023-11-06 00:14:26 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
bundler = mkOption {
|
Allow bundlers to be functions that take pkgs
In addition to a regular bundler of the form `x: x`, this allows setting
bundler options to a function of the form `pkgs: x: x` which is passed
the package set for the system (for example: `{ hello, ... }: x: hello`
to always return hello). This allows, in particular, an autoloaded
bundler in its own file to access the package set.
This is non-trivial as we must tell `x: x` and `pkgs: x: x` apart.
Fortunately, given some derivation `drv`, `pkgs // drv` is a valid
derivation and a set with attr names matching pkgs. When applying this
to the function, if it returns a derivation, it was of the `x: x` form,
and if it returns a function, it was of the `pkgs: x: x` form.
In order to prevent IFD when evaluating the flake if the bundler is of
form `x: x` and uses IFD, we determine the form and apply pkgs when
applying the bundler instead of during flake evaluation. This is done by
wrapping the bundler.
Of note, we cannot rely on `builtins.functionArgs`, since
`pkgs: { hello, ... }: (x: hello) pkgs` is the same as the inner
function but with args hidden.
2023-11-07 08:13:47 +01:00
|
|
|
type = nullOr function;
|
2023-11-06 00:14:26 +01:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
bundlers = mkOption {
|
Allow bundlers to be functions that take pkgs
In addition to a regular bundler of the form `x: x`, this allows setting
bundler options to a function of the form `pkgs: x: x` which is passed
the package set for the system (for example: `{ hello, ... }: x: hello`
to always return hello). This allows, in particular, an autoloaded
bundler in its own file to access the package set.
This is non-trivial as we must tell `x: x` and `pkgs: x: x` apart.
Fortunately, given some derivation `drv`, `pkgs // drv` is a valid
derivation and a set with attr names matching pkgs. When applying this
to the function, if it returns a derivation, it was of the `x: x` form,
and if it returns a function, it was of the `pkgs: x: x` form.
In order to prevent IFD when evaluating the flake if the bundler is of
form `x: x` and uses IFD, we determine the form and apply pkgs when
applying the bundler instead of during flake evaluation. This is done by
wrapping the bundler.
Of note, we cannot rely on `builtins.functionArgs`, since
`pkgs: { hello, ... }: (x: hello) pkgs` is the same as the inner
function but with args hidden.
2023-11-07 08:13:47 +01:00
|
|
|
type = nullOr (optFunctionTo (lazyAttrsOf function));
|
2023-11-22 07:15:52 +01:00
|
|
|
default = null;
|
2023-11-06 00:14:26 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
(mkIf (config.bundler != null) {
|
|
|
|
bundlers.default = config.bundler;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (config.bundlers != null) {
|
2024-01-10 10:16:24 +01:00
|
|
|
outputs.bundlers = genSystems (pkgs:
|
|
|
|
mapAttrs (_: wrapBundler pkgs) (config.bundlers pkgs));
|
2023-11-06 00:14:26 +01:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|