diff options
author | Alyssa Ross <hi@alyssa.is> | 2020-01-26 23:38:55 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2020-01-26 23:38:55 +0000 |
commit | 02e12559a420f8c544b2facc4931904900a05518 (patch) | |
tree | ba39d8fa9e874b935d204e74accfed5caaccf0fa /nixpkgs/lib | |
parent | 542f80867c380b0ac79250b6e5358dda8bc49e0d (diff) | |
parent | 3d085a399c436a41d875801619e8f976b8519196 (diff) | |
download | nixlib-02e12559a420f8c544b2facc4931904900a05518.tar nixlib-02e12559a420f8c544b2facc4931904900a05518.tar.gz nixlib-02e12559a420f8c544b2facc4931904900a05518.tar.bz2 nixlib-02e12559a420f8c544b2facc4931904900a05518.tar.lz nixlib-02e12559a420f8c544b2facc4931904900a05518.tar.xz nixlib-02e12559a420f8c544b2facc4931904900a05518.tar.zst nixlib-02e12559a420f8c544b2facc4931904900a05518.zip |
Merge commit '3d085a399c436a41d875801619e8f976b8519196'
Diffstat (limited to 'nixpkgs/lib')
-rw-r--r-- | nixpkgs/lib/cli.nix | 93 | ||||
-rw-r--r-- | nixpkgs/lib/default.nix | 9 | ||||
-rw-r--r-- | nixpkgs/lib/generators.nix | 3 | ||||
-rw-r--r-- | nixpkgs/lib/licenses.nix | 5 | ||||
-rw-r--r-- | nixpkgs/lib/sources.nix | 1 | ||||
-rw-r--r-- | nixpkgs/lib/tests/misc.nix | 46 | ||||
-rw-r--r-- | nixpkgs/lib/trivial.nix | 2 |
7 files changed, 101 insertions, 58 deletions
diff --git a/nixpkgs/lib/cli.nix b/nixpkgs/lib/cli.nix index f47625d2f537..c96d4dbb0432 100644 --- a/nixpkgs/lib/cli.nix +++ b/nixpkgs/lib/cli.nix @@ -6,50 +6,77 @@ rec { This helps protect against malformed command lines and also to reduce boilerplate related to command-line construction for simple use cases. - Example: - encodeGNUCommandLine - { } - { data = builtins.toJSON { id = 0; }; - - X = "PUT"; - - retry = 3; + `toGNUCommandLine` returns a list of nix strings. + `toGNUCommandLineShell` returns an escaped shell string. - retry-delay = null; - - url = [ "https://example.com/foo" "https://example.com/bar" ]; - - silent = false; + Example: + cli.toGNUCommandLine {} { + data = builtins.toJSON { id = 0; }; + X = "PUT"; + retry = 3; + retry-delay = null; + url = [ "https://example.com/foo" "https://example.com/bar" ]; + silent = false; + verbose = true; + } + => [ + "-X" "PUT" + "--data" "{\"id\":0}" + "--retry" "3" + "--url" "https://example.com/foo" + "--url" "https://example.com/bar" + "--verbose" + ] - verbose = true; - }; - => "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'" + cli.toGNUCommandLineShell {} { + data = builtins.toJSON { id = 0; }; + X = "PUT"; + retry = 3; + retry-delay = null; + url = [ "https://example.com/foo" "https://example.com/bar" ]; + silent = false; + verbose = true; + } + => "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'"; */ - encodeGNUCommandLine = + toGNUCommandLineShell = options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs); - toGNUCommandLine = - { renderKey ? - key: if builtins.stringLength key == 1 then "-${key}" else "--${key}" + toGNUCommandLine = { + # how to string-format the option name; + # by default one character is a short option (`-`), + # more than one characters a long option (`--`). + mkOptionName ? + k: if builtins.stringLength k == 1 + then "-${k}" + else "--${k}", - , renderOption ? - key: value: - if value == null - then [] - else [ (renderKey key) (builtins.toString value) ] + # how to format a boolean value to a command list; + # by default it’s a flag option + # (only the option name if true, left out completely if false). + mkBool ? k: v: lib.optional v (mkOptionName k), - , renderBool ? key: value: lib.optional value (renderKey key) + # how to format a list value to a command list; + # by default the option name is repeated for each value + # and `mkOption` is applied to the values themselves. + mkList ? k: v: lib.concatMap (mkOption k) v, - , renderList ? key: value: lib.concatMap (renderOption key) value + # how to format any remaining value to a command list; + # on the toplevel, booleans and lists are handled by `mkBool` and `mkList`, + # though they can still appear as values of a list. + # By default, everything is printed verbatim and complex types + # are forbidden (lists, attrsets, functions). `null` values are omitted. + mkOption ? + k: v: if v == null + then [] + else [ (mkOptionName k) (lib.generators.mkValueStringDefault {} v) ] }: options: let - render = key: value: - if builtins.isBool value - then renderBool key value - else if builtins.isList value - then renderList key value - else renderOption key value; + render = k: v: + if builtins.isBool v then mkBool k v + else if builtins.isList v then mkList k v + else mkOption k v; in builtins.concatLists (lib.mapAttrsToList render options); diff --git a/nixpkgs/lib/default.nix b/nixpkgs/lib/default.nix index 5abafe1b2acf..d2fe018aa6af 100644 --- a/nixpkgs/lib/default.nix +++ b/nixpkgs/lib/default.nix @@ -37,11 +37,13 @@ let licenses = callLibs ./licenses.nix; systems = callLibs ./systems; + # serialization + cli = callLibs ./cli.nix; + generators = callLibs ./generators.nix; + # misc asserts = callLibs ./asserts.nix; - cli = callLibs ./cli.nix; debug = callLibs ./debug.nix; - generators = callLibs ./generators.nix; misc = callLibs ./deprecated.nix; # domain-specific @@ -101,7 +103,7 @@ let inherit (sources) pathType pathIsDirectory cleanSourceFilter cleanSource sourceByRegex sourceFilesBySuffices commitIdFromGitRepo cleanSourceWith pathHasContext - canCleanSource pathIsRegularFile; + canCleanSource pathIsRegularFile pathIsGitRepo; inherit (modules) evalModules unifyModuleSyntax applyIfFunction mergeModules mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions @@ -121,7 +123,6 @@ let isOptionType mkOptionType; inherit (asserts) assertMsg assertOneOf; - inherit (cli) encodeGNUCommandLine toGNUCommandLine; inherit (debug) addErrorContextToAttrs traceIf traceVal traceValFn traceXMLVal traceXMLValMarked traceSeq traceSeqN traceValSeq traceValSeqFn traceValSeqN traceValSeqNFn traceShowVal diff --git a/nixpkgs/lib/generators.nix b/nixpkgs/lib/generators.nix index a71654bec6c3..a64e94bd5cbd 100644 --- a/nixpkgs/lib/generators.nix +++ b/nixpkgs/lib/generators.nix @@ -46,7 +46,10 @@ rec { else if isList v then err "lists" v # same as for lists, might want to replace else if isAttrs v then err "attrsets" v + # functions can’t be printed of course else if isFunction v then err "functions" v + # let’s not talk about floats. There is no sensible `toString` for them. + else if isFloat v then err "floats" v else err "this value is" (toString v); diff --git a/nixpkgs/lib/licenses.nix b/nixpkgs/lib/licenses.nix index 986b7fa1fdd9..e2f94e565ce2 100644 --- a/nixpkgs/lib/licenses.nix +++ b/nixpkgs/lib/licenses.nix @@ -536,11 +536,6 @@ lib.mapAttrs (n: v: v // { shortName = n; }) { fullName = "University of Illinois/NCSA Open Source License"; }; - notion_lgpl = { - url = "https://raw.githubusercontent.com/raboof/notion/master/LICENSE"; - fullName = "Notion modified LGPL"; - }; - nposl3 = spdx { spdxId = "NPOSL-3.0"; fullName = "Non-Profit Open Software License 3.0"; diff --git a/nixpkgs/lib/sources.nix b/nixpkgs/lib/sources.nix index a5765c0fda5b..05519c3e392e 100644 --- a/nixpkgs/lib/sources.nix +++ b/nixpkgs/lib/sources.nix @@ -105,6 +105,7 @@ rec { in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts; in cleanSourceWith { inherit filter; src = path; }; + pathIsGitRepo = path: (builtins.tryEval (commitIdFromGitRepo path)).success; # Get the commit id of a git repo # Example: commitIdFromGitRepo <nixpkgs/.git> diff --git a/nixpkgs/lib/tests/misc.nix b/nixpkgs/lib/tests/misc.nix index e47b48b5017d..59ed1e507e24 100644 --- a/nixpkgs/lib/tests/misc.nix +++ b/nixpkgs/lib/tests/misc.nix @@ -441,24 +441,40 @@ runTests { expected = "«foo»"; }; - testRenderOptions = { - expr = - encodeGNUCommandLine - { } - { data = builtins.toJSON { id = 0; }; - - X = "PUT"; - - retry = 3; - - retry-delay = null; - url = [ "https://example.com/foo" "https://example.com/bar" ]; +# CLI + + testToGNUCommandLine = { + expr = cli.toGNUCommandLine {} { + data = builtins.toJSON { id = 0; }; + X = "PUT"; + retry = 3; + retry-delay = null; + url = [ "https://example.com/foo" "https://example.com/bar" ]; + silent = false; + verbose = true; + }; - silent = false; + expected = [ + "-X" "PUT" + "--data" "{\"id\":0}" + "--retry" "3" + "--url" "https://example.com/foo" + "--url" "https://example.com/bar" + "--verbose" + ]; + }; - verbose = true; - }; + testToGNUCommandLineShell = { + expr = cli.toGNUCommandLineShell {} { + data = builtins.toJSON { id = 0; }; + X = "PUT"; + retry = 3; + retry-delay = null; + url = [ "https://example.com/foo" "https://example.com/bar" ]; + silent = false; + verbose = true; + }; expected = "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'"; }; diff --git a/nixpkgs/lib/trivial.nix b/nixpkgs/lib/trivial.nix index 940ec1a3d59e..a281cd70fb0e 100644 --- a/nixpkgs/lib/trivial.nix +++ b/nixpkgs/lib/trivial.nix @@ -191,7 +191,7 @@ rec { let revisionFile = "${toString ./..}/.git-revision"; gitRepo = "${toString ./..}/.git"; - in if builtins.pathExists gitRepo + in if lib.pathIsGitRepo gitRepo then lib.commitIdFromGitRepo gitRepo else if lib.pathExists revisionFile then lib.fileContents revisionFile else default; |