Skip to content

Commit

Permalink
Merge pull request #996 from dsalaza4/main
Browse files Browse the repository at this point in the history
feat(back): #995 new builtins
  • Loading branch information
dsalaza4 authored Jan 13, 2023
2 parents bd2848c + ee40f8f commit e09e125
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ Real life projects that run entirely on [Makes][makes]:
- [Patchers](#patchers)
- [pathShebangs](#pathshebangs)
- [Others](#others)
- [chunks](#chunks)
- [calculateCvss3](#calculatecvss3)
- [makeSslCertificate](#makesslcertificate)
- [sublist](#sublist)
- [Migrating to Makes](#migrating-to-makes)
- [From a Nix project](#from-a-nix-project)
- [Contact an expert](#contact-an-expert)
Expand Down Expand Up @@ -5097,6 +5099,47 @@ $ m . /example
### Others
#### chunks
Split a given list into N chunks
for workload distributed parallelization.
Types:
- chunks (`function list, ints.positive -> listOf (listOf Any)`):
- (`list`):
List to split into chunks.
- (`ints.positive`):
Number of chunks to create from list.
Example:
```nix
{
chunks,
inputs,
makeDerivation,
makeDerivationParallel,
}: let
numbers = [0 1 2 3 4 5 6 7 8 9];
myChunks = chunks numbers 3; # [[0 1 2 3] [4 5 6] [7 8 9]]
buildNumber = n: makeDerivation {
name = "build-number-${n}";
env.envNumber = n;
builder = ''
echo "$envNumber"
touch "$out"
'';
};
in
makeDerivationParallel {
dependencies = builtins.map buildNumber (inputs.nixpkgs.lib.lists.elemAt myChunks 0);
name = "build-numbers-0";
}
```
#### calculateCvss3
Calculate [CVSS3][cvss3]
Expand Down Expand Up @@ -5247,6 +5290,34 @@ $ m . /example
```
#### sublist
Return a sublist of a given list using a starting and an ending index.
Types:
- sublist (`function list, ints.positive, ints.positive -> listOf Any`):
- (`list`):
List to get sublist from.
- (`ints.positive`):
Starting list index.
- (`ints.positive`):
Ending list index.
Example:
```nix
{
sublist,
}: let
list = [0 1 2 3 4 5 6 7 8 9];
sublist = sublist list 3 5; # [3 4]
in {
inherit sublist;
}
```
# Migrating to Makes
## From a Nix project
Expand Down
2 changes: 2 additions & 0 deletions src/args/agnostic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
attrsOptional = self.__nixpkgs__.lib.optionalAttrs;
calculateCvss3 = import ./calculate-cvss-3/default.nix self;
calculateScorecard = import ./calculate-scorecard/default.nix self;
chunks = import ./chunks/default.nix self;
computeOnAwsBatch = import ./compute-on-aws-batch/default.nix self;
deployContainerImage = import ./deploy-container-image/default.nix self;
deployNomad = import ./deploy-nomad/default.nix self;
Expand Down Expand Up @@ -102,6 +103,7 @@
sortAscii = builtins.sort (a: b: a < b);
sortAsciiCaseless = builtins.sort (a: b: self.__nixpkgs__.lib.toLower a < self.__nixpkgs__.lib.toLower b);
stringCapitalize = import ./string-capitalize/default.nix self;
sublist = import ./sublist/default.nix self;
taintTerraform = import ./taint-terraform/default.nix self;
testTerraform = import ./test-terraform/default.nix self;
testPython = import ./test-python/default.nix self;
Expand Down
16 changes: 16 additions & 0 deletions src/args/chunks/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
__nixpkgs__,
sublist,
...
}: list: n: let
lib = __nixpkgs__.lib;

len = lib.lists.length list;
div = len / n;
mod = lib.trivial.mod len n;
range = lib.lists.range 0 (n - 1);
min = lib.trivial.min;
startIndex = i: i * div + (min i mod);
endIndex = i: (i + 1) * div + (min (i + 1) mod);
in
builtins.map (i: sublist list (startIndex i) (endIndex i)) range
6 changes: 6 additions & 0 deletions src/args/sublist/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{__nixpkgs__, ...}: list: start: end: let
lib = __nixpkgs__.lib;

range = lib.lists.range start (end - 1);
in
builtins.map (lib.lists.elemAt list) range

0 comments on commit e09e125

Please sign in to comment.