1
1
forked from extern/flakelight

Clean up devShell options

Using a submodule for devShell removes the need to make every option
nullable and the need to check all of them. By using nullOr submodule,
we can tell if the value has been set and have default values for
options.

This also enables enabling a devShell with no options set.
This commit is contained in:
Archit Gupta 2023-12-05 01:27:10 -08:00
parent a4e4a341f2
commit dccabae216
2 changed files with 41 additions and 41 deletions

View File

@ -4,43 +4,45 @@
{ config, lib, flakelight, ... }: { config, lib, flakelight, ... }:
let let
inherit (lib) any attrValues filterAttrs mapAttrs mkDefault mkIf mkMerge inherit (lib) filterAttrs mapAttrs mkDefault mkIf mkMerge mkOption;
mkOption optionalAttrs; inherit (lib.types) functionTo lazyAttrsOf lines listOf nullOr package str
inherit (lib.types) lazyAttrsOf functionTo lines listOf nullOr package str; submodule;
inherit (flakelight) supportedSystem; inherit (flakelight) supportedSystem;
inherit (flakelight.types) optFunctionTo packageDef; inherit (flakelight.types) optFunctionTo packageDef;
in in
{ {
options = { options = {
devShell = { devShell = mkOption {
inputsFrom = mkOption {
type = nullOr
(functionTo (listOf package));
default = null; default = null;
type = nullOr (submodule {
options = {
inputsFrom = mkOption {
type = functionTo (listOf package);
default = _: [ ];
}; };
packages = mkOption { packages = mkOption {
type = nullOr type = functionTo (listOf package);
(functionTo (listOf package)); default = _: [ ];
default = null;
}; };
shellHook = mkOption { shellHook = mkOption {
type = nullOr (optFunctionTo lines); type = optFunctionTo lines;
default = null; default = "";
}; };
env = mkOption { env = mkOption {
type = nullOr type = optFunctionTo (lazyAttrsOf str);
(optFunctionTo (lazyAttrsOf str)); default = { };
default = null;
}; };
stdenv = mkOption { stdenv = mkOption {
type = nullOr (functionTo package); type = functionTo package;
default = null; default = pkgs: pkgs.stdenv;
}; };
}; };
});
};
devShells = mkOption { devShells = mkOption {
type = lazyAttrsOf packageDef; type = lazyAttrsOf packageDef;
@ -49,23 +51,14 @@ in
}; };
config = mkMerge [ config = mkMerge [
(mkIf (any (x: x != null) (attrValues config.devShell)) { (mkIf (config.devShell != null) {
devShells.default = mkDefault ({ pkgs, mkShell }: mkShell.override devShells.default = mkDefault ({ pkgs, mkShell }:
(if config.devShell.stdenv == null then { } mkShell.override { stdenv = config.devShell.stdenv pkgs; }
else { stdenv = config.devShell.stdenv pkgs; }) ((config.devShell.env pkgs) // {
(
optionalAttrs (config.devShell.env != null)
(config.devShell.env pkgs)
// optionalAttrs (config.devShell.inputsFrom != null) {
inputsFrom = config.devShell.inputsFrom pkgs; inputsFrom = config.devShell.inputsFrom pkgs;
}
// optionalAttrs (config.devShell.packages != null) {
packages = config.devShell.packages pkgs; packages = config.devShell.packages pkgs;
}
// optionalAttrs (config.devShell.shellHook != null) {
shellHook = config.devShell.shellHook pkgs; shellHook = config.devShell.shellHook pkgs;
} }));
));
}) })
(mkIf (config.devShells != { }) { (mkIf (config.devShells != { }) {

View File

@ -289,6 +289,13 @@ in
}) })
(f: lib.isDerivation f.devShells.x86_64-linux.default); (f: lib.isDerivation f.devShells.x86_64-linux.default);
devShell-empty = test
(flakelight ./empty {
disabledModules = [ "builtinFormatters.nix" ];
devShell = { };
})
(f: lib.isDerivation f.devShells.x86_64-linux.default);
devShell-override = test devShell-override = test
(flakelight ./empty { (flakelight ./empty {
devShells.default = { mkShell }: mkShell { }; devShells.default = { mkShell }: mkShell { };