Allow devShell to be a package def or take args

Previously, devShell was inconsistent when setting it to a function.
When setting directly, a function was assumed to be a package definition
and was used to set devShells.default directly. When auto-loading, a
function was assumed to take module args, and result in config (not a
derivation).

This now enables both behaviors in either case by detecting if a
function is a package definition or if it expects module args and
handling it accordingly.
This commit is contained in:
Archit Gupta 2024-01-14 18:39:15 -08:00
parent 5215a657bf
commit 1d92662d82
2 changed files with 14 additions and 17 deletions

View File

@ -2,13 +2,15 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com> # Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
{ config, lib, flakelight, genSystems, ... }: { config, lib, flakelight, genSystems, moduleArgs, ... }:
let let
inherit (lib) filterAttrs mapAttrs mkDefault mkIf mkMerge mkOption; inherit (builtins) attrNames hasAttr;
inherit (lib) all filterAttrs functionArgs mapAttrs mkDefault mkIf mkMerge
mkOption;
inherit (lib.types) coercedTo functionTo lazyAttrsOf lines listOf nullOr inherit (lib.types) coercedTo functionTo lazyAttrsOf lines listOf nullOr
package str submodule; package str submodule;
inherit (flakelight) supportedSystem; inherit (flakelight) supportedSystem;
inherit (flakelight.types) optFunctionTo packageDef; inherit (flakelight.types) function optFunctionTo packageDef;
devShellModule.options = { devShellModule.options = {
inputsFrom = mkOption { inputsFrom = mkOption {
@ -42,13 +44,17 @@ let
default = null; default = null;
}; };
}; };
moduleFromFn = fn:
if all (a: hasAttr a moduleArgs) (attrNames (functionArgs fn))
then fn moduleArgs
else { overrideShell = fn; };
in in
{ {
options = { options = {
devShell = mkOption { devShell = mkOption {
default = null; default = null;
type = nullOr (coercedTo packageDef type = nullOr (coercedTo function moduleFromFn
(x: { overrideShell = x; })
(submodule devShellModule)); (submodule devShellModule));
}; };

View File

@ -4,7 +4,7 @@
{ config, src, lib, flakelight, moduleArgs, ... }: { config, src, lib, flakelight, moduleArgs, ... }:
let let
inherit (lib) mkOption mkIf mkMerge optionalAttrs; inherit (lib) mkOption mkIf mkMerge;
inherit (flakelight) autoImport autoImportArgs; inherit (flakelight) autoImport autoImportArgs;
inherit (flakelight.types) path; inherit (flakelight.types) path;
@ -25,7 +25,7 @@ in
package = autoImport' "package"; package = autoImport' "package";
packages = autoImportArgs' "packages"; packages = autoImportArgs' "packages";
overlays = autoImportArgs' "overlays"; overlays = autoImportArgs' "overlays";
devShell = autoImportArgs' "devShell"; devShell = autoImport' "devShell";
devShells = autoImportArgs' "devShells"; devShells = autoImportArgs' "devShells";
app = autoImport' "app"; app = autoImport' "app";
apps = autoImport' "apps"; apps = autoImport' "apps";
@ -53,16 +53,7 @@ in
(mkIf (package != null) { inherit package; }) (mkIf (package != null) { inherit package; })
(mkIf (packages != null) { inherit packages; }) (mkIf (packages != null) { inherit packages; })
(mkIf (overlays != null) { inherit overlays; }) (mkIf (overlays != null) { inherit overlays; })
(mkIf (devShell != null) { (mkIf (devShell != null) { inherit devShell; })
devShell = optionalAttrs (devShell ? inputsFrom)
{ inherit (devShell) inputsFrom; }
// optionalAttrs (devShell ? packages)
{ inherit (devShell) packages; }
// optionalAttrs (devShell ? shellHook)
{ inherit (devShell) shellHook; }
// optionalAttrs (devShell ? env)
{ inherit (devShell) env; };
})
(mkIf (devShells != null) { inherit devShells; }) (mkIf (devShells != null) { inherit devShells; })
(mkIf (app != null) { inherit app; }) (mkIf (app != null) { inherit app; })
(mkIf (apps != null) { inherit apps; }) (mkIf (apps != null) { inherit apps; })