From ea0319c60eab7d600fb31e521521a026270e1b2c 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 --- default.nix | 6 +++++- lib.nix | 44 ++++++++++++++++++++++++++++++++++++++++ structure/default.nix | 4 ++-- structure/types.nix | 47 ------------------------------------------- 4 files changed, 51 insertions(+), 50 deletions(-) delete mode 100644 structure/types.nix diff --git a/default.nix b/default.nix index be41a936..bdf13528 100644 --- a/default.nix +++ b/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/lib.nix b/lib.nix index 36ad68f1..87a66df5 100644 --- a/lib.nix +++ b/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/structure/default.nix b/structure/default.nix index bd6cfb01..d53316d2 100644 --- a/structure/default.nix +++ b/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/structure/types.nix b/structure/types.nix deleted file mode 100644 index 8a27cc53..00000000 --- a/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; - }; -}