move custom type into custom lib

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00
parent c9abe13377
commit ea0319c60e
4 changed files with 51 additions and 50 deletions

View file

@ -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
{

44
lib.nix
View file

@ -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;
};
};
}

View file

@ -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 ];

View file

@ -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;
};
}