From 101fd9ad153674a222ecccd3410503d8b3877981 Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Sun, 21 Jun 2026 16:06:53 -0400 Subject: [PATCH 1/5] lib.cli.toCommandLine: share let variables for a single optionFormat --- lib/cli.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cli.nix b/lib/cli.nix index 37f9e270a313..94e54a672a29 100644 --- a/lib/cli.nix +++ b/lib/cli.nix @@ -421,7 +421,7 @@ - `lib.cli.toCommandLineShellGNU` */ toCommandLine = - optionFormat: attrs: + optionFormat: let handlePair = k: v: @@ -458,5 +458,5 @@ arg ]; in - builtins.concatLists (lib.mapAttrsToList handlePair attrs); + attrs: builtins.concatLists (lib.mapAttrsToList handlePair attrs); } From 72785188e15648278130ffa65739da5d96467a0a Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Sun, 21 Jun 2026 16:10:20 -0400 Subject: [PATCH 2/5] lib.cli: inherit functions to global scope --- lib/cli.nix | 61 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/lib/cli.nix b/lib/cli.nix index 94e54a672a29..26c2b545ae72 100644 --- a/lib/cli.nix +++ b/lib/cli.nix @@ -1,6 +1,21 @@ { lib }: -{ +let + inherit (lib) + concatLists + concatMap + escapeShellArgs + isBool + isList + mapAttrsToList + oldestSupportedReleaseIsAtLeast + optional + stringLength + warnIf + ; + inherit (lib.generators) mkValueStringDefault; +in +rec { /** Automatically convert an attribute set to command-line options. @@ -40,9 +55,9 @@ ::: */ toGNUCommandLineShell = - lib.warnIf (lib.oldestSupportedReleaseIsAtLeast 2511) + warnIf (oldestSupportedReleaseIsAtLeast 2511) "lib.cli.toGNUCommandLineShell is deprecated, please use lib.cli.toCommandLineShell or lib.cli.toCommandLineShellGNU instead." - (options: attrs: lib.escapeShellArgs (lib.cli.toGNUCommandLine options attrs)); + (options: attrs: escapeShellArgs (toGNUCommandLine options attrs)); /** Automatically convert an attribute set to a list of command-line options. @@ -116,15 +131,15 @@ ::: */ toGNUCommandLine = - lib.warnIf (lib.oldestSupportedReleaseIsAtLeast 2511) + warnIf (oldestSupportedReleaseIsAtLeast 2511) "lib.cli.toGNUCommandLine is deprecated, please use lib.cli.toCommandLine or lib.cli.toCommandLineShellGNU instead." ( { - mkOptionName ? k: if builtins.stringLength k == 1 then "-${k}" else "--${k}", + mkOptionName ? k: if stringLength k == 1 then "-${k}" else "--${k}", - mkBool ? k: v: lib.optional v (mkOptionName k), + mkBool ? k: v: optional v (mkOptionName k), - mkList ? k: v: lib.concatMap (mkOption k) v, + mkList ? k: v: concatMap (mkOption k) v, mkOption ? k: v: @@ -133,10 +148,10 @@ else if optionValueSeparator == null then [ (mkOptionName k) - (lib.generators.mkValueStringDefault { } v) + (mkValueStringDefault { } v) ] else - [ "${mkOptionName k}${optionValueSeparator}${lib.generators.mkValueStringDefault { } v}" ], + [ "${mkOptionName k}${optionValueSeparator}${mkValueStringDefault { } v}" ], optionValueSeparator ? null, }: @@ -144,15 +159,15 @@ let render = k: v: - if builtins.isBool v then + if isBool v then mkBool k v - else if builtins.isList v then + else if isList v then mkList k v else mkOption k v; in - builtins.concatLists (lib.mapAttrsToList render options) + concatLists (mapAttrsToList render options) ); /** @@ -163,8 +178,7 @@ For further reference see: [`lib.cli.toCommandLineGNU`](#function-library-lib.cli.toCommandLineGNU) */ - toCommandLineShellGNU = - options: attrs: lib.escapeShellArgs (lib.cli.toCommandLineGNU options attrs); + toCommandLineShellGNU = options: attrs: escapeShellArgs (toCommandLineGNU options attrs); /** Converts an attribute set into a list of GNU-style command-line arguments. @@ -227,9 +241,9 @@ */ toCommandLineGNU = { - isLong ? optionName: builtins.stringLength optionName > 1, + isLong ? optionName: stringLength optionName > 1, explicitBool ? false, - formatArg ? lib.generators.mkValueStringDefault { }, + formatArg ? mkValueStringDefault { }, }: let optionFormat = optionName: { @@ -238,7 +252,7 @@ inherit explicitBool formatArg; }; in - lib.cli.toCommandLine optionFormat; + toCommandLine optionFormat; /** Converts the given attributes into a single shell-escaped command-line @@ -248,8 +262,7 @@ For further reference see: [`lib.cli.toCommandLine`](#function-library-lib.cli.toCommandLine) */ - toCommandLineShell = - optionFormat: attrs: lib.escapeShellArgs (lib.cli.toCommandLine optionFormat attrs); + toCommandLineShell = optionFormat: attrs: escapeShellArgs (toCommandLine optionFormat attrs); /** Converts an attribute set into a list of command-line arguments. @@ -426,9 +439,9 @@ handlePair = k: v: if k == "" then - lib.throw "lib.cli.toCommandLine only accepts non-empty option names." - else if builtins.isList v then - builtins.concatMap (handleOption k) v + throw "lib.cli.toCommandLine only accepts non-empty option names." + else if isList v then + concatMap (handleOption k) v else handleOption k v; @@ -439,7 +452,7 @@ option, sep, explicitBool, - formatArg ? lib.generators.mkValueStringDefault { }, + formatArg ? mkValueStringDefault { }, }: k: v: if v == null || (!explicitBool && v == false) then @@ -458,5 +471,5 @@ arg ]; in - attrs: builtins.concatLists (lib.mapAttrsToList handlePair attrs); + attrs: concatLists (mapAttrsToList handlePair attrs); } From 1ffe9499a21e711952fca064b9158e8e45bc2160 Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Sun, 21 Jun 2026 16:13:54 -0400 Subject: [PATCH 3/5] lib.cli.toGNUCommandLine: move variable definition to higher scope --- lib/cli.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/cli.nix b/lib/cli.nix index 26c2b545ae72..9995a062fad9 100644 --- a/lib/cli.nix +++ b/lib/cli.nix @@ -155,7 +155,6 @@ rec { optionValueSeparator ? null, }: - options: let render = k: v: @@ -165,9 +164,8 @@ rec { mkList k v else mkOption k v; - in - concatLists (mapAttrsToList render options) + options: concatLists (mapAttrsToList render options) ); /** From a023145c7091b63894736bb5cfc7475c2fd8359c Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Sun, 21 Jun 2026 16:16:02 -0400 Subject: [PATCH 4/5] lib.cli: use partially applied mkValueStringDefault --- lib/cli.nix | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/cli.nix b/lib/cli.nix index 9995a062fad9..6edc207c6dfa 100644 --- a/lib/cli.nix +++ b/lib/cli.nix @@ -14,6 +14,7 @@ let warnIf ; inherit (lib.generators) mkValueStringDefault; + mkValueString = mkValueStringDefault { }; in rec { /** @@ -148,10 +149,10 @@ rec { else if optionValueSeparator == null then [ (mkOptionName k) - (mkValueStringDefault { } v) + (mkValueString v) ] else - [ "${mkOptionName k}${optionValueSeparator}${mkValueStringDefault { } v}" ], + [ "${mkOptionName k}${optionValueSeparator}${mkValueString v}" ], optionValueSeparator ? null, }: @@ -241,7 +242,7 @@ rec { { isLong ? optionName: stringLength optionName > 1, explicitBool ? false, - formatArg ? mkValueStringDefault { }, + formatArg ? mkValueString, }: let optionFormat = optionName: { @@ -450,7 +451,7 @@ rec { option, sep, explicitBool, - formatArg ? mkValueStringDefault { }, + formatArg ? mkValueString, }: k: v: if v == null || (!explicitBool && v == false) then From 646409453c90b752eb8c2ff4bf9ee44203232fe3 Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Sun, 21 Jun 2026 16:21:45 -0400 Subject: [PATCH 5/5] lib.cli.toGNUCommandLine: beta reduce mkList for free --- lib/cli.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.nix b/lib/cli.nix index 6edc207c6dfa..fe2a03a7beef 100644 --- a/lib/cli.nix +++ b/lib/cli.nix @@ -140,7 +140,7 @@ rec { mkBool ? k: v: optional v (mkOptionName k), - mkList ? k: v: concatMap (mkOption k) v, + mkList ? k: concatMap (mkOption k), mkOption ? k: v: