From 3c61d227cf189967071501fa73b27697fc2d63a5 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 13 Jun 2016 01:12:10 +0200 Subject: lib.guard: init --- krebs/4lib/default.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'krebs/4lib') diff --git a/krebs/4lib/default.nix b/krebs/4lib/default.nix index bfe8c581c..da936fad6 100644 --- a/krebs/4lib/default.nix +++ b/krebs/4lib/default.nix @@ -15,6 +15,16 @@ let out = rec { addNames = mapAttrs addName; + guard = spec@{ type, value, ... }: + assert isOptionType type; + if type.check value + then value + else throw (toString (filter isString [ + "argument" + (if spec ? name then "‘${spec.name}’" else null) + "is not a ${type.name}" + ])); + types = import ./types.nix { inherit config; lib = lib // { inherit genid optionalTrace; }; -- cgit v1.2.3 From 2adb41310c16c43546a6855a1f6dbcc1c96dc344 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 13 Jun 2016 01:23:44 +0200 Subject: lib.lpad: init --- krebs/4lib/default.nix | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'krebs/4lib') diff --git a/krebs/4lib/default.nix b/krebs/4lib/default.nix index da936fad6..09d416d44 100644 --- a/krebs/4lib/default.nix +++ b/krebs/4lib/default.nix @@ -37,6 +37,11 @@ let out = rec { shell = import ./shell.nix { inherit lib; }; tree = import ./tree.nix { inherit lib; }; + lpad = n: c: s: + if stringLength s < n + then lpad n c (c + s) + else s; + toC = x: let type = typeOf x; reject = throw "cannot convert ${type}"; -- cgit v1.2.3 From fcfe4b646153e36aa9c8485693a13ae83c83a44d Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 13 Jun 2016 01:37:51 +0200 Subject: types.file-mode: init --- krebs/4lib/types.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'krebs/4lib') diff --git a/krebs/4lib/types.nix b/krebs/4lib/types.nix index 4742877a7..2f9828bb0 100644 --- a/krebs/4lib/types.nix +++ b/krebs/4lib/types.nix @@ -163,7 +163,7 @@ types // rec { secret-file = submodule ({ config, ... }: { options = { path = mkOption { type = str; }; - mode = mkOption { type = str; default = "0400"; }; + mode = mkOption { type = file-mode; default = "0400"; }; owner = mkOption { type = user; default = config.krebs.users.root; @@ -293,6 +293,12 @@ types // rec { }; }; + file-mode = mkOptionType { + name = "file mode"; + check = x: isString x && match "[0-7]{4}" x != null; + merge = mergeOneOption; + }; + haskell.conid = mkOptionType { name = "Haskell constructor identifier"; check = x: -- cgit v1.2.3 From 3846e08de8187fc3ba531d41f830002847466976 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 13 Jun 2016 01:40:57 +0200 Subject: types.{addr*,label,{host,file,{absolute-,}path}name}: use isString --- krebs/4lib/types.nix | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'krebs/4lib') diff --git a/krebs/4lib/types.nix b/krebs/4lib/types.nix index 2f9828bb0..0d5b51f76 100644 --- a/krebs/4lib/types.nix +++ b/krebs/4lib/types.nix @@ -239,7 +239,7 @@ types // rec { check = let IPv4address = let d = "([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; in concatMapStringsSep "." (const d) (range 1 4); - in x: match IPv4address x != null; + in x: isString x && match IPv4address x != null; merge = mergeOneOption; }; addr6 = mkOptionType { @@ -247,7 +247,7 @@ types // rec { check = let # TODO check IPv6 address harder IPv6address = "[0-9a-f.:]+"; - in x: match IPv6address x != null; + in x: isString x && match IPv6address x != null; merge = mergeOneOption; }; @@ -315,7 +315,7 @@ types // rec { # RFC952, B. Lexical grammar, hostname = mkOptionType { name = "hostname"; - check = x: all label.check (splitString "." x); + check = x: isString x && all label.check (splitString "." x); merge = mergeOneOption; }; @@ -324,14 +324,15 @@ types // rec { label = mkOptionType { name = "label"; # TODO case-insensitive labels - check = x: match "[0-9A-Za-z]([0-9A-Za-z-]*[0-9A-Za-z])?" x != null; + check = x: isString x + && match "[0-9A-Za-z]([0-9A-Za-z-]*[0-9A-Za-z])?" x != null; merge = mergeOneOption; }; # POSIX.1‐2013, 3.278 Portable Filename Character Set filename = mkOptionType { name = "POSIX filename"; - check = x: match "([0-9A-Za-z._])[0-9A-Za-z._-]*" x != null; + check = x: isString x && match "([0-9A-Za-z._])[0-9A-Za-z._-]*" x != null; merge = mergeOneOption; }; @@ -341,7 +342,7 @@ types // rec { absolute-pathname = mkOptionType { name = "POSIX absolute pathname"; check = x: let xs = splitString "/" x; xa = head xs; in - xa == "/" || (xa == "" && all filename.check (tail xs)); + isString x && (xa == "/" || (xa == "" && all filename.check (tail xs))); merge = mergeOneOption; }; @@ -350,7 +351,7 @@ types // rec { pathname = mkOptionType { name = "POSIX pathname"; check = x: let xs = splitString "/" x; in - all filename.check (if head xs == "" then tail xs else xs); + isString x && all filename.check (if head xs == "" then tail xs else xs); merge = mergeOneOption; }; -- cgit v1.2.3 From fb226f349843c080d6c81b60301a3a93977b99a4 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 13 Jun 2016 01:48:59 +0200 Subject: lib.genAttrs': init --- krebs/4lib/default.nix | 2 ++ 1 file changed, 2 insertions(+) (limited to 'krebs/4lib') diff --git a/krebs/4lib/default.nix b/krebs/4lib/default.nix index 09d416d44..afff17296 100644 --- a/krebs/4lib/default.nix +++ b/krebs/4lib/default.nix @@ -56,6 +56,8 @@ let out = rec { mapAttrs (name: _: path + "/${name}") (filterAttrs (_: eq "directory") (readDir path)); + genAttrs' = names: f: listToAttrs (map f names); + setAttr = name: value: set: set // { ${name} = value; }; optionalTrace = c: msg: x: if c then trace msg x else x; -- cgit v1.2.3