From 33ae3f2fe4baeb4db19d3c90dbb6cd35b9373004 Mon Sep 17 00:00:00 2001 From: Jan Malakhovski Date: Mon, 17 Aug 2015 17:55:35 +0000 Subject: [PATCH] nixos,lib: move environment generation related copy-paste to lib --- lib/types.nix | 19 ++++++++++++++++++- nixos/modules/config/shells-environment.nix | 15 +-------------- nixos/modules/config/system-environment.nix | 15 +-------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index a7f9bf1946e6..7276f9af9fee 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -6,7 +6,7 @@ with import ./attrsets.nix; with import ./options.nix; with import ./trivial.nix; with import ./strings.nix; -with {inherit (import ./modules.nix) mergeDefinitions; }; +with {inherit (import ./modules.nix) mergeDefinitions filterOverrides; }; rec { @@ -166,6 +166,23 @@ rec { substSubModules = m: loaOf (elemType.substSubModules m); }; + # List or element of ... + loeOf = elemType: mkOptionType { + name = "element or list of ${elemType.name}s"; + check = x: isList x || elemType.check x; + merge = loc: defs: + let + defs' = filterOverrides defs; + res = (head defs').value; + in + if isList res then concatLists (getValues defs') + else if lessThan 1 (length defs') then + throw "The option `${showOption loc}' is defined multiple times, in ${showFiles (getFiles defs)}." + else if !isString res then + throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}." + else res; + }; + uniq = elemType: mkOptionType { inherit (elemType) name check; merge = mergeOneOption; diff --git a/nixos/modules/config/shells-environment.nix b/nixos/modules/config/shells-environment.nix index bff0b2991323..533280890a70 100644 --- a/nixos/modules/config/shells-environment.nix +++ b/nixos/modules/config/shells-environment.nix @@ -41,20 +41,7 @@ in strings. The latter is concatenated, interspersed with colon characters. ''; - type = types.attrsOf (mkOptionType { - name = "a string or a list of strings"; - merge = loc: defs: - let - defs' = filterOverrides defs; - res = (head defs').value; - in - if isList res then concatLists (getValues defs') - else if lessThan 1 (length defs') then - throw "The option `${showOption loc}' is defined multiple times, in ${showFiles (getFiles defs)}." - else if !isString res then - throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}." - else res; - }); + type = types.attrsOf (types.loeOf types.str); apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v); }; diff --git a/nixos/modules/config/system-environment.nix b/nixos/modules/config/system-environment.nix index 3ab32f00fd1d..3362400326d2 100644 --- a/nixos/modules/config/system-environment.nix +++ b/nixos/modules/config/system-environment.nix @@ -23,20 +23,7 @@ in strings. The latter is concatenated, interspersed with colon characters. ''; - type = types.attrsOf (mkOptionType { - name = "a string or a list of strings"; - merge = loc: defs: - let - defs' = filterOverrides defs; - res = (head defs').value; - in - if isList res then concatLists (getValues defs') - else if lessThan 1 (length defs') then - throw "The option `${showOption loc}' is defined multiple times, in ${showFiles (getFiles defs)}." - else if !isString res then - throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}." - else res; - }); + type = types.attrsOf (types.loeOf types.str); apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v); };