From 30249d282145b7b767b1aa7fc871f3c781df1cbe Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:41 +0100 Subject: [PATCH] move custom type into custom lib --- website/default.nix | 6 ++++- website/lib.nix | 44 ++++++++++++++++++++++++++++++++ website/structure/default.nix | 4 +-- website/structure/types.nix | 47 ----------------------------------- 4 files changed, 51 insertions(+), 50 deletions(-) delete mode 100644 website/structure/types.nix diff --git a/website/default.nix b/website/default.nix index be41a93..bdf1352 100644 --- a/website/default.nix +++ b/website/default.nix @@ -8,7 +8,11 @@ , lib ? import "${sources.nixpkgs}/lib" }: let - lib' = final: prev: import ./lib.nix { lib = final; }; + lib' = final: prev: + let + new = import ./lib.nix { lib = final; }; + in + new // { types = prev.recursiveUpdate prev.types new.types; }; lib'' = lib.extend lib'; in { diff --git a/website/lib.nix b/website/lib.nix index 36ad68f..87a66df 100644 --- a/website/lib.nix +++ b/website/lib.nix @@ -37,4 +37,48 @@ rec { indent = prefix: s: join "\n" (map (x: if x == "" then x else "${prefix}${x}") (splitLines s)); + + types = rec { + collection = elemType: + let + unparenthesize = class: class == "noun"; + desc = type: + types.optionDescriptionPhrase unparenthesize type; + desc' = type: + let + typeDesc = lib.types.optionDescriptionPhrase unparenthesize type; + in + if type.descriptionClass == "noun" + then + typeDesc + "s" + else + "many instances of ${typeDesc}"; + in + lib.types.mkOptionType { + name = "collection"; + description = "separately specified ${desc elemType} for a collection of ${desc' elemType}"; + merge = loc: defs: + map + (def: + let + merged = lib.mergeDefinitions + (loc ++ [ "[definition ${toString def.file}]" ]) + elemType + [{ inherit (def) file; value = def.value; }]; + in + if merged ? mergedValue then merged.mergedValue else merged.value + ) + defs; + check = elemType.check; + getSubOptions = elemType.getSubOptions; + getSubModules = elemType.getSubModules; + substSubModules = m: collection (elemType.substSubModules m); + functor = (lib.defaultFunctor "collection") // { + type = collection; + wrapped = elemType; + payload = { }; + }; + nestedTypes.elemType = elemType; + }; + }; } diff --git a/website/structure/default.nix b/website/structure/default.nix index bd6cfb0..d53316d 100644 --- a/website/structure/default.nix +++ b/website/structure/default.nix @@ -5,7 +5,7 @@ let types ; cfg = config; - types' = import ./types.nix { inherit lib; } // { + types' = { article = { config, collectionName, ... }: { imports = [ types'.page ]; options = { @@ -115,7 +115,7 @@ in }; entry = mkOption { description = "An entry in the collection"; - type = types'.collection (types.submodule ({ + type = types.collection (types.submodule ({ _module.args.collection = config.entry; _module.args.collectionName = name; imports = [ config.type ]; diff --git a/website/structure/types.nix b/website/structure/types.nix deleted file mode 100644 index 8a27cc5..0000000 --- a/website/structure/types.nix +++ /dev/null @@ -1,47 +0,0 @@ -{ lib, ... }: -let - inherit (lib) types; -in -rec { - collection = elemType: - let - unparenthesize = class: class == "noun"; - desc = type: - types.optionDescriptionPhrase unparenthesize type; - desc' = type: - let - typeDesc = types.optionDescriptionPhrase unparenthesize type; - in - if type.descriptionClass == "noun" - then - typeDesc + "s" - else - "many instances of ${typeDesc}"; - in - types.mkOptionType { - name = "collection"; - description = "separately specified ${desc elemType} for a collection of ${desc' elemType}"; - merge = loc: defs: - map - (def: - let - merged = lib.mergeDefinitions - (loc ++ [ "[definition ${toString def.file}]" ]) - elemType - [{ inherit (def) file; value = def.value; }]; - in - if merged ? mergedValue then merged.mergedValue else merged.value - ) - defs; - check = elemType.check; - getSubOptions = elemType.getSubOptions; - getSubModules = elemType.getSubModules; - substSubModules = m: collection (elemType.substSubModules m); - functor = (lib.defaultFunctor "collection") // { - type = collection; - wrapped = elemType; - payload = { }; - }; - nestedTypes.elemType = elemType; - }; -}