Note that the regular expression that builtins.match accepts already
supports disjunction using: "a|b". So your example application could be
rewritten using:

  sourceByRegex /tmp/sourcetest [".*hs$|someTestFile"]

suggesting that the list of regexes could be replaced by a single regex:

  sourceByRegex /tmp/sourcetest ".*hs$|someTestFile"

which might also be more efficient since the disjunction will be handled by
the regex engine instead of by the Nix interpreter.

Lists of regexes might be easier to work with though in which case your
function can be written as:

  sourceByRegex = src: regexes: builtins.filterSource (path: type:
      let relPath = lib.removePrefix (toString src + "/") (toString path);
      in builtins.match (concatStringsSep "|" regexes) relPath != null)
    src;

I'll leave further discussion for the PR ;)

Bas

On 17 February 2017 at 18:46, Bas van Dijk <v.dijk....@gmail.com> wrote:

> Awesome function indeed!
>
> TIL there exists builtins.match and it's not documented in the manual[1].
>
> I would like to switch from my whitelistSource to your sourceByRegex so a
> PR would be much appreciated indeed.
>
> Bas
>
> [1] http://nixos.org/nix/manual/#ssec-builtins
>
> On 17 February 2017 at 11:16, Thomas Hunger <tehun...@gmail.com> wrote:
>
>> Thanks for your replies everyone!
>>
>> Bas - your "toString" helped me over the finishing line. Pretty obvious
>> in hindsight to use toString to force evaluation of src. I adapted your
>> code to this:
>>
>> sourceByRegex = src: regexes: builtins.filterSource (path: type:
>>     let relPath = lib.removePrefix (toString src + "/") (toString path);
>>     in lib.any (re: builtins.match re relPath != null) regexes)
>>   src;
>>
>> To be used like this:
>>
>> sourceByRegex /tmp/sourcetest [".*hs$", "someTestFile"]
>>
>> And then I have some predefined lists like cabalProject = [".*\.cabal$",
>> ".*\.hs"]
>>
>> ~
>>
>>
>>
>> On 16 February 2017 at 21:58, Bas van Dijk <v.dijk....@gmail.com> wrote:
>>
>>> At LumiGuide we're using the following function in our Haskell packages:
>>>
>>>   # Copy everything under src into the Nix store except those paths
>>> that don't
>>>   # have one of the specified allowedPrefixes.
>>>   whitelistSource = src: allowedPrefixes:
>>>     builtins.filterSource
>>>       (path: type:
>>>         lib.any (allowedPrefix: lib.hasPrefix (toString (src + "/${
>>> allowedPrefix}")) path)
>>>                 allowedPrefixes)
>>>       src;
>>>
>>> To be used as for example:
>>>
>>>   src = lib.whitelistSource ./. [
>>>           "lumi-central-server.cabal"
>>>           "src"
>>>           "default.conf"
>>>         ];
>>>
>>> Bas
>>>
>>> Op 16 feb. 2017 13:14 schreef "Thomas Hunger" <tehun...@gmail.com>:
>>>
>>> Hi,
>>>
>>> I am consistently struggling with the following in nix: I have a
>>> repository and I want to specify derivations for some local sub-projects.
>>> The obvious solution is
>>>
>>>   src = ./subproject-A;
>>>
>>> But that pulls in everything in that directory, including build
>>> artifacts, or random intermediate data files.
>>>
>>> Another solution is
>>>
>>>   src = sourceFilesBySuffices ./subproject-A [".cabal" ".hs"];
>>>
>>> Which works reasonably well but introduces this weird dance where I
>>> suffix files so they can be matched by sourceFilesBySuffices. Mostly I want
>>> to do this:
>>>
>>>   src = [ ./subproject-A/schema.sql ./subproject-A/lib ];
>>>
>>> Or even get all the files checked into git:
>>>
>>>   src = gitFiles ./subproject-A; # does not work
>>>
>>> I was wondering whether any of you have this issue, and if you do: how
>>> do you solve it?
>>>
>>> ~
>>>
>>> (see also https://github.com/NixOS/nix/issues/885)
>>>
>>> _______________________________________________
>>> nix-dev mailing list
>>> nix-dev@lists.science.uu.nl
>>> http://lists.science.uu.nl/mailman/listinfo/nix-dev
>>>
>>>
>>>
>>
>
_______________________________________________
nix-dev mailing list
nix-dev@lists.science.uu.nl
http://lists.science.uu.nl/mailman/listinfo/nix-dev

Reply via email to