From 543e3aaa4d810cdc0cc40a6315fc4669609c2520 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Wed, 7 Feb 2024 00:54:03 -0800 Subject: [PATCH] Replace nixpkgs nullOr type with custom type `nullOr`'s merge function requires definitions to all be null or all be non-null. It was being used where the intent was that null be used as a value representing unset, and as such the merge should return null if all definitions are null and ignore nulls otherwise. This adds a type with that merge semantics. --- builtinModules/apps.nix | 8 ++++---- builtinModules/builtinOverlay.nix | 7 ++++--- builtinModules/bundlers.nix | 8 ++++---- builtinModules/checks.nix | 6 +++--- builtinModules/devShells.nix | 9 +++++---- builtinModules/flakelightModules.nix | 6 +++--- builtinModules/formatter.nix | 8 ++++---- builtinModules/functor.nix | 7 ++++--- builtinModules/homeModules.nix | 6 +++--- builtinModules/legacyPackages.nix | 7 ++++--- builtinModules/nixosModules.nix | 6 +++--- builtinModules/overlays.nix | 6 +++--- builtinModules/packages.nix | 8 ++++---- builtinModules/templates.nix | 6 +++--- default.nix | 26 +++++++++++++++++++++++--- 15 files changed, 74 insertions(+), 50 deletions(-) diff --git a/builtinModules/apps.nix b/builtinModules/apps.nix index 99bedf2..a147551 100644 --- a/builtinModules/apps.nix +++ b/builtinModules/apps.nix @@ -5,8 +5,8 @@ { config, lib, flakelight, genSystems, ... }: let inherit (lib) isFunction mapAttrs mkIf mkMerge mkOption; - inherit (lib.types) lazyAttrsOf nullOr raw; - inherit (flakelight.types) optFunctionTo; + inherit (lib.types) lazyAttrsOf raw; + inherit (flakelight.types) nullable optFunctionTo; isApp = x: (x ? type) && (x.type == "app") && (x ? program); @@ -17,12 +17,12 @@ in { options = { app = mkOption { - type = nullOr raw; + type = nullable raw; default = null; }; apps = mkOption { - type = nullOr (optFunctionTo (lazyAttrsOf raw)); + type = nullable (optFunctionTo (lazyAttrsOf raw)); default = null; }; }; diff --git a/builtinModules/builtinOverlay.nix b/builtinModules/builtinOverlay.nix index 19dd23f..88ee44d 100644 --- a/builtinModules/builtinOverlay.nix +++ b/builtinModules/builtinOverlay.nix @@ -5,14 +5,15 @@ { config, src, lib, inputs, outputs, flakelight, moduleArgs, ... }: let inherit (lib) isList mkOption mkOrder mapAttrs optionalAttrs; - inherit (lib.types) listOf nullOr oneOf str; + inherit (lib.types) listOf oneOf str; inherit (builtins) pathExists; inherit (flakelight) selectAttr; + inherit (flakelight.types) nullable; in { options = { description = mkOption { - type = nullOr str; + type = nullable str; default = if pathExists (src + /flake.nix) then (import (src + /flake.nix)).description or null @@ -20,7 +21,7 @@ in }; license = mkOption { - type = nullOr (oneOf [ str (listOf str) ]); + type = nullable (oneOf [ str (listOf str) ]); default = null; }; }; diff --git a/builtinModules/bundlers.nix b/builtinModules/bundlers.nix index 93f17ff..eec5d2f 100644 --- a/builtinModules/bundlers.nix +++ b/builtinModules/bundlers.nix @@ -5,8 +5,8 @@ { config, lib, flakelight, genSystems, ... }: let inherit (lib) isFunction mapAttrs mkMerge mkOption mkIf; - inherit (lib.types) lazyAttrsOf nullOr; - inherit (flakelight.types) function optFunctionTo; + inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) function nullable optFunctionTo; wrapBundler = pkgs: bundler: drv: if isFunction (bundler (pkgs // drv)) @@ -16,12 +16,12 @@ in { options = { bundler = mkOption { - type = nullOr function; + type = nullable function; default = null; }; bundlers = mkOption { - type = nullOr (optFunctionTo (lazyAttrsOf function)); + type = nullable (optFunctionTo (lazyAttrsOf function)); default = null; }; }; diff --git a/builtinModules/checks.nix b/builtinModules/checks.nix index d0bac18..61761ca 100644 --- a/builtinModules/checks.nix +++ b/builtinModules/checks.nix @@ -5,8 +5,8 @@ { config, src, lib, flakelight, genSystems, ... }: let inherit (lib) isDerivation isFunction mkOption mkIf mapAttrs; - inherit (lib.types) lazyAttrsOf nullOr raw; - inherit (flakelight.types) optFunctionTo; + inherit (lib.types) lazyAttrsOf raw; + inherit (flakelight.types) nullable optFunctionTo; mkCheck = pkgs: name: cmd: let cmd' = if isFunction cmd then cmd pkgs else cmd; in @@ -20,7 +20,7 @@ let in { options.checks = mkOption { - type = nullOr (optFunctionTo (lazyAttrsOf raw)); + type = nullable (optFunctionTo (lazyAttrsOf raw)); default = null; }; diff --git a/builtinModules/devShells.nix b/builtinModules/devShells.nix index 8ba2be1..976f43a 100644 --- a/builtinModules/devShells.nix +++ b/builtinModules/devShells.nix @@ -6,10 +6,11 @@ let inherit (lib) filterAttrs functionArgs mapAttrs mkDefault mkIf mkMerge mkOption; - inherit (lib.types) attrs coercedTo functionTo lazyAttrsOf lines listOf nullOr + inherit (lib.types) attrs coercedTo functionTo lazyAttrsOf lines listOf package str submodule; inherit (flakelight) supportedSystem; - inherit (flakelight.types) function optCallWith optFunctionTo packageDef; + inherit (flakelight.types) function nullable optCallWith optFunctionTo + packageDef; devShellModule.options = { inputsFrom = mkOption { @@ -38,7 +39,7 @@ let }; overrideShell = mkOption { - type = nullOr packageDef; + type = nullable packageDef; internal = true; default = null; }; @@ -53,7 +54,7 @@ in options = { devShell = mkOption { default = null; - type = nullOr (coercedTo function wrapFn + type = nullable (coercedTo function wrapFn (coercedTo attrs (x: _: x) (functionTo (submodule devShellModule)))); }; diff --git a/builtinModules/flakelightModules.nix b/builtinModules/flakelightModules.nix index 2c04c08..922c2d0 100644 --- a/builtinModules/flakelightModules.nix +++ b/builtinModules/flakelightModules.nix @@ -5,13 +5,13 @@ { config, lib, flakelight, moduleArgs, ... }: let inherit (lib) mkOption mkIf mkMerge; - inherit (lib.types) lazyAttrsOf nullOr; - inherit (flakelight.types) module optCallWith; + inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) module nullable optCallWith; in { options = { flakelightModule = mkOption { - type = nullOr module; + type = nullable module; default = null; }; diff --git a/builtinModules/formatter.nix b/builtinModules/formatter.nix index e62c965..4b48cf5 100644 --- a/builtinModules/formatter.nix +++ b/builtinModules/formatter.nix @@ -5,17 +5,17 @@ { config, src, lib, flakelight, genSystems, ... }: let inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList; - inherit (lib.types) functionTo lazyAttrsOf nullOr package str; - inherit (flakelight.types) optFunctionTo; + inherit (lib.types) functionTo lazyAttrsOf package str; + inherit (flakelight.types) nullable optFunctionTo; in { options = { formatter = mkOption { - type = nullOr (functionTo package); + type = nullable (functionTo package); default = null; }; formatters = mkOption { - type = nullOr (optFunctionTo (lazyAttrsOf str)); + type = nullable (optFunctionTo (lazyAttrsOf str)); default = null; }; }; diff --git a/builtinModules/functor.nix b/builtinModules/functor.nix index 1697c9a..1c2c697 100644 --- a/builtinModules/functor.nix +++ b/builtinModules/functor.nix @@ -2,14 +2,15 @@ # Copyright (C) 2023 Archit Gupta # SPDX-License-Identifier: MIT -{ config, lib, ... }: +{ config, lib, flakelight, ... }: let inherit (lib) mkOption mkIf; - inherit (lib.types) functionTo nullOr raw uniq; + inherit (lib.types) functionTo raw uniq; + inherit (flakelight.types) nullable; in { options.functor = mkOption { - type = nullOr (uniq (functionTo (functionTo raw))); + type = nullable (uniq (functionTo (functionTo raw))); default = null; }; diff --git a/builtinModules/homeModules.nix b/builtinModules/homeModules.nix index a462eca..c382e85 100644 --- a/builtinModules/homeModules.nix +++ b/builtinModules/homeModules.nix @@ -5,13 +5,13 @@ { config, lib, flakelight, moduleArgs, ... }: let inherit (lib) mkOption mkIf mkMerge; - inherit (lib.types) lazyAttrsOf nullOr; - inherit (flakelight.types) module optCallWith; + inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) module nullable optCallWith; in { options = { homeModule = mkOption { - type = nullOr module; + type = nullable module; default = null; }; diff --git a/builtinModules/legacyPackages.nix b/builtinModules/legacyPackages.nix index 1169926..33ae4f5 100644 --- a/builtinModules/legacyPackages.nix +++ b/builtinModules/legacyPackages.nix @@ -2,14 +2,15 @@ # Copyright (C) 2023 Archit Gupta # SPDX-License-Identifier: MIT -{ config, lib, genSystems, ... }: +{ config, lib, flakelight, genSystems, ... }: let inherit (lib) mkIf mkOption; - inherit (lib.types) functionTo nullOr pkgs; + inherit (lib.types) functionTo pkgs; + inherit (flakelight.types) nullable; in { options.legacyPackages = mkOption { - type = nullOr (functionTo pkgs); + type = nullable (functionTo pkgs); default = null; }; diff --git a/builtinModules/nixosModules.nix b/builtinModules/nixosModules.nix index 57fa488..15f1078 100644 --- a/builtinModules/nixosModules.nix +++ b/builtinModules/nixosModules.nix @@ -5,13 +5,13 @@ { config, lib, flakelight, moduleArgs, ... }: let inherit (lib) mkOption mkIf mkMerge; - inherit (lib.types) lazyAttrsOf nullOr; - inherit (flakelight.types) module optCallWith; + inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) module nullable optCallWith; in { options = { nixosModule = mkOption { - type = nullOr module; + type = nullable module; default = null; }; diff --git a/builtinModules/overlays.nix b/builtinModules/overlays.nix index 160f66c..c3c9ef1 100644 --- a/builtinModules/overlays.nix +++ b/builtinModules/overlays.nix @@ -5,13 +5,13 @@ { config, lib, flakelight, moduleArgs, ... }: let inherit (lib) mkMerge mkOption mkIf; - inherit (lib.types) lazyAttrsOf nullOr; - inherit (flakelight.types) optCallWith overlay; + inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) nullable optCallWith overlay; in { options = { overlay = mkOption { - type = nullOr overlay; + type = nullable overlay; default = null; }; diff --git a/builtinModules/packages.nix b/builtinModules/packages.nix index f7e9f94..51075a1 100644 --- a/builtinModules/packages.nix +++ b/builtinModules/packages.nix @@ -7,9 +7,9 @@ let inherit (builtins) hasAttr parseDrvName tryEval; inherit (lib) filterAttrs findFirst functionArgs mapAttrs' mapAttrs mkIf mkMerge mkOption nameValuePair optionalAttrs; - inherit (lib.types) lazyAttrsOf nullOr str uniq; + inherit (lib.types) lazyAttrsOf str uniq; inherit (flakelight) supportedSystem; - inherit (flakelight.types) optCallWith overlay packageDef; + inherit (flakelight.types) nullable optCallWith overlay packageDef; genPkg = final: prev: name: pkg: let @@ -32,7 +32,7 @@ in { options = { package = mkOption { - type = nullOr packageDef; + type = nullable packageDef; default = null; }; @@ -42,7 +42,7 @@ in }; pname = mkOption { - type = nullOr str; + type = nullable str; default = null; }; diff --git a/builtinModules/templates.nix b/builtinModules/templates.nix index e3b63ec..245d0aa 100644 --- a/builtinModules/templates.nix +++ b/builtinModules/templates.nix @@ -6,9 +6,9 @@ let inherit (builtins) isPath isString; inherit (lib) mkOption mkOptionType mkIf mkMerge; - inherit (lib.types) lazyAttrsOf nullOr; + inherit (lib.types) lazyAttrsOf; inherit (lib.options) mergeEqualOption; - inherit (flakelight.types) optCallWith; + inherit (flakelight.types) nullable optCallWith; template = mkOptionType { name = "template"; @@ -23,7 +23,7 @@ in { options = { template = mkOption { - type = nullOr (optCallWith moduleArgs template); + type = nullable (optCallWith moduleArgs template); default = null; }; diff --git a/default.nix b/default.nix index 84f468d..9b8b9d5 100644 --- a/default.nix +++ b/default.nix @@ -6,11 +6,12 @@ inputs: let inherit (inputs) nixpkgs; inherit (builtins) isAttrs isPath readDir; - inherit (nixpkgs.lib) attrNames composeManyExtensions evalModules filter + inherit (nixpkgs.lib) all attrNames composeManyExtensions evalModules filter findFirst fix genAttrs getValues hasSuffix isFunction isList mapAttrs mapAttrsToList mkDefault mkOptionType pathExists pipe removePrefix removeSuffix singleton warn; - inherit (nixpkgs.lib.types) coercedTo functionTo listOf; + inherit (nixpkgs.lib.types) coercedTo defaultFunctor functionTo listOf + optionDescriptionPhrase; inherit (nixpkgs.lib.options) mergeEqualOption mergeOneOption; builtinModules = mapAttrsToList (k: _: ./builtinModules + ("/" + k)) @@ -40,7 +41,7 @@ let supportedSystem types; }; - types = { + types = rec { overlay = mkOptionType { name = "overlay"; description = "nixpkgs overlay"; @@ -95,6 +96,25 @@ let optCallWith = args: elemType: coercedTo (functionTo elemType) (x: x args) elemType; + + nullable = elemType: mkOptionType { + name = "nullable"; + description = "nullable ${optionDescriptionPhrase + (class: class == "noun" || class == "composite") elemType}"; + descriptionClass = "noun"; + check = x: x == null || elemType.check x; + merge = loc: defs: + if all (def: def.value == null) defs then null + else elemType.merge loc (filter (def: def.value != null) defs); + emptyValue.value = null; + inherit (elemType) getSubOptions getSubModules; + substSubModules = m: nullable (elemType.substSubModules m); + functor = (defaultFunctor "nullable") // { + type = nullable; + wrapped = elemType; + }; + nestedTypes = { inherit elemType; }; + }; }; supportedSystem = { lib, stdenv, ... }: