From 2adf0bdd4f4c6fbcb1f0dbe4b125b35d11da909d Mon Sep 17 00:00:00 2001 From: Mikael Voss Date: Sat, 24 Aug 2024 13:10:45 +0200 Subject: [PATCH] lib: Add auxiliary functions to deal with flags --- lib/flags.nix | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/flags.nix diff --git a/lib/flags.nix b/lib/flags.nix new file mode 100644 index 0000000..24884f9 --- /dev/null +++ b/lib/flags.nix @@ -0,0 +1,24 @@ +{ nixpkgs, ... }: + +let + inherit (builtins) any filter isList isString stringLength; + inherit (nixpkgs.lib.strings) addContextFrom; + + split = strOrList: + if isList strOrList then strOrList + else builtins.split "[[:space:]]+" strOrList + |> filter (flag: isString flag && stringLength flag > 0) + |> map (flag: addContextFrom strOrList flag); + + remerge = strOrList: list: + if isList strOrList then list + else toString list; +in { + remove = rem: strOrList: split strOrList + |> filter (flag: !any (elem: flag == elem) rem) + |> remerge strOrList; + + subst = sub: strOrList: split strOrList + |> map (flag: sub.${flag} or flag) + |> remerge strOrList; +}